forked from bndtools/bnd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP2Export.java
More file actions
1067 lines (886 loc) · 31.4 KB
/
P2Export.java
File metadata and controls
1067 lines (886 loc) · 31.4 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package aQute.p2.export;
import static aQute.p2.export.P2.getBundleId;
import static aQute.p2.export.P2.IUType.other;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
import java.util.jar.Manifest;
import org.osgi.framework.Version;
import org.osgi.framework.VersionRange;
import org.osgi.framework.namespace.BundleNamespace;
import aQute.bnd.build.Container;
import aQute.bnd.build.Project;
import aQute.bnd.build.Run;
import aQute.bnd.header.Attrs;
import aQute.bnd.header.Parameters;
import aQute.bnd.osgi.Builder;
import aQute.bnd.osgi.BundleId;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Domain;
import aQute.bnd.osgi.EmbeddedResource;
import aQute.bnd.osgi.FileResource;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.JarResource;
import aQute.bnd.osgi.Processor;
import aQute.bnd.osgi.Resource;
import aQute.lib.collections.MultiMap;
import aQute.lib.io.IO;
import aQute.lib.strings.Strings;
import aQute.lib.tag.Tag;
import aQute.p2.export.P2.Artifact;
import aQute.p2.export.P2.Artifacts;
import aQute.p2.export.P2.Bundle;
import aQute.p2.export.P2.Category;
import aQute.p2.export.P2.Content;
import aQute.p2.export.P2.Feature;
import aQute.p2.export.P2.IU;
import aQute.p2.export.P2.IUType;
import aQute.p2.export.P2.Provided;
import aQute.p2.export.P2.Required;
import aQute.p2.export.P2.Rule;
import aQute.p2.provider.Signer;
/**
* An instance of this class is created for each p2 export.
*/
class P2Export {
final Project bndrun;
final Map<String, String> options;
final Provided EQ_TYPE_FEATURE = new Provided("org.eclipse.equinox.p2.eclipse.type",
getBundleId("feature", "1.0.0"), other);
final Provided EQ_TYPE_BUNDLE = new Provided("org.eclipse.equinox.p2.eclipse.type",
getBundleId("bundle", "1.0.0"), other);
final String provider;
final String update;
final String updateLabel;
final String name;
private final boolean sign;
private final String sign_key;
private final String sign_passphrase;
private final String sign_pubkey;
/**
* Publisher
*
* @param bndrun the project
* @param options any options (name, update, updateLabel, and provider)
*/
P2Export(Project bndrun, Map<String, String> options) {
this.bndrun = bndrun;
this.options = options;
this.name = options.getOrDefault("name", bndrun.getName()
.replaceAll("\\.bndrun$", ".jar"));
this.update = bndrun.get("update");
this.updateLabel = bndrun.getProperty("update.label", "Update");
this.provider = bndrun.getProperty("Bundle-Vendor", "bnd");
this.sign = Project.isTrue(options.get("sign"));
this.sign_key = options.get("sign_key");
this.sign_passphrase = options.get("sign_passphrase");
this.sign_pubkey = options.get("sign_pubkey");
}
Map.Entry<String, Resource> generate() throws Exception {
P2 p2 = parse();
if (p2 == null || !bndrun.isOk()) {
bndrun.check();
return null;
}
@SuppressWarnings("resource")
Builder builder = new Builder(bndrun);
Jar jar = builder.build();
jar.setReproducible("true");
jar.putResource("content.jar", generateContent(p2));
jar.putResource("artifacts.jar", generateArtifacts(p2, jar));
jar.putResource("p2.index", generateP2Index());
return new AbstractMap.SimpleEntry<String, Resource>("p2", new JarResource(jar));
}
private Resource generateP2Index() {
// see https://wiki.eclipse.org/Equinox/p2/p2_index
String index = """
version=1
metadata.repository.factory.order=content.xml,\\!
artifact.repository.factory.order=artifacts.xml,\\!""";
return new EmbeddedResource(index, 0);
}
private Resource generateArtifacts(P2 p2, Jar jar) {
Signer signer = createSigner();
String pgp_publicKeys = signer != null ? this.sign_pubkey : null;
Tag repository = new Tag("repository");
repository.addAttribute("name", p2.name);
repository.addAttribute("type", "org.eclipse.equinox.p2.artifact.repository.simpleRepository");
repository.addAttribute("version", "1");
properties(repository, //
"p2.timestamp", System.currentTimeMillis(), //
"p2.compresssed", true, //
"pgp.publicKeys", pgp_publicKeys);
Set<String> addedUpdateUrls = new HashSet<>();
Tag references = new Tag(repository, "references");
if (update != null) {
addedUpdateUrls.add(update);
new Tag(references, "repository")//
.addAttribute("uri", update)
.addAttribute("url", update)
.addAttribute("type", 1)
.addAttribute("options", 0);
new Tag(references, "repository")//
.addAttribute("uri", update)
.addAttribute("url", update)
.addAttribute("type", 0)
.addAttribute("options", 0);
}
p2.content.units.stream()
.distinct()
.sorted()
.forEach(iu -> {
if (iu instanceof Feature f) {
if (f.update != null && addedUpdateUrls.add(f.update)) {
new Tag(references, "repository")//
.addAttribute("uri", f.update)
.addAttribute("url", f.update)
.addAttribute("type", 1)
.addAttribute("options", 1);
new Tag(references, "repository")//
.addAttribute("uri", f.update)
.addAttribute("url", f.update)
.addAttribute("type", 0)
.addAttribute("options", 1);
}
}
});
size(references);
Tag mappings = new Tag(repository, "mappings");
new Tag(mappings, "rule")//
.addAttribute("filter", "(&(classifier=osgi.bundle))")
.addAttribute("output", "${repoUrl}/plugins/${id}_${version}.jar");
new Tag(mappings, "rule")//
.addAttribute("filter", "(&(classifier=org.eclipse.update.feature))")
.addAttribute("output", "${repoUrl}/features/${id}_${version}.jar");
size(mappings);
Tag artifacts = new Tag(repository, "artifacts");
for (Artifact a : p2.artifacts.artifacts) {
String classifier = a.iu.getRefType();
if (classifier == null) {
continue;
}
Tag artifact = new Tag(artifacts, "artifact") //
.addAttribute("classifier", classifier)
.addAttribute("id", a.iu.id.getBsn())
.addAttribute("version", a.iu.id.getVersion());
String signature = sign(signer, a);
Tag properties = properties(artifact, //
"artifact.size", a.length, //
"download.size", a.length, //
"download.md5", a.md5(), //
"download.checksum.md5", a.md5(), //
"download.checksum.sha-256", a.sha256(), //
"pgp.signatures", signature);
jar.putResource(a.getPath(), a.resource);
}
size(artifacts);
return wrap("artifacts.jar", "artifacts.xml", repository);
}
private JarResource generateContent(P2 p2) {
Tag content = new Tag("repository");
content.addAttribute("name", name);
content.addAttribute("type", "org.eclipse.equinox.internal.p2.metadata.repository.LocalMetadataRepository");
content.addAttribute("version", "1.0.0");
properties(content, //
"p2.timestamp", System.currentTimeMillis(), //
"p2.compressed", true);
Set<String> addedUpdateUrls = new HashSet<>();
Tag references = new Tag(content, "references");
if (update != null) {
addedUpdateUrls.add(update);
new Tag(references, "repository")//
.addAttribute("uri", update)
.addAttribute("url", update)
.addAttribute("type", 1)
.addAttribute("options", 0);
new Tag(references, "repository")//
.addAttribute("uri", update)
.addAttribute("url", update)
.addAttribute("type", 0)
.addAttribute("options", 0);
}
p2.content.units.stream()
.distinct()
.sorted()
.forEach(iu -> {
if (iu instanceof Feature f) {
if (f.update != null && addedUpdateUrls.add(f.update)) {
new Tag(references, "repository")//
.addAttribute("uri", f.update)
.addAttribute("url", f.update)
.addAttribute("type", 1)
.addAttribute("options", 1);
new Tag(references, "repository")//
.addAttribute("uri", f.update)
.addAttribute("url", f.update)
.addAttribute("type", 0)
.addAttribute("options", 1);
}
}
});
size(references);
Tag units = new Tag(content, "units");
p2.content.units.stream()
.distinct()
.sorted()
.forEach(iu -> {
if (iu instanceof Feature f) {
doFeatureUnit(units, f);
} else if (iu instanceof Bundle b) {
doBundleUnit(units, b);
} else if (iu instanceof Category c) {
doCategoryUnit(units, c);
} else {
bndrun.error("Unknown unit type %s", iu);
}
});
size(units);
return wrap("content.jar", "content.xml", content);
}
void doFeatureUnit(Tag units, Feature f) {
{
Tag groupUnit = new Tag(units, "unit")//
.addAttribute("id", f.groupId.getBsn()) //
.addAttribute("version", f.id.getVersion())//
.addAttribute("singleton", "false");
update(groupUnit, f.groupId);
Tag ps = properties(groupUnit, //
"org.eclipse.equinox.p2.name", f.getName(), //
"org.eclipse.equinox.p2.description", f.getDescription(), //
"org.eclipse.equinox.p2.description.url", f.getDocUrl(), //
"org.eclipse.equinox.p2.provider", provider, //
"org.eclipse.equinox.p2.type.group", true);
Tag provides = new Tag(groupUnit, "provides");
provided(provides, "org.eclipse.equinox.p2.iu", f.groupId);
Tag requires = new Tag(groupUnit, "requires");
for (Required k : f.requires) {
required(requires, k);
}
Tag required = required(requires,
new Required("org.eclipse.equinox.p2.iu", toExact(f.jarId), IUType.other, false));
new Tag(required, "filter", "(org.eclipse.update.install.features=true)");
doLegal(groupUnit, f);
size(requires, "namespace", "name", "version");
size(provides, "namespace", "name", "version");
size(ps);
}
{
Tag jarUnit = new Tag(units, "unit")//
.addAttribute("id", f.jarId.getBsn()) //
.addAttribute("version", f.id.getVersion())//
.addAttribute("singleton", "false");
Tag ps = properties(jarUnit, //
"org.eclipse.equinox.p2.name", f.getName(), //
"org.eclipse.equinox.p2.description", f.getDescription(), //
"org.eclipse.equinox.p2.description.url", f.getDocUrl(), //
"org.eclipse.equinox.p2.provider", provider, //
"org.eclipse.update.feature.plugin", f.plugin);
Tag provides = new Tag(jarUnit, "provides");
provided(provides, "org.eclipse.equinox.p2.iu", f.jarId);
provided(provides, EQ_TYPE_FEATURE);
provided(provides, "org.eclipse.update.feature", f.id);
new Tag(jarUnit, "filter", "(org.eclipse.update.install.features=true)");
Tag artifacts = new Tag(jarUnit, "artifacts");
artifact(artifacts, "org.eclipse.update.feature", f.id);
touchpoint(jarUnit, "org.eclipse.equinox.p2.osgi", "zipped", true);
doLegal(jarUnit, f);
size(ps);
size(provides, "namespace", "name", "version");
size(artifacts);
}
}
void doBundleUnit(Tag units, Bundle b) {
try {
Tag unit = new Tag(units, "unit")//
.addAttribute("id", b.id.getBsn()) //
.addAttribute("version", b.id.getVersion())//
.addAttribute("singleton", b.isSingleton());
update(unit, b.id);
Tag properties = properties(unit, //
"org.eclipse.equinox.p2.name", b.getName(), //
"org.eclipse.equinox.p2.description", b.getDescription(), //
"org.eclipse.equinox.p2.description.url", b.getDocUrl(), //
"org.eclipse.equinox.p2.provider", provider, //
"org.eclipse.equinox.p2.doc.url", b.getDocUrl());
Tag provides = new Tag(unit, "provides");
provided(provides, "org.eclipse.equinox.p2.iu", b.id);
provided(provides, "osgi.bundle", b.id);
for (Provided k : b.provides) {
provided(provides, k);
}
provided(provides, EQ_TYPE_BUNDLE);
Tag requires = new Tag(unit, "requires");
for (Required k : b.requires) {
required(requires, k);
}
Tag artifacts = new Tag(unit, "artifacts");
artifact(artifacts, "osgi.bundle", b.id);
touchpoint(unit, "org.eclipse.equinox.p2.osgi", "manifest", b.getManifest());
size(requires, "namespace", "name", "version");
size(provides, "namespace", "name", "version");
size(artifacts);
} catch (Exception e) {
bndrun.error("failed to create unit for %s: %s", b, e, e);
}
}
void doCategoryUnit(Tag units, Category c) {
Tag unit = new Tag(units, "unit")//
.addAttribute("id", c.id.getBsn()) //
.addAttribute("version", c.id.getVersion())//
.addAttribute("singleton", "false");
Tag ps = properties(unit, //
"org.eclipse.equinox.p2.name", c.getName(), //
"org.eclipse.equinox.p2.description", c.getDescription(), //
"org.eclipse.equinox.p2.description.url", c.getDocUrl(), //
"org.eclipse.equinox.p2.type.category", true);
Tag provides = new Tag(unit, "provides");
provided(provides, "org.eclipse.equinox.p2.iu", c.id);
Tag requires = new Tag(unit, "requires");
c.requires.stream()
.filter(r -> r.type == IUType.feature)
.map(Required.class::cast)
.forEach(r -> {
required(requires, r);
});
size(ps);
size(requires, "namespace", "name", "version");
size(provides, "namespace", "name", "version");
}
private void touchpoint(Tag unit, String type, String key, Object v) {
new Tag(unit, "touchpoint")//
.addAttribute("id", type)//
.addAttribute("version", "1.0.0");
Tag touchpointData = new Tag(unit, "touchpointData");
Tag instructions = new Tag(touchpointData, "instructions");
Tag instruction = new Tag(instructions, "instruction", v).addAttribute("key", key);
size(instructions);
size(touchpointData);
}
private void update(Tag parent, BundleId id) {
Tag update = new Tag(parent, "update");
update.addAttribute("id", id.getBsn());
update.addAttribute("range", toUpdateRange(id.getVersion()));
update.addAttribute("severity", "0");
}
@SuppressWarnings("unused")
private VersionRange getExactRange(String version) {
Version low = new Version(version);
Version high = new Version(version);
return new VersionRange('[', low, high, ']');
}
private void artifact(Tag parent, String namespace, BundleId id) {
new Tag(parent, "artifact")//
.addAttribute("classifier", namespace) //
.addAttribute("id", id.getBsn())//
.addAttribute("version", id.getVersion());
}
private void provided(Tag requires, String namespace, BundleId r) {
new Tag(requires, "provided")//
.addAttribute("namespace", namespace)//
.addAttribute("name", r.getBsn())//
.addAttribute("version", r.getVersion());
}
private void provided(Tag provides, Provided p) {
provided(provides, p.namespace, p.id);
}
private Tag required(Tag requires, Required r) {
return required(requires, r.namespace, r.id, r.optional);
}
private Tag required(Tag requires, String namespace, BundleId id, boolean optional) {
Tag required = new Tag(requires, "required")//
.addAttribute("namespace", namespace)//
.addAttribute("name", id.getBsn())//
.addAttribute("range", id.getVersion());
if (optional) {
required.addAttribute("optional", true);
}
return required;
}
private Artifact generateFeature(Feature feature) throws Exception {
// Feature schema: It was a bit hard to find:
// https://help.eclipse.org/latest/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2Fproduct_def_feature.htm
// and
// https://help.eclipse.org/latest/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fmisc%2Ffeature_manifest.html
// and
// https://archive.eclipse.org/eclipse/downloads/documentation/2.0/html/plugins/org.eclipse.platform.doc.isv/reference/misc/feature_archive.html
Tag f = new Tag("feature");
f.addAttribute("id", feature.id.getBsn());
f.addAttribute("version", feature.id.getVersion());
f.addAttribute("label", feature.getName());
f.addAttribute("provider-name", provider);
doDescription(f, feature);
doLegalFeature(f, feature);
Tag url = new Tag(f, "url");
if (feature.update != null) {
Tag tUpdate = new Tag(url, "update");
tUpdate.addAttribute("url", feature.update);
if (feature.updateLabel != null)
tUpdate.addAttribute("label", feature.updateLabel);
Tag tDiscovery = new Tag(url, "discovery");
tDiscovery.addAttribute("url", feature.update);
if (feature.updateLabel != null)
tDiscovery.addAttribute("label", feature.updateLabel);
}
Tag requires = new Tag(f, "requires");
for (Required require : feature.requires) {
if (require.type == IUType.feature) {
new Tag(requires, "import")//
.addAttribute("feature", require.id.getBsn())//
.addAttribute("version", require.range.getLeft()) //
.addAttribute("match", getMatch(require.range));
}
}
for (Required require : feature.requires) {
if (require.type == IUType.bundle) {
new Tag(f, "plugin")//
.addAttribute("id", require.id.getBsn())//
.addAttribute("version", require.range) //
.addAttribute("unpack", false);
}
}
JarResource wrap = wrap(feature.id.getBsn() + ".jar", "feature.xml", f);
return new Artifact(feature, feature.groupId, Collections.emptyMap(), wrap);
}
/**
* The import element within a requires element in a feature.xml file is
* used to express a dependency on another plug-in or feature. The match
* attribute of the import element is used to specify the type of version
* matching that should be used when resolving this dependency. Here are the
* possible values for the match attribute:
* <ul>
* <li>perfect: The version of the dependency must match exactly the version
* specified in the version attribute of the import element.
* <li>equivalent: The major and minor parts of the version must match
* exactly. The service level (third segment) of the dependency's version
* must be greater or equal to the one specified.
* <li>compatible: The major part of the version must match exactly. The
* minor part of the dependency's version must be greater or equal to the
* one specified. This is the default value if the match attribute is not
* specified.
* <li>greaterOrEqual: The version of the dependency must be greater or
* equal to the one specified. perfectOrGreater: The version must either
* match perfectly or be greater than the one specified.
* </ul>
*
* @param range the version range
* @return the match value
*/
private String getMatch(VersionRange range) {
Version low = range.getLeft();
Version high = range.getRight();
if (high == null)
high = low;
// perfect requires [1.2.3,1.2.3]
if (low.equals(high) && range.getLeftType() == VersionRange.LEFT_CLOSED
&& range.getRightType() == VersionRange.RIGHT_CLOSED)
return "perfect";
// equivalent
if (low.getMajor() == high.getMajor() && low.getMinor() == high.getMinor())
return "equivalent";
// compatible
if (low.getMajor() + 1 == high.getMajor() && range.getLeftType() == VersionRange.LEFT_CLOSED
&& range.getRightType() == VersionRange.RIGHT_OPEN)
return "compatible";
return "greaterOrEqual";
}
/**
* -export p2;features="a.bndrun,b.bndrun";output=target/p2
* <p>
* In bndrun
* <p>
* -p2.feature.required = org.eclipse.platform;type=feature;version=4.25
*
* @param reporter
* @param name
* @param options
* @param workspace
* @return
*/
P2 parse() {
List<String> featurePaths = Strings.splitQuoted(bndrun.get("features"));
MultiMap<String, Feature> categories = new MultiMap<>();
Map<String, Feature> features = new LinkedHashMap<>();
Map<BundleId, Bundle> bundles = new LinkedHashMap<>();
List<URI> references = new ArrayList<>();
Set<IU> units = new TreeSet<>();
List<Artifact> artifacts = new ArrayList<>();
List<Rule> mappings = new ArrayList<>();
for (String featurePath : featurePaths) {
File featureFile = bndrun.getFile(featurePath);
try {
if (!featureFile.isFile()) {
bndrun.error("no such feature file %s", featureFile);
continue;
}
Run definition = Run.createRun(bndrun.getWorkspace(), featureFile);
definition.setParent(bndrun);
BundleId featureId = getFeatureId(definition);
List<Required> requires = new ArrayList<>();
List<Provided> provides = new ArrayList<>();
for (Container c : definition.getRunbundles()) {
if (c.getError() != null) {
bndrun.error("Feature %s dependency %s has an error", featureFile, c);
} else {
BundleId bundleId = c.getBundleId();
Bundle b = bundles.computeIfAbsent(bundleId, k -> {
try {
return new Bundle(k, Domain.domain(c.getManifest()), c.getAttributes(),
c.getManifest());
} catch (Exception e) {
bndrun.error("failed to retrieve manifest from bundle %s: %s", c, e);
return null;
}
});
units.add(b);
BundleId range = toExact(bundleId);
Map<String, String> attributes = c.getAttributes();
boolean optional = attributes != null
? BundleNamespace.RESOLUTION_OPTIONAL
.equals(attributes.get(BundleNamespace.REQUIREMENT_RESOLUTION_DIRECTIVE))
: false;
requires.add(new Required("org.eclipse.equinox.p2.iu", range, IUType.bundle, optional));
Artifact a = new Artifact(b, b.id, c.getAttributes(), new FileResource(c.getFile()));
artifacts.add(a);
}
}
VersionRange defaultRange = exact(new Version(featureId.getVersion()));
parseRequired(requires, definition, defaultRange.toString());
parseProvided(provides, definition);
Feature feature = new Feature(featureId, definition, provides, requires, definition.get("-p2.plugin"),
definition.get("update"), definition.get("update.label"));
units.add(feature);
Artifact art = generateFeature(feature);
artifacts.add(art);
for (String category : feature.getCategory()) {
categories.add(category, feature);
}
bndrun.getInfo(definition, featureFile.getName() + ": ");
} catch (FileNotFoundException e) {} catch (Exception e) {
bndrun.error("%s", e.getMessage());
}
}
Parameters categoryDescriptions = new Parameters(bndrun.mergeProperties("-categories"));
categories.forEach((k, v) -> {
List<Required> requires = v.stream()
.map(f -> new Required("org.eclipse.equinox.p2.iu", f.groupId, IUType.feature, false))
.toList();
Attrs attrs = categoryDescriptions.getOrDefault(k, new Attrs());
String label = attrs.get("label", k);
String description = attrs.get("description", "");
Category c = new Category(bndrun, k, label, description, requires);
units.add(c);
});
Artifacts artifact = new Artifacts(name, mappings, artifacts);
Content content = new P2.Content(name, references, units);
return new P2(name, content, artifact, categories, "", "");
}
private void parseRequired(List<Required> prs, Processor definition, String defaultRange) {
String required = definition.mergeProperties(Constants.REQUIRE_CAPABILITY);
Parameters pars = new Parameters(required);
for (Map.Entry<String, Attrs> e : pars.entrySet())
try {
String namespace = Processor.removeDuplicateMarker(e.getKey());
Attrs attrs = e.getValue();
String range = attrs.getVersion();
if (range == null) {
range = defaultRange;
}
boolean optional = P2.isOptional(attrs);
String bsn = attrs.get("name");
BundleId id = getBundleId(bsn, range);
switch (namespace) {
case "feature" :
BundleId groupId = new BundleId(id.getBsn() + ".feature.group", id.getVersion());
prs.add(new Required("org.eclipse.equinox.p2.iu", groupId, IUType.feature, optional));
break;
case "osgi.wiring.bundle" :
case "bundle" :
prs.add(new Required("org.eclipse.equinox.p2.iu", toExact(id), IUType.bundle, optional));
break;
case "osgi.wiring.package" :
case "package" :
case "java.package" :
prs.add(new Required("java.package", toCompatible(id), IUType.other, optional));
break;
default :
prs.add(new Required(namespace, id, IUType.other, optional));
break;
}
} catch (Exception ee) {
ee.printStackTrace();
}
}
private void parseProvided(List<Provided> prs, Processor definition) {
String provided = definition.mergeProperties(Constants.PROVIDE_CAPABILITY);
Parameters pars = new Parameters(provided);
for (Map.Entry<String, Attrs> e : pars.entrySet())
try {
String namespace = Processor.removeDuplicateMarker(e.getKey());
Attrs attrs = e.getValue();
String version = attrs.getVersion();
String bsn = attrs.get("name");
BundleId id = getBundleId(bsn, version);
switch (namespace) {
case "feature" :
prs.add(new Provided("org.eclipse.equinox.p2.iu", id, IUType.feature));
break;
case "bundle" :
prs.add(new Provided("org.eclipse.equinox.p2.iu", id, IUType.bundle));
break;
default :
prs.add(new Provided(namespace, id, IUType.other));
break;
}
} catch (Exception ee) {
ee.printStackTrace();
}
}
private Tag properties(Tag parent, Object... pairs) {
Tag properties = new Tag(parent, "properties");
for (int i = 0; i < pairs.length; i += 2) {
String key = (String) pairs[i];
Object value = pairs[i + 1];
if (value != null) {
new Tag(properties, "property").addAttribute("name", key)
.addAttribute("value", value);
}
}
size(properties);
return properties;
}
private void size(Tag tag, String... sorters) {
List<Object> contents = tag.getContents();
if (sorters.length > 0) {
Collections.sort(contents, (a, b) -> {
if (a instanceof Tag aa && b instanceof Tag bb) {
for (String sorter : sorters) {
String aaa = aa.getAttribute(sorter);
String bbb = bb.getAttribute(sorter);
if (aaa == bbb)
continue;
if (aaa == null)
return -1;
if (bbb == null)
return 1;
int result = aaa.compareTo(bbb);
if (result == 0)
continue;
return result;
}
}
return 0;
});
}
int size = contents.size();
if (size == 0)
tag.remove();
else
tag.addAttribute("size", size);
}
private JarResource wrap(String jarName, String xmlName, Tag xml) {
Jar jar = new Jar(jarName);
jar.setReproducible("true");
jar.setManifest((Manifest) null);
jar.setDoNotTouchManifest();
jar.putResource(xmlName, toResource(xml));
return new JarResource(jar);
}
private Resource toResource(Tag xml) {
byte[] bytes = xml.toBytes();
return new EmbeddedResource(bytes, 0);
}
private String toUpdateRange(String version) {
return "[0.0.0," + version + ")";
}
private void doDescription(Tag f, IU feature) {
Tag description = new Tag(f, "description", feature.getDescription());
description.setCDATA();
String url = feature.getDescriptionUrl();
if (url != null)
description.addAttribute("url", url);
}
/**
* Adds license information for a <unit> inside content.xml (Note: this is
* different than {@link #doLegalFeature(Tag, IU)})
*/
private void doLegal(Tag u, IU unit) {
if (unit.getLicense() != null) {
Tag licenses = new Tag(u, "licenses");
Parameters ls = new Parameters(unit.getLicense());
ls.forEach((k, v) -> {
Tag license = new Tag(licenses, "license");
license.setCDATA();
String link = v.get("link");
String description = v.get("description");
// better than description
// for large files
String licensefilename = v.get("file");
license.addAttribute("url", link != null ? link : k);
license.addAttribute("uri", link != null ? link : k);
if (licensefilename != null) {
try {
license.addContent(IO.collect(bndrun.getFile(licensefilename)));
} catch (IOException e) {
license.addContent("Error: Cannot read license file: " + licensefilename);
}
} else {
license.addContent(description != null ? description : v.toString());
}
});
size(licenses);
}
if (unit.getCopyright() != null) {
Tag copyright = new Tag(u, "copyright", unit.getCopyright());
copyright.setCDATA();
}
}
/**
* Adds license information inside feature.xml
*/
private void doLegalFeature(Tag f, IU feature) {
if (feature.getCopyright() != null) {
Tag copyright = new Tag(f, "copyright", feature.getCopyright());
copyright.setCDATA();
}
if (feature.getLicense() != null) {
Tag license = new Tag(f, "license");
license.setCDATA();
Parameters ls = new Parameters(feature.getLicense());
Entry<String, Attrs> first = ls.stream()
.findFirst()
.orElse(null);
String k = first.getKey();
Attrs v = first.getValue();
String link = v.get("link");
String description = v.get("description");
// better than description
// for large files
String licensefilename = v.get("file");
license.addAttribute("url", link != null ? link : k);
if (licensefilename != null) {
try {
license.addContent(IO.collect(bndrun.getFile(licensefilename)));
} catch (IOException e) {
license.addContent("Error: Cannot read license file: " + licensefilename);
}
} else {
license.addContent(description != null ? description : v.toString());
}
}
}
private BundleId getFeatureId(Processor definition) {
Entry<String, Attrs> entry = definition.getBundleSymbolicName();
String featureName;
if (entry != null) {
featureName = entry.getKey();
} else {
featureName = definition.getPropertiesFile()
.getName()
.replaceAll("\\.bndrun$", "");
}
String version = definition.getBundleVersion();
if (version == null) {
version = bndrun.getBundleVersion();
if (version == null) {
version = "0.0.0";
}
}
version = Builder.doSnapshot(version, definition.get(Constants.SNAPSHOT));
return getBundleId(featureName, version);
}
private VersionRange exact(Version low) {
return new VersionRange('[', low, low, ']');
}
private VersionRange compatible(Version low) {
Version high = new Version(low.getMajor() + 1, 0, 0);
return new VersionRange('[', low, high, ')');
}
private BundleId toExact(BundleId id) {
return new BundleId(id.getBsn(), toExact(id.getVersion()));
}
private BundleId toCompatible(BundleId id) {
return new BundleId(id.getBsn(), toCompatible(id.getVersion()));
}
private String toExact(String version) {
if (version == null)
version = "0.0.0";
VersionRange r = VersionRange.valueOf(version);
boolean hasupperbound = r.getRight() != null;
if (hasupperbound)
return r.toString();
return exact(r.getLeft()).toString();
}
private String toCompatible(String version) {
if (version == null)
version = "0.0.0";
VersionRange r = VersionRange.valueOf(version);
boolean hasupperbound = r.getRight() != null;