/
BndWorkspacePlugin.java
320 lines (291 loc) · 10.4 KB
/
BndWorkspacePlugin.java
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
package aQute.bnd.gradle;
import static aQute.bnd.gradle.BndUtils.unwrapFile;
import java.io.File;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import aQute.bnd.build.Workspace;
import aQute.bnd.exceptions.Exceptions;
import aQute.bnd.osgi.Constants;
import aQute.bnd.unmodifiable.Sets;
import aQute.lib.strings.Strings;
import groovy.lang.Closure;
import org.gradle.StartParameter;
import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.file.Directory;
import org.gradle.api.initialization.Settings;
import org.gradle.api.internal.plugins.DslObject;
import org.gradle.api.invocation.Gradle;
import org.gradle.api.plugins.ExtraPropertiesExtension;
import org.gradle.api.tasks.Delete;
import org.gradle.internal.metaobject.DynamicInvokeResult;
import org.gradle.internal.metaobject.DynamicObject;
import org.gradle.language.base.plugins.LifecycleBasePlugin;
/**
* BndWorkspacePlugin for Gradle.
* <p>
* The plugin name is {@code biz.aQute.bnd.workspace}.
* <p>
* This plugin can be applied to the Settings object in settings.gradle. It can
* also be applied to the root project in build.gradle.
*/
public class BndWorkspacePlugin implements Plugin<Object> {
/**
* Name of the plugin.
*/
public static final String PLUGINID = "biz.aQute.bnd.workspace";
private static final Pattern OPTION_P = Pattern.compile("--(?<option>\\w+)(?:=(?<value>\\S+)?)?");
private static final Pattern TASKNAME_SPLITTER = Pattern.compile(":");
private static final Set<String> SPECIAL_FOLDERS = Sets.of("buildSrc", "gradle");
/**
* Default public constructor.
*/
public BndWorkspacePlugin() {}
/**
* Apply the {@code biz.aQute.bnd.workspace} plugin.
*/
@Override
public void apply(Object target) {
try {
if (target instanceof Settings) {
configureSettings((Settings) target);
} else if (target instanceof Project) {
configureWorkspaceProject((Project) target);
} else {
throw new GradleException(String.format("The target %s is not a Settings or a Project", target));
}
} catch (Exception e) {
throw Exceptions.duck(e);
}
}
private void configureSettings(Settings settings) throws Exception {
DynamicObject dynamicObject = new DslObject(settings).getAsDynamicObject();
/* Start with the declared build project name */
DynamicInvokeResult result = dynamicObject.tryGetProperty("bnd_build");
String bnd_build = result.isFound() ? (String) result.getValue() : "";
String defaultProjectName = bnd_build;
/* If in a subproject, use the subproject name */
StartParameter startParameter = settings.getStartParameter();
File rootDir = settings.getRootDir();
for (File currentDir = startParameter.getCurrentDir(); !Objects.equals(currentDir,
rootDir); currentDir = currentDir.getParentFile()) {
defaultProjectName = currentDir.getName();
}
/*
* Build a set of project names we need to include from the specified
* tasks
*/
Set<String> projectNames = new LinkedHashSet<>();
for (Iterator<String> iter = startParameter.getTaskNames()
.iterator(); iter.hasNext();) {
String taskName = iter.next();
Matcher m = OPTION_P.matcher(taskName);
if (m.matches()) {
taskName = m.group("value"); // option value may be a task name
if (Objects.equals(TestOSGi.OPTION_TESTS, m.group("option"))) {
if (Objects.isNull(taskName) && iter.hasNext()) {
iter.next();
}
continue; // ignore --tests value
}
if (Objects.isNull(taskName)) {
continue; // no task name to process
}
}
String[] elements = TASKNAME_SPLITTER.split(taskName);
switch (elements.length) {
case 1 :
projectNames.add(defaultProjectName);
break;
case 2 :
projectNames.add(elements[0].isEmpty() ? bnd_build : elements[0]);
break;
default :
projectNames.add(elements[0].isEmpty() ? elements[1] : elements[0]);
break;
}
}
/*
* Include the default project name if in a subproject or no tasks
* specified
*/
if (!Objects.equals(startParameter.getCurrentDir(), rootDir) || projectNames.isEmpty()) {
projectNames.add(defaultProjectName);
}
/*
* If build used but empty, add all non-private folders of rootDir
* except special gradle folders
*/
if (projectNames.remove("")) {
for (File dir : rootDir.listFiles(File::isDirectory)) {
String projectName = dir.getName();
if (projectName.startsWith(".") || SPECIAL_FOLDERS.contains(projectName)) {
continue;
}
projectNames.add(projectName);
}
}
/* Add cnf project to the graph */
result = dynamicObject.tryGetProperty("bnd_cnf");
String cnf = result.isFound() ? (String) result.getValue() : Workspace.CNFDIR;
projectNames.add(cnf);
/* Add any projects which must always be included */
result = dynamicObject.tryGetProperty("bnd_include");
if (result.isFound()) {
Strings.splitAsStream((String) result.getValue())
.forEach(projectNames::add);
}
/* Remove any projects which must always be excluded */
result = dynamicObject.tryGetProperty("bnd_exclude");
if (result.isFound()) {
Strings.splitAsStream((String) result.getValue())
.forEach(projectNames::remove);
}
/* Remove any projects which are composite builds */
projectNames.removeIf(projectName -> {
File projectDir = new File(rootDir, projectName);
return new File(projectDir, Settings.DEFAULT_SETTINGS_FILE).isFile()
|| new File(projectDir, Settings.DEFAULT_SETTINGS_FILE + ".kts").isFile();
});
/* Initialize the Bnd workspace */
Workspace.setDriver(Constants.BNDDRIVER_GRADLE);
Workspace.addGestalt(Constants.GESTALT_BATCH, null);
Workspace workspace = new Workspace(rootDir, cnf);
workspace.setOffline(startParameter.isOffline());
Gradle gradle = settings.getGradle();
bndWorkspaceConfigure(workspace, gradle);
/*
* Prepare each project in the workspace to establish complete
* dependencies and dependents information.
*/
for (aQute.bnd.build.Project p : workspace.getAllProjects()) {
p.prepare();
}
/* Add each project and its dependents to the graph */
Set<String> projectGraph = new LinkedHashSet<>();
while (!projectNames.isEmpty()) {
String projectName = projectNames.iterator()
.next();
projectGraph.add(projectName);
aQute.bnd.build.Project p = workspace.getProject(projectName);
if (Objects.nonNull(p)) {
p.getDependents()
.stream()
.map(aQute.bnd.build.Project::getName)
.forEach(projectNames::add);
}
projectNames.removeAll(projectGraph);
}
/* Add each project and its dependencies to the graph */
projectNames = projectGraph;
projectGraph = new LinkedHashSet<>();
while (!projectNames.isEmpty()) {
String projectName = projectNames.iterator()
.next();
projectGraph.add(projectName);
aQute.bnd.build.Project p = workspace.getProject(projectName);
if (Objects.nonNull(p)) {
p.getTestDependencies()
.stream()
.map(aQute.bnd.build.Project::getName)
.forEach(projectNames::add);
}
projectNames.removeAll(projectGraph);
}
settings.include(projectGraph.toArray(new String[0]));
/* Apply workspace plugin to root project */
gradle.rootProject(project -> {
ExtraPropertiesExtension ext = project.getExtensions()
.getExtraProperties();
ext.set("bnd_cnf", cnf);
ext.set("bndWorkspace", workspace);
project.getPluginManager()
.apply(BndWorkspacePlugin.class);
});
}
private void configureWorkspaceProject(Project workspace) throws Exception {
Workspace bndWorkspace = getBndWorkspace(workspace);
/* Configure the Bnd projects */
for (Project project : workspace.getSubprojects()) {
if (Objects.nonNull(bndWorkspace.getProject(project.getName()))) {
project.getPluginManager()
.apply(BndPlugin.class);
}
}
}
/**
* Return the Bnd Workspace for the specified Gradle project.
*
* @param workspace The Gradle project for the workspace.
* @return The Bnd Workspace for the specified Gradle project.
* @throws Exception An exception while finding the Bnd Workspace.
*/
public static Workspace getBndWorkspace(Project workspace) throws Exception {
ExtraPropertiesExtension ext = workspace.getExtensions()
.getExtraProperties();
/* Initialize the Bnd workspace */
String bnd_cnf = (String) workspace.findProperty("bnd_cnf");
if (Objects.isNull(bnd_cnf)) {
// if not passed from settings
bnd_cnf = Workspace.CNFDIR;
ext.set("bnd_cnf", bnd_cnf);
}
Workspace bndWorkspace = (Workspace) workspace.findProperty("bndWorkspace");
if (Objects.isNull(bndWorkspace)) {
// if not passed from settings
Workspace.setDriver(Constants.BNDDRIVER_GRADLE);
Workspace.addGestalt(Constants.GESTALT_BATCH, null);
Gradle gradle = workspace.getGradle();
File rootDir = unwrapFile(workspace.getLayout()
.getProjectDirectory());
bndWorkspace = new Workspace(rootDir, bnd_cnf);
bndWorkspace.setOffline(gradle.getStartParameter()
.isOffline());
ext.set("bndWorkspace", bndWorkspace);
bndWorkspaceConfigure(bndWorkspace, gradle);
}
/* Configure cnf project */
Project cnfProject = (Project) workspace.findProperty("cnf");
if (Objects.isNull(cnfProject)) {
cnfProject = workspace.findProject(bnd_cnf);
if (Objects.nonNull(cnfProject)) {
ext.set("cnf", cnfProject);
Directory cacheDir = cnfProject.getLayout()
.getProjectDirectory()
.dir("cache");
cnfProject.getTasks()
.register("cleanCache", Delete.class, t -> {
t.setDescription("Clean the cnf cache folder.");
t.setGroup(LifecycleBasePlugin.BUILD_GROUP);
t.delete(cacheDir);
});
}
}
return bndWorkspace;
}
private static void bndWorkspaceConfigure(Workspace workspace, Gradle gradle) {
ExtraPropertiesExtension ext = new DslObject(gradle).getExtensions()
.getExtraProperties();
if (ext.has("bndWorkspaceConfigure")) {
Object bndWorkspaceConfigure = ext.get("bndWorkspaceConfigure");
if (bndWorkspaceConfigure instanceof Closure) {
@SuppressWarnings("unchecked")
Closure<?> closure = (Closure<?>) bndWorkspaceConfigure;
closure.call(workspace);
} else if (bndWorkspaceConfigure instanceof Action) {
@SuppressWarnings("unchecked")
Action<Workspace> action = (Action<Workspace>) bndWorkspaceConfigure;
action.execute(workspace);
} else {
throw new GradleException(
String.format("The bndWorkspaceConfigure %s is not a Closure or an Action", bndWorkspaceConfigure));
}
}
}
}