PackageDescriptor.prototype.save = function() { if(!this.path) { throw new PackageDescriptorError("Cannot save package descriptor"); } if(this.saveLocal) { var localSpec = UTIL.deepDiff(this.spec, this.globalSpec); if(localSpec) { this.path.dirname().join("package.local.json").write(JSON.encode(localSpec, null, " ")); } } else { this.path.write(JSON.encode(this.spec, null, " ")); } }
it('json', () => { assert.equal(json.encode({ a: 100, b: 200 }), '{"a":100,"b":200}'); assert.equal(json.encode(json.decode('{"a":100,"b":200}')), '{"a":100,"b":200}'); var buf = new Buffer('test'); var j = json.encode(buf); assert.isTrue(Buffer.isBuffer(json.decode(j))); });
_installPackage: function(pack, packFile) { var dontinstall = this.dontinstall; var zf = new ZipFile(packFile); var entries = zf.entries(); var root = this.env.root; var destination, entry; var packagesDir = this.env.getDirectory("packages"); var filelist = []; for (var i = 0; i < entries.length; i++) { entry = entries[i]; if (dontinstall.exec(entry.name)) { return; } if (entry.name == "package.json") { destination = packagesDir.join(pack.name + ".json"); } else { destination = root.join(entry.name); } filelist.push(root.to(destination)); zf.saveFile(entry, destination); } zf.close(); var fl = packagesDir.join(pack.name + ".filelist"); filelist.push(root.to(fl)); fl.write(json.encode(filelist)); }
exports.writeCatalog = function (catalog) { var catalogPath = exports.getCatalogPath(); print('Writing ' + catalogPath); return catalogPath.write( json.encode(catalog, null, 4), {charset: 'utf-8'} ); };
function exec(handle, method, key, o, expires) { expires = expires || 0; var flags = 0; // not a string if (isString(o)) { flags = 1; } else { o = Json.encode(o); } return memcached[method](handle, key, o, expires, flags); }
JsonStore.prototype.save = function(force) { if(!this.exists() && !force) { throw new JsonStoreError("Cannot save store. Store does not exist on disk at: " + this.file); } if(this.hasFileChanged() && !force) { throw new JsonStoreError("Cannot save store. Data changed on disk: "+this.file); } if(!this.dirty && !force) return; if(!this.file.dirname().exists()) this.file.dirname().mkdirs(); this.file.write(JSON.encode(this.data, null, ' ')); this.fileMtime = this.file.mtime(); this.dirty = false; };
exports.writeCatalog = function (catalogPath, catalog) { exports.getCatalogPath(catalogPath).write(JSON.encode(catalog, null, 4), {"charset": "UTF-8"}); };
assert.throws(() => { json.encode(a); })
Cache.prototype.setObject = function(key, obj) { this.setString(key, JSON.encode(obj, null, " ")); }
var process = require('process'); var json = require('json'); console.log(json.encode(process.env));
PlatformBuilder.prototype.triggerBuild = function(options) { TERM.stream.print("\0yellow(*** Building PLATFORM Package ***\0)"); TERM.stream.print("\0yellow( source: "+this.sourcePackage.getPath()+"\0)"); TERM.stream.print("\0yellow( target: "+this.targetPackage.getPath()+"\0)"); var targetBasePath = this.targetPackage.getPath(); targetBasePath.join("bin").mkdirs(); var sourcePackageImplements = this.sourcePackage.getDescriptor(). getImplementsForUri("http://registry.pinf.org/cadorn.org/github/pinf/@meta/platform/package/0.1.0"); // write package.json file for platform targetBasePath.join("package.json").write(JSON.encode({ "name": options.targetPackageName, "using": { "platform": this.sourcePackage.getLocator().getSpec() } }, null, " ")); // a platform may declare programs it depends on. // we need to link all binaries from built programs to the platform bin directory. if(UTIL.has(sourcePackageImplements, "programs")) { UTIL.every(sourcePackageImplements["programs"], function(item) { var locator = LOCATOR.PackageLocator(item[1]); if(!PINF.getDatabase().hasProgram(locator) || options.forceBuild) { PINF.getDatabase().buildProgram(locator, options); } var binPath = PINF.getDatabase().getProgram(locator).getPath().join("bin"); if(binPath.exists()) { binPath.listPaths().forEach(function(item) { print(" ... LINK BIN from "+item+" to " +targetBasePath.join("bin", item.basename())); item.symlink(targetBasePath.join("bin", item.basename())); }); } }); } // link all commands from built platform var binPath = this.targetPackage.getBuildPath().join("raw", "bin"); if(binPath.exists()) { binPath.listPaths().forEach(function(item) { if(item.basename().valueOf()!="activate.bash") { if(!targetBasePath.join("bin", item.basename()).exists()) { print(" ... LINK BIN from "+item+" to " +targetBasePath.join("bin", item.basename())); item.symlink(targetBasePath.join("bin", item.basename())); } } }); } /* TODO: Call custom platform builders!! var builder = pkg.getBuilder({ "packageStore": this.getPackageStore() }); // TODO: Deprecate 'path' property below builder.triggerBuild(this, { "path": path, "platformName": name }); */ // write bin/activate.bash if it does not exist (i.e. was not created by custom builder) var file = targetBasePath.join("bin", "activate.bash"); file.write("export PATH="+file.dirname()+":\"$PATH\""); }
exports.writeNotes = function (notes) { return exports.getNotesPath().write( json.encode(notes, null, 4), {charset: 'utf-8'} ); };
Dispatcher.prototype.send = function(data, meta) { return this.sendRaw( this.getEncoder().encode(data, meta), (meta)?JSON.encode(meta):"" ); }
exports.writeSources = function (sources) { return exports.getSourcesPath().write( json.encode(sources, null, 4), {charset: 'utf-8'} ); };
Program.prototype.build = function(options) { var self = this; options = options || {}; if(!options.skipClean) { this.clean(); } var descriptor = this.getDescriptor(), buildPath = this.getBuildPath(), rawBuildPath = buildPath.join("raw"), path; // write package.json file (merged with package.local.json if avaiable) path = rawBuildPath.join("package.json"); path.dirname().mkdirs(); path.write(JSON.encode(descriptor.spec, null, " ")); var spec = this.spec; if(!options.remoteProgram && !options.remoteDependencies) { spec = this.localSpec; if(!spec.exists()) { spec.init(); } } else if(options.remoteDependencies) { if(!this.spec.exists()) { this.spec.init(); } } // link all system and using packages to desired versions options.uidLocators = {}; UTIL.every({ "system": { "iterator": "traverseEveryDependency", "directory": "packages", "property": "dependencies", "id": "getName" }, "using": { "iterator": "traverseEveryUsing", "directory": "using", "property": "using", "id": "getTopLevelId" } }, function(type) { var locatorRewriteInfo = []; descriptor[type[1].iterator](function(parentPackage, name, locator, stacks, property) { if(!locator) { return; } var key = ["packages", type[0]].concat(stacks.names).concat([name, "@"]); if(options.remoteProgram) { if(!spec.has(key)) { throw new Error("remote program.json is missing a locator for key: " + key.join(" -> ")); } // overwite locator with the one from the program config locator = LOCATOR.PackageLocator(spec.get(key).locator); } if(options.remoteDependencies) { locator.setForceRemote(true); } var pkg = self.packageStore.get(locator); // linked packages do not contain 'version' properties if(pkg.getVersion()) { locator.pinAtVersion(pkg.getVersion()); } // when traversing dependencies, using packages are traversed as well if(property!=type[1]["property"]) { return locator; } // if(type[0]=="system") { // path = rawBuildPath.join(type[1].directory, stacks.names.concat(name).join(".")); // } else // if(type[0]=="using") { // path = rawBuildPath.join(type[1].directory, pkg.getTopLevelId()); path = rawBuildPath.join("using", pkg.getTopLevelId()); // } // update info in program.json file var info = {}; if(pkg.hasUid()) { info["uid"] = pkg.getUid(); options.uidLocators[info["uid"]] = locator; } info["locator"] = locator.getSpec(true); spec.set(key, info); // if package has a version we need to copy it, otherwise we can link it (as it is likely a sources overlay) if(pkg.getVersion()) { if(!path.exists()) { path.dirname().mkdirs(); FILE.copyTree(pkg.getPath(), path); } // since we copied it to a specific version we need to update all package locators // to include the exact version locatorRewriteInfo.push({ "id": parentPackage[type[1].id](), "name": name, "revision": pkg.getVersion() }); } else if(!path.exists()) { path.dirname().mkdirs(); pkg.getPath().symlink(path); } return locator; }, { "packageStore": self.packageStore, "package": self }); locatorRewriteInfo.forEach(function(info) { if(info.id==self[type[1].id]()) { path = rawBuildPath.join("package.json"); } else { path = rawBuildPath.join(type[1].directory, info.id).join("package.json"); } JSON_STORE.JsonStore(path).set([type[1].property, info.name, "revision"], info.revision); }); }); // link all packages with no UID property // these are packages that are managed as part of this package // if a UID is present the package will end up in /using/ based on dependency declarations var sourceBasePath = this.getPath().join("packages"), targetBasePath = rawBuildPath.join("packages"); if(sourceBasePath.exists()) { targetBasePath.mkdirs(); sourceBasePath.listPaths().forEach(function(item) { if(item.join("package.json").exists()) { if(!UTIL.has(JSON.decode(item.join("package.json").read().toString()), "uid")) { item.symlink(targetBasePath.join(item.basename())); } } }); }; var builder = this.getBuilder(); // options["skipWriteCommands"] = true; builder.triggerBuild(this, options); // build tester var spec = descriptor.getPinfSpec(); if(spec && spec.tester) { var pkg = PINF.getPackageForLocator(LOCATOR.PackageLocator(spec.tester)); options.testPackage = pkg; pkg.getBuilder().triggerBuild(PACKAGE.Package(rawBuildPath), options); } return this.getPath(); }
end: function() { if (this.started) { fs.writeFile(this.file, Json.encode(this.data)); this.data = {}; } }
].forEach(function(name) { file = filesPath.join(name, "package.json"); var descriptor = JSON.decode(file.read()); delete descriptor.uid; file.write(JSON.encode(descriptor, null, ' ')); });
ProgramBuilder.prototype.buildProgramPackage = function(sourcePackage, options) { options = options || {}; var self = this; var sourceBasePath = this.sourcePackage.getPath(), targetBasePath = this.targetPackage.getPath(); targetBasePath.mkdirs(); var sourceDescriptor = this.sourcePackage.getDescriptor(); // write package.json file (merged with package.local.json if avaiable) var file = targetBasePath.join("package.json"); // set version var descriptor = sourceDescriptor.getSpec(), locator = this.sourcePackage.getLocator(); // determine version for release // TODO: Use provided version instead (if applicable) var time = new Date() descriptor.version = [ "0.0.0" + locator.getRevision(), (""+time.getFullYear()).substr(2,2), UTIL.padBegin(time.getMonth()+1, 2), UTIL.padBegin(time.getDate(), 2), UTIL.padBegin(time.getHours(), 2), UTIL.padBegin(time.getMinutes(), 2) ].join(""); file.write(JSON.encode(descriptor, null, " ")); // HACK: write program file first above, then modify below // add options // TODO: Remove old builders when done var programOptions = this.getTargetOptions(options.target); descriptor['implements']['http://registry.pinf.org/cadorn.org/github/pinf/@meta/program/package/0.1.0'].options = programOptions; file.write(JSON.encode(descriptor, null, " ")); var buildPackage = PACKAGE.Package(this.sourcePackage.getBuildPath().join("raw"), this.sourcePackage.getLocator()); // write package.json after updating version var descriptor = JSON.decode(sourceBasePath.join("package.json").read()); // HACK: merge local on top if(sourceBasePath.join("package.local.json").exists()) { UTIL.update(descriptor, JSON.decode(sourceBasePath.join("package.local.json").read())); } descriptor.version = this.targetPackage.getDescriptor().getVersion(); descriptor['implements']['http://registry.pinf.org/cadorn.org/github/pinf/@meta/program/package/0.1.0'].options = programOptions; buildPackage.getPath().join("package.json").write(JSON.encode(descriptor, null, " ")); // sourceBasePath.join("package.json").copy(buildBasePath.join("package.json")); // instanciate program descriptor objects from source package // which will be updated during build process var remoteProgramDescriptor = JSON_STORE.JsonStore(sourceBasePath.join("program.json")), localProgramDescriptor = JSON_STORE.JsonStore(sourceBasePath.join("program.local.json")), programDescriptor = localProgramDescriptor; // select the appropriate program descriptor for our build if(options.remoteDependencies) { programDescriptor = remoteProgramDescriptor; } if(!programDescriptor.exists()) { programDescriptor.init(); } var path; self.traversePackages(programDescriptor, sourceDescriptor, buildPackage, options); /* // link all system and using packages to desired versions options.uidLocators = {}; UTIL.every({ "system": { "iterator": "traverseEveryDependency", "directory": "packages", "property": "dependencies", "id": "getName" }, "using": { "iterator": "traverseEveryUsing", "directory": "using", "property": "using", "id": "getTopLevelId" } }, function(type) { var locatorRewriteInfo = []; sourceDescriptor[type[1].iterator](function(parentPackage, name, locator, stacks, property) { if(!locator) { return; } var key = ["packages", type[0]].concat(stacks.names).concat([name, "@"]); print(key); if(options.remoteProgram) { if(!programDescriptor.has(key)) { throw new Error("remote program.json is missing a locator for key: " + key.join(" -> ")); } // overwite locator with the one from the program config locator = LOCATOR.PackageLocator(programDescriptor.get(key).locator); } if(options.remoteDependencies) { locator.setForceRemote(true); } var pkg = PINF.getPackageForLocator(locator); // linked packages do not contain 'version' properties if(pkg.getVersion()) { locator.pinAtVersion(pkg.getVersion()); } // when traversing dependencies, using packages are traversed as well if(property!=type[1]["property"]) { return locator; } // if(type[0]=="system") { // path = rawBuildPath.join(type[1].directory, stacks.names.concat(name).join(".")); // } else // if(type[0]=="using") { // path = rawBuildPath.join(type[1].directory, pkg.getTopLevelId()); // path = targetBasePath.join("using", pkg.getTopLevelId()); path = targetPackage.getPath().join("using", pkg.getTopLevelId()); // } // update info in program.json file var info = {}; if(pkg.hasUid()) { info["uid"] = pkg.getUid(); options.uidLocators[info["uid"]] = locator; } info["locator"] = locator.getSpec(true); programDescriptor.set(key, info); print("id: "+pkg.getTopLevelId()); dump(options.monitor.built); // only build package if not already built for program if(!UTIL.has(options.monitor.built, pkg.getTopLevelId())) { options.monitor.built[pkg.getTopLevelId()] = true; self.buildPackage(pkg, options); // if package has a version we need to copy it, otherwise we can link it (as it is likely a sources overlay) if(pkg.getVersion()) { if(!path.exists()) { path.dirname().mkdirs(); print("COPY "+pkg.getPath()+" to "+path); FILE.copyTree(pkg.getPath(), path); } // since we copied it to a specific version we need to update all package locators // to include the exact version if(!pkg.hasUid()) locatorRewriteInfo.push({ "id": parentPackage[type[1].id](), "name": name, "revision": pkg.getVersion() }); } else if(!path.exists() && !path.join("package.json").exists()) { path.dirname().mkdirs(); print("LINK "+pkg.getPath()+" to "+path); pkg.getPath().symlink(path); } if(type[0]=="system") { //print("link system package from " + path + " to " + targetPackage.getPath().join("using", "packages", name)); targetPackage.getPath().join("packages").mkdirs(); path.symlink(targetPackage.getPath().join("packages", name)); } } else { return false; } return locator; }, { "packageStore": PINF.getDatabase().getPackageStore(), "package": self.sourcePackage }); locatorRewriteInfo.forEach(function(info) { if(info.id==self.sourcePackage[type[1].id]()) { path = targetPackage.getPath().join("package.json"); } else { path = targetPackage.getPath().join(type[1].directory, info.id).join("package.json"); } // ensure we only update copied package.json files! if(targetPackage.getPath().join("using").relative(path.canonical()).valueOf().substring(0,1)==".") { // don't update as package.json file is not within built using directory } else { JSON_STORE.JsonStore(path).set([type[1].property, info.name, "revision"], info.revision); } }); }); */ // copy updated program descriptor programDescriptor.getFile().copy(targetBasePath.join("program.json")); // link all packages with no UID property which are packages that are managed as part of the sourcePackage // if a UID is present the package will end up in /using/ based on dependency declarations var sourcePackagesPath = sourceBasePath.join("packages"), targetPackagesPath = targetBasePath.join("packages"); if(sourcePackagesPath.exists()) { targetPackagesPath.mkdirs(); sourcePackagesPath.listPaths().forEach(function(item) { if(item.join("package.json").exists()) { if(!UTIL.has(JSON.decode(item.join("package.json").read().toString()), "uid")) { item.symlink(targetPackagesPath.join(item.basename())); } } }); }; /* // build tester /* TODO: This shoul happen dynamically when a test is executed var spec = descriptor.getPinfSpec(); if(spec && spec.tester) { var pkg = PINF.getPackageForLocator(LOCATOR.PackageLocator(spec.tester)); options.testPackage = pkg; pkg.getBuilder().triggerBuild(PACKAGE.Package(rawBuildPath), options); } */ }