forked from bndtools/bnd
/
Tool.java
298 lines (254 loc) · 8.59 KB
/
Tool.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
package aQute.bnd.repository.maven.provider;
import static aQute.bnd.exceptions.ConsumerWithException.asConsumer;
import static java.util.stream.Collectors.toList;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import aQute.bnd.header.Parameters;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Descriptors;
import aQute.bnd.osgi.Domain;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.Processor;
import aQute.bnd.osgi.Resource;
import aQute.bnd.version.Version;
import aQute.lib.io.FileTree;
import aQute.lib.io.IO;
import aQute.lib.strings.Strings;
import aQute.lib.tag.Tag;
import aQute.lib.utf8properties.UTF8Properties;
import aQute.libg.command.Command;
import aQute.maven.api.Archive;
public class Tool extends Processor {
private static final String OSGI_OPT_SRC = "OSGI-OPT/src";
private static final String OSGI_OPT_SRC_PREFIX = OSGI_OPT_SRC + "/";
private final Jar jar;
private final File tmp;
private final File sources;
private final File javadoc;
private final File javadocOptions;
public Tool(Processor parent, Jar jar) throws Exception {
super(parent);
this.jar = jar;
tmp = Files.createTempDirectory("tool")
.toFile();
sources = new File(tmp, "sources");
javadoc = new File(tmp, "javadoc");
javadocOptions = new File(tmp, "javadoc.options");
}
void setSources(Jar sourcesJar, String prefix) throws Exception {
IO.delete(sources);
IO.mkdirs(sources);
final int prefix_length = prefix.length();
for (Entry<String, Resource> e : sourcesJar.getResources()
.entrySet()) {
String path = e.getKey();
if (!path.startsWith(prefix)) {
continue;
}
File out = IO.getFile(sources, path.substring(prefix_length));
IO.mkdirs(out.getParentFile());
e.getValue()
.write(out);
}
}
public boolean hasSources() {
return sources.isDirectory() || jar.hasDirectory(OSGI_OPT_SRC);
}
public Jar doJavadoc(Map<String, String> options, boolean exportsOnly) throws Exception {
prepareSource(Collections.emptyMap());
if (!sources.isDirectory()) {
return new Jar(Archive.JAVADOC_CLASSIFIER);
}
IO.mkdirs(javadoc);
try (PrintWriter writer = IO.writer(javadocOptions)) {
writer.println("-quiet");
writer.println("-notimestamp"); // reproducibility
writer.println("-protected");
writer.printf("%s '%s'%n", "-d", fileName(javadoc));
writer.println("-charset 'UTF-8'");
writer.printf("%s '%s'%n", "-sourcepath", fileName(sources));
Properties pp = new UTF8Properties();
pp.putAll(options);
Domain manifest = Domain.domain(jar.getManifest());
String name = manifest.getBundleName();
if (name == null)
name = manifest.getBundleSymbolicName()
.getKey();
String version = manifest.getBundleVersion();
if (version == null)
version = Version.LOWEST.toString();
String bundleDescription = manifest.getBundleDescription();
if (bundleDescription != null && !Strings.trim(bundleDescription)
.isEmpty()) {
printOverview(manifest, name, version, bundleDescription);
}
set(pp, "-doctitle", name);
set(pp, "-windowtitle", name);
set(pp, "-header", manifest.getBundleVendor());
set(pp, "-bottom", manifest.getBundleCopyright());
set(pp, "-footer", manifest.getBundleDocURL());
writer.println("-tag 'Immutable:t:\"Immutable\"'");
writer.println("-tag 'ThreadSafe:t:\"ThreadSafe\"'");
writer.println("-tag 'NotThreadSafe:t:\"NotThreadSafe\"'");
writer.println("-tag 'GuardedBy:mf:\"Guarded By:\"'");
writer.println("-tag 'security:m:\"Required Permissions\"'");
writer.println("-tag 'noimplement:t:\"Consumers of this API must not implement this interface\"'");
for (Enumeration<?> e = pp.propertyNames(); e.hasMoreElements();) {
String key = (String) e.nextElement();
String value = pp.getProperty(key);
if (key.startsWith("-")) {
//
// Allow people to add the same command multiple times
// by suffixing it with '.' something
//
int n = key.lastIndexOf('.');
if (n > 0) {
key = key.substring(0, n);
}
writer.printf("%s '%s'%n", key, escape(value));
}
}
FileTree sourcefiles = new FileTree();
if (exportsOnly) {
Parameters exports = manifest.getExportPackage();
exports.keySet()
.stream()
.map(packageName -> Descriptors.fqnToBinary(packageName) + "/*.java")
.forEach(sourcefiles::addIncludes);
}
sourcefiles.stream(sources, "**/*.java")
.forEachOrdered(f -> writer.printf("'%s'%n", fileName(f)));
}
Command command = new Command();
command.add(getJavaExecutable("javadoc"));
command.add("@" + fileName(javadocOptions));
StringBuilder out = new StringBuilder();
StringBuilder err = new StringBuilder();
int result = command.execute(out, err);
if (result != 0) {
warning("Error during execution of javadoc command: %s\n******************\n%s", out, err);
}
return new Jar(Archive.JAVADOC_CLASSIFIER, javadoc);
}
private String fileName(File f) {
String result = IO.absolutePath(f);
return result;
}
private String escape(String input) {
return input.replace("\\", "\\\\")
.replace(System.getProperty("line.separator"), "\\" + System.getProperty("line.separator"));
}
private void printOverview(Domain manifest, String name, String version, String bundleDescription)
throws FileNotFoundException {
Tag body = new Tag("body");
new Tag(body, "h1", name);
new Tag(body, "p", "Version " + version);
new Tag(body, "p", bundleDescription);
Tag table = new Tag(body, "table");
for (String key : manifest) {
if (key.equalsIgnoreCase(Constants.BUNDLE_DESCRIPTION) || key.equalsIgnoreCase(Constants.BUNDLE_VERSION))
continue;
Tag tr = new Tag(table, "tr");
new Tag(tr, "td", key);
new Tag(tr, "td", manifest.get(key));
}
File overview = new File(sources, "overview.html");
try (PrintWriter pw = new PrintWriter(overview)) {
body.print(2, pw);
}
}
private void set(Properties pp, String key, String value) {
if (value == null)
return;
if (pp.containsKey(key))
return;
pp.put(key, value);
}
private static final String[] PACKAGE_FILES = {
"packageinfo", "package.html"
};
private static final String PATH_SEPARATORS = File.pathSeparator.concat(",");
private void prepareSource(Map<String, String> options) throws Exception {
if (sources.isDirectory()) { // extract source if not already present
return;
}
if (jar.hasDirectory(OSGI_OPT_SRC)) {
setSources(jar, OSGI_OPT_SRC_PREFIX);
return;
}
IO.delete(sources);
IO.mkdirs(sources);
String sourcepath = options.get(Constants.SOURCEPATH);
if (sourcepath == null) {
sourcepath = getProperty(Constants.SOURCEPATH);
}
// We split the dirs on path separators or comma
List<File> sourceDirs = Strings.splitQuotedAsStream(sourcepath, PATH_SEPARATORS)
.map(this::getFile)
.filter(File::isDirectory)
.collect(toList());
if (sourceDirs.isEmpty()) {
return;
}
Set<String> packagePaths = new HashSet<>();
jar.getResourceNames(name -> name.endsWith(".class"))
.forEach(asConsumer(name -> {
int n = name.lastIndexOf('/');
String packagePath = (n < 0) ? "" : name.substring(0, n + 1);
String sourcePath = name.substring(0, name.length() - 5)
.concat("java");
for (File dir : sourceDirs) {
File sourceFile = IO.getFile(dir, sourcePath);
if (sourceFile.isFile()) {
packagePaths.add(packagePath);
File targetFile = IO.getFile(sources, sourcePath);
IO.mkdirs(targetFile.getParentFile());
IO.copy(sourceFile, targetFile);
}
}
}));
packagePaths.stream()
.flatMap(packagePath -> Arrays.stream(PACKAGE_FILES)
.map(packagePath::concat))
.forEach(asConsumer(sourcePath -> {
for (File dir : sourceDirs) {
File sourceFile = IO.getFile(dir, sourcePath);
if (sourceFile.isFile()) {
File targetFile = IO.getFile(sources, sourcePath);
IO.mkdirs(targetFile.getParentFile());
IO.copy(sourceFile, targetFile);
}
}
}));
}
public Jar doSource(Map<String, String> options) throws Exception {
prepareSource(options);
if (!sources.isDirectory()) {
return new Jar(Archive.SOURCES_CLASSIFIER);
}
return new Jar(Archive.SOURCES_CLASSIFIER, sources);
}
public Jar doSource() throws Exception {
return doSource(Collections.emptyMap());
}
@Override
public void close() throws IOException {
try {
super.close();
} finally {
IO.delete(tmp);
}
}
}