Skip to content

Commit

Permalink
Merge pull request hyperledger-archives#385 from dselman/master
Browse files Browse the repository at this point in the history
Add support for package.json to BusinessNetworkMetadata
  • Loading branch information
dselman authored Mar 8, 2017
2 parents 2cb6e25 + 6759ea1 commit 4a002bf
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 48 deletions.
5 changes: 5 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
machine:
node:
version: 6.9.5


9 changes: 7 additions & 2 deletions packages/composer-common/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class BaseException extends Error {
+ void constructor(string)
}
class BusinessNetworkDefinition {
+ void constructor(String,String,String)
+ void constructor(String,String,object,String)
+ String getIdentifier()
+ BusinessNetworkMetadata getMetadata()
+ String getName()
Expand All @@ -16,8 +16,13 @@ class BusinessNetworkDefinition {
+ Serializer getSerializer()
}
class BusinessNetworkMetadata {
+ void constructor(String)
+ void constructor(object,String)
+ String getREADME()
+ object getPackageJson()
+ string getName()
+ string getDescription()
+ string getVersion()
+ string getIdentifier()
}
class Factory {
+ void constructor(ModelManager)
Expand Down
5 changes: 2 additions & 3 deletions packages/composer-common/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
# Note that the latest public API is documented using JSDocs and is available in api.txt.
#

Version 0.4.4 {7c78f7d15b222c507f9840f344f42bff} 2017-03-06
Version 0.4.4 {0e331be906f2098a98a845e92efec2b9} 2017-03-08
- Added package.json to BusinessNetworkMetadata
- Added getBusinessNetworkMetadata to BusinessNetworkDefinition

Version 0.4.4 {b64498dc085e58b975eebfce7430186a} 2017-02-22
- Added file name and file location to IllegalModelException

Version 0.3.7 {120eb2d3b0e487c6e95696aadecec93a} 2017-01-24
Expand Down
69 changes: 36 additions & 33 deletions packages/composer-common/lib/businessnetworkdefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,49 @@ class BusinessNetworkDefinition {
* </p>
* @param {String} identifier - the identifier of the business network. The
* identifier is formed from a business network name + '@' + version. The
* version is a semver valid version string.
* @param {String} description - the description of the business network
* @param {String} readme - the optional readme in markdown for the business network
* version is a semver valid version string. If package.json is passed this is ignored.
* @param {String} description - the description of the business network. If package.json is passed then this is ignored.
* @param {object} packageJson - the JS object for package.json (optional)
* @param {String} readme - the readme in markdown for the business network (optional)
*/
constructor(identifier, description, readme) {
constructor(identifier, description, packageJson, readme) {
const method = 'constructor';
LOG.entry(method, identifier, description);
this.identifier = identifier;

const atIndex = this.identifier.lastIndexOf('@');
// if package.json is not present we generate one based on the metadata passed
if(!packageJson) {
const atIndex = identifier.lastIndexOf('@');
let name = null;

if (atIndex >= 0) {
this.name = this.identifier.substring(0, atIndex);
} else {
throw new Error('Malformed business network identifier. It must be "[email protected]"');
}
if (atIndex >= 0) {
name = identifier.substring(0, atIndex);
} else {
throw new Error('Malformed business network identifier. It must be "[email protected]"');
}

const version = identifier.substring(atIndex + 1);
if (!semver.valid(version)) {
throw new Error('Version number is invalid. Should be valid according to semver but found: ' + version);
}

this.version = this.identifier.substring(atIndex + 1);
if (!semver.valid(this.version)) {
throw new Error('Version number is invalid. Should be valid according to semver but found: ' + this.version);
packageJson = {};
packageJson.name = name;
packageJson.version = version;
packageJson.description = description;
LOG.debug(method, 'Created package.json' + JSON.stringify(packageJson));
}
else {
LOG.debug(method, 'Using package.json' + JSON.stringify(packageJson));
}

this.description = description;
this.modelManager = new ModelManager();
this.aclManager = new AclManager(this.modelManager);
this.scriptManager = new ScriptManager(this.modelManager);
this.introspector = new Introspector(this.modelManager);
this.factory = new Factory(this.modelManager);
this.serializer = new Serializer(this.factory, this.modelManager);
this.metadata = new BusinessNetworkMetadata(readme);

this.metadata = new BusinessNetworkMetadata(packageJson,readme);
LOG.exit(method);
}

Expand All @@ -89,7 +102,7 @@ class BusinessNetworkDefinition {
* @return {String} the identifier of this business network
*/
getIdentifier() {
return this.identifier;
return this.getMetadata().getIdentifier();
}

/**
Expand All @@ -105,7 +118,7 @@ class BusinessNetworkDefinition {
* @return {String} the name of this business network
*/
getName() {
return this.name;
return this.getMetadata().getName();
}

/**
Expand All @@ -114,7 +127,7 @@ class BusinessNetworkDefinition {
* to parse.
*/
getVersion() {
return this.version;
return this.getMetadata().getVersion();
}


Expand All @@ -123,7 +136,7 @@ class BusinessNetworkDefinition {
* @return {String} the description of this business network
*/
getDescription() {
return this.description;
return this.getMetadata().getDescription();
}

/**
Expand Down Expand Up @@ -211,10 +224,7 @@ class BusinessNetworkDefinition {
return Promise.all(allPromises)
.then(() => {
LOG.debug(method, 'Loaded package.json');
let packageName = packageJsonContents.name;
let packageVersion = packageJsonContents.version;
let packageDescription = packageJsonContents.description;
businessNetworkDefinition = new BusinessNetworkDefinition(packageName + '@' + packageVersion, packageDescription, readmeContents);
businessNetworkDefinition = new BusinessNetworkDefinition(null, null, packageJsonContents, readmeContents);

LOG.debug(method, 'Loaded all model, JavaScript, and ACL files');
LOG.debug(method, 'Adding model files to model manager');
Expand Down Expand Up @@ -247,12 +257,7 @@ class BusinessNetworkDefinition {

let zip = new JSZip();

let packageFileContents = JSON.stringify({
name: this.name,
version: this.version,
description: this.description
});

let packageFileContents = JSON.stringify(this.getMetadata().getPackageJson());
zip.file('package.json', packageFileContents);

// save the README.md if present
Expand Down Expand Up @@ -370,11 +375,9 @@ class BusinessNetworkDefinition {
// parse the package.json
let jsonObject = JSON.parse(packageJsonContents);
let packageName = jsonObject.name;
let packageVersion = jsonObject.version;
let packageDescription = jsonObject.description;

// create the business network definition
const businessNetwork = new BusinessNetworkDefinition(packageName + '@' + packageVersion, packageDescription, readmeContents);
const businessNetwork = new BusinessNetworkDefinition(null, null, jsonObject, readmeContents);
const modelFiles = [];
const modelFileNames = [];

Expand Down
55 changes: 52 additions & 3 deletions packages/composer-common/lib/businessnetworkmetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const LOG = Logger.getLog('BusinessNetworkMetadata');
* <p>
* Defines the metadata for a BusinessNeworkDefinition. This includes:
* <ul>
* <li>README.md</li>
* <li>package.json</li>
* <li>README.md (optional)</li>
* </ul>
* </p>
* @class
Expand All @@ -35,16 +36,24 @@ class BusinessNetworkMetadata {
* <strong>Note: Only to be called by framework code. Applications should
* retrieve instances from {@link BusinessNetworkDefinition}</strong>
* </p>
* @param {String} readme - the README.md for the business network
* @param {object} packageJson - the JS object for package.json (required)
* @param {String} readme - the README.md for the business network (may be null)
*/
constructor(readme) {
constructor(packageJson, readme) {
const method = 'constructor';
LOG.entry(method, readme);

if(!packageJson || typeof(packageJson) !== 'object') {
throw new Error('package.json is required and must be an object');
}

this.packageJson = packageJson;

if(readme && typeof(readme) !== 'string') {
throw new Error('README must be a string');
}


this.readme = readme;
LOG.exit(method);
}
Expand All @@ -56,6 +65,46 @@ class BusinessNetworkMetadata {
getREADME() {
return this.readme;
}

/**
* Returns the package.json for this business network.
* @return {object} the Javascript object for package.json
*/
getPackageJson() {
return this.packageJson;
}

/**
* Returns the name for this business network.
* @return {string} the name of the business network
*/
getName() {
return this.packageJson.name;
}

/**
* Returns the description for this business network.
* @return {string} the description of the business network
*/
getDescription() {
return this.packageJson.description;
}

/**
* Returns the version for this business network.
* @return {string} the description of the business network
*/
getVersion() {
return this.packageJson.version;
}

/**
* Returns the identifier for this business network, formed from name@version.
* @return {string} the identifier of the business network
*/
getIdentifier() {
return this.packageJson.name + '@' + this.packageJson.version;
}
}

module.exports = BusinessNetworkMetadata;
7 changes: 4 additions & 3 deletions packages/composer-common/test/businessnetworkdefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('BusinessNetworkDefinition', () => {
});

it('should accept README', () => {
const bnd = new BusinessNetworkDefinition('[email protected]', 'description', 'readme');
const bnd = new BusinessNetworkDefinition('[email protected]', 'description', null, 'readme');
bnd.getMetadata().getREADME().should.equal('readme');
});
});
Expand Down Expand Up @@ -97,6 +97,7 @@ describe('BusinessNetworkDefinition', () => {
businessNetwork.getVersion().should.equal('0.0.1');
businessNetwork.getDescription().should.equal('A test business network.');
businessNetwork.getMetadata().getREADME().should.equal('This is a test');
businessNetwork.getMetadata().getPackageJson().customKey.should.equal('custom value');
Object.keys(businessNetwork.modelManager.modelFiles).should.have.length(3);
Object.keys(businessNetwork.scriptManager.scripts).should.have.length(2);
businessNetwork.aclManager.getAclRules().should.have.length(5);
Expand Down Expand Up @@ -141,8 +142,8 @@ describe('BusinessNetworkDefinition', () => {
let readFile = fs.readFileSync(__dirname + '/data/zip/test-archive.zip');
return BusinessNetworkDefinition.fromArchive(readFile).then((businessNetwork) => {
businessNetwork.should.be.BusinessNetworkDefinition;
businessNetwork.identifier.should.equal('[email protected]');
businessNetwork.description.should.equal('A test business network.');
businessNetwork.getIdentifier().should.equal('[email protected]');
businessNetwork.getDescription().should.equal('A test business network.');
businessNetwork.getMetadata().getREADME().should.equal('This is a test');
Object.keys(businessNetwork.modelManager.modelFiles).should.have.length(3);
Object.keys(businessNetwork.scriptManager.scripts).should.have.length(2);
Expand Down
14 changes: 11 additions & 3 deletions packages/composer-common/test/businessnetworkmetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,24 @@ describe('BusinessNetworkMetadata', () => {

describe('#constructor', () => {

it('should throw if package.json not specified', () => {
(() => {
new BusinessNetworkMetadata();
}).should.throw(/package.json is required/);
});

it('should throw if readme not specified', () => {
(() => {
new BusinessNetworkMetadata({});
new BusinessNetworkMetadata({}, {});
}).should.throw(/README must be a string/);
});

it('should store README', () => {
it('should store package.json and README', () => {
const readme = 'TEST README';
let metadata = new BusinessNetworkMetadata(readme);
const packageJson = {name: 'Foo'};
let metadata = new BusinessNetworkMetadata(packageJson,readme);
metadata.getREADME().should.equal(readme);
metadata.getPackageJson().should.equal(packageJson);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "test-archive",
"version": "0.0.1",
"description": "A test business network."
"description": "A test business network.",
"customKey": "custom value"
}

0 comments on commit 4a002bf

Please sign in to comment.