ern-api-gen
Version:
Electrode Native API generator
505 lines • 23.8 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* tslint:disable:variable-name */
const CliOption_1 = __importDefault(require("../CliOption"));
const CodegenConstants_1 = __importDefault(require("../CodegenConstants"));
const CodegenType_1 = __importDefault(require("../CodegenType"));
const SupportingFile_1 = __importDefault(require("../SupportingFile"));
const properties_1 = require("../models/properties");
const StringUtils_1 = __importDefault(require("../java/StringUtils"));
const LoggerFactory_1 = __importDefault(require("../java/LoggerFactory"));
const File_1 = __importDefault(require("../java/File"));
const javaUtil_1 = require("../java/javaUtil");
const DefaultCodegen_1 = __importDefault(require("../DefaultCodegen"));
const BooleanHelper_1 = require("../java/BooleanHelper");
const path_1 = __importDefault(require("path"));
class AndroidClientCodegen extends DefaultCodegen_1.default {
constructor() {
super();
this.invokerPackage = 'io.swagger.client';
this.groupId = 'io.swagger';
this.artifactId = 'swagger-android-client';
this.artifactVersion = '1.0.0';
this.projectFolder = 'src/main';
this.useAndroidMavenGradlePlugin = true;
this.requestPackage = 'io.swagger.client.request';
this.authPackage = 'io.swagger.client.auth';
this.gradleWrapperPackage = 'gradle.wrapper';
this.apiDocPath = 'docs/';
this.modelDocPath = 'docs/';
this.__outputFolder = 'generated-code/android';
this.__languageSpecificPrimitives = javaUtil_1.newHashSet('String', 'boolean', 'Boolean', 'Double', 'Integer', 'Long', 'Float', 'byte[]', 'Object');
this.__apiPackage = 'io.swagger.client.api';
this.__modelPackage = 'io.swagger.client.model';
this.__embeddedTemplateDir = 'android';
this.__templateDir = 'android';
this.__supportedLibraries = javaUtil_1.newHashMap(['volley', 'HTTP client: Volley 1.0.19 (default)'], [
'httpclient',
'HTTP client: Apache HttpClient 4.3.6. JSON processing: Gson 2.3.1. IMPORTANT: Android client using HttpClient is not actively maintained and will be depecreated in the next major release.',
]);
this.sourceFolder = this.projectFolder + '/java';
this.__modelTemplateFiles.put('model.mustache', '.java');
this.__apiTemplateFiles.put('api.mustache', '.java');
this.setReservedWordsLowerCase([
'localVarPostBody',
'localVarPath',
'localVarQueryParams',
'localVarHeaderParams',
'localVarFormParams',
'localVarContentTypes',
'localVarContentType',
'localVarResponse',
'localVarBuilder',
'authNames',
'basePath',
'apiInvoker',
'abstract',
'continue',
'for',
'new',
'switch',
'assert',
'default',
'if',
'package',
'synchronized',
'boolean',
'do',
'goto',
'private',
'this',
'break',
'double',
'implements',
'protected',
'throw',
'byte',
'else',
'import',
'public',
'throws',
'case',
'enum',
'instanceof',
'return',
'transient',
'catch',
'extends',
'int',
'short',
'try',
'char',
'final',
'interface',
'static',
'void',
'class',
'finally',
'long',
'strictfp',
'volatile',
'const',
'float',
'native',
'super',
'while',
]);
this.__instantiationTypes.put('array', 'ArrayList');
this.__instantiationTypes.put('map', 'HashMap');
this.__typeMapping.put('date', 'Date');
this.__typeMapping.put('file', 'File');
}
initalizeCliOptions() {
super.initalizeCliOptions();
this.__cliOptions.push(new CliOption_1.default(CodegenConstants_1.default.MODEL_PACKAGE, CodegenConstants_1.default.MODEL_PACKAGE_DESC));
this.__cliOptions.push(new CliOption_1.default(CodegenConstants_1.default.API_PACKAGE, CodegenConstants_1.default.API_PACKAGE_DESC));
this.__cliOptions.push(new CliOption_1.default(CodegenConstants_1.default.INVOKER_PACKAGE, CodegenConstants_1.default.INVOKER_PACKAGE_DESC));
this.__cliOptions.push(new CliOption_1.default(CodegenConstants_1.default.GROUP_ID, 'groupId for use in the generated build.gradle and pom.xml'));
this.__cliOptions.push(new CliOption_1.default(CodegenConstants_1.default.ARTIFACT_ID, 'artifactId for use in the generated build.gradle and pom.xml'));
this.__cliOptions.push(new CliOption_1.default(CodegenConstants_1.default.ARTIFACT_VERSION, 'artifact version for use in the generated build.gradle and pom.xml'));
this.__cliOptions.push(new CliOption_1.default(CodegenConstants_1.default.SOURCE_FOLDER, CodegenConstants_1.default.SOURCE_FOLDER_DESC));
this.__cliOptions.push(CliOption_1.default.newBoolean(AndroidClientCodegen.USE_ANDROID_MAVEN_GRADLE_PLUGIN, 'A flag to toggle android-maven gradle plugin.').defaultValue('true'));
const library = new CliOption_1.default(CodegenConstants_1.default.LIBRARY, 'library template (sub-template) to use');
library.setEnum(this.__supportedLibraries);
this.__cliOptions.push(library);
}
getTag() {
return CodegenType_1.default.CLIENT;
}
getName() {
return 'android';
}
getHelp() {
return 'Generates an Android client library.';
}
escapeReservedWord(name) {
return '_' + name;
}
apiFileFolder() {
return (this.__outputFolder +
path_1.default.sep +
this.sourceFolder +
path_1.default.sep +
this.apiPackage()
.split('.')
.join(File_1.default.separator));
}
modelFileFolder() {
return (this.__outputFolder +
path_1.default.sep +
this.sourceFolder +
path_1.default.sep +
this.modelPackage()
.split('.')
.join(File_1.default.separator));
}
apiDocFileFolder() {
return (this.__outputFolder + path_1.default.sep + this.apiDocPath)
.split(path_1.default.sep)
.join(File_1.default.separator);
}
modelDocFileFolder() {
return (this.__outputFolder + path_1.default.sep + this.modelDocPath)
.split(path_1.default.sep)
.join(File_1.default.separator);
}
toApiDocFilename(name) {
return this.toApiName(name);
}
toModelDocFilename(name) {
return this.toModelName(name);
}
getTypeDeclaration(p) {
if (p != null && p instanceof properties_1.ArrayProperty) {
const ap = p;
const inner = ap.getItems();
return this.getSwaggerType(p) + '<' + this.getTypeDeclaration(inner) + '>';
}
else if (p != null && p instanceof properties_1.MapProperty) {
const mp = p;
const inner = mp.getAdditionalProperties();
return (this.getSwaggerType(p) +
'<String, ' +
this.getTypeDeclaration(inner) +
'>');
}
return super.getTypeDeclaration(p);
}
getSwaggerType(p) {
const swaggerType = super.getSwaggerType(p);
let type = null;
if (this.__typeMapping.containsKey(swaggerType)) {
type = this.__typeMapping.get(swaggerType);
if (this.__languageSpecificPrimitives.contains(type) ||
type.indexOf('.') >= 0 ||
type === 'Map' ||
type === 'List' ||
type === 'File' ||
type === 'Date') {
return type;
}
}
else {
type = swaggerType;
}
return this.toModelName(type);
}
toVarName(name) {
name = this.sanitizeName(name);
name = name.replace(new RegExp('-', 'g'), '_');
if (name.match('^[A-Z_]*$')) {
return name;
}
name = DefaultCodegen_1.default.camelize(name, true);
if (this.isReservedWord(name) || name.match('^\\d.*')) {
name = this.escapeReservedWord(name);
}
return name;
}
toParamName(name) {
return this.toVarName(name);
}
toModelName(name) {
if (!StringUtils_1.default.isEmpty(this.modelNamePrefix)) {
name = this.modelNamePrefix + '_' + name;
}
if (!StringUtils_1.default.isEmpty(this.modelNameSuffix)) {
name = name + '_' + this.modelNameSuffix;
}
if (name.toUpperCase() === name) {
return name;
}
name = DefaultCodegen_1.default.camelize(this.sanitizeName(name));
if (this.isReservedWord(name)) {
const modelName = 'Model' + name;
Log.warn(name +
' (reserved word) cannot be used as model name. Renamed to ' +
modelName);
return modelName;
}
if (name.match('^\\d.*')) {
const modelName = 'Model' + name;
Log.warn(name +
' (model name starts with number) cannot be used as model name. Renamed to ' +
modelName);
return modelName;
}
return name;
}
toModelFilename(name) {
return this.toModelName(name);
}
setParameterExampleValue(p) {
let example;
if (p.defaultValue == null) {
example = p.example;
}
else {
example = p.defaultValue;
}
let type = p.baseType;
if (type == null) {
type = p.dataType;
}
if ('String' === type) {
if (example == null) {
example = p.paramName + '_example';
}
example = '"' + this.escapeText(example) + '"';
}
else if ('Integer' === type || 'Short' === type) {
if (example == null) {
example = '56';
}
}
else if ('Long' === type) {
if (example == null) {
example = '56';
}
example = example + 'L';
}
else if ('Float' === type) {
if (example == null) {
example = '3.4';
}
example = example + 'F';
}
else if ('Double' === type) {
example = '3.4';
example = example + 'D';
}
else if ('Boolean' === type) {
if (example == null) {
example = 'true';
}
}
else if ('File' === type) {
if (example == null) {
example = '/path/to/file';
}
example = 'new File("' + this.escapeText(example) + '")';
}
else if ('Date' === type) {
example = 'new Date()';
}
else if (!this.__languageSpecificPrimitives.contains(type)) {
example = 'new ' + type + '()';
}
if (example == null) {
example = 'null';
}
else if (p.isListContainer) {
example = 'Arrays.asList(' + example + ')';
}
else if (p.isMapContainer) {
example = 'new HashMap()';
}
p.example = example;
}
toOperationId(operationId) {
if (StringUtils_1.default.isEmpty(operationId)) {
throw new Error('Empty method name (operationId) not allowed');
}
operationId = DefaultCodegen_1.default.camelize(this.sanitizeName(operationId), true);
if (this.isReservedWord(operationId)) {
const newOperationId = DefaultCodegen_1.default.camelize('call_' + operationId, true);
Log.warn(operationId +
' (reserved word) cannot be used as method name. Renamed to ' +
newOperationId);
return newOperationId;
}
return operationId;
}
processOpts() {
super.processOpts();
if (this.__additionalProperties.containsKey(CodegenConstants_1.default.INVOKER_PACKAGE)) {
this.setInvokerPackage(this.__additionalProperties.get(CodegenConstants_1.default.INVOKER_PACKAGE));
}
else {
this.__additionalProperties.put(CodegenConstants_1.default.INVOKER_PACKAGE, this.invokerPackage);
}
if (this.__additionalProperties.containsKey(CodegenConstants_1.default.GROUP_ID)) {
this.setGroupId(this.__additionalProperties.get(CodegenConstants_1.default.GROUP_ID));
}
else {
this.__additionalProperties.put(CodegenConstants_1.default.GROUP_ID, this.groupId);
}
if (this.__additionalProperties.containsKey(CodegenConstants_1.default.ARTIFACT_ID)) {
this.setArtifactId(this.__additionalProperties.get(CodegenConstants_1.default.ARTIFACT_ID));
}
else {
this.__additionalProperties.put(CodegenConstants_1.default.ARTIFACT_ID, this.artifactId);
}
if (this.__additionalProperties.containsKey(CodegenConstants_1.default.ARTIFACT_VERSION)) {
this.setArtifactVersion(this.__additionalProperties.get(CodegenConstants_1.default.ARTIFACT_VERSION));
}
else {
this.__additionalProperties.put(CodegenConstants_1.default.ARTIFACT_VERSION, this.artifactVersion);
}
if (this.__additionalProperties.containsKey(CodegenConstants_1.default.SOURCE_FOLDER)) {
this.setSourceFolder(this.__additionalProperties.get(CodegenConstants_1.default.SOURCE_FOLDER));
}
if (this.__additionalProperties.containsKey(AndroidClientCodegen.USE_ANDROID_MAVEN_GRADLE_PLUGIN)) {
this.setUseAndroidMavenGradlePlugin(BooleanHelper_1.parseBoolean(this.__additionalProperties.get(AndroidClientCodegen.USE_ANDROID_MAVEN_GRADLE_PLUGIN)));
}
else {
this.__additionalProperties.put(AndroidClientCodegen.USE_ANDROID_MAVEN_GRADLE_PLUGIN, this.useAndroidMavenGradlePlugin);
}
if (this.__additionalProperties.containsKey(CodegenConstants_1.default.LIBRARY)) {
this.setLibrary(this.__additionalProperties.get(CodegenConstants_1.default.LIBRARY));
}
this.__additionalProperties.put('apiDocPath', this.apiDocPath);
this.__additionalProperties.put('modelDocPath', this.modelDocPath);
if (StringUtils_1.default.isEmpty(this.getLibrary())) {
this.setLibrary('volley');
}
else {
this.setLibrary(this.getLibrary());
}
const f = this[`addSupportingFilesFor${DefaultCodegen_1.default.camelize(this.getLibrary())}`];
if (f) {
f.call(this);
}
}
addSupportingForFilesHttpclient() {
this.addSupportingFilesForHttpClient();
}
addSupportingFilesForHttpClient() {
this.__modelDocTemplateFiles.put('model_doc.mustache', '.md');
this.__apiDocTemplateFiles.put('api_doc.mustache', '.md');
this.__supportingFiles.push(new SupportingFile_1.default('README.mustache', '', 'README.md'));
this.__supportingFiles.push(new SupportingFile_1.default('pom.mustache', '', 'pom.xml'));
this.__supportingFiles.push(new SupportingFile_1.default('settings.gradle.mustache', '', 'settings.gradle'));
this.__supportingFiles.push(new SupportingFile_1.default('build.mustache', '', 'build.gradle'));
this.__supportingFiles.push(new SupportingFile_1.default('manifest.mustache', this.projectFolder, 'AndroidManifest.xml'));
this.__supportingFiles.push(new SupportingFile_1.default('apiInvoker.mustache', (this.sourceFolder + File_1.default.separator + this.invokerPackage)
.split('.')
.join(File_1.default.separator), 'ApiInvoker.java'));
this.__supportingFiles.push(new SupportingFile_1.default('httpPatch.mustache', (this.sourceFolder + File_1.default.separator + this.invokerPackage)
.split('.')
.join(File_1.default.separator), 'HttpPatch.java'));
this.__supportingFiles.push(new SupportingFile_1.default('jsonUtil.mustache', (this.sourceFolder + File_1.default.separator + this.invokerPackage)
.split('.')
.join(File_1.default.separator), 'JsonUtil.java'));
this.__supportingFiles.push(new SupportingFile_1.default('apiException.mustache', (this.sourceFolder + File_1.default.separator + this.invokerPackage)
.split('.')
.join(File_1.default.separator), 'ApiException.java'));
this.__supportingFiles.push(new SupportingFile_1.default('Pair.mustache', (this.sourceFolder + File_1.default.separator + this.invokerPackage)
.split('.')
.join(File_1.default.separator), 'Pair.java'));
this.__supportingFiles.push(new SupportingFile_1.default('git_push.sh.mustache', '', 'git_push.sh'));
this.__supportingFiles.push(new SupportingFile_1.default('gitignore.mustache', '', '.gitignore'));
this.__supportingFiles.push(new SupportingFile_1.default('gradlew.mustache', '', 'gradlew'));
this.__supportingFiles.push(new SupportingFile_1.default('gradlew.bat.mustache', '', 'gradlew.bat'));
this.__supportingFiles.push(new SupportingFile_1.default('gradle-wrapper.properties.mustache', this.gradleWrapperPackage.split('.').join(File_1.default.separator), 'gradle-wrapper.properties'));
this.__supportingFiles.push(new SupportingFile_1.default('gradle-wrapper.jar', this.gradleWrapperPackage.split('.').join(File_1.default.separator), 'gradle-wrapper.jar'));
}
addSupportingFilesForVolley() {
this.__modelDocTemplateFiles.put('model_doc.mustache', '.md');
this.__apiDocTemplateFiles.put('api_doc.mustache', '.md');
this.__supportingFiles.push(new SupportingFile_1.default('README.mustache', '', 'README.md'));
this.__supportingFiles.push(new SupportingFile_1.default('git_push.sh.mustache', '', 'git_push.sh'));
this.__supportingFiles.push(new SupportingFile_1.default('gitignore.mustache', '', '.gitignore'));
this.__supportingFiles.push(new SupportingFile_1.default('pom.mustache', '', 'pom.xml'));
this.__supportingFiles.push(new SupportingFile_1.default('build.mustache', '', 'build.gradle'));
this.__supportingFiles.push(new SupportingFile_1.default('manifest.mustache', this.projectFolder, 'AndroidManifest.xml'));
this.__supportingFiles.push(new SupportingFile_1.default('apiInvoker.mustache', (this.sourceFolder + File_1.default.separator + this.invokerPackage)
.split('.')
.join(File_1.default.separator), 'ApiInvoker.java'));
this.__supportingFiles.push(new SupportingFile_1.default('jsonUtil.mustache', (this.sourceFolder + File_1.default.separator + this.invokerPackage)
.split('.')
.join(File_1.default.separator), 'JsonUtil.java'));
this.__supportingFiles.push(new SupportingFile_1.default('apiException.mustache', (this.sourceFolder + File_1.default.separator + this.invokerPackage)
.split('.')
.join(File_1.default.separator), 'ApiException.java'));
this.__supportingFiles.push(new SupportingFile_1.default('Pair.mustache', (this.sourceFolder + File_1.default.separator + this.invokerPackage)
.split('.')
.join(File_1.default.separator), 'Pair.java'));
this.__supportingFiles.push(new SupportingFile_1.default('request/getrequest.mustache', (this.sourceFolder + File_1.default.separator + this.requestPackage)
.split('.')
.join(File_1.default.separator), 'GetRequest.java'));
this.__supportingFiles.push(new SupportingFile_1.default('request/postrequest.mustache', (this.sourceFolder + File_1.default.separator + this.requestPackage)
.split('.')
.join(File_1.default.separator), 'PostRequest.java'));
this.__supportingFiles.push(new SupportingFile_1.default('request/putrequest.mustache', (this.sourceFolder + File_1.default.separator + this.requestPackage)
.split('.')
.join(File_1.default.separator), 'PutRequest.java'));
this.__supportingFiles.push(new SupportingFile_1.default('request/deleterequest.mustache', (this.sourceFolder + File_1.default.separator + this.requestPackage)
.split('.')
.join(File_1.default.separator), 'DeleteRequest.java'));
this.__supportingFiles.push(new SupportingFile_1.default('request/patchrequest.mustache', (this.sourceFolder + File_1.default.separator + this.requestPackage)
.split('.')
.join(File_1.default.separator), 'PatchRequest.java'));
this.__supportingFiles.push(new SupportingFile_1.default('auth/apikeyauth.mustache', (this.sourceFolder + File_1.default.separator + this.authPackage)
.split('.')
.join(File_1.default.separator), 'ApiKeyAuth.java'));
this.__supportingFiles.push(new SupportingFile_1.default('auth/httpbasicauth.mustache', (this.sourceFolder + File_1.default.separator + this.authPackage)
.split('.')
.join(File_1.default.separator), 'HttpBasicAuth.java'));
this.__supportingFiles.push(new SupportingFile_1.default('auth/authentication.mustache', (this.sourceFolder + File_1.default.separator + this.authPackage)
.split('.')
.join(File_1.default.separator), 'Authentication.java'));
this.__supportingFiles.push(new SupportingFile_1.default('gradlew.mustache', '', 'gradlew'));
this.__supportingFiles.push(new SupportingFile_1.default('gradlew.bat.mustache', '', 'gradlew.bat'));
this.__supportingFiles.push(new SupportingFile_1.default('gradle-wrapper.properties.mustache', this.gradleWrapperPackage.split('.').join(File_1.default.separator), 'gradle-wrapper.properties'));
this.__supportingFiles.push(new SupportingFile_1.default('gradle-wrapper.jar', this.gradleWrapperPackage.split('.').join(File_1.default.separator), 'gradle-wrapper.jar'));
}
getUseAndroidMavenGradlePlugin() {
return this.useAndroidMavenGradlePlugin;
}
setUseAndroidMavenGradlePlugin(useAndroidMavenGradlePlugin) {
this.useAndroidMavenGradlePlugin = useAndroidMavenGradlePlugin;
}
setInvokerPackage(invokerPackage) {
this.invokerPackage = invokerPackage;
}
setGroupId(groupId) {
this.groupId = groupId;
}
setArtifactId(artifactId) {
this.artifactId = artifactId;
}
setArtifactVersion(artifactVersion) {
this.artifactVersion = artifactVersion;
}
setSourceFolder(sourceFolder) {
this.sourceFolder = sourceFolder;
}
escapeQuotationMark(input) {
return input.split('"').join('');
}
escapeUnsafeCharacters(input) {
return input
.split('*/')
.join('*_/')
.split('/*')
.join('/_*');
}
}
AndroidClientCodegen.USE_ANDROID_MAVEN_GRADLE_PLUGIN = 'useAndroidMavenGradlePlugin';
exports.default = AndroidClientCodegen;
const Log = LoggerFactory_1.default.getLogger(AndroidClientCodegen);
//# sourceMappingURL=AndroidClientCodegen.js.map