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.DexCountExtension;
019import com.getkeepsafe.dexcount.PrintOptions;
020import org.gradle.api.DefaultTask;
021import org.gradle.api.file.ConfigurableFileCollection;
022import org.gradle.api.file.RegularFileProperty;
023import org.gradle.api.provider.Property;
024import org.gradle.api.tasks.Classpath;
025import org.gradle.api.tasks.Input;
026import org.gradle.api.tasks.InputFile;
027import org.gradle.api.tasks.Internal;
028import org.gradle.api.tasks.Nested;
029import org.gradle.api.tasks.TaskAction;
030import org.gradle.workers.WorkQueue;
031import org.gradle.workers.WorkerExecutor;
032
033import javax.inject.Inject;
034
035public abstract class DexCountOutputTask extends DefaultTask {
036    @Input
037    public abstract Property<String> getVariantNameProperty();
038
039    @Nested
040    public abstract Property<DexCountExtension> getConfigProperty();
041
042    @InputFile
043    public abstract RegularFileProperty getPackageTreeFileProperty();
044
045    @Internal
046    public abstract Property<Boolean> getAndroidProject();
047
048    @Classpath
049    public abstract ConfigurableFileCollection getWorkerClasspath();
050
051    @Inject
052    public abstract WorkerExecutor getWorkerExecutor();
053
054    @TaskAction
055    public void run() {
056        PrintOptions opts = PrintOptions.fromDexCountExtension(getConfigProperty().get())
057            .withIsAndroidProject(getAndroidProject().get());
058
059        WorkQueue queue = getWorkerExecutor().classLoaderIsolation(spec -> {
060            spec.getClasspath().from(getWorkerClasspath());
061        });
062
063        queue.submit(ReportOutputWorker.class, params -> {
064            params.getVariantName().set(getVariantNameProperty());
065            params.getPackageTreeFile().set(getPackageTreeFileProperty());
066            params.getPrintOptions().set(opts);
067        });
068    }
069}