Base entry for compacting task.
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / multivm / VMLibraryTask.java
blob36b8739f4191644a83564c329b438140fafad4c4
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // 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.multivm.ident.SourceTargetClassifier;
13 import java.nio.file.Path;
14 import javax.inject.Inject;
15 import lombok.Getter;
16 import org.gradle.api.DefaultTask;
17 import org.gradle.api.Project;
18 import org.gradle.api.Task;
19 import org.gradle.api.internal.AbstractTask;
20 import org.gradle.api.provider.Provider;
21 import org.gradle.api.tasks.Internal;
22 import org.gradle.api.tasks.bundling.AbstractArchiveTask;
23 import org.gradle.jvm.tasks.Jar;
25 /**
26 * This task is responsible for creating a library that is used for the task
27 * execution.
29 * @since 2020/08/07
31 public class VMLibraryTask
32 extends DefaultTask
33 implements VMBaseTask, VMExecutableTask
35 /** The base JAR. */
36 @Internal
37 @Getter
38 public final AbstractTask baseJar;
40 /** The classifier used. */
41 @Internal
42 @Getter
43 private final SourceTargetClassifier classifier;
45 /**
46 * Initializes the library creation task.
48 * @param __classifier The classifier used.
49 * @param __baseJar The task with the Jar output.
50 * @throws NullPointerException On null arguments.
51 * @since 2020/08/07
53 @Inject
54 public VMLibraryTask(SourceTargetClassifier __classifier,
55 AbstractTask __baseJar)
56 throws NullPointerException
58 if (__classifier == null || __baseJar == null)
59 throw new NullPointerException("NARG");
61 // These are used at the build stage
62 this.baseJar = __baseJar;
63 this.classifier = __classifier;
65 // Set details of this task
66 this.setGroup("squirreljme");
67 this.setDescription("Compiles/constructs the library for execution.");
69 // The JAR we are compiling has to be built first
70 // We also need the virtual machine library compiler as well
71 this.dependsOn(__baseJar, new VMLibraryTaskDependencies(this,
72 __classifier.getTargetClassifier()));
74 // Only run if the JAR would run
75 this.onlyIf(this::onlyIf);
77 // The input of this task is the JAR that was created
78 this.getInputs().file(__baseJar.getOutputs()
79 .getFiles().getSingleFile());
81 // The output depends on the task and its source set
82 this.getOutputs().files(
83 this.getProject().provider(() -> this.__taskOutputFile()));
84 this.getOutputs().upToDateWhen(
85 new VMLibraryTaskUpToDate(__classifier.getTargetClassifier()));
87 // Performs the action of the task
88 this.doLast(new VMLibraryTaskAction(__classifier));
91 /**
92 * When should this run?
94 * @param __task The task to check.
95 * @return If this should run.
96 * @since 2022/05/20
98 private boolean onlyIf(Task __task)
100 return this.baseJar.getOnlyIf().isSatisfiedBy(this.baseJar);
104 * Returns the output path of the archive.
106 * @return The output path.
107 * @since 2020/08/07
109 public final Provider<Path> outputPath()
111 return this.getProject().provider(() -> VMHelpers.cacheDir(
112 this.getProject(), this.classifier).get()
113 .resolve(this.classifier.getVmType()
114 .outputLibraryName(this.getProject(),
115 this.classifier.getSourceSet())));
119 * Returns the output file for this task.
121 * @return The output file for this task.
122 * @since 2022/05/20
124 private Object __taskOutputFile()
126 if (this.onlyIf(this))
127 return this.outputPath();
128 return null;