diff --git a/.gitignore b/.gitignore index 052cf1489..c0778946c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,13 @@ node_modules npm-debug.log output +###################### +# Intellij +###################### +.idea/ +*.iml +*.iws +*.ipr +*.ids +*.orig +out/ diff --git a/lib/generator.js b/lib/generator.js index fdbc48813..334c2d60f 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -8,6 +8,7 @@ const xfs = require('fs.extra'); const beautifier = require('./beautifier'); const registerPartial = require('./register-partial'); const parse = require('./parser'); +const minimatch = require("minimatch"); const generator = module.exports; @@ -50,22 +51,35 @@ const renderFile = (filePath, data) => new Promise((resolve, reject) => { */ const generateFile = options => new Promise((resolve, reject) => { const templates_dir = options.templates_dir; + const template_config = options.template_config; const target_dir = options.target_dir; const file_name = options.file_name; const root = options.root; const data = options.data; + const source_path = path.resolve(root, file_name); - renderFile(path.resolve(root, file_name), data) - .then((parsed_content) => { - const template_path = path.relative(templates_dir, path.resolve(root, file_name)); - const generated_path = path.resolve(target_dir, template_path); + if (isFileNonRenderable(file_name, template_config.nonRenderableFiles)) { + const template_path = path.relative(templates_dir, path.resolve(root, file_name)); + const generated_path = path.resolve(target_dir, template_path); - fs.writeFile(generated_path, parsed_content, 'utf8', (err) => { - if (err) return reject(err); - resolve(); - }); - }) - .catch(reject); + fs.copyFile(source_path, generated_path, (err) => { + if (err) return reject(err); + resolve(); + }); + } else { + renderFile(path.resolve(root, file_name), data) + .then((parsed_content) => { + const template_path = path.relative(templates_dir, path.resolve(root, file_name)); + const generated_path = path.resolve(target_dir, template_path); + + const stats = fs.statSync(source_path); + fs.writeFile(generated_path, parsed_content, {encoding: 'utf8', mode: stats.mode}, (err) => { + if (err) return reject(err); + resolve(); + }); + }) + .catch(reject); + } }); /** @@ -130,6 +144,7 @@ const generateTopicFiles = options => new Promise((resolve, reject) => { const generateDirectoryStructure = config => new Promise((resolve, reject) => { const target_dir = config.target_dir; const templates_dir = config.templates; + const template_config = resolveTemplateConfig(templates_dir); xfs.mkdirpSync(target_dir); @@ -159,7 +174,8 @@ const generateDirectoryStructure = config => new Promise((resolve, reject) => { templates_dir, target_dir, data: config, - file_name: stats.name + file_name: stats.name, + template_config }); } next(); @@ -369,3 +385,31 @@ generator.getTemplateFile = ({ resolve(content); }); }); + +/** + * check if a given file_name matches a list of Glob patterns + * + * @param {string} file name to check against a list of Glob patterns + * @param {string} list of Glob patterns + * @return {boolean} true | false + */ +function isFileNonRenderable (file_name, nonRenderableGlobs) { + if (!file_name || !nonRenderableGlobs) { + return false; + } + + const isNonRenderable = function(globExp) { + return minimatch(file_name, globExp); + }; + + return nonRenderableGlobs.some(isNonRenderable); +} + +function resolveTemplateConfig(templates_dir) { + try { + const config_path = path.resolve(templates_dir, ".tp-config"); + return require(config_path); + } catch (e) { + return {}; + } +} diff --git a/package-lock.json b/package-lock.json index f1d5186ed..1b10e856c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -37,6 +37,20 @@ "resolved": "https://registry.npmjs.org/asyncapi/-/asyncapi-2.0.2.tgz", "integrity": "sha512-iKgKWs2Ox+f9pFvnRjtnHOPXo5hU4GhkWK44Rw6lKYn/Um9eEx50VCayjCJBNT3cvzn7CSVFMZiioRAFwRGz4g==" }, + "balanced-match": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" + }, + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, "call-me-maybe": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/call-me-maybe/-/call-me-maybe-1.0.1.tgz", @@ -82,6 +96,11 @@ "resolved": "https://registry.npmjs.org/commander/-/commander-2.16.0.tgz", "integrity": "sha512-sVXqklSaotK9at437sFlFpyOcJonxe0yST/AG9DkQKUdIE6IqGIMv4SfAQSKaJbSdVEJYItASCrBiVQHq1HQew==" }, + "concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" + }, "debug": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", @@ -264,6 +283,14 @@ "resolved": "https://registry.npmjs.org/mdurl/-/mdurl-1.0.1.tgz", "integrity": "sha1-/oWy7HWlkDfyrf7BAP1sYBdhFS4=" }, + "minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "requires": { + "brace-expansion": "^1.1.7" + } + }, "minimist": { "version": "0.0.10", "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.10.tgz", diff --git a/package.json b/package.json index 3794b94e8..30f43b8f6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asyncapi-generator", - "version": "0.4.0", + "version": "0.5.0", "description": "The AsyncAPI generator. It can generate documentation, code, anything!", "main": "./lib/generator.js", "bin": { @@ -38,7 +38,8 @@ "mkdirp": "^0.5.1", "openapi-sampler": "^1.0.0-beta.9", "project-name-generator": "^2.1.5", - "z-schema": "^3.18.2" + "z-schema": "^3.18.2", + "minimatch": "^3.0.4" }, "devDependencies": {} } diff --git a/templates/java-spring/.helpers/handlebars.js b/templates/java-spring/.helpers/handlebars.js index 4a9facf0e..80ec4ce42 100644 --- a/templates/java-spring/.helpers/handlebars.js +++ b/templates/java-spring/.helpers/handlebars.js @@ -43,5 +43,8 @@ module.exports = (Handlebars, _) => { // Handlebars.registerHelper('json', function(context) { // return JSON.stringify(context); // }); - + + Handlebars.registerHelper('schemeExists', (collection, scheme) => { + return _.some(collection, {'scheme': scheme}); + }); }; diff --git a/templates/java-spring/.tp-config.json b/templates/java-spring/.tp-config.json new file mode 100644 index 000000000..5b9abbffd --- /dev/null +++ b/templates/java-spring/.tp-config.json @@ -0,0 +1,5 @@ +{ + "nonRenderableFiles": [ + "**/*.jar" + ] +} diff --git a/templates/java-spring/README.md b/templates/java-spring/README.md index 4a04610f1..5cfeca836 100644 --- a/templates/java-spring/README.md +++ b/templates/java-spring/README.md @@ -96,7 +96,14 @@ ag -o ./src asyncapi.yaml java-spring ``` ### Run it -To run the generated application you need the java virtual machine and gradle installed. After thats done go to the root folder of the generated code and run this command: +{{#if (schemeExists asyncapi.servers "amqp") }} +Start your RabbitMQ with: ```bash -gradle bootRun +docker-compose -f src/main/docker/rabbitmq.yml up -d +``` +{{/if}} + +Go to the root folder of the generated code and run this command (you need the JDK1.8): +```bash +./gradlew bootRun ``` diff --git a/templates/java-spring/gradle/wrapper/gradle-wrapper.jar b/templates/java-spring/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 000000000..1ce6e58f1 Binary files /dev/null and b/templates/java-spring/gradle/wrapper/gradle-wrapper.jar differ diff --git a/templates/java-spring/gradle/wrapper/gradle-wrapper.properties b/templates/java-spring/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 000000000..010f2c33b --- /dev/null +++ b/templates/java-spring/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Tue Oct 30 18:45:03 EDT 2018 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.8.1-all.zip diff --git a/templates/java-spring/gradlew b/templates/java-spring/gradlew old mode 100644 new mode 100755 diff --git a/templates/java-spring/src/main/docker/rabbitmq.yml b/templates/java-spring/src/main/docker/rabbitmq.yml new file mode 100644 index 000000000..5ccf3b4ef --- /dev/null +++ b/templates/java-spring/src/main/docker/rabbitmq.yml @@ -0,0 +1,7 @@ +version: '2' +services: + rabbitmq: + image: rabbitmq:3.6.2-management + ports: + - 5672:5672 + - 15672:15672 diff --git a/templates/java-spring/src/main/resources/application.yml b/templates/java-spring/src/main/resources/application.yml index 1f733a55d..08302970c 100644 --- a/templates/java-spring/src/main/resources/application.yml +++ b/templates/java-spring/src/main/resources/application.yml @@ -3,7 +3,7 @@ amqp: broker: host: localhost - port: 32771 + port: 5672 username: guest password: guest exchange: