forked from MinecraftForge/ForgeGradle
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTaskGenSubprojects.java
More file actions
341 lines (290 loc) · 11 KB
/
TaskGenSubprojects.java
File metadata and controls
341 lines (290 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* A Gradle plugin for the creation of Minecraft mods and MinecraftForge plugins.
* Copyright (C) 2013-2019 Minecraft Forge
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package net.minecraftforge.gradle.patcher;
import static net.minecraftforge.gradle.common.Constants.NEWLINE;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import net.minecraftforge.gradle.common.Constants;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.OutputFiles;
import org.gradle.api.tasks.TaskAction;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import com.google.common.io.Resources;
import groovy.lang.Closure;
class TaskGenSubprojects extends DefaultTask
{
//@formatter:off
@Input private String javaLevel;
private Object workspaceDir;
@Input private final String resource;
@Input private final List<Repo> repositories = Lists.newArrayList();
@Input private final List<String> dependencies = Lists.newArrayList();
@Input private Closure<Boolean> depFilter;
private final Map<String, DevProject> projects = Maps.newHashMap();
private static final String INDENT = " "; // 4 spaces
//@formatter:on
public TaskGenSubprojects() throws IOException
{
super();
resource = Resources.toString(Resources.getResource(TaskGenSubprojects.class, "globalGradle"), Constants.CHARSET);
}
@TaskAction
public void executeTask() throws IOException
{
File workspace = getWorkspaceDir();
workspace.mkdirs();
// make run dir just in case
new File(workspace, "run").mkdirs();
generateRootBuild(new File(workspace, "build.gradle"));
generateRootSettings(new File(workspace, "settings.gradle"), projects.keySet());
URI workspaceUri = workspace.toURI();
for (DevProject project : projects.values())
{
File projectDir = project.getProjectDir(workspace);
projectDir.mkdirs();
generateProjectBuild(workspaceUri, new File(projectDir, "build.gradle"), project);
}
}
private void generateRootBuild(File output) throws IOException
{
StringBuilder builder = new StringBuilder();
int start = 0;
int end = 0;
while ((start = resource.indexOf("@@", end)) != -1)
{
builder.append(resource.subSequence(end, start));
end = resource.indexOf("@@", start + 2);
if (end == -1)
{
builder.append(resource.subSequence(start, resource.length()));
}
else
{
String id = resource.substring(start + 2, end);
end += 2;
if ("repositories".equals(id))
{
for (Repo repo : repositories)
{
lines(builder, 2,
"maven {",
" name '" + repo.name + "'",
" url '" + repo.url + "'",
"}");
}
}
else if ("dependencies".equals(id))
{
for (String dep : dependencies)
{
if (this.depFilter != null && !this.depFilter.call(dep))
{
this.getProject().getLogger().debug("Filtering Dep: " + dep);
continue;
}
append(builder, INDENT, INDENT, dep, NEWLINE);
}
}
else if ("javaLevel".equals(id))
{
builder.append(getJavaLevel());
}
else
{
this.getProject().getLogger().lifecycle("Unknown subproject key: " + id);
}
}
}
if (end != -1)
builder.append(resource.subSequence(end, resource.length()));
Files.write(builder.toString(), output, Constants.CHARSET);
}
private static void generateRootSettings(File output, Collection<String> projects) throws IOException
{
StringBuilder builder = new StringBuilder();
builder.append("include '");
Joiner.on("', '").appendTo(builder, projects);
builder.append("'");
Files.write(builder.toString(), output, Constants.CHARSET);
}
private static void generateProjectBuild(URI workspace, File output, DevProject project) throws IOException
{
StringBuilder builder = new StringBuilder();
File src = project.getExternalSrcDir();
File res = project.getExternalResDir();
File testSrc = project.getExternalTestSrcDir();
File testRes = project.getExternalTestResDir();
// @formatter:off
// why use relative paths? so the eclipse hack below can work correctly.
// add extra sourceDirs
append(builder, "sourceSets {", NEWLINE);
append(builder, INDENT, "main.java.srcDir 'src/main/start'", NEWLINE); // add start dir to gradle sources
if (src != null ) append(builder, INDENT, "main.java.srcDir '", relative(workspace, src), "'", NEWLINE);
if (res != null ) append(builder, INDENT, "main.resources.srcDir '", relative(workspace, res), "'", NEWLINE);
if (testSrc != null ) append(builder, INDENT, "test.java.srcDir '", relative(workspace, testSrc), "'", NEWLINE);
if (testRes != null ) append(builder, INDENT, "test.resources.srcDir '", relative(workspace, testRes), "'", NEWLINE);
append(builder, "}");
// @formatter:on
// write
Files.write(builder.toString(), output, Constants.CHARSET);
}
private static void lines(StringBuilder out, int indentLevel, CharSequence... lines)
{
String indent = Strings.repeat(INDENT, indentLevel);
for (CharSequence line : lines)
{
out.append(indent).append(line).append(NEWLINE);
}
}
private static void append(StringBuilder out, CharSequence... things)
{
for (CharSequence str : things)
{
out.append(str);
}
}
private static String relative(URI base, File src)
{
String relative = base.relativize(src.toURI()).getPath().replace('\\', '/');
if (!relative.endsWith("/"))
relative += "/";
return relative;
}
@SuppressWarnings("serial")
private static class Repo implements Serializable
{
public final String name, url;
public Repo(String name, String url)
{
super();
this.name = name;
this.url = url;
}
}
@SuppressWarnings("serial")
private static class DevProject implements Serializable
{
//@formatter:off
private final transient Project project;
private final String name;
private final Object externalSrcDir, externalResDir;
private final Object externalTestSrcDir, externalTestResDir;
//@formatter:on
public DevProject(Project project, String name, Object externalSrcDir, Object externalResDir, Object externalTestSrcDir, Object externalTestResDir)
{
super();
this.project = project;
this.name = name;
this.externalSrcDir = externalSrcDir;
this.externalResDir = externalResDir;
this.externalTestSrcDir = externalTestSrcDir;
this.externalTestResDir = externalTestResDir;
}
public File getProjectDir(File root)
{
return new File(root, name);
}
public File getExternalSrcDir()
{
return externalSrcDir == null ? null : project.file(externalSrcDir);
}
public File getExternalResDir()
{
return externalResDir == null ? null : project.file(externalResDir);
}
public File getExternalTestSrcDir()
{
return externalTestSrcDir == null ? null : project.file(externalTestSrcDir);
}
public File getExternalTestResDir()
{
return externalTestResDir == null ? null : project.file(externalTestResDir);
}
}
public String getJavaLevel()
{
return javaLevel;
}
public void setJavaLevel(String javaLevel)
{
this.javaLevel = javaLevel;
}
public void addCompileDep(String depString)
{
dependencies.add("compile '" + depString + "'");
}
public void addTestCompileDep(String depString)
{
dependencies.add("testCompile '" + depString + "'");
}
public void addRepo(String name, String url)
{
repositories.add(new Repo(name, url));
}
@OutputFiles
public List<File> getGeneratedFiles()
{
List<File> files = new ArrayList<File>(2 + projects.size());
File workspace = getWorkspaceDir();
files.add(new File(workspace, "build.gradle"));
files.add(new File(workspace, "settings.gradle"));
for (DevProject p : projects.values())
{
files.add(new File(p.getProjectDir(workspace) + "/build.gradle"));
}
return files;
}
public void putProject(String name, Object externalSrcDir, Object externalResDir, Object externalTestSrcDir, Object externalTestResDir)
{
projects.put(name, new DevProject(getProject(), name, externalSrcDir, externalResDir, externalTestSrcDir, externalTestResDir));
}
public void removeProject(String name)
{
projects.remove(name);
}
public File getWorkspaceDir()
{
return getProject().file(workspaceDir);
}
public void setWorkspaceDir(Object workspaceDir)
{
this.workspaceDir = workspaceDir;
}
public void setFilter(Closure<Boolean> filter)
{
this.depFilter = filter;
}
public Closure<Boolean> getFilter()
{
return this.depFilter;
}
}