|
1 |
| -'use strict'; |
| 1 | +// Generated on 2016-01-04 using generator-mendix 1.1.0 :: http://github.com/git+https://github.com/mendix/generator-mendix.git |
| 2 | +/*jshint -W069*/ |
| 3 | +/*global module*/ |
| 4 | +"use strict"; |
| 5 | + |
| 6 | +var path = require("path"), |
| 7 | + mendixApp = require("node-mendix-modeler-path"), |
| 8 | + base64 = require("node-base64-image"), |
| 9 | + semver = require("semver"), |
| 10 | + xml2js = require("xml2js"), |
| 11 | + parser = new xml2js.Parser(), |
| 12 | + builder = new xml2js.Builder({ |
| 13 | + renderOpts: { pretty: true, indent: " ", newline: "\n" }, |
| 14 | + xmldec: { standalone: null, encoding: "utf-8" } |
| 15 | + }), |
| 16 | + shelljs = require("shelljs"); |
2 | 17 |
|
3 | 18 | // In case you seem to have trouble starting Mendix through `grunt start-mendix`, you might have to set the path to the Mendix application.
|
4 | 19 | // If it works, leave MODELER_PATH at null
|
5 | 20 | var MODELER_PATH = null;
|
6 |
| -var MODELER_ARGS = '/file:{path}'; |
7 |
| - |
8 |
| -var path = require('path'), |
9 |
| - mendixApp = require('node-mendix-modeler-path'), |
10 |
| - shelljs = require('shelljs'); |
| 21 | +var MODELER_ARGS = "/file:{path}"; |
11 | 22 |
|
12 | 23 | // 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
|
13 |
| -var TEST_PATH = path.join(shelljs.pwd(), './test/Test.mpr'); |
| 24 | +var TEST_PATH = null; |
| 25 | +// Use this example if you want to point it to a different subfolder and specific Test project Name: |
| 26 | +// var TEST_PATH = path.join(shelljs.pwd(), "./<custom folder>/<Custom Test Project Name>.mpr"); |
14 | 27 |
|
15 | 28 | module.exports = function (grunt) {
|
16 |
| - var pkg = grunt.file.readJSON("package.json"); |
17 |
| - grunt.verbose; |
18 |
| - grunt.initConfig({ |
19 |
| - watch: { |
20 |
| - autoDeployUpdate: { |
21 |
| - "files": [ "./src/**/*" ], |
22 |
| - "tasks": [ "newer:copy", "compress" ], |
23 |
| - options: { |
24 |
| - debounceDelay: 250, |
25 |
| - livereload: true |
26 |
| - } |
27 |
| - } |
28 |
| - }, |
29 |
| - compress: { |
30 |
| - makezip: { |
31 |
| - options: { |
32 |
| - archive: "./dist/" + pkg.name + ".mpk", |
33 |
| - mode: "zip" |
| 29 | + var pkg = grunt.file.readJSON("package.json"); |
| 30 | + var widgetXml = path.join(shelljs.pwd(), "/src/", pkg.name, "/", pkg.name + ".xml"); |
| 31 | + var packageXml = path.join(shelljs.pwd(), "/src/package.xml"); |
| 32 | + |
| 33 | + grunt.initConfig({ |
| 34 | + watch: { |
| 35 | + autoDeployUpdate: { |
| 36 | + "files": [ "./src/**/*" ], |
| 37 | + "tasks": [ "compress", "newer:copy" ], |
| 38 | + options: { |
| 39 | + debounceDelay: 250, |
| 40 | + livereload: true |
| 41 | + } |
| 42 | + } |
| 43 | + }, |
| 44 | + compress: { |
| 45 | + makezip: { |
| 46 | + options: { |
| 47 | + archive: "./dist/" + pkg.name + ".mpk", |
| 48 | + mode: "zip" |
| 49 | + }, |
| 50 | + files: [{ |
| 51 | + expand: true, |
| 52 | + date: new Date(), |
| 53 | + store: false, |
| 54 | + cwd: "./src", |
| 55 | + src: ["**/*"] |
| 56 | + }] |
| 57 | + } |
| 58 | + }, |
| 59 | + copy: { |
| 60 | + deployment: { |
| 61 | + files: [ |
| 62 | + { dest: "./test/deployment/web/widgets", cwd: "./src/", src: ["**/*"], expand: true } |
| 63 | + ] |
| 64 | + }, |
| 65 | + mpks: { |
| 66 | + files: [ |
| 67 | + { dest: "./test/widgets", cwd: "./dist/", src: [ pkg.name + ".mpk"], expand: true } |
| 68 | + ] |
| 69 | + } |
34 | 70 | },
|
35 |
| - files: [{ |
36 |
| - expand: true, |
37 |
| - date: new Date(), |
38 |
| - store: false, |
39 |
| - cwd: "./src", |
40 |
| - src: ["**/*"] |
41 |
| - }] |
42 |
| - } |
43 |
| - }, |
44 |
| - copy: { |
45 |
| - deployment: { |
46 |
| - files: [ |
47 |
| - { dest: "./test/deployment/web/widgets", cwd: "./src/", src: ["**/*"], expand: true } |
48 |
| - ] |
49 |
| - }, |
50 |
| - mpks: { |
51 |
| - files: [ |
52 |
| - { dest: "./test/widgets", cwd: "./dist/", src: [ pkg.name + ".mpk"], expand: true } |
53 |
| - ] |
54 |
| - } |
55 |
| - }, |
56 |
| - clean: { |
57 |
| - build: [ |
58 |
| - "./dist/" + pkg.name + "/*", |
59 |
| - "./test/deployment/web/widgets/" + pkg.name + "/*", |
60 |
| - "./test/widgets/" + pkg.name + ".mpk" |
61 |
| - ] |
62 |
| - } |
63 |
| - }); |
64 |
| - |
65 |
| - grunt.loadNpmTasks("grunt-contrib-compress"); |
66 |
| - grunt.loadNpmTasks("grunt-contrib-clean"); |
67 |
| - grunt.loadNpmTasks("grunt-contrib-watch"); |
68 |
| - grunt.loadNpmTasks("grunt-contrib-copy"); |
69 |
| - grunt.loadNpmTasks("grunt-newer"); |
70 |
| - |
71 |
| - grunt.registerTask("start-modeler", function () { |
72 |
| - var done = this.async(), |
73 |
| - testProjectPath = TEST_PATH !== null ? TEST_PATH : path.join(shelljs.pwd(), '/test/Test.mpr'); |
74 |
| - |
75 |
| - if (MODELER_PATH !== null || (mendixApp.err === null && mendixApp.output !== null && mendixApp.output.cmd && mendixApp.output.arg)) { |
76 |
| - grunt.util.spawn({ |
77 |
| - cmd: MODELER_PATH || mendixApp.output.cmd, |
78 |
| - args: [ |
79 |
| - (MODELER_PATH !== null ? MODELER_ARGS : mendixApp.output.arg).replace('{path}', testProjectPath) |
80 |
| - ] |
81 |
| - }, function () { |
82 |
| - done(); |
83 |
| - }); |
84 |
| - } else { |
85 |
| - console.error('Cannot start Modeler, see error:'); |
86 |
| - console.log(mendixApp.err); |
87 |
| - done(); |
88 |
| - } |
89 |
| - }); |
90 |
| - |
91 |
| - grunt.registerTask( |
92 |
| - "default", |
93 |
| - "Watches for changes and automatically creates an MPK file, as well as copying the changes to your deployment folder", |
94 |
| - [ "watch" ] |
95 |
| - ); |
96 |
| - |
97 |
| - grunt.registerTask( |
98 |
| - "clean build", |
99 |
| - "Compiles all the assets and copies the files to the build directory.", |
100 |
| - [ "clean", "compress", "copy" ] |
101 |
| - ); |
102 |
| - |
103 |
| - grunt.registerTask( |
104 |
| - "build", |
105 |
| - [ "clean build" ] |
106 |
| - ); |
| 71 | + clean: { |
| 72 | + build: [ |
| 73 | + "./dist/" + pkg.name + "/*", |
| 74 | + "./test/deployment/web/widgets/" + pkg.name + "/*", |
| 75 | + "./test/widgets/" + pkg.name + ".mpk" |
| 76 | + ] |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + grunt.loadNpmTasks("grunt-contrib-compress"); |
| 81 | + grunt.loadNpmTasks("grunt-contrib-clean"); |
| 82 | + grunt.loadNpmTasks("grunt-contrib-watch"); |
| 83 | + grunt.loadNpmTasks("grunt-contrib-copy"); |
| 84 | + grunt.loadNpmTasks("grunt-newer"); |
| 85 | + |
| 86 | + grunt.registerTask("start-modeler", function () { |
| 87 | + var done = this.async(), |
| 88 | + testProjectPath = TEST_PATH !== null ? TEST_PATH : path.join(shelljs.pwd(), "/test/Test.mpr"); |
| 89 | + |
| 90 | + if (MODELER_PATH !== null || (mendixApp.err === null && mendixApp.output !== null && mendixApp.output.cmd && mendixApp.output.arg)) { |
| 91 | + grunt.util.spawn({ |
| 92 | + cmd: MODELER_PATH || mendixApp.output.cmd, |
| 93 | + args: [ |
| 94 | + (MODELER_PATH !== null ? MODELER_ARGS : mendixApp.output.arg).replace("{path}", testProjectPath) |
| 95 | + ] |
| 96 | + }, function () { |
| 97 | + done(); |
| 98 | + }); |
| 99 | + } else { |
| 100 | + console.error("Cannot start Modeler, see error:"); |
| 101 | + console.log(mendixApp.err); |
| 102 | + done(); |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + grunt.registerTask("version", function (version) { |
| 107 | + var done = this.async(); |
| 108 | + if (!grunt.file.exists(packageXml)) { |
| 109 | + grunt.log.error("Cannot find " + packageXml); |
| 110 | + return done(); |
| 111 | + } |
| 112 | + |
| 113 | + var xml = grunt.file.read(packageXml); |
| 114 | + parser.parseString(xml, function (err, res) { |
| 115 | + if (err) { |
| 116 | + grunt.log.error(err); |
| 117 | + return done(); |
| 118 | + } |
| 119 | + if (res.package.clientModule[0]["$"]["version"]) { |
| 120 | + var currentVersion = res.package.clientModule[0]["$"]["version"]; |
| 121 | + if (!version) { |
| 122 | + grunt.log.writeln("\nCurrent version is " + currentVersion); |
| 123 | + grunt.log.writeln("Set new version by running 'grunt version:x.y.z'"); |
| 124 | + done(); |
| 125 | + } else { |
| 126 | + if (!semver.valid(version) || !semver.satisfies(version, ">= 1.0.0")) { |
| 127 | + grunt.log.error("\nPlease provide a valid version that is higher than 1.0.0. Current version: " + currentVersion); |
| 128 | + done(); |
| 129 | + } else { |
| 130 | + res.package.clientModule[0]["$"]["version"] = version; |
| 131 | + pkg.version = version; |
| 132 | + var xmlString = builder.buildObject(res); |
| 133 | + grunt.file.write(packageXml, xmlString); |
| 134 | + grunt.file.write("package.json", JSON.stringify(pkg, null, 2)); |
| 135 | + done(); |
| 136 | + } |
| 137 | + } |
| 138 | + } else { |
| 139 | + grunt.log.error("Cannot find current version number"); |
| 140 | + } |
| 141 | + }); |
| 142 | + |
| 143 | + }); |
| 144 | + |
| 145 | + grunt.registerTask("generate-icon", function () { |
| 146 | + var iconPath = path.join(shelljs.pwd(), "/icon.png"), |
| 147 | + options = {localFile: true, string: true}, |
| 148 | + done = this.async(); |
| 149 | + |
| 150 | + grunt.log.writeln("Processing icon"); |
| 151 | + |
| 152 | + if (!grunt.file.exists(iconPath) || !grunt.file.exists(widgetXml)) { |
| 153 | + grunt.log.error("can\'t generate icon"); |
| 154 | + return done(); |
| 155 | + } |
| 156 | + |
| 157 | + base64.base64encoder(iconPath, options, function (err, image) { |
| 158 | + if (!err) { |
| 159 | + var xmlOld = grunt.file.read(widgetXml); |
| 160 | + parser.parseString(xmlOld, function (err, result) { |
| 161 | + if (!err) { |
| 162 | + if (result && result.widget && result.widget.icon) { |
| 163 | + result.widget.icon[0] = image; |
| 164 | + } |
| 165 | + var xmlString = builder.buildObject(result); |
| 166 | + grunt.file.write(widgetXml, xmlString); |
| 167 | + done(); |
| 168 | + } |
| 169 | + }); |
| 170 | + } else { |
| 171 | + grunt.log.error("can\'t generate icon"); |
| 172 | + return done(); |
| 173 | + } |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + grunt.registerTask("start-mendix", [ "start-modeler" ]); |
| 178 | + |
| 179 | + grunt.registerTask( |
| 180 | + "default", |
| 181 | + "Watches for changes and automatically creates an MPK file, as well as copying the changes to your deployment folder", |
| 182 | + [ "watch" ] |
| 183 | + ); |
| 184 | + |
| 185 | + grunt.registerTask( |
| 186 | + "clean build", |
| 187 | + "Compiles all the assets and copies the files to the build directory.", |
| 188 | + [ "clean", "compress", "copy" ] |
| 189 | + ); |
| 190 | + |
| 191 | + grunt.registerTask( |
| 192 | + "build", |
| 193 | + [ "clean build" ] |
| 194 | + ); |
107 | 195 | };
|
0 commit comments