35
35
import java .nio .file .StandardCopyOption ;
36
36
import java .util .ArrayList ;
37
37
import java .util .Arrays ;
38
+ import java .util .Collection ;
38
39
import java .util .Collections ;
39
40
import java .util .HashMap ;
40
41
import java .util .List ;
@@ -61,16 +62,19 @@ final class BundleSupport {
61
62
static final String BUNDLE_OPTION = "--bundle" ;
62
63
63
64
enum BundleStatus {
64
- prepare (false , false ),
65
- create (false , false ),
66
- apply (true , true );
65
+ prepare (false , false , true ),
66
+ create (false , false , true ),
67
+ update (false , true , true ),
68
+ apply (true , true , false );
67
69
68
70
final boolean hidden ;
69
71
final boolean loadBundle ;
72
+ final boolean writeBundle ;
70
73
71
- BundleStatus (boolean hidden , boolean loadBundle ) {
74
+ BundleStatus (boolean hidden , boolean loadBundle , boolean writeBundle ) {
72
75
this .hidden = hidden ;
73
76
this .loadBundle = loadBundle ;
77
+ this .writeBundle = writeBundle ;
74
78
}
75
79
76
80
boolean show () {
@@ -95,6 +99,7 @@ boolean show() {
95
99
Map <Path , Path > pathSubstitutions = new HashMap <>();
96
100
97
101
private final List <String > buildArgs ;
102
+ private Collection <String > updatedBuildArgs ;
98
103
99
104
private static final String bundleTempDirPrefix = "bundleRoot-" ;
100
105
private static final String bundleFileExtension = ".nib" ;
@@ -130,26 +135,9 @@ static BundleSupport create(NativeImage nativeImage, String bundleArg, NativeIma
130
135
bundleSupport = new BundleSupport (nativeImage , bundleStatus , bundleFilename );
131
136
List <String > buildArgs = bundleSupport .getBuildArgs ();
132
137
for (int i = buildArgs .size () - 1 ; i >= 0 ; i --) {
133
- String buildArg = buildArgs .get (i );
134
- if (buildArg .startsWith (BUNDLE_OPTION )) {
135
- assert !BundleStatus .valueOf (buildArg .substring (BUNDLE_OPTION .length () + 1 )).loadBundle ;
136
- continue ;
137
- }
138
- if (buildArg .startsWith (nativeImage .oHPath )) {
139
- continue ;
140
- }
141
- if (buildArg .equals (DefaultOptionHandler .verboseOption )) {
142
- continue ;
143
- }
144
- if (buildArg .startsWith ("-Dllvm.bin.dir=" )) {
145
- Optional <String > existing = nativeImage .config .getBuildArgs ().stream ().filter (arg -> arg .startsWith ("-Dllvm.bin.dir=" )).findFirst ();
146
- if (existing .isPresent () && !existing .get ().equals (buildArg )) {
147
- throw NativeImage .showError ("Bundle native-image argument '" + buildArg + "' conflicts with existing '" + existing .get () + "'." );
148
- }
149
- continue ;
150
- }
151
- args .push (buildArg );
138
+ args .push (buildArgs .get (i ));
152
139
}
140
+ bundleSupport .updatedBuildArgs = args .snapshot ();
153
141
} else {
154
142
bundleSupport = new BundleSupport (nativeImage , bundleStatus );
155
143
}
@@ -261,10 +249,6 @@ private BundleSupport(NativeImage nativeImage, BundleStatus status, String bundl
261
249
}
262
250
}
263
251
264
- public boolean isBundleCreation () {
265
- return !status .loadBundle ;
266
- }
267
-
268
252
public List <String > getBuildArgs () {
269
253
return buildArgs ;
270
254
}
@@ -455,9 +439,7 @@ void complete() {
455
439
}
456
440
457
441
try {
458
- if (isBundleCreation ()) {
459
- writeBundle ();
460
- }
442
+ writeBundle ();
461
443
} finally {
462
444
nativeImage .deleteAllFiles (rootDir );
463
445
}
@@ -469,7 +451,19 @@ public void setBundleLocation(Path imagePath, String imageName) {
469
451
}
470
452
471
453
void writeBundle () {
472
- assert isBundleCreation ();
454
+ if (!status .writeBundle ) {
455
+ return ;
456
+ }
457
+
458
+ String originalOutputDirName = outputDir .getFileName ().toString () + originalDirExtension ;
459
+ Path originalOutputDir = rootDir .resolve (originalOutputDirName );
460
+ if (Files .exists (originalOutputDir )) {
461
+ nativeImage .deleteAllFiles (originalOutputDir );
462
+ }
463
+ Path metaInfDir = rootDir .resolve ("META-INF" );
464
+ if (Files .exists (metaInfDir )) {
465
+ nativeImage .deleteAllFiles (metaInfDir );
466
+ }
473
467
474
468
Path pathCanonicalizationsFile = stageDir .resolve ("path_canonicalizations.json" );
475
469
try (JsonWriter writer = new JsonWriter (pathCanonicalizationsFile )) {
@@ -488,8 +482,29 @@ void writeBundle() {
488
482
489
483
Path buildArgsFile = stageDir .resolve ("build.json" );
490
484
try (JsonWriter writer = new JsonWriter (buildArgsFile )) {
485
+ ArrayList <String > cleanBuildArgs = new ArrayList <>();
486
+ for (String buildArg : updatedBuildArgs != null ? updatedBuildArgs : buildArgs ) {
487
+ if (buildArg .startsWith (BUNDLE_OPTION )) {
488
+ assert !BundleStatus .valueOf (buildArg .substring (BUNDLE_OPTION .length () + 1 )).loadBundle ;
489
+ continue ;
490
+ }
491
+ if (buildArg .startsWith (nativeImage .oHPath )) {
492
+ continue ;
493
+ }
494
+ if (buildArg .equals (DefaultOptionHandler .verboseOption )) {
495
+ continue ;
496
+ }
497
+ if (buildArg .startsWith ("-Dllvm.bin.dir=" )) {
498
+ Optional <String > existing = nativeImage .config .getBuildArgs ().stream ().filter (arg -> arg .startsWith ("-Dllvm.bin.dir=" )).findFirst ();
499
+ if (existing .isPresent () && !existing .get ().equals (buildArg )) {
500
+ throw NativeImage .showError ("Bundle native-image argument '" + buildArg + "' conflicts with existing '" + existing .get () + "'." );
501
+ }
502
+ continue ;
503
+ }
504
+ cleanBuildArgs .add (buildArg );
505
+ }
491
506
/* Printing as list with defined sort-order ensures useful diffs are possible */
492
- JsonPrinter .printCollection (writer , buildArgs , null , BundleSupport ::printBuildArg );
507
+ JsonPrinter .printCollection (writer , cleanBuildArgs , null , BundleSupport ::printBuildArg );
493
508
} catch (IOException e ) {
494
509
throw NativeImage .showError ("Failed to write bundle-file " + pathSubstitutionsFile , e );
495
510
}
0 commit comments