[generator] Adds a .swagger/VERSION file for all generators (#5556)

This commit is contained in:
Jim Schubert 2017-05-09 04:59:48 -04:00 committed by wing328
parent 824842290d
commit f25a7575d3
2 changed files with 24 additions and 2 deletions

View File

@ -3,6 +3,7 @@ package io.swagger.codegen;
import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template;
import io.swagger.codegen.ignore.CodegenIgnoreProcessor;
import io.swagger.codegen.utils.ImplementationVersion;
import io.swagger.models.*;
import io.swagger.models.auth.OAuth2Definition;
import io.swagger.models.auth.SecuritySchemeDefinition;
@ -126,8 +127,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
}
config.processOpts();
config.preprocessSwagger(swagger);
// TODO need to obtain version from a file instead of hardcoding it
config.additionalProperties().put("generatorVersion", "2.2.3-SNAPSHOT");
config.additionalProperties().put("generatorVersion", ImplementationVersion.read());
config.additionalProperties().put("generatedDate", DateTime.now().toString());
config.additionalProperties().put("generatorClass", config.getClass().getName());
config.additionalProperties().put("inputSpec", config.getInputSpec());
@ -581,6 +581,15 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
files.add(ignoreFile);
}
final String swaggerVersionMetadata = config.outputFolder() + File.separator + ".swagger" + File.separator + "VERSION";
File swaggerVersionMetadataFile = new File(swaggerVersionMetadata);
try {
writeToFile(swaggerVersionMetadata, ImplementationVersion.read());
files.add(swaggerVersionMetadataFile);
} catch (IOException e) {
throw new RuntimeException("Could not generate supporting file '" + swaggerVersionMetadata + "'", e);
}
/*
* The following code adds default LICENSE (Apache-2.0) for all generators
* To use license other than Apache2.0, update the following file:

View File

@ -0,0 +1,13 @@
package io.swagger.codegen.utils;
public class ImplementationVersion {
public static String read() {
// Assumes this version is required at runtime. This could be modified to use a properties file like the CLI.
String compiledVersion = ImplementationVersion.class.getPackage().getImplementationVersion();
if(compiledVersion != null) {
return compiledVersion;
}
return "unset";
}
}