This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Gruntfile.js
233 lines (212 loc) · 8.53 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Generated on 2018-03-28 using generator-mendix 2.2.3 :: git+https://github.com/mendix/generator-mendix.git
/*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"),
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"),
pkg = require("./package.json"),
currentFolder = shelljs.pwd().toString();
var TEST_PATH = path.join(currentFolder, "/test/Test.mpr");
var WIDGET_XML = path.join(currentFolder, "/src/", pkg.name, "/", pkg.name + ".xml");
var PACKAGE_XML = path.join(currentFolder, "/src/package.xml");
var TEST_WIDGETS_FOLDER = path.join(currentFolder, "./test/widgets");
var TEST_WIDGETS_DEPLOYMENT_FOLDER = path.join(currentFolder, "./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(currentFolder, 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) {
grunt.initConfig({
watch: {
autoDeployUpdate: {
"files": [ "./src/**/*" ],
"tasks": [ "compress", "newer:copy" ],
options: {
debounceDelay: 250,
livereload: true
}
}
},
compress: {
makezip: {
options: {
archive: "./dist/MxTestProject/widgets/" + pkg.name + ".mpk",
mode: "zip"
},
files: [{
expand: true,
date: new Date(),
store: false,
cwd: "./src",
src: ["**/*"]
}]
}
},
copy: {
deployment: {
files: [
{ dest: TEST_WIDGETS_DEPLOYMENT_FOLDER, cwd: "./src/", src: ["**/*"], expand: true }
]
},
mpks: {
files: [
{ dest: TEST_WIDGETS_FOLDER, cwd: "./dist/", src: [ pkg.name + ".mpk"], expand: true }
]
}
},
clean: {
build: [
path.join(currentFolder, "dist", pkg.name, "/*")
]
},
csslint: {
strict: {
options: {
import: 2
},
src: ["src/" + pkg.name + "/widget/ui/*.css"]
}
}
});
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.loadNpmTasks("grunt-contrib-csslint");
grunt.registerTask("start-modeler", function () {
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}", TEST_PATH)
]
}, 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(PACKAGE_XML)) {
grunt.log.error("Cannot find " + PACKAGE_XML);
return done();
}
var xml = grunt.file.read(PACKAGE_XML);
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(PACKAGE_XML, 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(currentFolder, "/icon.png"),
options = {localFile: true, string: true},
done = this.async();
grunt.log.writeln("Processing icon");
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(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(WIDGET_XML, xmlString);
done();
}
});
} else {
grunt.log.error("can\'t generate icon");
return done();
}
});
});
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(
"default",
"Watches for changes and automatically creates an MPK file, as well as copying the changes to your deployment folder",
[ "clean build", "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" ]
);
};