This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGruntfile.js
195 lines (176 loc) · 7.06 KB
/
Gruntfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Generated on 2015-12-29 using generator-mendix 1.1.0 :: https://github.com/mendix/generator-mendix
/*jshint -W069*/
/*global module*/
"use strict";
var path = require("path"),
mendixApp = require("node-mendix-modeler-path"),
base64 = require("node-base64-image"),
semver = require("semver"),
xml2js = require("xml2js"),
parser = new xml2js.Parser(),
builder = new xml2js.Builder({
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");
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: {
"files": [ "./src/**/*" ],
"tasks": [ "compress", "newer:copy" ],
options: {
debounceDelay: 250,
livereload: true
}
}
},
compress: {
makezip: {
options: {
archive: "./dist/" + pkg.name + ".mpk",
mode: "zip"
},
files: [{
expand: true,
date: new Date(),
store: false,
cwd: "./src",
src: ["**/*"]
}]
}
},
copy: {
deployment: {
files: [
{ dest: "./test/deployment/web/widgets", cwd: "./src/", src: ["**/*"], expand: true }
]
},
mpks: {
files: [
{ dest: "./test/widgets", cwd: "./dist/", src: [ pkg.name + ".mpk"], expand: true }
]
}
},
clean: {
build: [
"./dist/" + pkg.name + "/*",
"./test/deployment/web/widgets/" + pkg.name + "/*",
"./test/widgets/" + pkg.name + ".mpk"
]
}
});
grunt.loadNpmTasks("grunt-contrib-compress");
grunt.loadNpmTasks("grunt-contrib-clean");
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks("grunt-contrib-copy");
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");
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)
]
}, function () {
done();
});
} else {
console.error("Cannot start Modeler, see error:");
console.log(mendixApp.err);
done();
}
});
grunt.registerTask("version", function (version) {
var done = this.async();
if (!grunt.file.exists(packageXml)) {
grunt.log.error("Cannot find " + packageXml);
return done();
}
var xml = grunt.file.read(packageXml);
parser.parseString(xml, function (err, res) {
if (err) {
grunt.log.error(err);
return done();
}
if (res.package.clientModule[0]["$"]["version"]) {
var currentVersion = res.package.clientModule[0]["$"]["version"];
if (!version) {
grunt.log.writeln("\nCurrent version is " + currentVersion);
grunt.log.writeln("Set new version by running 'grunt version:x.y.z'");
done();
} else {
if (!semver.valid(version) || !semver.satisfies(version, ">= 1.0.0")) {
grunt.log.error("\nPlease provide a valid version that is higher than 1.0.0. Current version: " + currentVersion);
done();
} else {
res.package.clientModule[0]["$"]["version"] = version;
pkg.version = version;
var xmlString = builder.buildObject(res);
grunt.file.write(packageXml, xmlString);
grunt.file.write("package.json", JSON.stringify(pkg, null, 2));
done();
}
}
} else {
grunt.log.error("Cannot find current version number");
}
});
});
grunt.registerTask("generate-icon", function () {
var iconPath = path.join(shelljs.pwd(), "/icon.png"),
options = {localFile: true, string: true},
done = this.async();
grunt.log.writeln("Processing icon");
if (!grunt.file.exists(iconPath) || !grunt.file.exists(widgetXml)) {
grunt.log.error("can\'t generate icon");
return done();
}
base64.base64encoder(iconPath, options, function (err, image) {
if (!err) {
var xmlOld = grunt.file.read(widgetXml);
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);
done();
}
});
} else {
grunt.log.error("can\'t generate icon");
return done();
}
});
});
grunt.registerTask("start-mendix", [ "start-modeler" ]);
grunt.registerTask(
"default",
"Watches for changes and automatically creates an MPK file, as well as copying the changes to your deployment folder",
[ "watch" ]
);
grunt.registerTask(
"clean build",
"Compiles all the assets and copies the files to the build directory.",
[ "clean", "compress", "copy" ]
);
grunt.registerTask(
"build",
[ "clean build" ]
);
};