|
| 1 | +/* |
| 2 | +Copyright (C) 2016 Migeran |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package org.moe.gradle.tasks; |
| 18 | + |
| 19 | +import org.gradle.api.GradleException; |
| 20 | +import org.gradle.api.Project; |
| 21 | +import org.gradle.api.file.ConfigurableFileCollection; |
| 22 | +import org.gradle.api.logging.Logger; |
| 23 | +import org.gradle.api.logging.Logging; |
| 24 | +import org.gradle.api.tasks.InputFiles; |
| 25 | +import org.gradle.api.tasks.Internal; |
| 26 | +import org.gradle.api.tasks.OutputFile; |
| 27 | +import org.gradle.api.tasks.SourceSet; |
| 28 | +import org.moe.gradle.MoeExtension; |
| 29 | +import org.moe.gradle.MoePlugin; |
| 30 | +import org.moe.gradle.MoeSDK; |
| 31 | +import org.moe.gradle.anns.IgnoreUnused; |
| 32 | +import org.moe.gradle.anns.NotNull; |
| 33 | +import org.moe.gradle.anns.Nullable; |
| 34 | +import org.moe.gradle.utils.FileUtils; |
| 35 | +import org.moe.gradle.utils.Mode; |
| 36 | +import org.moe.gradle.utils.Require; |
| 37 | +import org.moe.tools.classvalidator.proguard.ProguardCollector; |
| 38 | + |
| 39 | +import java.io.File; |
| 40 | +import java.io.IOException; |
| 41 | +import java.nio.file.Path; |
| 42 | +import java.nio.file.Paths; |
| 43 | +import java.util.Collection; |
| 44 | +import java.util.HashSet; |
| 45 | +import java.util.LinkedHashSet; |
| 46 | +import java.util.Set; |
| 47 | + |
| 48 | +public class ProguardCollect extends AbstractBaseTask { |
| 49 | + |
| 50 | + private static final Logger LOG = Logging.getLogger(ProguardCollect.class); |
| 51 | + |
| 52 | + private static final String CONVENTION_OUT_CFG_FILE = "outCfgFile"; |
| 53 | + private static final String CONVENTION_INPUT_FILES = "inputFiles"; |
| 54 | + private static final String CONVENTION_CLASSPATH_FILES = "classpathFiles"; |
| 55 | + |
| 56 | + |
| 57 | + @Nullable |
| 58 | + private Object outCfgFile; |
| 59 | + |
| 60 | + @OutputFile |
| 61 | + @NotNull |
| 62 | + public File getOutCfgFile() { |
| 63 | + return getProject().file(getOrConvention(this.outCfgFile, CONVENTION_OUT_CFG_FILE)); |
| 64 | + } |
| 65 | + |
| 66 | + @IgnoreUnused |
| 67 | + public void setOutCfgFile(@Nullable Object outCfgFile) { |
| 68 | + this.outCfgFile = outCfgFile; |
| 69 | + } |
| 70 | + |
| 71 | + @Nullable |
| 72 | + private Set<Object> inputFiles; |
| 73 | + |
| 74 | + @InputFiles |
| 75 | + @NotNull |
| 76 | + public ConfigurableFileCollection getInputFiles() { |
| 77 | + return getProject().files(getOrConvention(inputFiles, CONVENTION_INPUT_FILES)); |
| 78 | + } |
| 79 | + |
| 80 | + @IgnoreUnused |
| 81 | + public void setInputFiles(@Nullable Collection<Object> inputFiles) { |
| 82 | + this.inputFiles = inputFiles == null ? null : new HashSet<>(inputFiles); |
| 83 | + } |
| 84 | + |
| 85 | + @Nullable |
| 86 | + private Set<Object> classpathFiles; |
| 87 | + |
| 88 | + @InputFiles |
| 89 | + @NotNull |
| 90 | + public ConfigurableFileCollection getClasspathFiles() { |
| 91 | + return getProject().files(getOrConvention(classpathFiles, CONVENTION_CLASSPATH_FILES)); |
| 92 | + } |
| 93 | + |
| 94 | + @IgnoreUnused |
| 95 | + public void setClasspathFiles(@Nullable Set<Object> classpathFiles) { |
| 96 | + this.classpathFiles = classpathFiles == null ? null : new HashSet<>(classpathFiles); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + protected void run() { |
| 101 | + try { |
| 102 | + FileUtils.deleteFileOrFolder(getOutCfgFile()); |
| 103 | + } catch (IOException e) { |
| 104 | + throw new GradleException("an IOException occurred", e); |
| 105 | + } |
| 106 | + ProguardCollector.process(getInputFiles().getFiles(), getOutCfgFile().toPath(), getClasspathFiles().getFiles()); |
| 107 | + } |
| 108 | + |
| 109 | + private ClassValidate classValidateTaskDep; |
| 110 | + |
| 111 | + @Nullable |
| 112 | + @IgnoreUnused |
| 113 | + @Internal |
| 114 | + public ClassValidate getClassValidateTaskDep() { |
| 115 | + return classValidateTaskDep; |
| 116 | + } |
| 117 | + |
| 118 | + protected final void setupMoeTask(final @NotNull SourceSet sourceSet, final @NotNull Mode mode) { |
| 119 | + Require.nonNull(sourceSet); |
| 120 | + |
| 121 | + setSupportsRemoteBuild(false); |
| 122 | + |
| 123 | + final Project project = getProject(); |
| 124 | + final MoeExtension ext = getMoeExtension(); |
| 125 | + final MoeSDK sdk = getMoeSDK(); |
| 126 | + |
| 127 | + // Construct default output path |
| 128 | + final Path out = Paths.get(MoePlugin.MOE, sourceSet.getName(), "collect", mode.name); |
| 129 | + |
| 130 | + setDescription("Generate code specific proguard keep configs (sourceset: " + sourceSet.getName() + ", mode: " + mode.name + ")."); |
| 131 | + |
| 132 | + // Add dependencies |
| 133 | + final ClassValidate classValidateTask = getMoePlugin().getTaskBy(ClassValidate.class, sourceSet, mode); |
| 134 | + classValidateTaskDep = classValidateTask; |
| 135 | + dependsOn(classValidateTask); |
| 136 | + |
| 137 | + addConvention(CONVENTION_OUT_CFG_FILE, () -> resolvePathInBuildDir(out, "proguard_collect.cfg")); |
| 138 | + addConvention(CONVENTION_INPUT_FILES, () -> new LinkedHashSet<Object>(classValidateTask.getOutputJars().getFiles())); |
| 139 | + addConvention(CONVENTION_CLASSPATH_FILES, () -> { |
| 140 | + HashSet<Object> hashSet = new LinkedHashSet<Object>(classValidateTask.getClasspathFiles().getFiles()); |
| 141 | + hashSet.add(sdk.getJava8SupportJar()); |
| 142 | + hashSet.add(sdk.getCoreJar()); |
| 143 | + hashSet.add(ext.getPlatformJar()); |
| 144 | + |
| 145 | + return hashSet; |
| 146 | + }); |
| 147 | + addConvention(CONVENTION_LOG_FILE, () -> resolvePathInBuildDir(out, "ProguardCollect.log")); |
| 148 | + } |
| 149 | +} |
0 commit comments