allow specify arguments for composer

reviewable/pr18780/r1
Ramunas Dronga 9 years ago
parent eeeb1264d3
commit 92dc7cd65c

@ -36,6 +36,11 @@ options:
- Composer command like "install", "update" and so on - Composer command like "install", "update" and so on
required: false required: false
default: install default: install
arguments:
description:
- Composer arguments like required package, version and so on
required: false
default: null
working_dir: working_dir:
description: description:
- Directory of your project ( see --working-dir ) - Directory of your project ( see --working-dir )
@ -102,6 +107,17 @@ notes:
EXAMPLES = ''' EXAMPLES = '''
# Downloads and installs all the libs and dependencies outlined in the /path/to/project/composer.lock # Downloads and installs all the libs and dependencies outlined in the /path/to/project/composer.lock
- composer: command=install working_dir=/path/to/project - composer: command=install working_dir=/path/to/project
- composer:
command: "require my/package"
working_dir: "/path/to/project"
# Clone project and install with all dependencies
- composer:
command: "create-project"
arguments: "package/package /path/to/project ~1.0"
working_dir: "/path/to/project"
prefer_dist: "yes"
''' '''
import os import os
@ -116,6 +132,8 @@ 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 string == "":
return False
return "Nothing to install or update" not in string return "Nothing to install or update" not in string
def get_available_options(module, command='install'): def get_available_options(module, command='install'):
@ -128,16 +146,17 @@ def get_available_options(module, command='install'):
command_help_json = json.loads(out) command_help_json = json.loads(out)
return command_help_json['definition']['options'] return command_help_json['definition']['options']
def composer_command(module, command, options=[]): def composer_command(module, command, arguments = "", 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 %s" % (php_path, composer_path, command, " ".join(options), arguments)
return module.run_command(cmd) return module.run_command(cmd)
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
command = dict(default="install", type="str", required=False), command = dict(default="install", type="str", required=False),
arguments = dict(default="", type="str", required=False),
working_dir = dict(aliases=["working-dir"], required=True), working_dir = dict(aliases=["working-dir"], required=True),
prefer_source = dict(default="no", type="bool", aliases=["prefer-source"]), prefer_source = dict(default="no", type="bool", aliases=["prefer-source"]),
prefer_dist = dict(default="no", type="bool", aliases=["prefer-dist"]), prefer_dist = dict(default="no", type="bool", aliases=["prefer-dist"]),
@ -152,6 +171,7 @@ def main():
# Get composer command with fallback to default # Get composer command with fallback to default
command = module.params['command'] command = module.params['command']
arguments = module.params['arguments']
available_options = get_available_options(module=module, command=command) available_options = get_available_options(module=module, command=command)
options = [] options = []
@ -188,7 +208,7 @@ def main():
if module.check_mode: if module.check_mode:
options.append('--dry-run') options.append('--dry-run')
rc, out, err = composer_command(module, command, options) rc, out, err = composer_command(module, command, arguments, options)
if rc != 0: if rc != 0:
output = parse_out(err) output = parse_out(err)

Loading…
Cancel
Save