|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +REQUESTED_PHP_VERSION=${1:-7.4} |
| 6 | +OUTPUT_PATH=${2:-} |
| 7 | +IGNORED_FILES=${3:-} |
| 8 | +PHPDOC_VERSION=${4:-v2.8.5} |
| 9 | + |
| 10 | +if ! php -r '$version = isset($argv[1]) ? $argv[1] : ""; if (!preg_match("/^(5\\.[4-6]|7\\.[0-4])$/", $version)) { fwrite(STDERR, "Input '\''php_version'\'' must be between 5.4 and 7.4 (inclusive).\n"); exit(1); }' "$REQUESTED_PHP_VERSION"; then |
| 11 | + exit 1 |
| 12 | +fi |
| 13 | + |
| 14 | +RUNTIME_PHP_VERSION=$(php -r 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;') |
| 15 | +if [[ "$REQUESTED_PHP_VERSION" != "$RUNTIME_PHP_VERSION" ]]; then |
| 16 | + echo "Input 'php_version' is '$REQUESTED_PHP_VERSION' but action container uses PHP '$RUNTIME_PHP_VERSION'. Build the action image with matching PHP_VERSION." >&2 |
| 17 | + exit 1 |
| 18 | +fi |
| 19 | + |
| 20 | +if [[ -z "$OUTPUT_PATH" ]]; then |
| 21 | + echo "Input 'output_path' is required." >&2 |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | + |
| 25 | +WORKSPACE_PATH=${GITHUB_WORKSPACE:-/github/workspace} |
| 26 | + |
| 27 | +if [[ ! -d "$WORKSPACE_PATH" ]]; then |
| 28 | + echo "Workspace path does not exist: $WORKSPACE_PATH" >&2 |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | + |
| 32 | +if [[ "$OUTPUT_PATH" = /* ]]; then |
| 33 | + DOCS_PATH=$OUTPUT_PATH |
| 34 | +else |
| 35 | + DOCS_PATH="$WORKSPACE_PATH/$OUTPUT_PATH" |
| 36 | +fi |
| 37 | + |
| 38 | +TMP_ROOT=$(mktemp -d) |
| 39 | +PHPDOC_XML_PATH="$TMP_ROOT/phpdoc-xml" |
| 40 | +PHPDOC_PHAR_PATH="$TMP_ROOT/phpDocumentor.phar" |
| 41 | +PHPDOC_TAR_GZ_PATH="$TMP_ROOT/phpDocumentor.tgz" |
| 42 | +PHPDOC_EXTRACT_PATH="$TMP_ROOT/phpDocumentor" |
| 43 | +PHPDOC_MD_PATH=/opt/phpdoc-md |
| 44 | +PHPDOC_CACHE_PATH=/opt/phpdocumentor-cache |
| 45 | + |
| 46 | +cleanup() { |
| 47 | + rm -rf "$TMP_ROOT" |
| 48 | +} |
| 49 | +trap cleanup EXIT |
| 50 | + |
| 51 | +mkdir -p "$DOCS_PATH" |
| 52 | +mkdir -p "$PHPDOC_XML_PATH" |
| 53 | +mkdir -p "$PHPDOC_EXTRACT_PATH" |
| 54 | + |
| 55 | +download_file() { |
| 56 | + local url=$1 |
| 57 | + local target=$2 |
| 58 | + php -r '$url = isset($argv[1]) ? $argv[1] : null; $target = isset($argv[2]) ? $argv[2] : null; if (!$url || !$target) { fwrite(STDERR, "Missing download arguments\n"); exit(1); } $data = @file_get_contents($url); if ($data === false) { exit(1); } if (@file_put_contents($target, $data) === false) { fwrite(STDERR, "Unable to write file: $target\n"); exit(1); }' "$url" "$target" |
| 59 | +} |
| 60 | + |
| 61 | +extract_tgz() { |
| 62 | + local archive=$1 |
| 63 | + local target=$2 |
| 64 | + php -r '$archive = isset($argv[1]) ? $argv[1] : null; $target = isset($argv[2]) ? $argv[2] : null; if (!$archive || !$target) { fwrite(STDERR, "Missing extract arguments\n"); exit(1); } if (!is_dir($target) && !mkdir($target, 0777, true)) { fwrite(STDERR, "Unable to create extract directory\n"); exit(1); } $tarPath = preg_replace("/\\.tgz$/", ".tar", $archive); if ($tarPath === null) { fwrite(STDERR, "Unable to derive tar path\n"); exit(1); } try { $tgz = new PharData($archive); if (!file_exists($tarPath)) { $tgz->decompress(); } $tar = new PharData($tarPath); $tar->extractTo($target, null, true); } catch (Exception $e) { fwrite(STDERR, "Unable to extract phpDocumentor archive: " . $e->getMessage() . "\n"); exit(1); }' "$archive" "$target" |
| 65 | +} |
| 66 | + |
| 67 | +PHPDOC_COMMAND=() |
| 68 | +PHPDOC_COMMAND_SET=0 |
| 69 | +if [[ "$PHPDOC_VERSION" == "latest" ]]; then |
| 70 | + PHPDOC_PHAR_URL="https://phpdoc.org/phpDocumentor.phar" |
| 71 | + download_file "$PHPDOC_PHAR_URL" "$PHPDOC_PHAR_PATH" |
| 72 | + PHPDOC_COMMAND=(php "$PHPDOC_PHAR_PATH" run) |
| 73 | + PHPDOC_COMMAND_SET=1 |
| 74 | +else |
| 75 | + PHPDOC_VERSION_NO_PREFIX=${PHPDOC_VERSION#v} |
| 76 | + CACHED_PHPDOC_TAR_GZ_PATH="$PHPDOC_CACHE_PATH/phpDocumentor-${PHPDOC_VERSION_NO_PREFIX}.tgz" |
| 77 | + CACHED_PHPDOC_PHAR_PATH="$PHPDOC_CACHE_PATH/phpDocumentor-${PHPDOC_VERSION_NO_PREFIX}.phar" |
| 78 | + PHPDOC_TAR_GZ_URL="https://github.com/phpDocumentor/phpDocumentor/releases/download/v${PHPDOC_VERSION_NO_PREFIX}/phpDocumentor-${PHPDOC_VERSION_NO_PREFIX}.tgz" |
| 79 | + PHPDOC_PHAR_URL="https://github.com/phpDocumentor/phpDocumentor/releases/download/v${PHPDOC_VERSION_NO_PREFIX}/phpDocumentor.phar" |
| 80 | + |
| 81 | + if [[ "$PHPDOC_COMMAND_SET" -eq 0 ]] && [[ -f "$CACHED_PHPDOC_TAR_GZ_PATH" ]]; then |
| 82 | + cp "$CACHED_PHPDOC_TAR_GZ_PATH" "$PHPDOC_TAR_GZ_PATH" |
| 83 | + elif [[ "$PHPDOC_COMMAND_SET" -eq 0 ]] && ! download_file "$PHPDOC_TAR_GZ_URL" "$PHPDOC_TAR_GZ_PATH"; then |
| 84 | + download_file "$PHPDOC_PHAR_URL" "$PHPDOC_PHAR_PATH" |
| 85 | + PHPDOC_COMMAND=(php "$PHPDOC_PHAR_PATH" run) |
| 86 | + PHPDOC_COMMAND_SET=1 |
| 87 | + fi |
| 88 | + |
| 89 | + if [[ "$PHPDOC_COMMAND_SET" -eq 0 ]]; then |
| 90 | + extract_tgz "$PHPDOC_TAR_GZ_PATH" "$PHPDOC_EXTRACT_PATH" |
| 91 | + PHPDOC_BIN_PATH="$PHPDOC_EXTRACT_PATH/phpDocumentor-${PHPDOC_VERSION_NO_PREFIX}/bin/phpdoc" |
| 92 | + if [[ ! -f "$PHPDOC_BIN_PATH" ]]; then |
| 93 | + echo "Unable to find phpDocumentor binary after extraction." >&2 |
| 94 | + exit 1 |
| 95 | + fi |
| 96 | + PHPDOC_COMMAND=(php "$PHPDOC_BIN_PATH") |
| 97 | + PHPDOC_COMMAND_SET=1 |
| 98 | + fi |
| 99 | +fi |
| 100 | + |
| 101 | +declare -a PHPDOC_IGNORE_ARGS |
| 102 | +PHPDOC_IGNORE_ARGS+=("--ignore=vendor/**") |
| 103 | +while IFS= read -r LINE; do |
| 104 | + LINE=${LINE%$'\r'} |
| 105 | + if [[ -n "$LINE" ]]; then |
| 106 | + PHPDOC_IGNORE_ARGS+=("--ignore=$LINE") |
| 107 | + fi |
| 108 | +done <<< "$IGNORED_FILES" |
| 109 | + |
| 110 | +"${PHPDOC_COMMAND[@]}" \ |
| 111 | + --target="$PHPDOC_XML_PATH" \ |
| 112 | + --directory="$WORKSPACE_PATH" \ |
| 113 | + --cache-folder="$TMP_ROOT/cache" \ |
| 114 | + --template=xml \ |
| 115 | + --no-interaction \ |
| 116 | + --ansi \ |
| 117 | + "${PHPDOC_IGNORE_ARGS[@]}" |
| 118 | + |
| 119 | +if [[ ! -f "$PHPDOC_MD_PATH/bin/phpdocmd" ]]; then |
| 120 | + echo "phpdoc-md binary is missing: $PHPDOC_MD_PATH/bin/phpdocmd" >&2 |
| 121 | + exit 1 |
| 122 | +fi |
| 123 | + |
| 124 | +php "$PHPDOC_MD_PATH/bin/phpdocmd" "$PHPDOC_XML_PATH/structure.xml" "$DOCS_PATH" |
0 commit comments