001/*
002 * Copyright (C) 2015-2021 KeepSafe Software
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package com.getkeepsafe.dexcount.report;
017
018import com.getkeepsafe.dexcount.CountReporter;
019import com.getkeepsafe.dexcount.PackageTree;
020import com.getkeepsafe.dexcount.thrift.TreeGenOutput;
021import com.microsoft.thrifty.KtApiKt;
022import com.microsoft.thrifty.protocol.Protocol;
023import com.microsoft.thrifty.transport.Transport;
024import okio.BufferedSource;
025import okio.GzipSource;
026import okio.Okio;
027import okio.Source;
028import org.gradle.workers.WorkAction;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032import java.io.File;
033import java.io.IOException;
034
035public abstract class ReportOutputWorker implements WorkAction<ReportOutputWorkerParams> {
036    private static final Logger LOGGER = LoggerFactory.getLogger(ReportOutputWorker.class);
037
038    @Override
039    public void execute() {
040        try {
041            actuallyExecute();
042        } catch (IOException e) {
043            LOGGER.error("Error reporting dexcount output; please clean and rebuild.", e);
044        }
045    }
046
047    private void actuallyExecute() throws IOException {
048        TreeGenOutput treeGen = readTreeGenFile();
049        if (treeGen.tree == null) {
050            LOGGER.error("Corrupted dexcount data; please clean and rebuild.");
051            return;
052        }
053
054        String inputRepresentation = treeGen.inputRepresentation;
055        if (inputRepresentation == null) {
056            LOGGER.error("Corrupted dexcount data; please clean and rebuild.");
057            return;
058        }
059
060        PackageTree tree = PackageTree.fromThrift(treeGen.tree);
061        CountReporter reporter = new CountReporter(
062            tree,
063            getParameters().getVariantName().get(),
064            LOGGER,
065            getParameters().getPrintOptions().get(),
066            inputRepresentation,
067            false);
068
069        reporter.report();
070    }
071
072    private TreeGenOutput readTreeGenFile() throws IOException {
073        File file = getParameters().getPackageTreeFile().getAsFile().get();
074
075        try (
076            Source source = Okio.source(file);
077            GzipSource gzip = new GzipSource(source);
078            BufferedSource bufferedSource = Okio.buffer(gzip);
079            Transport transport = KtApiKt.transport(bufferedSource);
080            Protocol protocol = KtApiKt.compactProtocol(transport)
081        ) {
082            return TreeGenOutput.ADAPTER.read(protocol);
083        }
084    }
085}