Skip to content

Commit

Permalink
Fixes #13
Browse files Browse the repository at this point in the history
skip platform add is platform is already present

skip plugin if it's already install
  • Loading branch information
csantanapr committed Jan 25, 2015
1 parent f7cbd56 commit 9e8758c
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 12 deletions.
23 changes: 23 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ module.exports = function (grunt) {
platforms: ['ios', 'android'] //valid platforms for command platform are ios, android, blackberry10, wp8, wp7
}
},
add_platforms_ios: {
options: {
command: 'platform',
action: 'add', //valid actions for command platform are add , remove, rm
platforms: ['ios'] //valid platforms for command platform are ios, android, blackberry10, wp8, wp7
}
},
add_platforms_ios_folder: {
options: {
command: 'platform',
action: 'add', //valid actions for command platform are add , remove, rm
platforms: ['../node_modules/cordova-ios'] //valid platforms for command platform are ios, android, blackberry10, wp8, or folder
}
},
add_plugins: {
options: {
command: 'plugin',
Expand Down Expand Up @@ -104,6 +118,15 @@ module.exports = function (grunt) {
]
}
},
add_plugins_id: {
options: {
command: 'plugin',
action: 'add', //valid actions for command plugin are add , remove, rm
plugins: [ //plugins are fetched from Apache Foundation Repo https://git-wip-us.apache.org/repos/asf/
'battery-status'
]
}
},
build: {
options: {
command: 'build',
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,13 @@ Specify the action to perform

#### options.platforms
Type: `String` `'Array'`
Valid value: `'ios'` `'android'` `'blackberry10'` `'wp7'` `'wp8'`
Valid value: `'ios'` `'android'` `'blackberry10'` `'wp8'` `'ubuntu'` `'firefoxos'` `'amazon-fireos'`
Version can be specified like `'[email protected]'`
Can also pass a director containing the platform git repository
Required for Commands: `'platform'`
Not Applicable for Commands: `'plugin'`
Optional for other commands
If using with option.action=add and platform already added, then it's skip

Specify the platform type

Expand Down Expand Up @@ -181,10 +184,11 @@ Specify the plugin to add to the Cordova project
It can be specify in 4 forms:

* Shortcut (i.e. 'camera' it will be downloaded form plugins.cordova.io)
* ID (i.e. org.apache.cordova.device it will be downloaded from plugins.cordova.io)
* ID or ID@version (i.e. org.apache.cordova.device it will be downloaded from plugins.cordova.io)
* Git Url (i.e. https://git-wip-us.apache.org/repos/asf/cordova-plugin-file.git)
* Directory Path (~/userid/cordova/plugins/plugin1)

If options.action=add and using ID or ID@version, and plugin already added, then it's skip

## Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).
Expand Down
101 changes: 91 additions & 10 deletions tasks/cordovacli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
*/
/*global module */
var path = require('path'),
os = require('os');
os = require('os'),
fs = require('fs');


module.exports = function (grunt) {
'use strict';
Expand All @@ -22,6 +24,8 @@ module.exports = function (grunt) {
runCreate,
runPlatform,
runPlugin,
isPlatformExists,
isPluginExists,
cordova_path = path.dirname(require.resolve('cordova')),
cordova_json = path.join(cordova_path,'package.json'),
cordova_pkg = grunt.file.readJSON(cordova_json),
Expand All @@ -44,7 +48,18 @@ module.exports = function (grunt) {
'network-information': 'org.apache.cordova.network-information',
'splashscreen': 'org.apache.cordova.splashscreen',
'vibration': 'org.apache.cordova.vibration'
};
},
validPlatforms = [
'ios',
'android',
'ubuntu',
'amazon-fireos',
'wp8',
'blackberry10',
'firefoxos',
'windows8',
'windows',
'browser'];
runCordova = function (args, opts, done) {
var cordova_cli, spawn_cmd;

Expand Down Expand Up @@ -101,20 +116,74 @@ module.exports = function (grunt) {
var args = ['create', options.path, options.id, options.name].concat(options.args);
runCordova(args, {}, done);
};

isPlatformExists = function (p, cordovaRootPath) {
var platform_name;
var platform_cdv_dir;
var platform_src_dir;
var pkg;
// valid platform is like android or [email protected]
platform_name = p.split('@')[0];
if(validPlatforms.indexOf(p) === -1){
//then a directory is passed, let's check what platform it is
platform_src_dir = path.resolve(cordovaRootPath,p);
try {
pkg = require(path.join(platform_src_dir, 'package'));
platform_name = pkg.name.split('-')[1];
} catch(err){
grunt.log.writeln("For some reason can't read platform package.json");
}
}
//let check if platform is already added
platform_cdv_dir = path.resolve(cordovaRootPath, 'platforms', platform_name);
if (fs.existsSync(platform_cdv_dir)) {
return platform_name;
} else {
return false;
}

};

runPlatform = function (options, done) {
//platform(s) [{add|remove|rm} <PLATFORM>]
var tasks = [];
tasks.length = 0;
options.platforms.forEach(function (p) {
var f;
f = function (callback) {
runCordova(['platform', options.action, p ].concat(options.args), {cwd:options.path}, callback);
};
tasks.push(f);
var skip = false;
var platform_name;

if(options.action === 'add'){
platform_name = isPlatformExists(p,options.path);
if(platform_name){
skip = true;
grunt.log.writeln('Platform '+platform_name+' already exists skipping add');
}
}
if(!skip){
f = function (callback) {
runCordova(['platform', options.action, p ].concat(options.args), {cwd:options.path}, callback);
};
tasks.push(f);
}
});
runCordovaParallel(tasks, done);
};

isPluginExists = function(p, cordovaRootPath) {
var plugin_cdv_dir;
var plugin_id;

// valid platform is like org.apache.cordova.console or [email protected]
plugin_id = p.split('@')[0];
//let check if plugin already added
plugin_cdv_dir = path.resolve(cordovaRootPath, 'plugins', plugin_id);
if (fs.existsSync(plugin_cdv_dir)) {
return plugin_id;
} else {
return false;
}
};
runPlugin = function (options, done) {
//plugin(s) [{add|remove|rm} <PATH|URI>]
var tasks = [];
Expand All @@ -126,13 +195,25 @@ module.exports = function (grunt) {

options.plugins.forEach(function (p) {
var f;
var skip = false;
var plugin_id;
if(cordova_plugins_map[p]){
p = cordova_plugins_map[p];
}
f = function (callback) {
runCordova(['plugin', options.action, p ].concat(options.args), {cwd:options.path}, callback);
};
tasks.push(f);
if(options.action == 'add'){
plugin_id = isPluginExists(p,options.path);
if(plugin_id){
skip = true;
grunt.log.writeln('Plugin '+plugin_id+' already exists skipping add');
}
}
if(!skip){
f = function (callback) {
runCordova(['plugin', options.action, p ].concat(options.args), {cwd:options.path}, callback);
};
tasks.push(f);
}

});
runCordovaSeries(tasks, done);
};
Expand Down

0 comments on commit 9e8758c

Please sign in to comment.