build-support/php: DRY improvements (#419094)

authored by Pol Dellaiera and committed by GitHub 6ab71ddb b32441ec

+177 -179
+37 -34
pkgs/build-support/php/builders/v2/hooks/composer-install-hook.sh
··· 1 - declare -g version 1 + # shellcheck shell=bash 2 + 3 + declare -g out 2 4 declare -g pname 3 5 declare -g composerVendor 4 6 declare -g -i composerStrictValidation="${composerStrictValidation:-0}" ··· 8 10 preCheckHooks+=(composerInstallCheckHook) 9 11 preInstallHooks+=(composerInstallInstallHook) 10 12 13 + # shellcheck source=/dev/null 11 14 source @phpScriptUtils@ 12 15 13 16 composerInstallConfigureHook() { 14 - echo "Executing composerInstallConfigureHook" 17 + echo "Executing composerInstallConfigureHook" 15 18 16 - setComposerRootVersion 19 + setComposerRootVersion 17 20 18 - if [[ ! -e "${composerVendor}" ]]; then 19 - echo "No local composer vendor found." >&2 20 - exit 1 21 - fi 21 + if [[ ! -e "${composerVendor}" ]]; then 22 + echo "No local composer vendor found." >&2 23 + exit 1 24 + fi 22 25 23 - install -Dm644 ${composerVendor}/composer.json . 26 + install -Dm644 "${composerVendor}"/composer.json . 24 27 25 - if [[ -f "${composerVendor}/composer.lock" ]]; then 26 - install -Dm644 ${composerVendor}/composer.lock . 27 - fi 28 + if [[ -f "${composerVendor}/composer.lock" ]]; then 29 + install -Dm644 "${composerVendor}"/composer.lock . 30 + fi 28 31 29 - if [[ -f "composer.lock" ]]; then 30 - chmod +w composer.lock 31 - fi 32 + if [[ -f "composer.lock" ]]; then 33 + chmod +w composer.lock 34 + fi 32 35 33 - chmod +w composer.json 36 + chmod +w composer.json 34 37 35 - echo "Finished composerInstallConfigureHook" 38 + echo "Finished composerInstallConfigureHook" 36 39 } 37 40 38 41 composerInstallBuildHook() { 39 - echo "Executing composerInstallBuildHook" 42 + echo "Executing composerInstallBuildHook" 40 43 41 - echo "Finished composerInstallBuildHook" 44 + echo "Finished composerInstallBuildHook" 42 45 } 43 46 44 47 composerInstallCheckHook() { 45 - echo "Executing composerInstallCheckHook" 48 + echo "Executing composerInstallCheckHook" 46 49 47 - checkComposerValidate 50 + checkComposerValidate 48 51 49 - echo "Finished composerInstallCheckHook" 52 + echo "Finished composerInstallCheckHook" 50 53 } 51 54 52 55 composerInstallInstallHook() { 53 - echo "Executing composerInstallInstallHook" 56 + echo "Executing composerInstallInstallHook" 54 57 55 - cp -ar ${composerVendor}/* . 58 + cp -ar "${composerVendor}"/* . 56 59 57 - # Copy the relevant files only in the store. 58 - mkdir -p "$out"/share/php/"${pname}" 59 - cp -r . "$out"/share/php/"${pname}"/ 60 + # Copy the relevant files only in the store. 61 + mkdir -p "$out"/share/php/"${pname}" 62 + cp -r . "$out"/share/php/"${pname}"/ 60 63 61 - # Create symlinks for the binaries. 62 - mapfile -t BINS < <(jq -r -c 'try (.bin[] | select(test(".bat$")? | not) )' composer.json) 63 - for bin in "${BINS[@]}"; do 64 - echo -e "\e[32mCreating symlink ${bin}...\e[0m" 65 - mkdir -p "$out/bin" 66 - ln -s "$out/share/php/${pname}/${bin}" "$out/bin/$(basename "$bin")" 67 - done 64 + # Create symlinks for the binaries. 65 + mapfile -t BINS < <(jq -r -c 'try (.bin[] | select(test(".bat$")? | not) )' composer.json) 66 + for bin in "${BINS[@]}"; do 67 + echo -e "\e[32mCreating symlink ${bin}...\e[0m" 68 + mkdir -p "$out/bin" 69 + ln -s "$out/share/php/${pname}/${bin}" "$out/bin/$(basename "$bin")" 70 + done 68 71 69 - echo "Finished composerInstallInstallHook" 72 + echo "Finished composerInstallInstallHook" 70 73 }
+82 -77
pkgs/build-support/php/builders/v2/hooks/composer-vendor-hook.sh
··· 1 + # shellcheck shell=bash 2 + 3 + # shellcheck source=/dev/null 1 4 source @phpScriptUtils@ 2 5 6 + declare -g out 7 + declare -g composerLock 3 8 declare -g composerNoDev="${composerNoDev:+--no-dev}" 4 9 declare -g composerNoPlugins="${composerNoPlugins:+--no-plugins}" 5 10 declare -g composerNoScripts="${composerNoScripts:+--no-scripts}" ··· 10 15 preInstallHooks+=(composerVendorInstallHook) 11 16 12 17 composerVendorConfigureHook() { 13 - echo "Executing composerVendorConfigureHook" 18 + echo "Executing composerVendorConfigureHook" 14 19 15 - setComposerRootVersion 20 + setComposerRootVersion 16 21 17 - if [[ -f "composer.lock" ]]; then 18 - echo -e "\e[32mUsing \`composer.lock\` file from the source package\e[0m" 19 - fi 22 + if [[ -f "composer.lock" ]]; then 23 + echo -e "\e[32mUsing \`composer.lock\` file from the source package\e[0m" 24 + fi 20 25 21 - if [[ -e "$composerLock" ]]; then 22 - echo -e "\e[32mUsing user provided \`composer.lock\` file from \`$composerLock\`\e[0m" 23 - install -Dm644 $composerLock ./composer.lock 24 - fi 26 + if [[ -e "$composerLock" ]]; then 27 + echo -e "\e[32mUsing user provided \`composer.lock\` file from \`$composerLock\`\e[0m" 28 + install -Dm644 "$composerLock" ./composer.lock 29 + fi 25 30 26 - if [[ ! -f "composer.lock" ]]; then 27 - composer \ 28 - --no-cache \ 29 - --no-install \ 30 - --no-interaction \ 31 - --no-progress \ 32 - --optimize-autoloader \ 33 - ${composerNoDev} \ 34 - ${composerNoPlugins} \ 35 - ${composerNoScripts} \ 36 - update 31 + if [[ ! -f "composer.lock" ]]; then 32 + composer \ 33 + --no-cache \ 34 + --no-install \ 35 + --no-interaction \ 36 + --no-progress \ 37 + --optimize-autoloader \ 38 + "${composerNoDev}" \ 39 + "${composerNoPlugins}" \ 40 + "${composerNoScripts}" \ 41 + update 37 42 38 - if [[ -f "composer.lock" ]]; then 39 - install -Dm644 composer.lock -t $out/ 43 + if [[ -f "composer.lock" ]]; then 44 + install -Dm644 composer.lock -t "$out"/ 40 45 41 - echo 42 - echo -e "\e[31mERROR: No composer.lock found\e[0m" 43 - echo 44 - echo -e '\e[31mNo composer.lock file found, consider adding one to your repository to ensure reproducible builds.\e[0m' 45 - echo -e "\e[31mIn the meantime, a composer.lock file has been generated for you in $out/composer.lock\e[0m" 46 - echo 47 - echo -e '\e[31mTo fix the issue:\e[0m' 48 - echo -e "\e[31m1. Copy the composer.lock file from $out/composer.lock to the project's source:\e[0m" 49 - echo -e "\e[31m cp $out/composer.lock <path>\e[0m" 50 - echo -e '\e[31m2. Add the composerLock attribute, pointing to the copied composer.lock file:\e[0m' 51 - echo -e '\e[31m composerLock = ./composer.lock;\e[0m' 52 - echo 46 + echo 47 + echo -e "\e[31mERROR: No composer.lock found\e[0m" 48 + echo 49 + echo -e '\e[31mNo composer.lock file found, consider adding one to your repository to ensure reproducible builds.\e[0m' 50 + echo -e "\e[31mIn the meantime, a composer.lock file has been generated for you in $out/composer.lock\e[0m" 51 + echo 52 + echo -e '\e[31mTo fix the issue:\e[0m' 53 + echo -e "\e[31m1. Copy the composer.lock file from $out/composer.lock to the project's source:\e[0m" 54 + echo -e "\e[31m cp $out/composer.lock <path>\e[0m" 55 + echo -e '\e[31m2. Add the composerLock attribute, pointing to the copied composer.lock file:\e[0m' 56 + echo -e '\e[31m composerLock = ./composer.lock;\e[0m' 57 + echo 53 58 54 - exit 1 55 - fi 59 + exit 1 56 60 fi 61 + fi 57 62 58 - if [[ -f "composer.lock" ]]; then 59 - chmod +w composer.lock 60 - fi 63 + if [[ -f "composer.lock" ]]; then 64 + chmod +w composer.lock 65 + fi 61 66 62 - chmod +w composer.json 67 + chmod +w composer.json 63 68 64 - echo "Finished composerVendorConfigureHook" 69 + echo "Finished composerVendorConfigureHook" 65 70 } 66 71 67 72 composerVendorBuildHook() { 68 - echo "Executing composerVendorBuildHook" 73 + echo "Executing composerVendorBuildHook" 69 74 70 - setComposerEnvVariables 75 + setComposerEnvVariables 71 76 72 - composer \ 73 - --no-cache \ 74 - --no-interaction \ 75 - --no-progress \ 76 - --optimize-autoloader \ 77 - ${composerNoDev} \ 78 - ${composerNoPlugins} \ 79 - ${composerNoScripts} \ 80 - install 77 + composer \ 78 + --no-cache \ 79 + --no-interaction \ 80 + --no-progress \ 81 + --optimize-autoloader \ 82 + "${composerNoDev}" \ 83 + "${composerNoPlugins}" \ 84 + "${composerNoScripts}" \ 85 + install 81 86 82 - echo "Finished composerVendorBuildHook" 87 + echo "Finished composerVendorBuildHook" 83 88 } 84 89 85 90 composerVendorCheckHook() { 86 - echo "Executing composerVendorCheckHook" 91 + echo "Executing composerVendorCheckHook" 87 92 88 - checkComposerValidate 93 + checkComposerValidate 89 94 90 - echo "Finished composerVendorCheckHook" 95 + echo "Finished composerVendorCheckHook" 91 96 } 92 97 93 98 composerVendorInstallHook() { 94 - echo "Executing composerVendorInstallHook" 99 + echo "Executing composerVendorInstallHook" 95 100 96 - mkdir -p $out 101 + mkdir -p "$out" 97 102 98 - cp -ar composer.json $(composer config vendor-dir) $out/ 99 - mapfile -t installer_paths < <(jq -r -c 'try((.extra."installer-paths") | keys[])' composer.json) 103 + cp -ar composer.json "$(composer config vendor-dir)" "$out"/ 104 + mapfile -t installer_paths < <(jq -r -c 'try((.extra."installer-paths") | keys[])' composer.json) 100 105 101 - for installer_path in "${installer_paths[@]}"; do 102 - # Remove everything after {$name} placeholder 103 - installer_path="${installer_path/\{\$name\}*/}"; 104 - out_installer_path="$out/${installer_path/\{\$name\}*/}"; 105 - # Copy the installer path if it exists 106 - if [[ -d "$installer_path" ]]; then 107 - mkdir -p $(dirname "$out_installer_path") 108 - echo -e "\e[32mCopying installer path $installer_path to $out_installer_path\e[0m" 109 - cp -ar "$installer_path" "$out_installer_path" 110 - # Strip out the git repositories 111 - find $out_installer_path -name .git -type d -prune -print -exec rm -rf {} ";" 112 - fi 113 - done 106 + for installer_path in "${installer_paths[@]}"; do 107 + # Remove everything after {$name} placeholder 108 + installer_path="${installer_path/\{\$name\}*/}" 109 + out_installer_path="$out/${installer_path/\{\$name\}*/}" 110 + # Copy the installer path if it exists 111 + if [[ -d "$installer_path" ]]; then 112 + mkdir -p "$(dirname "$out_installer_path")" 113 + echo -e "\e[32mCopying installer path $installer_path to $out_installer_path\e[0m" 114 + cp -ar "$installer_path" "$out_installer_path" 115 + # Strip out the git repositories 116 + find "$out_installer_path" -name .git -type d -prune -print -exec rm -rf {} ";" 117 + fi 118 + done 114 119 115 - if [[ -f "composer.lock" ]]; then 116 - cp -ar composer.lock $out/ 117 - fi 120 + if [[ -f "composer.lock" ]]; then 121 + cp -ar composer.lock "$out"/ 122 + fi 118 123 119 - echo "Finished composerVendorInstallHook" 124 + echo "Finished composerVendorInstallHook" 120 125 }
+58 -68
pkgs/build-support/php/builders/v2/hooks/php-script-utils.bash
··· 1 + # shellcheck shell=bash 2 + 1 3 declare -g version 2 4 declare -g -i composerStrictValidation="${composerStrictValidation:-0}" 3 5 4 6 setComposerRootVersion() { 5 - if [[ -n $version ]]; then 6 - echo -e "\e[32mSetting COMPOSER_ROOT_VERSION to $version\e[0m" 7 - export COMPOSER_ROOT_VERSION="$version" 8 - fi 7 + if [[ -n $version ]]; then 8 + echo -e "\e[32mSetting COMPOSER_ROOT_VERSION to $version\e[0m" 9 + export COMPOSER_ROOT_VERSION="$version" 10 + fi 9 11 } 10 12 11 13 setComposerEnvVariables() { 12 - echo -e "\e[32mSetting some required environment variables for Composer...\e[0m" 13 - export COMPOSER_MIRROR_PATH_REPOS=1 14 - export COMPOSER_HTACCESS_PROTECT=0 15 - export COMPOSER_FUND=0 14 + echo -e "\e[32mSetting some required environment variables for Composer...\e[0m" 15 + export COMPOSER_MIRROR_PATH_REPOS=1 16 + export COMPOSER_HTACCESS_PROTECT=0 17 + export COMPOSER_FUND=0 16 18 } 17 19 18 20 checkComposerValidate() { 19 - command="composer validate --strict --quiet --no-interaction --no-check-all --no-check-lock" 20 - if ! $command; then 21 - if [[ "${composerStrictValidation}" == "1" ]]; then 22 - echo 23 - echo -e "\e[31mERROR: composer files validation failed\e[0m" 24 - echo 25 - echo -e '\e[31mThe validation of the composer.json failed.\e[0m' 26 - echo -e '\e[31mMake sure that the file composer.json is valid.\e[0m' 27 - echo 28 - echo -e '\e[31mTo address the issue efficiently, follow one of these steps:\e[0m' 29 - echo -e '\e[31m 1. File an issue in the project'\''s issue tracker with detailed information, and apply any available remote patches as a temporary solution '\('with fetchpatch'\)'.\e[0m' 30 - echo -e '\e[31m 2. If an immediate fix is needed or if reporting upstream isn'\''t suitable, develop a temporary local patch.\e[0m' 31 - echo 32 - exit 1 33 - else 34 - echo 35 - echo -e "\e[33mWARNING: composer files validation failed\e[0m" 36 - echo 37 - echo -e '\e[33mThe validation of the composer.json failed.\e[0m' 38 - echo -e '\e[33mMake sure that the file composer.json is valid.\e[0m' 39 - echo 40 - echo -e '\e[33mTo address the issue efficiently, follow one of these steps:\e[0m' 41 - echo -e '\e[33m 1. File an issue in the project'\''s issue tracker with detailed information, and apply any available remote patches as a temporary solution with '\('with fetchpatch'\)'.\e[0m' 42 - echo -e '\e[33m 2. If an immediate fix is needed or if reporting upstream isn'\''t suitable, develop a temporary local patch.\e[0m' 43 - echo 44 - echo -e '\e[33mThis check is not blocking, but it is recommended to fix the issue.\e[0m' 45 - echo 46 - fi 21 + command="composer validate --strict --quiet --no-interaction --no-check-all --no-check-lock" 22 + if ! $command; then 23 + echo 24 + echo -e "\e[31mERROR: composer files validation failed\e[0m" 25 + echo 26 + echo -e '\e[31mThe validation of the composer.json failed.\e[0m' 27 + echo -e '\e[31mMake sure that the file composer.json is valid.\e[0m' 28 + echo 29 + echo -e '\e[31mTo address the issue efficiently, follow one of these steps:\e[0m' 30 + echo -e '\e[31m 1. File an issue in the project'\''s issue tracker with detailed information, and apply any available remote patches as a temporary solution '\('with fetchpatch'\)'.\e[0m' 31 + echo -e '\e[31m 2. If an immediate fix is needed or if reporting upstream isn'\''t suitable, develop a temporary local patch.\e[0m' 32 + echo 33 + 34 + if [[ "${composerStrictValidation}" == "1" ]]; then 35 + echo 36 + echo -e '\e[33mThis check is blocking, set the attribute composerStrictValidation to false to make it not blocking.\e[0m' 37 + echo 38 + exit 1 39 + else 40 + echo 41 + echo -e '\e[33mThis check is not blocking, but it is recommended to fix the issue.\e[0m' 42 + echo 47 43 fi 44 + fi 48 45 49 - command="composer validate --strict --no-ansi --no-interaction --quiet --no-check-all --check-lock" 50 - if ! $command; then 51 - if [[ "${composerStrictValidation}" == "1" ]]; then 52 - echo 53 - echo -e "\e[31mERROR: composer files validation failed\e[0m" 54 - echo 55 - echo -e '\e[31mThe validation of the composer.json and composer.lock failed.\e[0m' 56 - echo -e '\e[31mMake sure that the file composer.lock is consistent with composer.json.\e[0m' 57 - echo 58 - echo -e '\e[31mThis often indicates an issue with the upstream project, which can typically be resolved by reporting the issue to the relevant project maintainers.\e[0m' 59 - echo 60 - echo -e '\e[31mTo address the issue efficiently, follow one of these steps:\e[0m' 61 - echo -e '\e[31m 1. File an issue in the project'\''s issue tracker with detailed information '\('run '\''composer update --lock --no-install'\'' to fix the issue'\)', and apply any available remote patches as a temporary solution with '\('with fetchpatch'\)'.\e[0m' 62 - echo -e '\e[31m 2. If an immediate fix is needed or if reporting upstream isn'\''t suitable, develop a temporary local patch.\e[0m' 63 - echo 64 - exit 1 65 - else 66 - echo 67 - echo -e "\e[33mWARNING: composer files validation failed\e[0m" 68 - echo 69 - echo -e '\e[33mThe validation of the composer.json and composer.lock failed.\e[0m' 70 - echo -e '\e[33mMake sure that the file composer.lock is consistent with composer.json.\e[0m' 71 - echo 72 - echo -e '\e[33mThis often indicates an issue with the upstream project, which can typically be resolved by reporting the issue to the relevant project maintainers.\e[0m' 73 - echo 74 - echo -e '\e[33mTo address the issue efficiently, follow one of these steps:\e[0m' 75 - echo -e '\e[33m 1. File an issue in the project'\''s issue tracker with detailed information '\('run '\''composer update --lock --no-install'\'' to fix the issue'\)', and apply any available remote patches as a temporary solution with '\('with fetchpatch'\)'.\e[0m' 76 - echo -e '\e[33m 2. If an immediate fix is needed or if reporting upstream isn'\''t suitable, develop a temporary local patch.\e[0m' 77 - echo 78 - echo -e '\e[33mThis check is not blocking, but it is recommended to fix the issue.\e[0m' 79 - echo 80 - fi 46 + command="composer validate --strict --no-ansi --no-interaction --quiet --no-check-all --check-lock" 47 + if ! $command; then 48 + echo 49 + echo -e "\e[31mERROR: composer files validation failed\e[0m" 50 + echo 51 + echo -e '\e[31mThe validation of the composer.json and composer.lock failed.\e[0m' 52 + echo -e '\e[31mMake sure that the file composer.lock is consistent with composer.json.\e[0m' 53 + echo 54 + echo -e '\e[31mThis often indicates an issue with the upstream project, which can typically be resolved by reporting the issue to the relevant project maintainers.\e[0m' 55 + echo 56 + echo -e '\e[31mTo address the issue efficiently, follow one of these steps:\e[0m' 57 + echo -e '\e[31m 1. File an issue in the project'\''s issue tracker with detailed information '\('run '\''composer update --lock --no-install'\'' to fix the issue'\)', and apply any available remote patches as a temporary solution with '\('with fetchpatch'\)'.\e[0m' 58 + echo -e '\e[31m 2. If an immediate fix is needed or if reporting upstream isn'\''t suitable, develop a temporary local patch.\e[0m' 59 + echo 60 + 61 + if [[ "${composerStrictValidation}" == "1" ]]; then 62 + echo 63 + echo -e '\e[33mThis check is blocking, set the attribute composerStrictValidation to false to make it not blocking.\e[0m' 64 + echo 65 + exit 1 66 + else 67 + echo 68 + echo -e '\e[33mThis check is not blocking, but it is recommended to fix the issue.\e[0m' 69 + echo 81 70 fi 71 + fi 82 72 }