ern-api-gen
Version:
Electrode Native API generator
385 lines • 14.4 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ClientOptInput_1 = __importDefault(require("../ClientOptInput"));
const ClientOpts_1 = __importDefault(require("../ClientOpts"));
const CodegenConfigLoader_1 = __importDefault(require("../CodegenConfigLoader"));
const CodegenConstants_1 = __importDefault(require("../CodegenConstants"));
const AuthParser_1 = __importDefault(require("../auth/AuthParser"));
const File_1 = __importDefault(require("../java/File"));
const LoggerFactory_1 = __importDefault(require("../java/LoggerFactory"));
const StringUtils_1 = require("../java/StringUtils");
const beanUtils_1 = require("../java/beanUtils");
const System_1 = __importDefault(require("../java/System"));
const fs_1 = __importDefault(require("fs"));
const javaUtil_1 = require("../java/javaUtil");
const Swagger_1 = __importDefault(require("../java/Swagger"));
const Validate = {
notEmpty(value, message) {
if (StringUtils_1.isEmpty(value)) {
throw new Error(message);
}
},
};
/**
* A class that contains all codegen configuration properties a user would want to manipulate.
* An instance could be created by deserializing a JSON file or being populated from CLI or Maven plugin parameters.
* It also has a convenience method for creating a ClientOptInput class which is THE object DefaultGenerator.java needs
* to generate code.
*/
class CodegenConfigurator {
constructor(opts = { outputDir: '.' }) {
this.systemProperties = javaUtil_1.newHashMap();
this.instantiationTypes = javaUtil_1.newHashMap();
this.typeMappings = javaUtil_1.newHashMap();
this.additionalProperties = javaUtil_1.newHashMap();
this.importMappings = javaUtil_1.newHashMap();
this.languageSpecificPrimitives = javaUtil_1.newHashSet();
this.dynamicProperties = javaUtil_1.newHashMap();
this.gitUserId = 'GIT_USER_ID';
this.gitRepoId = 'GIT_REPO_ID';
this.releaseNote = 'Minor update';
this.verbose = false;
this.skipOverwrite = false;
beanUtils_1.apply(this, opts);
}
static toAbsolutePathStr(path) {
if (StringUtils_1.isNotEmpty(path)) {
return new File_1.default(path).toAbsolutePath();
}
return path;
}
static fromFile(configFile) {
if (StringUtils_1.isNotEmpty(configFile)) {
try {
const conf = JSON.parse(fs_1.default.readFileSync(new File_1.default(configFile).getAbsolutePath(), 'utf-8'));
return beanUtils_1.apply(new CodegenConfigurator(), conf);
}
catch (e) {
Log.error('Unable to deserialize config file: ' + configFile, e);
}
}
return null;
}
setBridgeVersion(version) {
this.bridgeVersion = version;
}
getBridgeVersion() {
return this.bridgeVersion;
}
setLang(lang) {
this.lang = lang;
return this;
}
setInputSpec(inputSpec) {
this.inputSpec = inputSpec;
return this;
}
getInputSpec() {
return this.inputSpec;
}
getOutputDir() {
return this.outputDir;
}
setOutputDir(outputDir) {
this.outputDir = CodegenConfigurator.toAbsolutePathStr(outputDir);
return this;
}
getModelPackage() {
return this.modelPackage;
}
setModelPackage(modelPackage) {
this.modelPackage = modelPackage;
return this;
}
getModelNamePrefix() {
return this.modelNamePrefix;
}
setModelNamePrefix(prefix) {
this.modelNamePrefix = prefix;
return this;
}
getModelNameSuffix() {
return this.modelNameSuffix;
}
setModelNameSuffix(suffix) {
this.modelNameSuffix = suffix;
return this;
}
isVerbose() {
return this.verbose;
}
setVerbose(verbose) {
this.verbose = verbose;
return this;
}
isSkipOverwrite() {
return this.skipOverwrite;
}
setSkipOverwrite(skipOverwrite) {
this.skipOverwrite = skipOverwrite;
return this;
}
getLang() {
return this.lang;
}
getTemplateDir() {
return this.templateDir;
}
setTemplateDir(templateDir) {
const f = new File_1.default(templateDir);
if (!(f != null && f.exists() && f.isDirectory())) {
throw new Error('Template directory ' + templateDir + ' does not exist.');
}
this.templateDir = f.getAbsolutePath();
return this;
}
getAuth() {
return this.auth;
}
setAuth(auth) {
this.auth = auth;
return this;
}
getApiPackage() {
return this.apiPackage;
}
setApiPackage(apiPackage) {
this.apiPackage = apiPackage;
return this;
}
getInvokerPackage() {
return this.invokerPackage;
}
setInvokerPackage(invokerPackage) {
this.invokerPackage = invokerPackage;
return this;
}
getGroupId() {
return this.groupId;
}
setGroupId(groupId) {
this.groupId = groupId;
return this;
}
getArtifactId() {
return this.artifactId;
}
setArtifactId(artifactId) {
this.artifactId = artifactId;
return this;
}
getArtifactVersion() {
return this.artifactVersion;
}
setArtifactVersion(artifactVersion) {
this.artifactVersion = artifactVersion;
return this;
}
getSystemProperties() {
return this.systemProperties;
}
setSystemProperties(systemProperties) {
if (arguments.length) {
this.systemProperties = systemProperties;
}
else {
for (const [key, value] of this.systemProperties) {
System_1.default.setProperty(key, value);
}
}
}
addSystemProperty(key, value) {
this.systemProperties.put(key, value);
return this;
}
getInstantiationTypes() {
return this.instantiationTypes;
}
setInstantiationTypes(instantiationTypes) {
this.instantiationTypes = instantiationTypes;
return this;
}
addInstantiationType(key, value) {
this.instantiationTypes.put(key, value);
return this;
}
getTypeMappings() {
return this.typeMappings;
}
setTypeMappings(typeMappings) {
this.typeMappings = javaUtil_1.asMap(typeMappings);
return this;
}
addTypeMapping(key, value) {
this.typeMappings.put(key, value);
return this;
}
getAdditionalProperties() {
return this.additionalProperties;
}
setAdditionalProperties(additionalProperties) {
this.additionalProperties = javaUtil_1.asMap(additionalProperties);
return this;
}
addAdditionalProperty(key, value) {
this.additionalProperties.put(key, value);
return this;
}
getImportMappings() {
return this.importMappings;
}
setImportMappings(importMappings) {
this.importMappings = javaUtil_1.asMap(importMappings);
return this;
}
addImportMapping(key, value) {
this.importMappings.put(key, value);
return this;
}
setDynamicProperties(properties) {
properties = javaUtil_1.asMap(properties);
this.dynamicProperties.clear();
this.dynamicProperties.putAll(properties);
}
getLanguageSpecificPrimitives() {
return this.languageSpecificPrimitives;
}
setLanguageSpecificPrimitives(languageSpecificPrimitives) {
this.languageSpecificPrimitives = languageSpecificPrimitives;
return this;
}
addLanguageSpecificPrimitive(value) {
this.languageSpecificPrimitives.add(value);
return this;
}
getLibrary() {
return this.library;
}
setLibrary(library) {
this.library = library;
return this;
}
getGitUserId() {
return this.gitUserId;
}
setGitUserId(gitUserId) {
this.gitUserId = gitUserId;
return this;
}
getGitRepoId() {
return this.gitRepoId;
}
setGitRepoId(gitRepoId) {
this.gitRepoId = gitRepoId;
return this;
}
getReleaseNote() {
return this.releaseNote;
}
setReleaseNote(releaseNote) {
this.releaseNote = releaseNote;
return this;
}
getHttpUserAgent() {
return this.httpUserAgent;
}
setHttpUserAgent(httpUserAgent) {
this.httpUserAgent = httpUserAgent;
return this;
}
toClientOptInput() {
return __awaiter(this, void 0, void 0, function* () {
Validate.notEmpty(this.lang, 'language must be specified');
Validate.notEmpty(this.inputSpec, 'input spec must be specified');
this.setVerboseFlags();
this.setSystemProperties();
const config = CodegenConfigLoader_1.default.forName(this.lang);
beanUtils_1.applyStrict(config, this);
config.setOutputDir(this.outputDir);
config.setSkipOverwrite(this.skipOverwrite);
config.instantiationTypes().putAll(this.instantiationTypes);
config.typeMapping().putAll(this.typeMappings);
config.importMapping().putAll(this.importMappings);
config.languageSpecificPrimitives().addAll(this.languageSpecificPrimitives);
this.checkAndSetAdditionalProperty(this.apiPackage, CodegenConstants_1.default.API_PACKAGE);
this.checkAndSetAdditionalProperty(this.modelPackage, CodegenConstants_1.default.MODEL_PACKAGE);
this.checkAndSetAdditionalProperty(this.invokerPackage, CodegenConstants_1.default.INVOKER_PACKAGE);
this.checkAndSetAdditionalProperty(this.bridgeVersion, 'bridgeVersion');
this.checkAndSetAdditionalProperty(this.groupId, CodegenConstants_1.default.GROUP_ID);
this.checkAndSetAdditionalProperty(this.artifactId, CodegenConstants_1.default.ARTIFACT_ID);
this.checkAndSetAdditionalProperty(this.artifactVersion, CodegenConstants_1.default.ARTIFACT_VERSION);
this.checkAndSetAdditionalProperty(this.templateDir, CodegenConfigurator.toAbsolutePathStr(this.templateDir), CodegenConstants_1.default.TEMPLATE_DIR);
this.checkAndSetAdditionalProperty(this.modelNamePrefix, CodegenConstants_1.default.MODEL_NAME_PREFIX);
this.checkAndSetAdditionalProperty(this.modelNameSuffix, CodegenConstants_1.default.MODEL_NAME_SUFFIX);
this.checkAndSetAdditionalProperty(this.gitUserId, CodegenConstants_1.default.GIT_USER_ID);
this.checkAndSetAdditionalProperty(this.gitRepoId, CodegenConstants_1.default.GIT_REPO_ID);
this.checkAndSetAdditionalProperty(this.releaseNote, CodegenConstants_1.default.RELEASE_NOTE);
this.checkAndSetAdditionalProperty(this.httpUserAgent, CodegenConstants_1.default.HTTP_USER_AGENT);
this.handleDynamicProperties(config);
if (StringUtils_1.isNotEmpty(this.library)) {
config.setLibrary(this.library);
}
config.additionalProperties().putAll(this.additionalProperties);
const input = new ClientOptInput_1.default().config(config);
const authorizationValues = AuthParser_1.default.parse(this.auth);
const swagger = yield Swagger_1.default.create({ definition: this.inputSpec });
return input.opts(new ClientOpts_1.default()).swagger(swagger);
});
}
addDynamicProperty(name, value) {
this.dynamicProperties.put(name, value.toString());
return this;
}
getDynamicProperties() {
return this.dynamicProperties;
}
handleDynamicProperties(codegenConfig) {
for (const langCliOption of codegenConfig.cliOptions()) {
const opt = langCliOption.getOpt();
if (this.dynamicProperties.containsKey(opt)) {
codegenConfig
.additionalProperties()
.put(opt, this.dynamicProperties.get(opt));
}
else if (this.systemProperties.containsKey(opt)) {
codegenConfig
.additionalProperties()
.put(opt, this.systemProperties.get(opt).toString());
}
}
}
setVerboseFlags() {
if (!this.verbose) {
return;
}
Log.info('\nVERBOSE MODE: ON. Additional debug options are injected\n - [debugSwagger] prints the swagger specification as interpreted by the codegen\n - [debugModels] prints models passed to the template engine\n - [debugOperations] prints operations passed to the template engine\n - [debugSupportingFiles] prints additional data passed to the template engine');
System_1.default.setProperty('debugSwagger', '');
System_1.default.setProperty('debugModels', '');
System_1.default.setProperty('debugOperations', '');
System_1.default.setProperty('debugSupportingFiles', '');
}
checkAndSetAdditionalProperty(property, valueToSet, propertyKey) {
if (arguments.length > 2) {
if (StringUtils_1.isNotEmpty(property)) {
this.additionalProperties.put(propertyKey, valueToSet);
}
}
else {
this.checkAndSetAdditionalProperty(property, property, valueToSet);
}
}
}
exports.default = CodegenConfigurator;
const Log = LoggerFactory_1.default.getLogger(CodegenConfigurator);
//# sourceMappingURL=CodegenConfigurator.js.map