mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Added Composer Command global (#24158)
* Added Composer Command global Added a parameter to run composer commands globally. The `working_dir` parameter is only required if `global_command` is `False`. Fixes #24052 * Added Composer Command global Added a parameter to run composer commands globally. The `working_dir` parameter is only required if `global_command` is `False`. Fixes #24052
This commit is contained in:
parent
033fe5548b
commit
77e550192c
1 changed files with 29 additions and 7 deletions
|
@ -52,10 +52,20 @@ options:
|
||||||
default: null
|
default: null
|
||||||
working_dir:
|
working_dir:
|
||||||
description:
|
description:
|
||||||
- Directory of your project (see --working-dir).
|
- Directory of your project (see --working-dir). This is required when
|
||||||
required: true
|
the command is not run globally.
|
||||||
|
- Will be ignored if C(global_command=true).
|
||||||
|
required: false
|
||||||
default: null
|
default: null
|
||||||
aliases: [ "working-dir" ]
|
aliases: [ "working-dir" ]
|
||||||
|
global_command:
|
||||||
|
version_added: "2.4"
|
||||||
|
description:
|
||||||
|
- Runs the specified command globally.
|
||||||
|
required: false
|
||||||
|
choices: [ true, false]
|
||||||
|
default: false
|
||||||
|
aliases: [ "global-command" ]
|
||||||
prefer_source:
|
prefer_source:
|
||||||
description:
|
description:
|
||||||
- Forces installation from package sources when possible (see --prefer-source).
|
- Forces installation from package sources when possible (see --prefer-source).
|
||||||
|
@ -133,6 +143,12 @@ EXAMPLES = '''
|
||||||
arguments: package/package /path/to/project ~1.0
|
arguments: package/package /path/to/project ~1.0
|
||||||
working_dir: /path/to/project
|
working_dir: /path/to/project
|
||||||
prefer_dist: yes
|
prefer_dist: yes
|
||||||
|
|
||||||
|
# Installs package globally
|
||||||
|
- composer:
|
||||||
|
command: require
|
||||||
|
global_command: yes
|
||||||
|
arguments: my/package
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
@ -158,10 +174,12 @@ def get_available_options(module, command='install'):
|
||||||
return command_help_json['definition']['options']
|
return command_help_json['definition']['options']
|
||||||
|
|
||||||
|
|
||||||
def composer_command(module, command, arguments="", options=[]):
|
def composer_command(module, command, arguments="", options=None, global_command=False):
|
||||||
|
if options is None:
|
||||||
|
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 %s" % (php_path, composer_path, command, " ".join(options), arguments)
|
cmd = "%s %s %s %s %s %s" % (php_path, composer_path, "global" if global_command else "", command, " ".join(options), arguments)
|
||||||
return module.run_command(cmd)
|
return module.run_command(cmd)
|
||||||
|
|
||||||
|
|
||||||
|
@ -170,7 +188,8 @@ def main():
|
||||||
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),
|
arguments=dict(default="", type="str", required=False),
|
||||||
working_dir=dict(type="path", aliases=["working-dir"], required=True),
|
working_dir=dict(type="path", aliases=["working-dir"]),
|
||||||
|
global_command=dict(default=False, type="bool", aliases=["global-command"]),
|
||||||
prefer_source=dict(default=False, type="bool", aliases=["prefer-source"]),
|
prefer_source=dict(default=False, type="bool", aliases=["prefer-source"]),
|
||||||
prefer_dist=dict(default=False, type="bool", aliases=["prefer-dist"]),
|
prefer_dist=dict(default=False, type="bool", aliases=["prefer-dist"]),
|
||||||
no_dev=dict(default=True, type="bool", aliases=["no-dev"]),
|
no_dev=dict(default=True, type="bool", aliases=["no-dev"]),
|
||||||
|
@ -179,6 +198,7 @@ def main():
|
||||||
optimize_autoloader=dict(default=True, type="bool", aliases=["optimize-autoloader"]),
|
optimize_autoloader=dict(default=True, type="bool", aliases=["optimize-autoloader"]),
|
||||||
ignore_platform_reqs=dict(default=False, type="bool", aliases=["ignore-platform-reqs"]),
|
ignore_platform_reqs=dict(default=False, type="bool", aliases=["ignore-platform-reqs"]),
|
||||||
),
|
),
|
||||||
|
required_if=[('global_command', False, ['working_dir'])],
|
||||||
supports_check_mode=True
|
supports_check_mode=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -188,6 +208,7 @@ def main():
|
||||||
module.fail_json(msg="Use the 'arguments' param for passing arguments with the 'command'")
|
module.fail_json(msg="Use the 'arguments' param for passing arguments with the 'command'")
|
||||||
|
|
||||||
arguments = module.params['arguments']
|
arguments = module.params['arguments']
|
||||||
|
global_command = module.params['global_command']
|
||||||
available_options = get_available_options(module=module, command=command)
|
available_options = get_available_options(module=module, command=command)
|
||||||
|
|
||||||
options = []
|
options = []
|
||||||
|
@ -204,7 +225,8 @@ def main():
|
||||||
option = "--%s" % option
|
option = "--%s" % option
|
||||||
options.append(option)
|
options.append(option)
|
||||||
|
|
||||||
options.extend(['--working-dir', "'%s'" % module.params['working_dir']])
|
if not global_command:
|
||||||
|
options.extend(['--working-dir', "'%s'" % module.params['working_dir']])
|
||||||
|
|
||||||
option_params = {
|
option_params = {
|
||||||
'prefer_source': 'prefer-source',
|
'prefer_source': 'prefer-source',
|
||||||
|
@ -224,7 +246,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, arguments, options)
|
rc, out, err = composer_command(module, command, arguments, options, global_command)
|
||||||
|
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
output = parse_out(err)
|
output = parse_out(err)
|
||||||
|
|
Loading…
Reference in a new issue