get-php-versions.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/bin/bash
  2. ###################################################
  3. # Usage: get-php-versions.sh [--skip-download]
  4. ###################################################
  5. # This file takes the official latest PHP releases from php.net merges them with our
  6. # "base php configuration". These files get merged into a final file called "php-versions.yml"
  7. # which is used to build our GitHub Actions jobs.
  8. #
  9. # 👉 REQUIRED FILES
  10. # - BASE_PHP_VERSIONS_CONFIG_FILE must be valid and set to a valid file path
  11. # (defaults to scripts/conf/php-versions-base-config.yml)
  12. set -oue pipefail
  13. # Uncomment below for step-by-step execution
  14. # set -x
  15. # trap read DEBUG
  16. ##########################
  17. # Argument Parsing
  18. SKIP_DOWNLOAD="${SKIP_DOWNLOAD:-false}"
  19. while [[ "$#" -gt 0 ]]; do
  20. case $1 in
  21. --skip-download) SKIP_DOWNLOAD=true ;;
  22. *) echo "Unknown parameter passed: $1"; exit 1 ;;
  23. esac
  24. shift
  25. done
  26. ##########################
  27. # Environment Settings
  28. # Script variables
  29. SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
  30. # PHP Versions JSON feed URL
  31. PHP_VERSIONS_ACTIVE_JSON_FEED="${PHP_VERSIONS_ACTIVE_JSON_FEED:-"https://www.php.net/releases/active.php"}"
  32. # File settings
  33. BASE_PHP_VERSIONS_CONFIG_FILE="${BASE_PHP_VERSIONS_CONFIG_FILE:-"$SCRIPT_DIR/conf/php-versions-base-config.yml"}"
  34. DOWNLOADED_PHP_VERSIONS_CONFIG_FILE="$SCRIPT_DIR/conf/php-versions-downloaded.yml.tmp"
  35. FINAL_PHP_VERSIONS_CONFIG_FILE="$SCRIPT_DIR/conf/php-versions.yml"
  36. # UI Colors
  37. function ui_set_yellow {
  38. printf $'\033[0;33m'
  39. }
  40. function ui_set_green {
  41. printf $'\033[0;32m'
  42. }
  43. function ui_set_red {
  44. printf $'\033[0;31m'
  45. }
  46. function ui_reset_colors {
  47. printf "\e[0m"
  48. }
  49. function echo_color_message (){
  50. color=$1
  51. message=$2
  52. ui_set_$color
  53. echo "$message"
  54. ui_reset_colors
  55. }
  56. ##########################
  57. # Main script starts here
  58. if [ "$SKIP_DOWNLOAD" = false ]; then
  59. echo_color_message yellow "⚡️ Getting PHP Versions from $PHP_VERSIONS_ACTIVE_JSON_FEED"
  60. # Fetch the JSON from the PHP website
  61. php_net_version_json=$(curl -s $PHP_VERSIONS_ACTIVE_JSON_FEED)
  62. # Parse the fetched JSON data and transform it to a specific YAML structure using jq and yq.
  63. php_net_yaml_data=$(echo "$php_net_version_json" | jq -r "
  64. {
  65. \"php_versions\": [
  66. . as \$major |
  67. to_entries[] |
  68. {
  69. \"major\": .key,
  70. \"minor_versions\": [
  71. .value |
  72. to_entries[] |
  73. {
  74. \"minor\": .key,
  75. \"patch_versions\": (if .value.version | type == \"null\" then [] elif .value.version | type == \"array\" then .value.version else [.value.version] end)
  76. }
  77. ]
  78. }
  79. ]
  80. }" | yq eval -P -)
  81. # Save the YAML data in our data standard to a file
  82. echo "$php_net_yaml_data" > "$DOWNLOADED_PHP_VERSIONS_CONFIG_FILE"
  83. # Convert YAML to JSON
  84. downloaded_and_normalized_json_data=$(yq eval -o=json "$DOWNLOADED_PHP_VERSIONS_CONFIG_FILE")
  85. base_json_data=$(yq eval -o=json "$BASE_PHP_VERSIONS_CONFIG_FILE")
  86. echo_color_message yellow "⚡️ Combining data from $BASE_PHP_VERSIONS_CONFIG_FILE..."
  87. # Use 'echo' to pass the JSON data to 'jq'
  88. merged_json=$(jq -s '
  89. {
  90. php_versions: (
  91. .[0].php_versions + .[1].php_versions
  92. | group_by(.major)
  93. | map({
  94. major: .[0].major,
  95. minor_versions: (
  96. map(.minor_versions[] | select(. != null))
  97. | group_by(.minor)
  98. | map({
  99. minor: .[0].minor,
  100. base_os: (map(.base_os // []) | add),
  101. patch_versions: (map(.patch_versions // []) | flatten | unique | select(. != null))
  102. })
  103. )
  104. })
  105. ),
  106. php_variations: (. | map(.php_variations // []) | add)
  107. }
  108. ' <(echo "$downloaded_and_normalized_json_data") <(echo "$base_json_data"))
  109. # Convert updated JSON data back to YAML
  110. merged_and_finalized_yaml=$(echo "$merged_json" | yq eval -P -)
  111. # Save the merged YAML data back to the file
  112. echo "$merged_and_finalized_yaml" > "$FINAL_PHP_VERSIONS_CONFIG_FILE"
  113. rm "$DOWNLOADED_PHP_VERSIONS_CONFIG_FILE"
  114. echo_color_message green "✅ Data is finalized compiled into $FINAL_PHP_VERSIONS_CONFIG_FILE"
  115. else
  116. echo_color_message yellow "⚡️ Skipping download of PHP versions because \"--skip-download\" was set..."
  117. cp "$BASE_PHP_VERSIONS_CONFIG_FILE" "$FINAL_PHP_VERSIONS_CONFIG_FILE"
  118. fi
  119. cat $FINAL_PHP_VERSIONS_CONFIG_FILE
  120. echo_color_message green "✅ Saved PHP versions to $FINAL_PHP_VERSIONS_CONFIG_FILE"