Skip to content

Commit

Permalink
Add folders task, add package.json paths && update parsing old packag…
Browse files Browse the repository at this point in the history
…e.json
  • Loading branch information
JelteMX committed Jan 8, 2016
1 parent 205253c commit 164936c
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 42 deletions.
36 changes: 28 additions & 8 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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));
Expand Down Expand Up @@ -91,7 +105,7 @@ module.exports = yeoman.generators.Base.extend({
type: 'input',
name: 'copyright',
message: 'Add a copyright',
default: '<Your Company> 2015',
default: '<Your Company> 2016',
store: true
},{
type: 'input',
Expand Down Expand Up @@ -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 !== '<none>' && 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'));
Expand Down Expand Up @@ -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, {});

Expand Down
94 changes: 62 additions & 32 deletions generators/app/templates/Gruntfile.js
Original file line number Diff line number Diff line change
@@ -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"),
Expand All @@ -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(), "./<custom folder>/<Custom Test Project Name>.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: {
Expand Down Expand Up @@ -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")
]
}
});
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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();
}
Expand All @@ -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();
}
});
Expand All @@ -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(
Expand Down
10 changes: 9 additions & 1 deletion generators/app/templates/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"name": "<%= widgetName %>",
"version": "<%= version %>",
"description": "<%= description %>",
"license": "<%= license %>",
"author": "<%= author %>",
"private": true,
"dependencies": {
},
Expand All @@ -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') { %>
<% } %>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 164936c

Please sign in to comment.