-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathbuild.js
396 lines (364 loc) · 12.2 KB
/
build.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
This script is designed to be run using either NodeJS or WSH (Windows Script Host), and is used to run build script modules. This script takes care of some common bootstrapping for these build script modules, such as setting up the service adapters, parsing out the named parameters for the build script module, and doing the bootstrapping so that modules required by build script modules are loaded correctly. This leaves the build script modules to only have to deal with implementing their build processes.
USAGES:
Using NodeJS...
node build.js Uize.Build.SomeBuildScript
node build.js Uize.Build.SomeBuildScript p1Name=p1Value p2Name=p2Value
Using WSH (Windows Script Host)...
wscript build.js Uize.Build.SomeBuildScript
wscript build.js Uize.Build.SomeBuildScript p1Name=p1Value p2Name=p2Value
NOTE:
The working directory for running this script in node should be the UIZE root directory.
*/
function _eval (_toEval) {
return eval (_toEval);
}
(function () {
var _isWsh = typeof ActiveXObject != 'undefined';
/*** get the command line args, excluding the first two ***/
var _args;
if (_isWsh) {
_args = [];
for (
var _wshArgNo = -1, _wshArgs = WScript.Arguments, _wshArgsLength = _wshArgs.length;
++_wshArgNo < _wshArgsLength;
) {
_args.push (_wshArgs (_wshArgNo));
}
} else {
_args = process.argv.slice (2);
}
/*** determine build module name (if this script is being used in that way) ***/
var
_buildModuleName,
_thisScriptName = 'build.js',
_scriptFullName = _isWsh ? WScript.ScriptFullName : process.argv [1]
;
if (_scriptFullName.slice (_scriptFullName.length - _thisScriptName.length) == _thisScriptName) {
_buildModuleName = _args [0];
_args = _args.slice (1);
}
/*** parse out named arguments ***/
var _params = {};
for (var _argNo = -1, _arg, _delimPos; ++_argNo < _args.length;) {
_delimPos = (_arg = _args [_argNo]).indexOf ('=');
if (_delimPos > -1)
_params [_arg.slice (0,_delimPos)] = _arg.slice (_delimPos + 1)
;
}
function _build (_buildModuleName,_paramOverrides) {
function _copyInto (_target,_source) {
for (var _key in _source)
_target [_key] = _source [_key]
;
return _target;
}
_copyInto (_params,_paramOverrides);
/*** minimal file system functions ***/
var
_pathToRoot = _params.pathToRoot || '',
_fileSystem,
_readFile,
_fileExists,
_folderExists
;
function _resolvePath (_path) {
return /^\.?\//.test (_path) ? _path : _pathToRoot + _path;
}
if (_isWsh) {
_fileSystem = new ActiveXObject ('Scripting.FileSystemObject');
_fileExists = function (_path) {
return _fileSystem.FileExists (_resolvePath (_path));
};
_folderExists = function (_path) {
return _fileSystem.FolderExists (_resolvePath (_path));
};
_readFile = function (_path) {
var
_file = _fileSystem.OpenTextFile (_resolvePath (_path),1),
_fileText = _file.ReadAll ()
;
_file.Close();
//BOM meaning it is UTF8. WScript can't read these and treats
// them as UTF16, but ignored the BOM, so we just clip it off here.
//http://en.wikipedia.org/wiki/Byte_order_mark
if (
_fileText.charCodeAt(0) === 239
&& _fileText.charCodeAt(1) === 187
&& _fileText.charCodeAt(2) === 191
) {
_fileText = _fileText.substring(3);
}
return _fileText;
};
} else {
_fileSystem = require ('fs');
var _pathExists = function (_path,_mustBeFolder) {
try {
var _stat = _fileSystem.statSync (_resolvePath (_path));
return _mustBeFolder == undefined || !!(_stat.mode & (1 << 14)) == _mustBeFolder;
} catch (_error) {
return false;
}
};
_fileExists = function (_path) {
return _pathExists (_path);
};
_folderExists = function (_path) {
return _pathExists (_path,true);
};
_readFile = function (_path) {
return _fileSystem.readFileSync (_resolvePath (_path),'utf8');
};
}
/*** miscellaneous global JavaScript functions (to mirror what's available in the browser context) ***/
if (_isWsh) {
/*** implementation for console.log (absent in the WSH environment) ***/
if (typeof console == 'undefined')
console = {}
;
if (typeof console.log != 'function')
console.log = function () {
WScript.Echo ([].slice.call (arguments).join (', '))
}
;
/*** implementations for alert, confirm, and timeouts (absent in the WSH environment) ***/
function _popup (_message,_title,_buttonsAndIconMask) {
return (
(_popup._wscriptShell || (_popup._wscriptShell = new ActiveXObject ('wscript.shell'))).Popup (
_message + '',
0, // seconds to wait before auto-dismissing (0 = stay open forever)
_title,
_buttonsAndIconMask
)
);
}
alert = function (_message) {
_popup (_message,'Windows Script Host Alert',0 | 48 /* 0 = ok button only, 48 = warning icon */);
};
confirm = function (_message) {
return _popup (
_message,
'Please Confirm...',
1 | 32 /* 1 = ok and cancel, 32 = question mark icon */
) == 1;
};
/*** timeouts emulation code for WSH ***/
var
_timeouts = [],
_timeoutsAdded = 0,
_timeoutByLookup = {}
;
setTimeout = function (_function,_timeFromNow) {
_timeouts.push (
_timeoutByLookup [_timeoutsAdded] = {
_func:_function,
_when:+new Date + Math.max (_timeFromNow || 0,0)
}
);
return _timeoutsAdded++;
};
clearTimeout = function (_timeoutId) {
var _timeout = _timeoutByLookup [_timeoutId];
if (_timeout) {
for (
var _timeoutNo = -1, _timeoutsLength = _timeouts.length;
++_timeoutNo < _timeoutsLength;
) {
if (_timeouts [_timeoutNo] == _timeout) {
_timeouts.splice (_timeoutNo,1);
break;
}
}
}
};
executeTimeouts = function () {
while (_timeouts.length) {
var
_nextTimeoutNo,
_minWhen = Infinity
;
for (
var _timeoutNo = -1, _timeoutsLength = _timeouts.length;
++_timeoutNo < _timeoutsLength;
) {
var _timeout = _timeouts [_timeoutNo];
if (_timeout._when < _minWhen) {
_minWhen = _timeout._when;
_nextTimeoutNo = _timeoutNo;
}
}
var
_nextTimeout = _timeouts [_nextTimeoutNo],
_sleepTime = _nextTimeout._when - new Date
;
_timeouts.splice (_nextTimeoutNo,1);
if (_sleepTime > 0)
WScript.Sleep (_sleepTime)
;
_nextTimeout._func ();
}
};
/* TO DO:
for prompt, try to figure out how to use VBSCRIPT's InputBox built-in function
http://wsh2.uw.hu/ch08c.html
Function WSHInputBox(Message, Title, Value)
WSHInputBox = InputBox(Message, Title, Value)
End Function
*/
} else {
alert = function (_message) {
console.log (_message);
};
}
/*** load config and copy in params ***/
var _config = eval ('(' + _readFile (_params.configFile || 'uize-config.json') + ')');
delete _params.configFile;
_params = _copyInto (_copyInto ({},_config),_params);
(function () {return this}) ().env = _params;
/*** stitch in override params ***/
for (var _paramName in _params) {
if (_paramName.slice (0,7) == 'config.') {
var _paramValue = _params [_paramName];
delete _params [_paramName];
if (_paramName.indexOf ('[') > -1)
_paramName = _paramName.replace (
/\[([^\]]+)\]/g,
function (_match,_paramNamePart) {return _paramNamePart.replace (/\./g,'•')}
)
;
for (
var
_node = _params,
_paramNameParts = _paramName.split ('.'),
_paramNamePartsLength = _paramNameParts.length,
_paramNamePart,
_paramNamePartNo = 0
;
++_paramNamePartNo < _paramNamePartsLength;
) {
_paramNamePart = _paramNameParts [_paramNamePartNo];
if (_paramNamePart.indexOf ('•') > -1)
_paramNamePart = _paramNamePart.replace (/•/g,'.')
;
if (_paramNamePartNo == _paramNamePartsLength - 1) {
if (_paramValue == 'true') {
_paramValue = true;
} else if (_paramValue == 'false') {
_paramValue = false;
} else {
var _paramValueAsNumber = +_paramValue;
if (_paramValueAsNumber == _paramValueAsNumber)
_paramValue = _paramValueAsNumber
;
}
_node [_paramNamePart] = _paramValue;
} else {
_node = _node [_paramNamePart];
if (_node == null || typeof _node != 'object')
_node = _node [_paramNamePart] = {}
;
}
}
}
}
/*** set logFilePath ***/
if (_params.logFilePath == null)
_params.logFilePath = (_params.logsPath || 'logs') + '/' + _buildModuleName + '.log'
;
/*** resolve staleBefore param ***/
if (_params.staleBefore == 'now')
_params.staleBefore = +new Date
;
/*** load Uize base class and set up with module loader ***/
var _useSource = _params.useSource + '' != 'false';
function _modulePathResolver (_moduleName) {
return _moduleName;
}
function _moduleLoader (_moduleName,_callback) {
var _moduleText = '';
if (_params.modulesToStub && _params.modulesToStub.test (_moduleName)) {
_moduleText = 'Uize.module ({name:\'' + _moduleName + '\'})';
} else {
var
_moduleFilePath = '/' + _modulePathResolver (_moduleName),
_modulesFolder = _params.modulesFolder,
_moduleSourcePath =
(
/^Uize(\.|$)/.test (_moduleName)
? _params.uizePath + '/' + (_params.uizeModulesFolder || 'js')
: _params.sourcePath + '/' + _modulesFolder
) + _moduleFilePath,
_moduleBuiltPath = _params.builtPath + '/' + _modulesFolder + _moduleFilePath,
_modulePath = _useSource ? _moduleSourcePath : _moduleBuiltPath
;
if (_fileExists (_modulePath + '.js')) {
_moduleText = _readFile (_modulePath + '.js');
} else if (_fileExists (_modulePath + '.js.jst')) {
Uize.require (
'Uize.Template.Module',
function (_Uize_Template_Module) {
_moduleText = _Uize_Template_Module.buildTemplateModuleText (
_moduleName,
_readFile (_modulePath + '.js.jst')
);
}
);
} else if (_fileExists (_modulePath + '.csst')) {
_moduleText =
'Uize.module ({\n' +
' name:\'' + _moduleName + '\',\n' +
' superclass:\'Uize.Dom.CssModule\',\n' +
' builder:function (_superclass) {return _superclass.subclass ()}\n' +
'});';
} else if (
_fileExists (_modulePath + '.loc') ||
_fileExists (_modulePath + '.htmlt') ||
_fileExists (_modulePath + '.simpledata') // TODO: should really read and parse .simpledata file
) {
_moduleText =
'Uize.module ({\n' +
' name:\'' + _moduleName + '\',\n' +
' builder:function () {return Uize.package ()}\n' +
'});';
} else if (_folderExists (_moduleSourcePath)) {
_moduleText = 'Uize.module ({name:\'' + _moduleName + '\'});';
} else {
throw Error ('Module loader can\'t find module ' + _moduleName + ' at path ' + _modulePath + '.js');
}
}
_callback (_moduleText);
}
_moduleLoader (
'Uize',
function (_uizeCode) {
_eval (_uizeCode);
if (!_isWsh)
Uize.laxEval = _eval // this actually *needs* to be overridden for NodeJS context
;
_modulePathResolver = Uize.modulePathResolver;
Uize.moduleLoader = _moduleLoader;
Uize.addFolderOrgNamespaces (_params.folderOrgNamespaces || []);
}
);
/*** services setup & run build module (if specified) ***/
Uize.require (
_params.servicesSetup || 'Uize.Build.ServicesSetup',
function (_servicesSetup) {
_servicesSetup.setup ();
_buildModuleName &&
Uize.require (
_buildModuleName,
function (_buildModule) {
_buildModule.perform (_params);
_isWsh && executeTimeouts ();
}
)
;
}
);
};
/*** if this script is being run directly, then run the specified build process ***/
_buildModuleName && _build (_buildModuleName);
return _build;
}) ();