Skip to content

Instantly share code, notes, and snippets.

@kvendrik
Last active September 7, 2015 12:58
Show Gist options
  • Select an option

  • Save kvendrik/f37341152c1c5e4b4ee4 to your computer and use it in GitHub Desktop.

Select an option

Save kvendrik/f37341152c1c5e4b4ee4 to your computer and use it in GitHub Desktop.
Small Cordova Plugin Manager Script
#!/usr/bin/env python
import json
import sys
import subprocess
import argparse
import re
parser = argparse.ArgumentParser(description='Cordova Package Manager')
parser.add_argument('command', metavar='C', type=str, nargs='+',
help='install or i | add <plugin_name> [-f | --force] | rm <plugin_name> | ls [-c | --compare] | [-v | --verbose]')
# global flags
parser.add_argument('-v', '--verbose', action='store_true')
parser.add_argument('-f', '--force', action='store_true')
# command_ls flag
parser.add_argument('-c', '--compare', action='store_true')
args = parser.parse_args()
MAIN_COMMAND = args.command[0]
package_file = open('package.json', 'r+')
package_json = json.load(package_file)
package_plugin_names = package_json['cordovaPlugins']
def log_msg(msg, line_break=False):
if line_break:
prefix = '\n> '
else:
prefix = '> '
print prefix+msg
def add_plugin_to_package(plugin_name):
if plugin_name not in package_plugin_names:
package_plugin_names.append(plugin_name)
else:
log_msg(plugin_name+' is already in your package.json')
def rm_plugin_from_package(plugin_name):
if plugin_name in package_plugin_names:
package_plugin_names.remove(plugin_name)
else:
log_msg(plugin_name+' was not found in your package.json')
def write_json():
package_file.seek(0)
package_file.write(json.dumps(package_json, sort_keys=True, indent=4))
package_file.truncate()
def exec_cordova_cmd(cmd, silent=False):
try:
output = subprocess.check_output('cordova plugin '+cmd, shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as exc:
if not silent:
print '\n'+exc.output
if args.verbose:
print exc
log_msg('error while executing "cordova plugin '+cmd+'"', True)
sys.exit(1)
return False
else:
if not silent:
print output
return output
def get_installed_plugins():
raw = exec_cordova_cmd('ls', True).splitlines()
output = []
for plugin_str in raw:
plugin_name = re.search('[^\s]+', plugin_str, re.M|re.I).group(0)
output.append(plugin_name)
return output
def get_missing_plugins():
# get missing plugins
installed_plugins = get_installed_plugins()
output = []
for plugin_name in package_plugin_names:
if plugin_name not in installed_plugins:
output.append(plugin_name)
return output
def get_non_required_plugins():
# get non_required_plugins
installed_plugins = get_installed_plugins()
output = []
for plugin_name in installed_plugins:
if plugin_name not in package_plugin_names:
output.append(plugin_name)
return output
def command_ls():
if args.compare:
non_req_plugins = get_non_required_plugins()
missing_plugins = get_missing_plugins()
non_req_plugins_len = len(non_req_plugins)
missing_plugins_len = len(missing_plugins)
if non_req_plugins_len > 0:
log_msg('Installed but not in cordovaPlugins ('+str(non_req_plugins_len)+')')
for plugin_name in non_req_plugins:
print plugin_name
if missing_plugins_len > 0:
log_msg('Missing plugins ('+str(missing_plugins_len)+')')
for plugin_name in missing_plugins:
print plugin_name
if non_req_plugins_len == 0 and missing_plugins_len == 0:
log_msg('It\'s all good baby, baby')
else:
log_msg('Installed plugins')
exec_cordova_cmd('ls')
log_msg('Dependencies (cordovaPlugins)')
for plugin_name in package_plugin_names:
print plugin_name
def command_add_rm(command=MAIN_COMMAND):
if len(args.command) == 2:
plugin_name = args.command[1]
plugin_ref = plugin_name
is_url = re.match('http.*', plugin_name)
if MAIN_COMMAND == 'add' and is_url:
if args.force:
match = re.search('[^\/]+$', plugin_name)
if not match:
if arg.verbose:
log_msg('Failed to extract plugin name from url')
sys.exit(1)
else:
plugin_name = match.group(0).replace('.git', '')
if args.verbose:
log_msg('Extracted plugin name from url: '+plugin_name)
else:
print '''Using URLs to add plugins is currently not fully supported.
Make sure to only use URLs if running this command with the plugin name
(which can be found in the plugins\'s config.xml) doesn\'t work.
If you wish to use a URL then run this command with the --force flag.'''
sys.exit(1)
if command == 'add' and exec_cordova_cmd('add '+plugin_ref):
add_plugin_to_package(plugin_name)
elif command == 'rm' and exec_cordova_cmd('rm '+plugin_ref):
rm_plugin_from_package(plugin_name)
else:
log_msg('No plugin_name provided')
sys.exit(1)
def command_install():
missing_plugins = get_missing_plugins()
if args.verbose:
installed_plugins = get_installed_plugins()
for plugin_name in package_plugin_names:
if plugin_name in installed_plugins:
print '[OK] '+plugin_name
else:
print '[NOT INSTALLED] '+plugin_name
if len(missing_plugins) > 0:
for plugin_name in missing_plugins:
exec_cordova_cmd('add '+plugin_name)
else:
log_msg('All required plugins are installed')
if __name__ == '__main__':
if MAIN_COMMAND == 'add' or MAIN_COMMAND == 'rm':
command_add_rm()
elif MAIN_COMMAND == 'ls':
command_ls()
elif MAIN_COMMAND == 'install' or MAIN_COMMAND == 'i':
command_install()
else:
parser.print_help()
package_file.close()
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment