Skip to content
This repository has been archived by the owner on Mar 26, 2018. It is now read-only.

Commit

Permalink
Update permission gen for gen 0.17 and fix that created manifest.json
Browse files Browse the repository at this point in the history
  • Loading branch information
ragingwind committed Jul 15, 2014
1 parent 6a0c5aa commit 505c0ba
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 116 deletions.
8 changes: 6 additions & 2 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ module.exports = yeoman.generators.Base.extend({
}
});

this.hookFor('chromeapp:permission', { as: 'subgen' });

this.option('test-framework', {
desc: 'Test framework to be invoked',
Expand Down Expand Up @@ -113,7 +112,12 @@ module.exports = yeoman.generators.Base.extend({
this.directory(this.coffee ? 'coffees' : 'scripts', 'app/scripts');
this.template('index.html', 'app/index.html', this);
this.template('_locales/en/messages.json', 'app/_locales/en/messages.json', this);
this.write('app/manifest.json', this.manifest.stringify());
},

permission: function() {
this.invoke('chromeapp:permission', {
options: { manifest: this.manifest }
});
},

install: function () {
Expand Down
227 changes: 114 additions & 113 deletions permission/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,133 +7,134 @@ var fs = require('fs');
var _ = require('lodash');
var manifest = require('../manifest');

var PermissionGenerator = module.exports = function PermissionGenerator(args, options, config) {
var dest = path.join(process.cwd(), '/app/manifest.json');
var fields;

yeoman.generators.Base.apply(this, arguments);

// load the manifest.json if already exist
if (fs.existsSync(dest)) {
this.log.info('Load manifest.json');
fields = JSON.parse(this.read(dest));
}

// create manifest with exist feields or not
this.manifest = new manifest(fields ? fields : {});
};

util.inherits(PermissionGenerator, yeoman.generators.Base);

PermissionGenerator.prototype.askForPermissions = function askForPermissions() {
var cb = this.async();
var currentPerms = this.manifest.fields.permissions;
var permissions = manifest.query({
type: 'app',
devFeatures: true
});
var choices = [];

_.each(permissions, function(perm, name) {
var choice = {
key: name,
name: perm.status ? name + ' (' + perm.status + ')' : name
module.exports = yeoman.generators.Base.extend({
constructor: function () {
yeoman.generators.Base.apply(this, arguments);

var dest = path.join(process.cwd(), '/app/manifest.json');

// load the manifest.json if already exist
if (this.options.manifest) {
this.manifest = this.options.manifest;
}

choices.push(choice);
});

var prompt = {
type: 'checkbox',
name: 'permissions',
message: 'Select a permissions for Chrome App',
paginated : true,
choices: choices
};

this.prompt(prompt, function (answers) {
this.permissions = answers.permissions;
cb();
}.bind(this));
};

PermissionGenerator.prototype.askForMatchPatterns = function askForMatchPatterns() {
var cb = this.async();
var prompt = {
type: 'checkbox',
name: 'matchPatterns',
message: 'Select URL match patterns',
choices: [{
name: 'All of permitted scheme',
value: 'allURLs',
}, {
name: 'HTTP/S scheme',
value: 'httpScheme',
}, {
name: 'HTTP scheme with host 127.0.0.1',
value: 'localhost'
}, {
name: 'chrome-extension scheme',
value: 'extensionScheme'
}]
};

this.prompt(prompt, function (answers) {
var patterns = answers.matchPatterns;

function hasPattern(pattern) {return patterns.indexOf(pattern) !== -1;};

// Push match-patterns wichi user has selected.
if (hasPattern('allURLs'))
this.permissions.push('<all_urls>');

if (hasPattern('httpScheme')) {
this.permissions.push('http://*/*');
this.permissions.push('https://*/*');
else if (fs.existsSync(dest)) {
this.log.info('Load manifest.json');
this.manifest = new manifest(JSON.parse(this.read(dest)));
} else {
this.manifest = new manifest({});
}
},

if (hasPattern('localhost')) {
this.permissions.push('http://127.0.0.1/*');
}
askForPermissions: function () {
var cb = this.async();
var currentPerms = this.manifest.fields.permissions;
var permissions = manifest.query({
type: 'app',
devFeatures: true
});
var choices = [];

_.each(permissions, function(perm, name) {
var choice = {
key: name,
name: perm.status ? name + ' (' + perm.status + ')' : name
}

if (hasPattern('extensionScheme')) {
this.permissions.push('chrome-extension://*/*');
}
choices.push(choice);
});

cb();
}.bind(this));
};
var prompt = {
type: 'checkbox',
name: 'permissions',
message: 'Select a permissions for Chrome App',
paginated : true,
choices: choices
};

PermissionGenerator.prototype.askForSocketPermission = function askForSocketPermission() {
if (_.indexOf(this.permissions, 'socket') !== -1) {
var cb = this.async();
this.prompt(prompt, function (answers) {
this.permissions = answers.permissions;
cb();
}.bind(this));
},

askForMatchPatterns: function () {
var cb = this.async();
var prompt = {
type: 'checkbox',
name: 'socketPermission',
message: 'Select a Socket permission rules',
name: 'matchPatterns',
message: 'Select URL match patterns',
choices: [{
name: 'TCP connecting and listening rules',
value: ['tcp-listen:*:*', 'tcp-connect:*:*']
name: 'All of permitted scheme',
value: 'allURLs',
}, {
name: 'UDP receiving and sending rules',
value: ['udp-bind:*:*', 'udp-send-to:*:*']
}],
filter: function(val) {
return _.union(val[0], val[1] ? val[1] : []);
}
name: 'HTTP/S scheme',
value: 'httpScheme',
}, {
name: 'HTTP scheme with host 127.0.0.1',
value: 'localhost'
}, {
name: 'chrome-extension scheme',
value: 'extensionScheme'
}]
};

this.prompt(prompt, function (answers) {
var perm = manifest.query({name: 'socket'});
perm.socket.permission.socket = answers.socketPermission;
this.manifest.setPermissions(perm);
var patterns = answers.matchPatterns;

function hasPattern(pattern) {return patterns.indexOf(pattern) !== -1;};

// Push match-patterns wichi user has selected.
if (hasPattern('allURLs'))
this.permissions.push('<all_urls>');

if (hasPattern('httpScheme')) {
this.permissions.push('http://*/*');
this.permissions.push('https://*/*');
}

if (hasPattern('localhost')) {
this.permissions.push('http://127.0.0.1/*');
}

if (hasPattern('extensionScheme')) {
this.permissions.push('chrome-extension://*/*');
}

cb();
}.bind(this));
}
};
},

askForSocketPermission: function () {
if (_.indexOf(this.permissions, 'socket') !== -1) {
var cb = this.async();

var prompt = {
type: 'checkbox',
name: 'socketPermission',
message: 'Select a Socket permission rules',
choices: [{
name: 'TCP connecting and listening rules',
value: ['tcp-listen:*:*', 'tcp-connect:*:*']
}, {
name: 'UDP receiving and sending rules',
value: ['udp-bind:*:*', 'udp-send-to:*:*']
}],
filter: function(val) {
return _.union(val[0], val[1] ? val[1] : []);
}
};

this.prompt(prompt, function (answers) {
var perm = manifest.query({name: 'socket'});
perm.socket.permission.socket = answers.socketPermission;
this.manifest.setPermissions(perm);
cb();
}.bind(this));
}
},

PermissionGenerator.prototype.app = function app() {
this.manifest.setPermissions(this.permissions);
this.write('app/manifest.json', this.manifest.stringify());
};
app: function () {
this.manifest.setPermissions(this.permissions);
this.write('app/manifest.json', this.manifest.stringify());
}
});
1 change: 0 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ describe('Chromeapp generator', function () {
var expected = [
'app/bower_components',
'Gruntfile.js',
'app/manifest.json',
'app/_locales/en/messages.json',
'app/images/icon-128.png',
'app/images/icon-16.png',
Expand Down

0 comments on commit 505c0ba

Please sign in to comment.