@@ -733,24 +733,11 @@ private void generateSupportingFiles(List<File> files, Map<String, Object> bundl
733
733
} catch (Exception e ) {
734
734
throw new RuntimeException ("Could not generate supporting file '" + ignoreFileNameTarget + "'" , e );
735
735
}
736
- }
737
-
738
- String versionMetadata = config .outputFolder () + File .separator + METADATA_DIR + File .separator + "VERSION" ;
739
- if (generateMetadata ) {
740
- File versionMetadataFile = new File (versionMetadata );
741
- try {
742
- this .templateProcessor .writeToFile (versionMetadata , ImplementationVersion .read ().getBytes (StandardCharsets .UTF_8 ));
743
- files .add (versionMetadataFile );
744
- if (config .isEnablePostProcessFile () && !dryRun ) {
745
- config .postProcessFile (ignoreFile , "openapi-generator-version" );
746
- }
747
- } catch (IOException e ) {
748
- throw new RuntimeException ("Could not generate supporting file '" + versionMetadata + "'" , e );
749
- }
750
736
} else {
751
- Path metadata = java .nio .file .Paths .get (versionMetadata );
752
- this .templateProcessor .skip (metadata , "Skipped by generateMetadata option supplied by user." );
737
+ this .templateProcessor .skip (ignoreFile .toPath (), "Skipped by generateMetadata option supplied by user." );
753
738
}
739
+
740
+ generateVersionMetadata (files );
754
741
}
755
742
756
743
@ SuppressWarnings ("unchecked" )
@@ -912,39 +899,9 @@ public List<File> generate() {
912
899
913
900
System .err .println (sb .toString ());
914
901
} else {
915
- if (generateMetadata ) {
916
- try {
917
- StringBuilder sb = new StringBuilder ();
918
- File outDir = new File (this .config .getOutputDir ());
919
-
920
- List <File > filesToSort = new ArrayList <>();
921
-
922
- // Avoid side-effecting sort in this path when generateMetadata=true
923
- files .forEach (f -> {
924
- // We have seen NPE on CI for getPath() returning null, so guard against this (to be fixed in 5.0 template management refactor)
925
- //noinspection ConstantConditions
926
- if (f != null && f .getPath () != null ) {
927
- filesToSort .add (f );
928
- }
929
- });
930
-
931
- filesToSort .sort (PathFileComparator .PATH_COMPARATOR );
932
- filesToSort .forEach (f -> {
933
- String relativePath = outDir .toPath ().relativize (f .toPath ()).toString ();
934
- if (!relativePath .equals (METADATA_DIR + File .separator + "VERSION" )) {
935
- sb .append (relativePath ).append (System .lineSeparator ());
936
- }
937
- });
938
-
939
- String targetFile = config .outputFolder () + File .separator + METADATA_DIR + File .separator + "FILES" ;
940
-
941
- File filesFile = this .templateProcessor .writeToFile (targetFile , sb .toString ().getBytes (StandardCharsets .UTF_8 ));
942
- if (filesFile != null ) {
943
- files .add (filesFile );
944
- }
945
- } catch (Exception e ) {
946
- LOGGER .warn ("Failed to write FILES metadata to track generated files." );
947
- }
902
+ // This exists here rather than in the method which generates supporting files to avoid accidentally adding files after this metadata.
903
+ if (generateSupportingFiles ) {
904
+ generateFilesMetadata (files );
948
905
}
949
906
}
950
907
@@ -1316,4 +1273,74 @@ private List<CodegenSecurity> filterAuthMethods(List<CodegenSecurity> authMethod
1316
1273
1317
1274
return result ;
1318
1275
}
1276
+
1277
+ /**
1278
+ * Generates a file at .openapi-generator/VERSION to track the version of user's latest run.
1279
+ *
1280
+ * @param files The list tracking generated files
1281
+ */
1282
+ private void generateVersionMetadata (List <File > files ) {
1283
+ String versionMetadata = config .outputFolder () + File .separator + METADATA_DIR + File .separator + "VERSION" ;
1284
+ if (generateMetadata ) {
1285
+ File versionMetadataFile = new File (versionMetadata );
1286
+ try {
1287
+ File written = this .templateProcessor .writeToFile (versionMetadata , ImplementationVersion .read ().getBytes (StandardCharsets .UTF_8 ));
1288
+ if (written != null ) {
1289
+ files .add (versionMetadataFile );
1290
+ if (config .isEnablePostProcessFile () && !dryRun ) {
1291
+ config .postProcessFile (written , "openapi-generator-version" );
1292
+ }
1293
+ }
1294
+ } catch (IOException e ) {
1295
+ throw new RuntimeException ("Could not generate supporting file '" + versionMetadata + "'" , e );
1296
+ }
1297
+ } else {
1298
+ Path metadata = java .nio .file .Paths .get (versionMetadata );
1299
+ this .templateProcessor .skip (metadata , "Skipped by generateMetadata option supplied by user." );
1300
+ }
1301
+ }
1302
+
1303
+ /**
1304
+ * Generates a file at .openapi-generator/FILES to track the files created by the user's latest run.
1305
+ * This is ideal for CI and regeneration of code without stale/unused files from older generations.
1306
+ *
1307
+ * @param files The list tracking generated files
1308
+ */
1309
+ private void generateFilesMetadata (List <File > files ) {
1310
+ if (generateMetadata ) {
1311
+ try {
1312
+ StringBuilder sb = new StringBuilder ();
1313
+ File outDir = new File (this .config .getOutputDir ());
1314
+
1315
+ List <File > filesToSort = new ArrayList <>();
1316
+
1317
+ // Avoid side-effecting sort in this path when generateMetadata=true
1318
+ files .forEach (f -> {
1319
+ // We have seen NPE on CI for getPath() returning null, so guard against this (to be fixed in 5.0 template management refactor)
1320
+ //noinspection ConstantConditions
1321
+ if (f != null && f .getPath () != null ) {
1322
+ filesToSort .add (f );
1323
+ }
1324
+ });
1325
+
1326
+ filesToSort .sort (PathFileComparator .PATH_COMPARATOR );
1327
+ filesToSort .forEach (f -> {
1328
+ String relativePath = outDir .toPath ().relativize (f .toPath ()).toString ();
1329
+ if (!relativePath .equals (METADATA_DIR + File .separator + "VERSION" )) {
1330
+ sb .append (relativePath ).append (System .lineSeparator ());
1331
+ }
1332
+ });
1333
+
1334
+ String targetFile = config .outputFolder () + File .separator + METADATA_DIR + File .separator + "FILES" ;
1335
+
1336
+ File filesFile = this .templateProcessor .writeToFile (targetFile , sb .toString ().getBytes (StandardCharsets .UTF_8 ));
1337
+ if (filesFile != null ) {
1338
+ files .add (filesFile );
1339
+ }
1340
+ } catch (Exception e ) {
1341
+ LOGGER .warn ("Failed to write FILES metadata to track generated files." );
1342
+ }
1343
+ }
1344
+ }
1345
+
1319
1346
}
0 commit comments