yunohost/helpers/helpers.v2.1.d/composer

59 lines
2.2 KiB
Bash

#!/bin/bash
composer_install_dir="/opt/yunohost/composer"
# Install and initialize Composer in the given directory
#
# The installed version is defined by `$composer_version` which should be defined
# as global prior to calling this helper.
#
# Will use `$install_dir` as workdir unless `$composer_workdir` exists (but that shouldnt be necessary)
#
# usage: ynh_composer_install
ynh_composer_install() {
local workdir="${composer_workdir:-$install_dir}"
[[ -n "${composer_version}" ]] || ynh_die "\$composer_version should be defined before calling ynh_composer_install. (In the past, this was called \$YNH_COMPOSER_VERSION)"
local composer_url="https://getcomposer.org/download/$composer_version/composer.phar"
local composer_phar_path="$composer_install_dir/composer_${composer_version}.phar"
# Remove legacy composer.phar in work directory
if [ -f "$workdir/composer.phar" ]; then
ynh_safe_rm "$workdir/composer.phar"
fi
# Early exit if already downloaded
if [ -f "$composer_phar_path" ]; then
return
fi
mkdir -p "$composer_install_dir"
# NB. we have to declare the var as local first,
# otherwise 'local foo=$(false) || echo 'pwet'" does'nt work
# because local always return 0 ...
local out
# Timeout option is here to enforce the timeout on dns query and tcp connect (c.f. man wget)
out=$(wget --tries 3 --no-dns-cache --timeout 900 --no-verbose --output-document="$composer_phar_path" "$composer_url" 2>&1) \
|| ynh_die "$out"
}
# Execute a command with Composer
#
# Will use `$install_dir` as workdir unless `$composer_workdir` exists (but that shouldnt be necessary)
#
# You may also define `composer_user=root` prior to call this helper if you
# absolutely need composer to run as root, but this is discouraged...
#
# usage: ynh_composer_exec commands
ynh_composer_exec() {
local workdir="${composer_workdir:-$install_dir}"
local composer_phar_path="$composer_install_dir/composer_${composer_version}.phar"
COMPOSER_HOME="$workdir/.composer" \
COMPOSER_MEMORY_LIMIT=-1 \
sudo -E -u "${composer_user:-$app}" \
"php${php_version}" "$composer_phar_path" $@ \
-d "$workdir" --no-interaction --no-ansi 2>&1
}