Merge pull request #965 from resmo/fix/composer

composer: implemented smarter arguments per command handling
reviewable/pr18780/r1
Greg DeKoenigsberg 9 years ago
commit d9b801a4c2

@ -22,7 +22,9 @@
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
module: composer module: composer
author: "Dimitrios Tydeas Mengidis (@dmtrs)" author:
- "Dimitrios Tydeas Mengidis (@dmtrs)"
- "René Moser (@resmo)"
short_description: Dependency Manager for PHP short_description: Dependency Manager for PHP
version_added: "1.6" version_added: "1.6"
description: description:
@ -94,7 +96,7 @@ requirements:
- php - php
- composer installed in bin path (recommended /usr/local/bin) - composer installed in bin path (recommended /usr/local/bin)
notes: notes:
- Default options that are always appended in each execution are --no-ansi, --no-progress, and --no-interaction - Default options that are always appended in each execution are --no-ansi, --no-interaction and --no-progress if available.
''' '''
EXAMPLES = ''' EXAMPLES = '''
@ -105,20 +107,31 @@ EXAMPLES = '''
import os import os
import re import re
try:
import json
except ImportError:
import simplejson as json
def parse_out(string): def parse_out(string):
return re.sub("\s+", " ", string).strip() return re.sub("\s+", " ", string).strip()
def has_changed(string): def has_changed(string):
if "Nothing to install or update" in string: return "Nothing to install or update" not in string
return False
else: def get_available_options(module, command='install'):
return True # get all availabe options from a composer command using composer help to json
rc, out, err = composer_command(module, "help %s --format=json" % command)
if rc != 0:
output = parse_out(err)
module.fail_json(msg=output)
def composer_install(module, command, options): command_help_json = json.loads(out)
return command_help_json['definition']['options']
def composer_command(module, command, options=[]):
php_path = module.get_bin_path("php", True, ["/usr/local/bin"]) php_path = module.get_bin_path("php", True, ["/usr/local/bin"])
composer_path = module.get_bin_path("composer", True, ["/usr/local/bin"]) composer_path = module.get_bin_path("composer", True, ["/usr/local/bin"])
cmd = "%s %s %s %s" % (php_path, composer_path, command, " ".join(options)) cmd = "%s %s %s %s" % (php_path, composer_path, command, " ".join(options))
return module.run_command(cmd) return module.run_command(cmd)
def main(): def main():
@ -137,38 +150,45 @@ def main():
supports_check_mode=True supports_check_mode=True
) )
# Get composer command with fallback to default
command = module.params['command']
available_options = get_available_options(module=module, command=command)
options = [] options = []
# Default options # Default options
options.append('--no-ansi') default_options = [
options.append('--no-progress') 'no-ansi',
options.append('--no-interaction') 'no-interaction',
'no-progress',
]
options.extend(['--working-dir', os.path.abspath(module.params['working_dir'])]) for option in default_options:
if option in available_options:
option = "--%s" % option
options.append(option)
# Get composer command with fallback to default options.extend(['--working-dir', os.path.abspath(module.params['working_dir'])])
command = module.params['command']
# Prepare options option_params = {
if module.params['prefer_source']: 'prefer_source': 'prefer-source',
options.append('--prefer-source') 'prefer_dist': 'prefer-dist',
if module.params['prefer_dist']: 'no_dev': 'no-dev',
options.append('--prefer-dist') 'no_scripts': 'no-scripts',
if module.params['no_dev']: 'no_plugins': 'no_plugins',
options.append('--no-dev') 'optimize_autoloader': 'optimize-autoloader',
if module.params['no_scripts']: 'ignore_platform_reqs': 'ignore-platform-reqs',
options.append('--no-scripts') }
if module.params['no_plugins']:
options.append('--no-plugins') for param, option in option_params.iteritems():
if module.params['optimize_autoloader']: if module.params.get(param) and option in available_options:
options.append('--optimize-autoloader') option = "--%s" % option
if module.params['ignore_platform_reqs']: options.append(option)
options.append('--ignore-platform-reqs')
if module.check_mode: if module.check_mode:
options.append('--dry-run') options.append('--dry-run')
rc, out, err = composer_install(module, command, options) rc, out, err = composer_command(module, command, options)
if rc != 0: if rc != 0:
output = parse_out(err) output = parse_out(err)
@ -180,5 +200,5 @@ def main():
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
if __name__ == '__main__':
main() main()

Loading…
Cancel
Save