diff --git a/script/install.sh b/script/install.sh index b4e8affb..078e11a1 100755 --- a/script/install.sh +++ b/script/install.sh @@ -124,17 +124,19 @@ check_tools() { # ------------------------------------------------------------------------------ -mkdir -p "$ROOT" -TMP_ROOT=$(mktemp -d "$ROOT/.installer_tmp_XXXXXX") +init() { + mkdir -p "$ROOT" + TMP_ROOT=$(mktemp -d "$ROOT/.installer_tmp_XXXXXX") -mkdtemp() { - mktemp -d "$TMP_ROOT/XXXXXX" -} + mkdtemp() { + mktemp -d "$TMP_ROOT/XXXXXX" + } -cleanup() { - rm -rf "$TMP_ROOT"; + cleanup() { + rm -rf "$TMP_ROOT"; + } + trap cleanup EXIT } -trap cleanup EXIT # ------------------------------------------------------------------------------ @@ -155,12 +157,31 @@ Options -h --help Show this help -p --path Install location (default ~/dlang) -v --verbose Verbose output + --config List all available configuration parameters Run "install.sh --help to get help for a specific command. If no argument are provided, the latest DMD compiler will be installed. ' } +command_config() { + log 'Configuration + +~/.config/dlang/installer.cfg can be used to store permanent configuration settings. +Alternatively a global configuration file can be used (/etc/dlang/installer.cfg). +The parameters are listed in line-wise a = format. +Lines starting with # are ignored. + +Example: + +ROOT=~/.dlang + +Available options: + + ROOT Install location (default ~/dlang) +' +} + command_help() { if [ -z "${1-}" ]; then usage @@ -283,6 +304,11 @@ parse_args() { COMPILER=$1 ;; + --config) + command_config + exit 0 + ;; + *) usage fatal "Unrecognized command-line parameter: $1" @@ -685,9 +711,48 @@ install_dub() { ln -s "$dub" "$ROOT/dub" } +# ------------------------------------------------------------------------------ +# Read user configuration +# ------------------------------------------------------------------------------ + +local config_file config_files +config_files=( + "${XDG_CONFIG_HOME:-$HOME/.config/}/dlang/installer.cfg" \ + "${XDG_CONFIG_DIRS:-/etc}/dlang/installer.cfg" \ + "$HOME/Library/Application Support/dlang/installer.cfg" \ +) +config_file="" +for file in "${config_files[@]}" ; do + if [ -e "${file}" ] ; then + config_file="${file}" + fi +done +if [ -n "${config_file}" ] ; then + # Use a whitelist of allowed parameters + allowedParams=("ROOT") + containsValue() { + local e value="$1" + shift + for e; do [[ "${e}" == "${value}" ]] && return 0; done + return 1 + } + IFS="=" + # Ignore empty lines or lines with comments + grep -vE "^(#|$)" "${config_file}" | while read -r param value ; do + if containsValue "${param}" "${allowedParams[@]}" ; then + log "${param}: ${value//\"/}" + declare -a "${param}=${value//\"/}" + else + log "${param} is invalid. Please check." + fi + done + ROOT="${ROOT/#\~/$HOME}" +fi + # ------------------------------------------------------------------------------ parse_args "$@" +init resolve_latest "$COMPILER" run_command ${COMMAND:-install} "$COMPILER" } diff --git a/travis.sh b/travis.sh index 8216d3d5..093878f3 100755 --- a/travis.sh +++ b/travis.sh @@ -91,3 +91,19 @@ git checkout -- script/install.sh if [ "${TRAVIS_OS_NAME:-}" != "osx" ]; then shellcheck script/install.sh fi + +# Test user config +mkdir -p ~/.config/dlang +echo "ROOT=~/.dlang" > ~/.config/dlang/installer.cfg +./script/install.sh dmd +has_user_installed_dmd=0 +for file in ~/.dlang/dmd* ; do + if [ -e "$file" ] ; then + has_user_installed_dmd=1 + break + fi +done +if [ ${has_user_installed_dmd} -eq 0 ] ; then + echo "Failed to install DMD in the user-specified directory." +fi +rm -rf ~/.dlang ~/.config