Permalink
Browse files

program.json URL based booting

  • Loading branch information...
1 parent 0a595e9 commit 5f78a22d3b0d749daa4f6eb8720d8a190a375347 @cadorn cadorn committed Aug 10, 2011
Showing with 176 additions and 148 deletions.
  1. +43 −27 lib/pinf-loader-js/assembler.js
  2. +123 −120 lib/pinf-loader-js/loader.js
  3. +10 −1 lib/pinf-loader-js/program.js
@@ -246,40 +246,56 @@ Assembler.prototype.provisonProgramForURL = function(url, callback, options)
DEBUG.print("URL: " + url);
- self.downloader.getForArchive(url, function(path)
+ if (/\.json/.test(url))
{
- DEBUG.print("Path: " + path);
-
- // Look for program.json file. If it does not exist create a default one
-
- var descriptorPath = path + "/program.json";
-
- if (!API.FILE.exists(descriptorPath))
+ self.downloader.getFileForURL(url, function(path)
+ {
+ DEBUG.print("Path: " + path);
+
+ DEBUG.indent(di);
+
+ callback(path);
+ }, {
+ verifyPackageDescriptor: false
+ });
+ }
+ else
+ {
+ self.downloader.getForArchive(url, function(path)
{
- DEBUG.print("Creating program descriptor at: " + descriptorPath);
+ DEBUG.print("Path: " + path);
- var id = API.FILE.dirname(descriptorPath.substring(self.downloader.basePath.length+1));
- id = id.substring(0, id.length-8) + "/";
+ // Look for program.json file. If it does not exist create a default one
- var descriptor = {
- "boot": id,
- "packages": {}
- };
- descriptor.packages[id] = {
- "locator": {
- "location": "./"
- }
- };
+ var descriptorPath = path + "/program.json";
+
+ if (!API.FILE.exists(descriptorPath))
+ {
+ DEBUG.print("Creating program descriptor at: " + descriptorPath);
+
+ var id = API.FILE.dirname(descriptorPath.substring(self.downloader.basePath.length+1));
+ id = id.substring(0, id.length-8) + "/";
+
+ var descriptor = {
+ "boot": id,
+ "packages": {}
+ };
+ descriptor.packages[id] = {
+ "locator": {
+ "location": "./"
+ }
+ };
- API.FILE.write(descriptorPath, API.JSON.stringify(descriptor));
- }
+ API.FILE.write(descriptorPath, API.JSON.stringify(descriptor));
+ }
- DEBUG.indent(di);
+ DEBUG.indent(di);
- callback(descriptorPath);
- }, {
- verifyPackageDescriptor: false
- });
+ callback(descriptorPath);
+ }, {
+ verifyPackageDescriptor: false
+ });
+ }
}
if (ENV.mustClean && !this.cleaned[this.downloader.basePath + "/downloads"])
@@ -369,127 +369,130 @@ var boot = exports.boot = function(options)
API.ENV.program = program;
API.ENV.sandbox = sandbox;
- var dependencies = program.getBootPackages();
- if (dependencies.length > 1)
+ program.getBootPackages(assembler, function(dependencies)
{
- throw new Error("Only one program boot package allowed in: " + path);
- }
-
- // ######################################################################
- // # Program Booting
- // ######################################################################
-
- API.ENV.booting = true;
-
- if (API.DEBUG.enabled) {
- API.DEBUG.print("Loading program's main packages:");
- for (var i=0, ic=dependencies.length ; i<ic ; i++ )
- {
- if (typeof dependencies[i]["_package-" + i] != "undefined")
- API.DEBUG.print(" " + dependencies[i]["_package-" + i].location);
- }
- }
-
- timers.load = new Date().getTime()
-
- var env = options.env || {};
- env.args = env.args || options.args || cliOptions.args.slice(1, cliOptions.args.length) || [];
- env.programDescriptorPath = path;
- env.bootPackagePath = dependencies[0]["_package-0"].location;
-
- sandbox.declare(dependencies, function(require, exports, module)
- {
- timers.run = new Date().getTime()
-
- // ######################################################################
- // # Program Script
- // ######################################################################
-
- if (cliOptions.script)
- {
- var pkg = sandbox.packageForId(dependencies[0]['_package-0'].location);
- if (typeof pkg.normalizedDescriptor.json.scripts == "undefined")
- throw new Error("No 'scripts' defined in package descriptor: " + pkg.normalizedDescriptor.path);
- if (typeof pkg.normalizedDescriptor.json.scripts[cliOptions.script] == "undefined")
- throw new Error("Script '" + cliOptions.script + "' not found in 'scripts' defined in package descriptor: " + pkg.normalizedDescriptor.path);
- if (typeof pkg.normalizedDescriptor.json.scripts[cliOptions.script] == "object")
- {
- assembler.addPackageToProgram(sandbox, sandbox.program, pkg.normalizedDescriptor.json.scripts[cliOptions.script], function(pkg)
- {
- module.load(pkg.getMainId(pkg.normalizedDescriptor.json.scripts[cliOptions.script]), function(id)
- {
- var script = require(id);
-
- if (typeof script.main == "undefined")
- throw new Error("Script does not export main() in " + id);
-
- API.ENV.booting = false;
-
- API.DEBUG.print("\0magenta(\0:blue(----- | Program script stdout & stderr follows ====>\0:)\0)");
-
- script.main(env);
- });
- }, {
- "discover-packages": cliOptions["discover-packages"] || false,
- sourceDescriptors: API.ENV.loadSourceDescriptorsForProgram(path)
- });
- }
- else
- throw new Error("NYI - Non-locator based script pointers");
- }
- else
-
- // ######################################################################
- // # Program Boot Packages
- // ######################################################################
-
- {
- API.DEBUG.print("Booting program. Output for boot package follows in green between ==> ... <==");
-
- // Run the program by calling main() on each packages' main module
- var pkg,
- hl;
- // TODO: Refactor as there is only ever one boot package
- for (var i=0, ic=dependencies.length ; i<ic ; i++ )
- {
- var pkg = require("_package-" + i);
-
- if (typeof pkg.main === "undefined")
- throw new Error("Package's main module does not export main() in package: " + dependencies[i]["_package-" + i].location);
-
- if (API.DEBUG.enabled)
- {
- var h = "----- " + dependencies[i]["_package-" + i].location + " -> [package.json].main -> main() -----";
- hl = h.length;
- API.DEBUG.print("\0magenta(\0:blue(" + h + "\0:)");
- API.SYSTEM.print("\0:blue(=====>\0:)\0)\0green(\0bold(", false, true);
- }
-
- pkg.main(env);
-
- if (API.DEBUG.enabled)
- {
- API.SYSTEM.print("\0)\0)\0magenta(\0:blue(<=====\0:)\0)\n");
- var f = "";
- for (var i=0 ; i<hl-8 ; i++) f += "-";
- API.DEBUG.print("\0magenta(\0:blue(----- ^ " + f + "\0:)\0)");
- }
- }
-
- API.ENV.booting = false;
- }
-
- timers.end = new Date().getTime()
-
- API.DEBUG.print("Program Booted ~ Timing (Assembly: "+(timers.load-timers.start)/1000+", Load: "+(timers.run-timers.load)/1000+", Boot: "+(timers.end-timers.run-timers.loadAdditional)/1000+", Additional Load: "+(timers.loadAdditional)/1000+")");
- var f = "";
- for (var i=0 ; i<hl ; i++) f += "|";
- API.DEBUG.print("\0magenta(\0:blue(----- | Program stdout & stderr follows (if not already terminated) ====>\0:)\0)");
-
- if (typeof options.callback != "undefined")
- {
- options.callback(sandbox, require);
- }
+ if (dependencies.length > 1)
+ {
+ throw new Error("Only one program boot package allowed in: " + path);
+ }
+
+ // ######################################################################
+ // # Program Booting
+ // ######################################################################
+
+ API.ENV.booting = true;
+
+ if (API.DEBUG.enabled) {
+ API.DEBUG.print("Loading program's main packages:");
+
+ for (var i=0, ic=dependencies.length ; i<ic ; i++ )
+ {
+ if (typeof dependencies[i]["_package-" + i] != "undefined")
+ API.DEBUG.print(" " + dependencies[i]["_package-" + i].location);
+ }
+ }
+
+ timers.load = new Date().getTime()
+
+ var env = options.env || {};
+ env.args = env.args || options.args || cliOptions.args.slice(1, cliOptions.args.length) || [];
+ env.programDescriptorPath = path;
+ env.bootPackagePath = dependencies[0]["_package-0"].location;
+
+ sandbox.declare(dependencies, function(require, exports, module)
+ {
+ timers.run = new Date().getTime()
+
+ // ######################################################################
+ // # Program Script
+ // ######################################################################
+
+ if (cliOptions.script)
+ {
+ var pkg = sandbox.packageForId(dependencies[0]['_package-0'].location);
+ if (typeof pkg.normalizedDescriptor.json.scripts == "undefined")
+ throw new Error("No 'scripts' defined in package descriptor: " + pkg.normalizedDescriptor.path);
+ if (typeof pkg.normalizedDescriptor.json.scripts[cliOptions.script] == "undefined")
+ throw new Error("Script '" + cliOptions.script + "' not found in 'scripts' defined in package descriptor: " + pkg.normalizedDescriptor.path);
+ if (typeof pkg.normalizedDescriptor.json.scripts[cliOptions.script] == "object")
+ {
+ assembler.addPackageToProgram(sandbox, sandbox.program, pkg.normalizedDescriptor.json.scripts[cliOptions.script], function(pkg)
+ {
+ module.load(pkg.getMainId(pkg.normalizedDescriptor.json.scripts[cliOptions.script]), function(id)
+ {
+ var script = require(id);
+
+ if (typeof script.main == "undefined")
+ throw new Error("Script does not export main() in " + id);
+
+ API.ENV.booting = false;
+
+ API.DEBUG.print("\0magenta(\0:blue(----- | Program script stdout & stderr follows ====>\0:)\0)");
+
+ script.main(env);
+ });
+ }, {
+ "discover-packages": cliOptions["discover-packages"] || false,
+ sourceDescriptors: API.ENV.loadSourceDescriptorsForProgram(path)
+ });
+ }
+ else
+ throw new Error("NYI - Non-locator based script pointers");
+ }
+ else
+
+ // ######################################################################
+ // # Program Boot Packages
+ // ######################################################################
+
+ {
+ API.DEBUG.print("Booting program. Output for boot package follows in green between ==> ... <==");
+
+ // Run the program by calling main() on each packages' main module
+ var pkg,
+ hl;
+ // TODO: Refactor as there is only ever one boot package
+ for (var i=0, ic=dependencies.length ; i<ic ; i++ )
+ {
+ var pkg = require("_package-" + i);
+
+ if (typeof pkg.main === "undefined")
+ throw new Error("Package's main module does not export main() in package: " + dependencies[i]["_package-" + i].location);
+
+ if (API.DEBUG.enabled)
+ {
+ var h = "----- " + dependencies[i]["_package-" + i].location + " -> [package.json].main -> main() -----";
+ hl = h.length;
+ API.DEBUG.print("\0magenta(\0:blue(" + h + "\0:)");
+ API.SYSTEM.print("\0:blue(=====>\0:)\0)\0green(\0bold(", false, true);
+ }
+
+ pkg.main(env);
+
+ if (API.DEBUG.enabled)
+ {
+ API.SYSTEM.print("\0)\0)\0magenta(\0:blue(<=====\0:)\0)\n");
+ var f = "";
+ for (var i=0 ; i<hl-8 ; i++) f += "-";
+ API.DEBUG.print("\0magenta(\0:blue(----- ^ " + f + "\0:)\0)");
+ }
+ }
+
+ API.ENV.booting = false;
+ }
+
+ timers.end = new Date().getTime()
+
+ API.DEBUG.print("Program Booted ~ Timing (Assembly: "+(timers.load-timers.start)/1000+", Load: "+(timers.run-timers.load)/1000+", Boot: "+(timers.end-timers.run-timers.loadAdditional)/1000+", Additional Load: "+(timers.loadAdditional)/1000+")");
+ var f = "";
+ for (var i=0 ; i<hl ; i++) f += "|";
+ API.DEBUG.print("\0magenta(\0:blue(----- | Program stdout & stderr follows (if not already terminated) ====>\0:)\0)");
+
+ if (typeof options.callback != "undefined")
+ {
+ options.callback(sandbox, require);
+ }
+ });
});
}, {
"discover-packages": cliOptions["discover-packages"] || false,
@@ -71,8 +71,9 @@ Program.prototype.getEngines = function()
return this.descriptor.json.engine;
}
-Program.prototype.getBootPackages = function()
+Program.prototype.getBootPackages = function(assembler, callback)
{
+ // TODO: Refactor as we will never have more than one boot package.
var self = this,
dependencies = [],
i = 0;
@@ -85,6 +86,14 @@ Program.prototype.getBootPackages = function()
dependencies.push(dep);
i++;
});
+ if (typeof assembler !== "undefined" && typeof callback !== "undefined")
+ {
+ self.resolveLocator(assembler, dependencies[0]["_package-0"], function(locator)
+ {
+ dependencies[0]["_package-0"] = locator;
+ callback(dependencies);
+ });
+ }
return dependencies;
}

0 comments on commit 5f78a22

Please sign in to comment.