From e48327090bd8cbf87c13f8da6faf0ed1ff63dca8 Mon Sep 17 00:00:00 2001 From: jrburke Date: Sun, 17 Feb 2013 00:20:43 -0800 Subject: [PATCH 01/20] start of windows script host support. Still much to do. --- build/jslib/wsh.js | 20 +++++++++++++++++++ build/jslib/x.js | 49 +++++++++++++++++++++++++++++++++++++++++++++- dist.js | 2 +- 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 build/jslib/wsh.js diff --git a/build/jslib/wsh.js b/build/jslib/wsh.js new file mode 100644 index 00000000..b1adc7f4 --- /dev/null +++ b/build/jslib/wsh.js @@ -0,0 +1,20 @@ +/** + * @license RequireJS wsh Copyright (c) 2013, The Dojo Foundation All Rights Reserved. + * Available via the MIT or new BSD license. + * see: http://github.com/jrburke/requirejs for details + */ + +/*jslint */ +/*global require, load */ + +(function () { + 'use strict'; + require.load = function (context, moduleName, url) { + + //?load(url); + + //Support anonymous modules. + context.completeLoad(moduleName); + }; + +}()); diff --git a/build/jslib/x.js b/build/jslib/x.js index 0ee388fc..8115f8a9 100644 --- a/build/jslib/x.js +++ b/build/jslib/x.js @@ -14,7 +14,8 @@ /*jslint evil: true, nomen: true, sloppy: true */ /*global readFile: true, process: false, Packages: false, print: false, console: false, java: false, module: false, requirejsVars, navigator, -document, importScripts, self, location, Components, FileUtils */ +document, importScripts, self, location, Components, FileUtils, WScript, +ActiveXObject */ var requirejs, require, define, xpcUtil; (function (console, args, readFileFunc) { @@ -229,6 +230,49 @@ var requirejs, require, define, xpcUtil; } }; } + } else if (typeof WScript !== 'undefined' && + typeof ActiveXObject !== 'undefined') { + env = 'wsh'; + + var fso = new ActiveXObject("Scripting.FileSystemObject"); + + readFile = function (path) { + var contents, + stream = new ActiveXObject("ADODB.Stream"); + + stream.Open(); + stream.Type = 2; + stream.Charset = 'utf-8'; + stream.LoadFromFile(path); + contents = stream.ReadText(-1); + stream.Close(); + return contents; + }; + + exec = function (string, name) { + return eval(string); + }; + + exists = function (fileName) { + return fso.FileExists(fileName); + }; + + //Define a console.log for easier logging. Don't + //get fancy though. + if (typeof console === 'undefined') { + console = { + log: function () { + WScript.Echo.apply(undefined, arguments); + } + }; + } + + fileName = WScript.Arguments.Item(0); + + if (fileName && fileName.indexOf('-') === 0) { + commandOption = fileName.substring(1); + fileName = WScript.Arguments.Item(1); + } } //INSERT require.js @@ -252,8 +296,11 @@ var requirejs, require, define, xpcUtil; } else if (env === 'xpconnect') { //INSERT build/jslib/xpconnect.js + } else if (env === 'wsh') { + //INSERT build/jslib/wsh.js } + //Support a default file name to execute. Useful for hosted envs //like Joyent where it defaults to a server.js as the only executed //script. But only do it if this is not an optimization run. diff --git a/dist.js b/dist.js index fbccbe33..3980a6cc 100644 --- a/dist.js +++ b/dist.js @@ -22,7 +22,7 @@ var fs = require('fs'), loadRegExp = /\/\/INSERT ([\w\/\.]+)/g, moduleNameRegExp = /build\/jslib\/([\w\/\-]+)\.js$/, defRegExp = /define\s*\(/, - envs = ['browser', 'node', 'rhino', 'xpconnect'], + envs = ['browser', 'node', 'rhino', 'xpconnect', 'wsh'], //Update this list of files by running the optimizer against //build/jslib/opto.build.js, //but then remove any jslib/node entries and make sure there is From 6eb9c9bab833721d003dd1e4f2f871258f641d36 Mon Sep 17 00:00:00 2001 From: jrburke Date: Sun, 17 Feb 2013 11:34:08 -0800 Subject: [PATCH 02/20] More wsh build-out, cleanup of env-related helpers across xpconnect and wsh. --- build/jslib/rhino/args.js | 4 +-- build/jslib/wsh.js | 7 +++-- build/jslib/wsh/args.js | 25 +++++++++++++++++ build/jslib/wsh/assert.js | 13 +++++++++ build/jslib/wsh/load.js | 17 ++++++++++++ build/jslib/wsh/optimize.js | 1 + build/jslib/wsh/print.js | 14 ++++++++++ build/jslib/wsh/quit.js | 6 ++++ build/jslib/x.js | 52 ++++++++++++++++++----------------- build/jslib/xpconnect/args.js | 4 +-- build/jslib/xpconnect/file.js | 8 +++--- 11 files changed, 115 insertions(+), 36 deletions(-) create mode 100644 build/jslib/wsh/args.js create mode 100644 build/jslib/wsh/assert.js create mode 100644 build/jslib/wsh/load.js create mode 100644 build/jslib/wsh/optimize.js create mode 100644 build/jslib/wsh/print.js create mode 100644 build/jslib/wsh/quit.js diff --git a/build/jslib/rhino/args.js b/build/jslib/rhino/args.js index 425465b2..53ca20bd 100644 --- a/build/jslib/rhino/args.js +++ b/build/jslib/rhino/args.js @@ -5,9 +5,9 @@ */ /*jslint strict: false */ -/*global define: false, process: false */ +/*global define */ -var jsLibRhinoArgs = (typeof rhinoArgs !== 'undefined' && rhinoArgs) || [].concat(Array.prototype.slice.call(arguments, 0)); +var jsLibRhinoArgs = [].concat(Array.prototype.slice.call(arguments, 0)); define(function () { var args = jsLibRhinoArgs; diff --git a/build/jslib/wsh.js b/build/jslib/wsh.js index b1adc7f4..4f9a0a32 100644 --- a/build/jslib/wsh.js +++ b/build/jslib/wsh.js @@ -4,14 +4,15 @@ * see: http://github.com/jrburke/requirejs for details */ -/*jslint */ -/*global require, load */ +/*jshint evil: true */ +/*global require, requirejsEnvUtil */ (function () { 'use strict'; require.load = function (context, moduleName, url) { - //?load(url); + var contents = requirejsEnvUtil.readFile(url); + eval(contents); //Support anonymous modules. context.completeLoad(moduleName); diff --git a/build/jslib/wsh/args.js b/build/jslib/wsh/args.js new file mode 100644 index 00000000..45798d15 --- /dev/null +++ b/build/jslib/wsh/args.js @@ -0,0 +1,25 @@ +/** + * @license Copyright (c) 2013, The Dojo Foundation All Rights Reserved. + * Available via the MIT or new BSD license. + * see: http://github.com/jrburke/requirejs for details + */ + +/*jslint */ +/*global define WScript */ + +define(function () { + var i, + args = [], + wargs = WScript.Arguments; + + for (i = 0; i < wargs.length; i++) { + args.push(wargs(i)); + } + + //Ignore any command option used for r.js + if (args[0] && args[0].indexOf('-' === 0)) { + args = args.slice(1); + } + + return args; +}); diff --git a/build/jslib/wsh/assert.js b/build/jslib/wsh/assert.js new file mode 100644 index 00000000..1d761fe3 --- /dev/null +++ b/build/jslib/wsh/assert.js @@ -0,0 +1,13 @@ +/** + * @license RequireJS Copyright (c) 2013, The Dojo Foundation All Rights Reserved. + * Available via the MIT or new BSD license. + * see: http://github.com/jrburke/requirejs for details + */ + +/*jslint strict: false */ +/*global define: false, load: false */ + +//Just a stub for use with uglify's consolidator.js +define(function () { + return {}; +}); diff --git a/build/jslib/wsh/load.js b/build/jslib/wsh/load.js new file mode 100644 index 00000000..63860e2b --- /dev/null +++ b/build/jslib/wsh/load.js @@ -0,0 +1,17 @@ +/** + * @license RequireJS Copyright (c) 2013, The Dojo Foundation All Rights Reserved. + * Available via the MIT or new BSD license. + * see: http://github.com/jrburke/requirejs for details + */ + +/*jshint evil: true */ +/*global define, requirejsEnvUtil */ + +define(function () { + function load(path) { + var contents = requirejsEnvUtil.readFile(path); + return eval(contents); + } + + return load; +}); diff --git a/build/jslib/wsh/optimize.js b/build/jslib/wsh/optimize.js new file mode 100644 index 00000000..54df66a0 --- /dev/null +++ b/build/jslib/wsh/optimize.js @@ -0,0 +1 @@ +define({}); \ No newline at end of file diff --git a/build/jslib/wsh/print.js b/build/jslib/wsh/print.js new file mode 100644 index 00000000..b3b51173 --- /dev/null +++ b/build/jslib/wsh/print.js @@ -0,0 +1,14 @@ +/** + * @license RequireJS Copyright (c) 2013, The Dojo Foundation All Rights Reserved. + * Available via the MIT or new BSD license. + * see: http://github.com/jrburke/requirejs for details + */ + +/*global define, WScript */ + +define(function () { + function print() { + WScript.Echo.apply(undefined, arguments); + } + return print; +}); diff --git a/build/jslib/wsh/quit.js b/build/jslib/wsh/quit.js new file mode 100644 index 00000000..d7964b29 --- /dev/null +++ b/build/jslib/wsh/quit.js @@ -0,0 +1,6 @@ +/*global define, WScript */ +define(function () { + return function quit(code) { + WScript.Quit(code || 0); + }; +}); diff --git a/build/jslib/x.js b/build/jslib/x.js index 8115f8a9..423fd77f 100644 --- a/build/jslib/x.js +++ b/build/jslib/x.js @@ -17,7 +17,7 @@ console: false, java: false, module: false, requirejsVars, navigator, document, importScripts, self, location, Components, FileUtils, WScript, ActiveXObject */ -var requirejs, require, define, xpcUtil; +var requirejs, require, define, requirejsEnvUtil; (function (console, args, readFileFunc) { var fileName, env, fs, vm, path, exec, rhinoContext, dir, nodeRequire, nodeDefine, exists, reqMain, loadedOptimizedLib, existsForNode, Cc, Ci, @@ -25,10 +25,6 @@ var requirejs, require, define, xpcUtil; jsSuffixRegExp = /\.js$/, commandOption = '', useLibLoaded = {}, - //Used by jslib/rhino/args.js - rhinoArgs = args, - //Used by jslib/xpconnect/args.js - xpconnectArgs = args, readFile = typeof readFileFunc !== 'undefined' ? readFileFunc : null; function showHelp() { @@ -136,7 +132,7 @@ var requirejs, require, define, xpcUtil; fileName = args[1]; } - xpcUtil = { + requirejsEnvUtil = { cwd: function () { return FileUtils.getFile("CurWorkD", []).path; }, @@ -151,7 +147,7 @@ var requirejs, require, define, xpcUtil; firstChar !== '\\' && path.indexOf(':') === -1) { //A relative path. Use the current working directory. - path = xpcUtil.cwd() + '/' + path; + path = requirejsEnvUtil.cwd() + '/' + path; } ary = path.replace(/\\/g, '/').split('/'); @@ -171,7 +167,7 @@ var requirejs, require, define, xpcUtil; xpfile: function (path) { try { - return new FileUtils.File(xpcUtil.normalize(path)); + return new FileUtils.File(requirejsEnvUtil.normalize(path)); } catch (e) { throw new Error(path + ' failed: ' + e); } @@ -183,7 +179,7 @@ var requirejs, require, define, xpcUtil; var inStream, convertStream, readData = {}, - fileObj = xpcUtil.xpfile(path); + fileObj = requirejsEnvUtil.xpfile(path); //XPCOM, you so crazy try { @@ -211,14 +207,14 @@ var requirejs, require, define, xpcUtil; } }; - readFile = xpcUtil.readFile; + readFile = requirejsEnvUtil.readFile; exec = function (string) { return eval(string); }; exists = function (fileName) { - return xpcUtil.xpfile(fileName).exists(); + return requirejsEnvUtil.xpfile(fileName).exists(); }; //Define a console.log for easier logging. Don't @@ -232,29 +228,35 @@ var requirejs, require, define, xpcUtil; } } else if (typeof WScript !== 'undefined' && typeof ActiveXObject !== 'undefined') { + //WScript ref: + //http://msdn.microsoft.com/en-us/library/98591fh7%28v=vs.85%29.aspx env = 'wsh'; - var fso = new ActiveXObject("Scripting.FileSystemObject"); + requirejsEnvUtil = { + fso: new ActiveXObject("Scripting.FileSystemObject"), - readFile = function (path) { - var contents, - stream = new ActiveXObject("ADODB.Stream"); - - stream.Open(); - stream.Type = 2; - stream.Charset = 'utf-8'; - stream.LoadFromFile(path); - contents = stream.ReadText(-1); - stream.Close(); - return contents; + readFile: function (path) { + var contents, + stream = new ActiveXObject("ADODB.Stream"); + + stream.Open(); + stream.Type = 2; + stream.Charset = 'utf-8'; + stream.LoadFromFile(path); + contents = stream.ReadText(-1); + stream.Close(); + return contents; + } }; - exec = function (string, name) { + readFile = readFile; + + exec = function (string) { return eval(string); }; exists = function (fileName) { - return fso.FileExists(fileName); + return requirejsEnvUtil.fso.FileExists(fileName); }; //Define a console.log for easier logging. Don't diff --git a/build/jslib/xpconnect/args.js b/build/jslib/xpconnect/args.js index d6cfd10d..7fd597b2 100644 --- a/build/jslib/xpconnect/args.js +++ b/build/jslib/xpconnect/args.js @@ -5,9 +5,9 @@ */ /*jslint strict: false */ -/*global define, xpconnectArgs */ +/*global define */ -var jsLibXpConnectArgs = (typeof xpconnectArgs !== 'undefined' && xpconnectArgs) || [].concat(Array.prototype.slice.call(arguments, 0)); +var jsLibXpConnectArgs = [].concat(Array.prototype.slice.call(arguments, 0)); define(function () { var args = jsLibXpConnectArgs; diff --git a/build/jslib/xpconnect/file.js b/build/jslib/xpconnect/file.js index 57cfda22..bf4d4c22 100644 --- a/build/jslib/xpconnect/file.js +++ b/build/jslib/xpconnect/file.js @@ -6,14 +6,14 @@ //Helper functions to deal with file I/O. /*jslint plusplus: false */ -/*global define, Components, xpcUtil */ +/*global define, Components, requirejsEnvUtil */ define(['prim'], function (prim) { var file, Cc = Components.classes, Ci = Components.interfaces, - //Depends on xpcUtil which is set up in x.js - xpfile = xpcUtil.xpfile; + //Depends on requirejsEnvUtil which is set up in x.js + xpfile = requirejsEnvUtil.xpfile; function mkFullDir(dirObj) { //1 is DIRECTORY_TYPE, 511 is 0777 permissions @@ -168,7 +168,7 @@ define(['prim'], function (prim) { return xpfile(from).moveTo(toFile.parent, toFile.leafName); }, - readFile: xpcUtil.readFile, + readFile: requirejsEnvUtil.readFile, readFileAsync: function (path, encoding) { var d = prim(); From f490281b0cb6437135be55223071e4296a443b6d Mon Sep 17 00:00:00 2001 From: jrburke Date: Sun, 17 Feb 2013 21:53:30 -0800 Subject: [PATCH 03/20] fleshing out file module --- build/jslib/wsh/file.js | 237 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 237 insertions(+) create mode 100644 build/jslib/wsh/file.js diff --git a/build/jslib/wsh/file.js b/build/jslib/wsh/file.js new file mode 100644 index 00000000..6f996f0c --- /dev/null +++ b/build/jslib/wsh/file.js @@ -0,0 +1,237 @@ +/** + * @license RequireJS Copyright (c) 2013, The Dojo Foundation All Rights Reserved. + * Available via the MIT or new BSD license. + * see: http://github.com/jrburke/requirejs for details + */ +//Helper functions to deal with file I/O. + +/*global define, requirejsEnvUtil, Enumerator */ + +define(['prim'], function (prim) { + var file, + //Depends on requirejsEnvUtil which is set up in x.js + //Defined via new ActiveXObject("Scripting.FileSystemObject") + //http://msdn.microsoft.com/en-us/library/hww8txat%28v=vs.84%29.aspx + fso = requirejsEnvUtil.fso; + + file = { + backSlashRegExp: /\\/g, + + exclusionRegExp: /^\./, + + getLineSeparator: function () { + return file.lineSeparator; + }, + + lineSeparator: '\r\n', + + exists: function (fileName) { + return fso.FileExists(fileName) || fso.FolderExists(fileName); + }, + + parent: function (fileName) { + return fso.GetParentFolderName(fileName); + }, + + normalize: function (fileName) { + return file.absPath(fileName); + }, + + isFile: function (path) { + return fso.FileExists(path); + }, + + isDirectory: function (path) { + return fso.FolderExists(path); + }, + + /** + * Gets the absolute file path as a string, normalized + * to using front slashes for path separators. + * @param {java.io.File||String} file + */ + absPath: function (path) { + return fso.GetAbsolutePathName(path).replace(/\\/g, '/'); + }, + + getFilteredFileList: function (/*String*/startDir, /*RegExp*/regExpFilters, /*boolean?*/makeUnixPaths, /*boolean?*/startDirIsObject) { + //summary: Recurses startDir and finds matches to the files that match regExpFilters.include + //and do not match regExpFilters.exclude. Or just one regexp can be passed in for regExpFilters, + //and it will be treated as the "include" case. + //Ignores files/directories that start with a period (.) unless exclusionRegExp + //is set to another value. + var files = [], topDir, regExpInclude, regExpExclude, + fileObj, filePath, ok, dirFiles, ne; + + topDir = startDir; + if (!startDirIsObject) { + topDir = fso.GetFolder(startDir); + } + + regExpInclude = regExpFilters.include || regExpFilters; + regExpExclude = regExpFilters.exclude || null; + + if (topDir.FolderExists()) { + ne = new Enumerator(topDir.Files); + + //Files in the directory + while (!ne.atEnd()) { + fileObj = ne.item(); + + filePath = fileObj.GetAbsolutePathName(); + if (makeUnixPaths) { + if (filePath.indexOf("/") === -1) { + filePath = filePath.replace(/\\/g, "/"); + } + } + + ok = true; + if (regExpInclude) { + ok = filePath.match(regExpInclude); + } + if (ok && regExpExclude) { + ok = !filePath.match(regExpExclude); + } + + if (ok && (!file.exclusionRegExp || + !file.exclusionRegExp.test(fileObj.GetFileName()))) { + files.push(filePath); + } + + ne.moveNext(); + } + + //Folders in the directory + ne = new Enumerator(topDir.SubFolders); + while (!ne.atEnd()) { + fileObj = ne.item(); + + if (!file.exclusionRegExp || + !file.exclusionRegExp.test(fileObj.GetFileName())) { + dirFiles = this.getFilteredFileList(fileObj, regExpFilters, makeUnixPaths, true); + files.push.apply(files, dirFiles); + } + + ne.moveNext(); + } + } + + return files; //Array + }, + + copyDir: function (/*String*/srcDir, /*String*/destDir, /*RegExp?*/regExpFilter, /*boolean?*/onlyCopyNew) { + //summary: copies files from srcDir to destDir using the regExpFilter to determine if the + //file should be copied. Returns a list file name strings of the destinations that were copied. + regExpFilter = regExpFilter || /\w/; + + var fileNames = file.getFilteredFileList(srcDir, regExpFilter, true), + copiedFiles = [], i, srcFileName, destFileName; + + for (i = 0; i < fileNames.length; i += 1) { + srcFileName = fileNames[i]; + destFileName = srcFileName.replace(srcDir, destDir); + + if (file.copyFile(srcFileName, destFileName, onlyCopyNew)) { + copiedFiles.push(destFileName); + } + } + + return copiedFiles.length ? copiedFiles : null; //Array or null + }, + + copyFile: function (/*String*/srcFileName, /*String*/destFileName, /*boolean?*/onlyCopyNew) { + //summary: copies srcFileName to destFileName. If onlyCopyNew is set, it only copies the file if + //srcFileName is newer than destFileName. Returns a boolean indicating if the copy occurred. + var destFile = fso.GetFile(destFileName), + srcFile = fso.GetFile(srcFileName); + + //If onlyCopyNew is true, then compare dates and only copy if the src is newer + //than dest. + if (onlyCopyNew) { + if (destFile.FileExists() && destFile.DateLastModified >= srcFile.DateLastModified) { + return false; //Boolean + } + } + + srcFile.Copy(destFileName); + + return true; //Boolean + }, + + /** + * Renames a file. May fail if "to" already exists or is on another drive. + */ + renameFile: function (from, to) { + fso.MoveFile(from, to); + }, + + readFile: requirejsEnvUtil.readFile, + + readFileAsync: function (path, encoding) { + var d = prim(); + try { + d.resolve(file.readFile(path, encoding)); + } catch (e) { + d.reject(e); + } + return d.promise; + }, + + saveUtf8File: function (/*String*/fileName, /*String*/fileContents) { + //summary: saves a file using UTF-8 encoding. + file.saveFile(fileName, fileContents, "utf-8"); + }, + + saveFile: function (/*String*/fileName, /*String*/fileContents, /*String?*/encoding) { + //tristate values explained here: + //http://msdn.microsoft.com/en-us/library/314cz14s%28v=vs.84%29.aspx + //-1 for unicode, -2 system default + var tristate = !encoding || encoding === "utf-8" ? -1 : -2, + handle; + + //2 is write permission + handle = fso.OpenTextFile(fileName, 2, true, tristate); + handle.Write(fileContents); + handle.Close(); + }, + + deleteFile: function (/*String*/fileName) { + //summary: deletes a file or directory if it exists. + if (fso.FileExists(fileName)) { + fso.DeleteFile(fileName); + } else if (fso.FolderExists(fileName)) { + fso.DeleteFolder(fileName); + } + }, + + /** + * Deletes any empty directories under the given directory. + * The startDirIsJavaObject is private to this implementation's + * recursion needs. + */ + deleteEmptyDirs: function (startDir, startDirIsObject) { + var topDir = startDir, + ne; + + if (!startDirIsObject) { + topDir = fso.GetFolder(startDir); + } + + if (topDir.FolderExists()) { + //Folders in the directory + ne = new Enumerator(topDir.SubFolders); + while (!ne.atEnd()) { + file.deleteEmptyDirs(ne.item(), true); + ne.moveNext(); + } + + //If the directory is empty now, delete it. + if (topDir.Files.Count === 0 && topDir.Folders.Count === 0) { + file.deleteFile(topDir.path); + } + } + } + }; + + return file; +}); From 6528369d2ac6c934520c806f1948fa2b22e209ad Mon Sep 17 00:00:00 2001 From: jrburke Date: Sun, 17 Feb 2013 22:10:42 -0800 Subject: [PATCH 04/20] wiring up test adapter for wsh --- build/jslib/env.js | 7 +++++-- build/jslib/x.js | 2 +- tests/doh/_wshRunner.js | 23 +++++++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 tests/doh/_wshRunner.js diff --git a/build/jslib/env.js b/build/jslib/env.js index d15d8dc4..a7e2f702 100644 --- a/build/jslib/env.js +++ b/build/jslib/env.js @@ -5,8 +5,9 @@ */ /*jslint strict: false */ -/*global Packages: false, process: false, window: false, navigator: false, - document: false, define: false */ +/*global Packages: false, process: false, navigator: false, + document: false, define: false, WScript, ActiveXObject, Components, + self, importScripts */ /** * A plugin that modifies any /env/ path to be the right path based on @@ -25,6 +26,8 @@ env = 'browser'; } else if (typeof Components !== 'undefined' && Components.classes && Components.interfaces) { env = 'xpconnect'; + } else if (typeof WScript !== 'undefined' && typeof ActiveXObject !== 'undefined') { + env = 'wsh'; } define({ diff --git a/build/jslib/x.js b/build/jslib/x.js index 423fd77f..388790b4 100644 --- a/build/jslib/x.js +++ b/build/jslib/x.js @@ -249,7 +249,7 @@ var requirejs, require, define, requirejsEnvUtil; } }; - readFile = readFile; + readFile = requirejsEnvUtil.readFile; exec = function (string) { return eval(string); diff --git a/tests/doh/_wshRunner.js b/tests/doh/_wshRunner.js new file mode 100644 index 00000000..ec1d89a0 --- /dev/null +++ b/tests/doh/_wshRunner.js @@ -0,0 +1,23 @@ +doh.debug = print; + +//Define a console.log for easier logging. Don't +//get fancy though. +if (typeof console === 'undefined') { + console = { + log: function () { + WScript.Echo.apply(undefined, arguments); + } + }; +} + +// Override the doh._report method to make it quit with an +// appropriate exit code in case of test failures. +(function(){ + var oldReport = doh._report; + doh._report = function(){ + oldReport.apply(doh, arguments); + if(this._failureCount > 0 || this._errorCount > 0){ + WScript.Quit(1); + } + }; +})(); From 145ed005cedcd4ca1eb533edf55732a85718be64 Mon Sep 17 00:00:00 2001 From: jrburke Date: Sun, 17 Feb 2013 22:14:13 -0800 Subject: [PATCH 05/20] fix doh.debug for wsh --- tests/doh/_wshRunner.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/doh/_wshRunner.js b/tests/doh/_wshRunner.js index ec1d89a0..79000544 100644 --- a/tests/doh/_wshRunner.js +++ b/tests/doh/_wshRunner.js @@ -1,4 +1,6 @@ -doh.debug = print; +doh.debug = function () { + WScript.Echo.apply(undefined, arguments); +}; //Define a console.log for easier logging. Don't //get fancy though. From 3ad8b956325cb1d64aa18d82f988c19b7a62f16e Mon Sep 17 00:00:00 2001 From: jrburke Date: Sun, 17 Feb 2013 23:20:34 -0800 Subject: [PATCH 06/20] Echo no like apply --- build/jslib/wsh/print.js | 4 ++-- build/jslib/x.js | 4 ++-- tests/doh/_wshRunner.js | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/build/jslib/wsh/print.js b/build/jslib/wsh/print.js index b3b51173..56d536b5 100644 --- a/build/jslib/wsh/print.js +++ b/build/jslib/wsh/print.js @@ -7,8 +7,8 @@ /*global define, WScript */ define(function () { - function print() { - WScript.Echo.apply(undefined, arguments); + function print(msg) { + WScript.Echo(msg); } return print; }); diff --git a/build/jslib/x.js b/build/jslib/x.js index 388790b4..cde86e7e 100644 --- a/build/jslib/x.js +++ b/build/jslib/x.js @@ -263,8 +263,8 @@ var requirejs, require, define, requirejsEnvUtil; //get fancy though. if (typeof console === 'undefined') { console = { - log: function () { - WScript.Echo.apply(undefined, arguments); + log: function (msg) { + WScript.Echo(msg); } }; } diff --git a/tests/doh/_wshRunner.js b/tests/doh/_wshRunner.js index 79000544..cbc32d96 100644 --- a/tests/doh/_wshRunner.js +++ b/tests/doh/_wshRunner.js @@ -1,13 +1,13 @@ -doh.debug = function () { - WScript.Echo.apply(undefined, arguments); +doh.debug = function (msg) { + WScript.Echo(msg); }; //Define a console.log for easier logging. Don't //get fancy though. if (typeof console === 'undefined') { console = { - log: function () { - WScript.Echo.apply(undefined, arguments); + log: function (msg) { + WScript.Echo(msg); } }; } From 7c9c1f96521b071b192b6c48aa2b6e32ac195d4d Mon Sep 17 00:00:00 2001 From: jrburke Date: Mon, 18 Feb 2013 10:08:13 -0800 Subject: [PATCH 07/20] fix multiple arg passing to console.log for wsh, and showing better errors --- build/jslib/wsh/print.js | 6 +++--- build/jslib/x.js | 19 ++++++++++++++++-- tests/doh/_wshRunner.js | 43 ++++++++++++++++++++++++++++------------ 3 files changed, 50 insertions(+), 18 deletions(-) diff --git a/build/jslib/wsh/print.js b/build/jslib/wsh/print.js index 56d536b5..10bf8807 100644 --- a/build/jslib/wsh/print.js +++ b/build/jslib/wsh/print.js @@ -4,11 +4,11 @@ * see: http://github.com/jrburke/requirejs for details */ -/*global define, WScript */ +/*global define, WScript, requirejsEnvUtil */ define(function () { - function print(msg) { - WScript.Echo(msg); + function print() { + WScript.Echo(requirejsEnvUtil.wshFormatArgs(arguments)); } return print; }); diff --git a/build/jslib/x.js b/build/jslib/x.js index cde86e7e..f22b6937 100644 --- a/build/jslib/x.js +++ b/build/jslib/x.js @@ -246,6 +246,21 @@ var requirejs, require, define, requirejsEnvUtil; contents = stream.ReadText(-1); stream.Close(); return contents; + }, + + //wsh cannot seem to handle passing Echo multiple args, + //at via apply, and when printing errors, will just do + //the dumb [Object Error] instead of the more helpful + //message + wshFormatArgs: function (args) { + var i, item, + ary = []; + for (i = 0; i < args.length; i += 1) { + item = args[i]; + ary.push(item.message || item); + } + + return ary.join(' '); } }; @@ -263,8 +278,8 @@ var requirejs, require, define, requirejsEnvUtil; //get fancy though. if (typeof console === 'undefined') { console = { - log: function (msg) { - WScript.Echo(msg); + log: function () { + WScript.Echo(requirejsEnvUtil.wshFormatArgs(arguments)); } }; } diff --git a/tests/doh/_wshRunner.js b/tests/doh/_wshRunner.js index cbc32d96..4147a031 100644 --- a/tests/doh/_wshRunner.js +++ b/tests/doh/_wshRunner.js @@ -1,20 +1,37 @@ -doh.debug = function (msg) { - WScript.Echo(msg); -}; +/*global doh, WScript, console: true */ +(function(){ + //wsh cannot seem to handle passing Echo multiple args, + //at via apply, and when printing errors, will just do + //the dumb [Object Error] instead of the more helpful + //message + function wshFormatArgs(args) { + var i, item, + ary = []; -//Define a console.log for easier logging. Don't -//get fancy though. -if (typeof console === 'undefined') { - console = { - log: function (msg) { - WScript.Echo(msg); + for (i = 0; i < args.length; i += 1) { + item = args[i]; + ary.push(item.message || item); } + + return ary.join(' '); + } + + doh.debug = function () { + WScript.Echo(wshFormatArgs(arguments)); }; -} -// Override the doh._report method to make it quit with an -// appropriate exit code in case of test failures. -(function(){ + //Define a console.log for easier logging. Don't + //get fancy though. + if (typeof console === 'undefined') { + console = { + log: function () { + WScript.Echo(wshFormatArgs(arguments)); + } + }; + } + + // Override the doh._report method to make it quit with an + // appropriate exit code in case of test failures. var oldReport = doh._report; doh._report = function(){ oldReport.apply(doh, arguments); From 8416aac7624e312c5fd00661f3e867bd32f178e9 Mon Sep 17 00:00:00 2001 From: jrburke Date: Tue, 19 Feb 2013 23:09:37 -0800 Subject: [PATCH 08/20] Make sure array extras are there, enough to be usable. May help wsh which may not have them. --- build/jslib/build.js | 16 ++++++++---- build/jslib/lang.js | 48 +++++++++++++++++++++++++++++++++-- build/jslib/parse.js | 4 +-- build/jslib/rhino/optimize.js | 31 ---------------------- build/jslib/transform.js | 2 +- 5 files changed, 60 insertions(+), 41 deletions(-) diff --git a/build/jslib/build.js b/build/jslib/build.js index c0014ace..c512e16d 100644 --- a/build/jslib/build.js +++ b/build/jslib/build.js @@ -473,10 +473,15 @@ define(function (require) { //Be sure not to remove other build layers. if (config.removeCombined) { module.layer.buildFilePaths.forEach(function (path) { - if (file.exists(path) && !modules.some(function (mod) { - return mod._buildPath === path; - })) { - file.deleteFile(path); + if (file.exists(path)) { + var pathMatched; + lang.each(modules, function (mod) { + pathMatched = mod._buildPath === path; + return pathMatched; + }); + if (!pathMatched) { + file.deleteFile(path); + } } }); } @@ -1220,7 +1225,8 @@ define(function (require) { syncChecks = { rhino: true, node: true, - xpconnect: true + xpconnect: true, + wsh: true }, deferred = prim(); diff --git a/build/jslib/lang.js b/build/jslib/lang.js index 7a5bdac5..2bda3b3b 100644 --- a/build/jslib/lang.js +++ b/build/jslib/lang.js @@ -7,6 +7,50 @@ /*jslint plusplus: true */ /*global define */ +(function () { + //Add some array extras for tools like esprima and uglify to work. + //Inspired by https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/reduce + //but rewritten for brevity, and to be good enough for use by UglifyJS. + if (!Array.prototype.reduce) { + Array.prototype.reduce = function (fn /*, initialValue */) { + var i = 0, + length = this.length, + accumulator; + + if (arguments.length >= 2) { + accumulator = arguments[1]; + } else { + if (length) { + while (!(i in this)) { + i++; + } + accumulator = this[i++]; + } + } + + for (; i < length; i++) { + if (i in this) { + accumulator = fn.call(undefined, accumulator, this[i], i, this); + } + } + + return accumulator; + }; + } + + //From + //https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach + if (!Array.prototype.forEach) { + Array.prototype.forEach = function(fn, scope) { + var i, + len = this.length; + for (i = 0; i < len; i += 1) { + fn.call(scope, this[i], i, this); + } + }; + } +}()); + define(function () { 'use strict'; @@ -49,8 +93,8 @@ define(function () { _mixin: function(dest, source, override){ var name; for (name in source) { - if(source.hasOwnProperty(name) - && (override || !dest.hasOwnProperty(name))) { + if(source.hasOwnProperty(name) && + (override || !dest.hasOwnProperty(name))) { dest[name] = source[name]; } } diff --git a/build/jslib/parse.js b/build/jslib/parse.js index e1a9cf3c..495fcf32 100644 --- a/build/jslib/parse.js +++ b/build/jslib/parse.js @@ -7,7 +7,7 @@ /*jslint plusplus: true */ /*global define: false */ -define(['./esprima'], function (esprima) { +define(['./esprima', 'lang'], function (esprima, lang) { 'use strict'; //This string is saved off because JSLint complains @@ -56,7 +56,7 @@ define(['./esprima'], function (esprima) { var deps = []; - node.elements.some(function (elem) { + lang.each(node.elements, function (elem) { if (elem.type === 'Literal') { deps.push(elem.value); } diff --git a/build/jslib/rhino/optimize.js b/build/jslib/rhino/optimize.js index a326a528..72e8555e 100644 --- a/build/jslib/rhino/optimize.js +++ b/build/jslib/rhino/optimize.js @@ -8,37 +8,6 @@ /*global define, java, Packages, com */ define(['logger', 'env!env/file'], function (logger, file) { - - //Add .reduce to Rhino so UglifyJS can run in Rhino, - //inspired by https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/reduce - //but rewritten for brevity, and to be good enough for use by UglifyJS. - if (!Array.prototype.reduce) { - Array.prototype.reduce = function (fn /*, initialValue */) { - var i = 0, - length = this.length, - accumulator; - - if (arguments.length >= 2) { - accumulator = arguments[1]; - } else { - if (length) { - while (!(i in this)) { - i++; - } - accumulator = this[i++]; - } - } - - for (; i < length; i++) { - if (i in this) { - accumulator = fn.call(undefined, accumulator, this[i], i, this); - } - } - - return accumulator; - }; - } - var JSSourceFilefromCode, optimize, mapRegExp = /"file":"[^"]+"/; diff --git a/build/jslib/transform.js b/build/jslib/transform.js index 3ce94fb6..3afaef0d 100644 --- a/build/jslib/transform.js +++ b/build/jslib/transform.js @@ -45,7 +45,7 @@ define([ './esprima', './parse', 'logger', 'lang'], function (esprima, parse, lo } //Find the define calls and their position in the files. - tokens.some(function (token, i) { + lang.each(tokens, function (token, i) { var prev, prev2, next, next2, next3, next4, next5, needsId, depAction, nameCommaRange, foundId, sourceUrlData, range, From 937e5bfcf5099b714895139c2ea30cf9067d7c13 Mon Sep 17 00:00:00 2001 From: jrburke Date: Fri, 1 Mar 2013 17:54:43 -0800 Subject: [PATCH 09/20] Add es5-shim 2.0.5, to help out cscript --- build/jslib/es5-shim.js | 981 ++++++++++++++++++++++++++++++++++++++++ build/jslib/lang.js | 2 +- dist.js | 1 + 3 files changed, 983 insertions(+), 1 deletion(-) create mode 100644 build/jslib/es5-shim.js diff --git a/build/jslib/es5-shim.js b/build/jslib/es5-shim.js new file mode 100644 index 00000000..00a11bff --- /dev/null +++ b/build/jslib/es5-shim.js @@ -0,0 +1,981 @@ +// Copyright 2009-2012 by contributors, MIT License +// vim: ts=4 sts=4 sw=4 expandtab + +// Module systems magic dance +(function (definition) { + // RequireJS + if (typeof define == "function") { + define(definition); + // YUI3 + } else if (typeof YUI == "function") { + YUI.add("es5", definition); + // CommonJS and