From 164936c40bdb1f9bce1d419ba8b8a16d82df2d60 Mon Sep 17 00:00:00 2001 From: JelteMX Date: Fri, 8 Jan 2016 13:49:25 +0100 Subject: [PATCH] Add folders task, add package.json paths && update parsing old package.json --- generators/app/index.js | 36 +++++++--- generators/app/templates/Gruntfile.js | 94 +++++++++++++++++--------- generators/app/templates/_package.json | 10 ++- package.json | 2 +- 4 files changed, 100 insertions(+), 42 deletions(-) diff --git a/generators/app/index.js b/generators/app/index.js index 98fb371..4dbe4a3 100644 --- a/generators/app/index.js +++ b/generators/app/index.js @@ -42,6 +42,16 @@ module.exports = yeoman.generators.Base.extend({ if (srcFolderContent.length === 1) { this.current.name = srcFolderContent[0]; } + if (!extfs.isEmptySync(this.destinationPath('package.json'))) { + try { + var destPkg = JSON.parse(fs.readFileSync(this.destinationPath('package.json')).toString()); + this.current.description = destPkg.description; + this.current.author = destPkg.author; + this.current.copyright = destPkg.copyright; + this.current.license = destPkg.license; + this.current.repository = destPkg.repository ? JSON.stringify(destPkg.repository) : false; + } catch (e) {} + } if (!extfs.isEmptySync(this.destinationPath('src/package.xml'))) { this.isNew = false; var pkgXml = fs.readFileSync(this.destinationPath('src/package.xml')).toString(); @@ -51,7 +61,11 @@ module.exports = yeoman.generators.Base.extend({ process.exit(0); } if (result.package.clientModule[0]["$"]["version"]) { - this.current.version = result.package.clientModule[0]["$"]["version"]; + var version = result.package.clientModule[0]["$"]["version"]; + if (version.split(".").length === 2) { + version += ".0"; + } + this.current.version = version; } done(); }.bind(this)); @@ -91,7 +105,7 @@ module.exports = yeoman.generators.Base.extend({ type: 'input', name: 'copyright', message: 'Add a copyright', - default: ' 2015', + default: ' 2016', store: true },{ type: 'input', @@ -187,21 +201,21 @@ module.exports = yeoman.generators.Base.extend({ this.widget = {}; this.widget.widgetName = this.props.widgetName; this.widget.packageName = this.props.widgetName; - this.widget.description = this.props.description; + this.widget.description = this.props.description || this.current.description; this.widget.version = this.props.version; - this.widget.author = this.props.author; + this.widget.author = this.props.author || this.current.author; this.widget.date = (new Date()).toLocaleDateString(); - this.widget.copyright = this.props.copyright; - this.widget.license = this.props.license; + this.widget.copyright = this.props.copyright || this.current.copyright; + this.widget.license = this.props.license || this.current.license; + this.widget.generatorVersion = pkg.version; this.widget.github = (this.props.github !== '' && typeof this.props.github !== 'undefined') ? '"http://github.com/' + this.props.github + '/' + this.widget.widgetName + '"' : false; + this.widget.repository = this.current.repository || false; // Using grunt (future version will include Gulp) this.widget.builder = 'grunt'; if (this.isNew) { // Copy generic files - this.fs.copy(this.templatePath(boilerPlatePath + '.jshintrc'), this.destinationPath('.jshintrc')); - this.fs.copy(this.templatePath('_gitignore'), this.destinationPath('.gitignore')); this.fs.copy(this.templatePath('icon.png'), this.destinationPath('icon.png')); this.fs.copy(this.templatePath(boilerPlatePath + 'assets/app_store_banner.png'), this.destinationPath('assets/app_store_banner.png')); this.fs.copy(this.templatePath(boilerPlatePath + 'assets/app_store_icon.png'), this.destinationPath('assets/app_store_icon.png')); @@ -278,6 +292,12 @@ module.exports = yeoman.generators.Base.extend({ ); } + // Gitignore + this.fs.copy(this.templatePath('_gitignore'), this.destinationPath('.gitignore')); + + // jshint + this.fs.copy(this.templatePath(boilerPlatePath + '.jshintrc'), this.destinationPath('.jshintrc')); + // Package.JSON this.template('_package.json', 'package.json', this.widget, {}); diff --git a/generators/app/templates/Gruntfile.js b/generators/app/templates/Gruntfile.js index e0947cc..e4137fc 100644 --- a/generators/app/templates/Gruntfile.js +++ b/generators/app/templates/Gruntfile.js @@ -1,8 +1,17 @@ -// Generated on <%= (new Date).toISOString().split('T')[0] %> using <%= pkg.name %> <%= pkg.version %> :: http://github.com/<%= pkg.repository.url %> +// Generated on <%= (new Date).toISOString().split('T')[0] %> using <%= pkg.name %> <%= pkg.version %> :: <%= pkg.repository.url %> /*jshint -W069*/ /*global module*/ "use strict"; +// In case you seem to have trouble starting Mendix through `grunt start-mendix`, you might have to set the path to the Mendix application. +// If it works, leave MODELER_PATH at null +var MODELER_PATH = null; +var MODELER_ARGS = "/file:{path}"; + +/******************************************************************************** + * Do not edit anything below, unless you know what you are doing + ********************************************************************************/ + var path = require("path"), mendixApp = require("node-mendix-modeler-path"), base64 = require("node-base64-image"), @@ -13,23 +22,35 @@ var path = require("path"), renderOpts: { pretty: true, indent: " ", newline: "\n" }, xmldec: { standalone: null, encoding: "utf-8" } }), - shelljs = require("shelljs"); - -// In case you seem to have trouble starting Mendix through `grunt start-mendix`, you might have to set the path to the Mendix application. -// If it works, leave MODELER_PATH at null -var MODELER_PATH = null; -var MODELER_ARGS = "/file:{path}"; - -// In case you have a different path to the test project (currently in ./test/Test.mpr) point TEST_PATH to the Test-project (full path). Otherwise, leave at null -var TEST_PATH = null; -// Use this example if you want to point it to a different subfolder and specific Test project Name: -// var TEST_PATH = path.join(shelljs.pwd(), ".//.mpr"); + shelljs = require("shelljs"), + pkg = require("./package.json"); + +var TEST_PATH = path.join(shelljs.pwd(), "/test/Test.mpr"); +var WIDGET_XML = path.join(shelljs.pwd(), "/src/", pkg.name, "/", pkg.name + ".xml"); +var PACKAGE_XML = path.join(shelljs.pwd(), "/src/package.xml"); +var TEST_WIDGETS_FOLDER = path.join(shelljs.pwd(), "./test/widgets"); +var TEST_WIDGETS_DEPLOYMENT_FOLDER = path.join(shelljs.pwd(), "./test/deployment/web/widgets"); + +/** + * If you want to use a custom folder for the test project, make sure these are added to package.json: + * "paths": { + * "testProjectFolder": "./test/", + * "testProjectFileName": "Test.mpr" + * }, + * You can test it by running: `grunt folders` + **/ + +if (pkg.paths && pkg.paths.testProjectFolder && pkg.paths.testProjectFileName) { + var folder = pkg.paths.testProjectFolder; + if (folder.indexOf(".") === 0) { + folder = path.join(shelljs.pwd(), folder); + } + TEST_PATH = path.join(folder, pkg.paths.testProjectFileName); + TEST_WIDGETS_FOLDER = path.join(folder, "/widgets"); + TEST_WIDGETS_DEPLOYMENT_FOLDER = path.join(folder, "/deployment/web/widgets"); +} module.exports = function (grunt) { - var pkg = grunt.file.readJSON("package.json"); - var widgetXml = path.join(shelljs.pwd(), "/src/", pkg.name, "/", pkg.name + ".xml"); - var packageXml = path.join(shelljs.pwd(), "/src/package.xml"); - grunt.initConfig({ watch: { autoDeployUpdate: { @@ -59,20 +80,20 @@ module.exports = function (grunt) { copy: { deployment: { files: [ - { dest: "./test/deployment/web/widgets", cwd: "./src/", src: ["**/*"], expand: true } + { dest: TEST_WIDGETS_DEPLOYMENT_FOLDER, cwd: "./src/", src: ["**/*"], expand: true } ] }, mpks: { files: [ - { dest: "./test/widgets", cwd: "./dist/", src: [ pkg.name + ".mpk"], expand: true } + { dest: TEST_WIDGETS_FOLDER, cwd: "./dist/", src: [ pkg.name + ".mpk"], expand: true } ] } }, clean: { build: [ - "./dist/" + pkg.name + "/*", - "./test/deployment/web/widgets/" + pkg.name + "/*", - "./test/widgets/" + pkg.name + ".mpk" + path.join(shelljs.pwd(), "dist", pkg.name, "/*"), + path.join(TEST_WIDGETS_DEPLOYMENT_FOLDER, pkg.name, "/*"), + path.join(TEST_WIDGETS_FOLDER, pkg.name + ".mpk") ] } }); @@ -84,14 +105,12 @@ module.exports = function (grunt) { grunt.loadNpmTasks("grunt-newer"); grunt.registerTask("start-modeler", function () { - var done = this.async(), - testProjectPath = TEST_PATH !== null ? TEST_PATH : path.join(shelljs.pwd(), "/test/Test.mpr"); - + var done = this.async(); if (MODELER_PATH !== null || (mendixApp.err === null && mendixApp.output !== null && mendixApp.output.cmd && mendixApp.output.arg)) { grunt.util.spawn({ cmd: MODELER_PATH || mendixApp.output.cmd, args: [ - (MODELER_PATH !== null ? MODELER_ARGS : mendixApp.output.arg).replace("{path}", testProjectPath) + (MODELER_PATH !== null ? MODELER_ARGS : mendixApp.output.arg).replace("{path}", TEST_PATH) ] }, function () { done(); @@ -105,12 +124,12 @@ module.exports = function (grunt) { grunt.registerTask("version", function (version) { var done = this.async(); - if (!grunt.file.exists(packageXml)) { - grunt.log.error("Cannot find " + packageXml); + if (!grunt.file.exists(PACKAGE_XML)) { + grunt.log.error("Cannot find " + PACKAGE_XML); return done(); } - var xml = grunt.file.read(packageXml); + var xml = grunt.file.read(PACKAGE_XML); parser.parseString(xml, function (err, res) { if (err) { grunt.log.error(err); @@ -130,7 +149,7 @@ module.exports = function (grunt) { res.package.clientModule[0]["$"]["version"] = version; pkg.version = version; var xmlString = builder.buildObject(res); - grunt.file.write(packageXml, xmlString); + grunt.file.write(PACKAGE_XML, xmlString); grunt.file.write("package.json", JSON.stringify(pkg, null, 2)); done(); } @@ -149,21 +168,21 @@ module.exports = function (grunt) { grunt.log.writeln("Processing icon"); - if (!grunt.file.exists(iconPath) || !grunt.file.exists(widgetXml)) { + if (!grunt.file.exists(iconPath) || !grunt.file.exists(WIDGET_XML)) { grunt.log.error("can\'t generate icon"); return done(); } base64.base64encoder(iconPath, options, function (err, image) { if (!err) { - var xmlOld = grunt.file.read(widgetXml); + var xmlOld = grunt.file.read(WIDGET_XML); parser.parseString(xmlOld, function (err, result) { if (!err) { if (result && result.widget && result.widget.icon) { result.widget.icon[0] = image; } var xmlString = builder.buildObject(result); - grunt.file.write(widgetXml, xmlString); + grunt.file.write(WIDGET_XML, xmlString); done(); } }); @@ -174,6 +193,17 @@ module.exports = function (grunt) { }); }); + grunt.registerTask("folders", function () { + var done = this.async(); + grunt.log.writeln("\nShowing file paths that Grunt will use. You can edit the package.json accordingly\n"); + grunt.log.writeln("TEST_PATH: ", TEST_PATH); + grunt.log.writeln("WIDGET_XML: ", WIDGET_XML); + grunt.log.writeln("PACKAGE_XML: ", PACKAGE_XML); + grunt.log.writeln("TEST_WIDGETS_FOLDER: ", TEST_WIDGETS_FOLDER); + grunt.log.writeln("TEST_WIDGETS_DEPLOYMENT_FOLDER: ", TEST_WIDGETS_DEPLOYMENT_FOLDER); + return done(); + }); + grunt.registerTask("start-mendix", [ "start-modeler" ]); grunt.registerTask( diff --git a/generators/app/templates/_package.json b/generators/app/templates/_package.json index 2b08aba..b461d22 100644 --- a/generators/app/templates/_package.json +++ b/generators/app/templates/_package.json @@ -2,6 +2,8 @@ "name": "<%= widgetName %>", "version": "<%= version %>", "description": "<%= description %>", + "license": "<%= license %>", + "author": "<%= author %>", "private": true, "dependencies": { }, @@ -21,10 +23,16 @@ "repository": { "type": "git", "url": <%= github %> - },<% } %> + },<% } %><% if (repository) { %> + "repository": <%= repository %>,<% } %> "engines": { "node": ">=0.12.0" }, + "generatorVersion": "<%= generatorVersion %>", + "paths": { + "testProjectFolder": "./test/", + "testProjectFileName": "Test.mpr" + }, "scripts": {<% if (builder == 'grunt') { %> "test": "grunt test"<% } if (builder == 'gulp') { %> <% } %> diff --git a/package.json b/package.json index e238339..5b6f958 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "generator-mendix", - "version": "1.2.0", + "version": "1.3.0", "description": "Mendix Widget generator", "license": "MIT", "main": "app/index.js",