Add test for launcher.
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / multivm / VMCompactLibraryTask.java
blob642bfd2f256b50f929d373bd014cbb8c9d8fe142
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // Multi-Phasic Applications: SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.plugin.multivm;
12 import cc.squirreljme.plugin.SquirrelJMEPluginConfiguration;
13 import java.nio.file.Path;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.LinkedHashSet;
18 import java.util.List;
19 import java.util.Objects;
20 import javax.inject.Inject;
21 import lombok.Getter;
22 import org.gradle.api.DefaultTask;
23 import org.gradle.api.Project;
24 import org.gradle.api.Task;
25 import org.gradle.api.provider.Provider;
26 import org.gradle.api.tasks.Internal;
27 import org.gradle.api.tasks.SourceSet;
28 import org.gradle.jvm.tasks.Jar;
30 /**
31 * Performs compaction of the library so that it does not have debug
32 * information and is smaller.
34 * @since 2023/02/01
36 public class VMCompactLibraryTask
37 extends DefaultTask
39 /** The base JAR. */
40 @Internal
41 @Getter
42 public final Jar baseJar;
44 /** The source set used. */
45 @Internal
46 @Getter
47 private final String sourceSet;
49 /**
50 * Initializes the compacting task.
52 * @param __sourceSet The source set this belongs to.
53 * @param __baseJar The original source Jar.
54 * @throws NullPointerException On null arguments.
55 * @since 2023/02/01
57 @Inject
58 public VMCompactLibraryTask(String __sourceSet,
59 Jar __baseJar)
60 throws NullPointerException
62 if (__sourceSet == null || __baseJar == null)
63 throw new NullPointerException("NARG");
65 Project project = this.getProject();
67 this.baseJar = __baseJar;
68 this.sourceSet = __sourceSet;
70 // Set details of this task
71 this.setGroup("squirreljme");
72 this.setDescription(
73 "Compacts the library and removes debugging info.");
75 // Depends on the base Jar but also depends on all the
76 // compactLibs for all dependencies
77 this.dependsOn(__baseJar, this.getProject().provider(() ->
78 VMHelpers.compactLibTaskDepends(this.getProject(),
79 this.sourceSet)));
81 // Only run if the source JAR would run
82 this.onlyIf(this::onlyIf);
84 // The input of this is the original Jar and any Jars and maps that are
85 // needed by ProGuard
86 this.getInputs().files(this.inputBaseJarPath(),
87 this.getProject().provider(() -> {
88 Collection<Object> result = new LinkedHashSet<>();
89 for (VMCompactLibraryTask compactTask :
90 VMHelpers.compactLibTaskDepends(this.getProject(),
91 this.sourceSet))
93 // If the input Jar changes, we want to update this one
94 result.add(compactTask.inputBaseJarPath());
96 // If the output Jar changes, we want that one as well
97 // since the mappings will be different
98 result.add(compactTask.outputJarPath());
101 return result;
102 }));
104 // Inputs are the proguard options, so if this changes we need to
105 // do a rebuild!
106 this.getInputs().property("squirreljme.proguardOptions",
107 this.getProject().provider(() -> Objects.toString(
108 VMCompactLibraryTask.__optionsBySourceSet(
109 this.getProject(), __sourceSet).get())));
111 // Error code that is used for the prefix
112 this.getInputs().property("squirreljme.javaDocErrorCode",
113 this.getProject().provider(() -> Objects.toString(
114 SquirrelJMEPluginConfiguration.configuration(project)
115 .javaDocErrorCode)));
117 // Also include the built-in plugin options, in case those change as
118 // well!
119 this.getInputs().property("squirreljme.proguardOptionsDefault",
120 this.getProject().provider(() ->
121 Arrays.toString(VMCompactLibraryTaskAction._OPTIMIZATIONS) +
122 Arrays.toString(VMCompactLibraryTaskAction._PARSE_SETTINGS) +
123 Arrays.toString(VMCompactLibraryTaskAction._TEST_SETTINGS)));
125 // The output of this JAR is just where it should be placed, this
126 // includes the mapping file for incremental mapping
127 this.getOutputs().files(this.outputJarPath(), this.outputMapPath());
129 // Performs the action of the task
130 this.doLast(new VMCompactLibraryTaskAction(__sourceSet));
134 * When should this run?
136 * @param __task The task to check.
137 * @return If this should run.
138 * @since 2023/02/01
140 private boolean onlyIf(Task __task)
142 return this.baseJar.getOnlyIf().isSatisfiedBy(this.baseJar);
146 * Returns the input Jar that will get transformed accordingly.
148 * @return The input Jar that we will be transforming.
149 * @since 2023/02/05
151 public final Provider<Path> inputBaseJarPath()
153 return this.getProject().provider(() -> this.baseJar.getOutputs()
154 .getFiles().getSingleFile().toPath());
158 * Returns the output path of the archive.
160 * @return The output path.
161 * @since 2023/02/01
163 public final Provider<Path> outputJarPath()
165 return this.getProject().provider(() ->
166 this.getProject().getBuildDir().toPath()
167 .resolve("squirreljme").resolve("obfuscated")
168 .resolve(this.baseJar.getOutputs().getFiles()
169 .getSingleFile().toPath().getFileName()));
173 * Returns the output path of the mapping file.
175 * @return The output path of the mapping file.
176 * @since 2023/02/05
178 public final Provider<Path> outputMapPath()
180 return this.getProject().provider(() ->
182 Path jarOut = this.outputJarPath().get();
184 return jarOut.resolveSibling(
185 VMHelpers.getBaseName(jarOut) + ".map");
190 * Returns the options to use for the given source set.
192 * @param __project The project to get from.
193 * @param __sourceSet The source set to use.
194 * @return The provider to the ProGuard options for the source set.
195 * @throws NullPointerException On null arguments.
196 * @since 2023/02/11
198 static Provider<List<String>> __optionsBySourceSet(Project __project,
199 String __sourceSet)
200 throws NullPointerException
202 if (__project == null || __sourceSet == null)
203 throw new NullPointerException("NARG");
205 return __project.provider(() ->
207 SquirrelJMEPluginConfiguration projectConfig =
208 SquirrelJMEPluginConfiguration.configuration(__project);
210 switch (__sourceSet)
212 // Main sources
213 case SourceSet.MAIN_SOURCE_SET_NAME:
214 return projectConfig.proGuardOptions;
216 // Test sources
217 case SourceSet.TEST_SOURCE_SET_NAME:
218 return projectConfig.proGuardOptionsTest;
220 // Test fixtures
221 case VMHelpers.TEST_FIXTURES_SOURCE_SET_NAME:
222 return projectConfig.proGuardOptionsTestFixtures;
225 return Collections.emptyList();