Cherry pick the banglets and such from wip-l1summercoat, this will be the basis for...
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / multivm / VMRomDependencies.java
blobfedc9d5b243968eea4141b19bb168b793761309d
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.SquirrelJMEPluginConfiguration;
13 import cc.squirreljme.plugin.multivm.ident.SourceTargetClassifier;
14 import cc.squirreljme.plugin.util.ProjectAndSourceSet;
15 import java.util.ArrayList;
16 import java.util.LinkedHashMap;
17 import java.util.Map;
18 import java.util.concurrent.Callable;
19 import org.gradle.api.Project;
20 import org.gradle.api.Task;
21 import org.gradle.api.tasks.SourceSet;
23 /**
24 * Dependencies for building the ROM, will depend on every library that exists.
26 * @since 2020/08/23
28 public class VMRomDependencies
29 implements Callable<Iterable<VMLibraryTask>>
31 /** The task to execute for. */
32 protected final VMRomTask task;
34 /** The classifier used. */
35 protected final SourceTargetClassifier classifier;
37 /**
38 * Initializes the ROM dependency task.
40 * @param __task The task to use.
41 * @param __classifier The classifier used.
42 * @throws NullPointerException on null arguments.
43 * @since 2020/08/23
45 public VMRomDependencies(VMRomTask __task,
46 SourceTargetClassifier __classifier)
47 throws NullPointerException
49 if (__task == null || __classifier == null)
50 throw new NullPointerException("NARG");
52 this.task = __task;
53 this.classifier = __classifier;
56 /**
57 * {@inheritDoc}
58 * @since 2020/08/23
60 @Override
61 public Iterable<VMLibraryTask> call()
63 return VMRomDependencies.libraries(this.task, this.classifier);
66 /**
67 * Returns all the libraries that should make up the ROM.
69 * @param __task The task to get from.
70 * @param __classifier The classifier used.
71 * @return The libraries that are used as a dependency to build the ROM.
72 * @throws NullPointerException On null arguments.
73 * @since 2020/11/27
75 public static Iterable<VMLibraryTask> libraries(Task __task,
76 SourceTargetClassifier __classifier)
77 throws NullPointerException
79 if (__task == null || __classifier == null)
80 throw new NullPointerException("NARG");
82 // Where all the libraries will go
83 Map<ProjectAndSourceSet, VMLibraryTask> result =
84 new LinkedHashMap<>();
86 // This could be recursive
87 VMRomDependencies.__libraries(__task, __classifier, result);
89 return new ArrayList<>(result.values());
92 /**
93 * Returns all the mapped libraries.
95 * @param __task The task to add for.
96 * @param __classifier The classifier used.
97 * @param __result Where all the results are stored.
98 * @throws NullPointerException On null arguments.
99 * @since 2022/08/07
101 private static void __libraries(Task __task,
102 SourceTargetClassifier __classifier,
103 Map<ProjectAndSourceSet, VMLibraryTask> __result)
104 throws NullPointerException
106 if (__task == null || __classifier == null || __result == null)
107 throw new NullPointerException("NARG");
109 // If we are not on the main source set, we need to include everything
110 // the main source set has. For tests for example, we need the main
111 // libraries to even test them properly.
112 if (!__classifier.isMainSourceSet())
113 VMRomDependencies.__libraries(__task,
114 __classifier.withSourceSet(SourceSet.MAIN_SOURCE_SET_NAME),
115 __result);
117 // If we are using tests, then we need to include all the test
118 // fixtures as well
119 if (__classifier.isTestSourceSet())
120 VMRomDependencies.__libraries(__task, __classifier.withSourceSet(
121 VMHelpers.TEST_FIXTURES_SOURCE_SET_NAME), __result);
123 // Go through all projects and map dependencies
124 for (Project project : __task.getProject().getRootProject()
125 .getAllprojects())
127 // Only consider SquirrelJME projects
128 SquirrelJMEPluginConfiguration config =
129 SquirrelJMEPluginConfiguration.configurationOrNull(project);
130 if (config == null)
131 continue;
133 // Only consider library tasks of this given type
134 Task task = project.getTasks().findByName(TaskInitialization
135 .task("lib", __classifier));
136 if (!(task instanceof VMLibraryTask))
137 continue;
139 // Add the task
140 __result.put(new ProjectAndSourceSet(task.getProject(),
141 __classifier.getSourceSet()), (VMLibraryTask)task);