From 496e6a4d699a1d5b5e1a3d410590a699d4a1ae29 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 12 Aug 2026 21:35:06 +0800 Subject: [PATCH] feat(build): add bash completion support for TypePHP compiler - Include completions/tpc.bash in package distribution - Create completions directory during build process - Copy bash completion file to target directory - Add bash completion script with comprehensive command options - Implement file and directory completion for various parameters - Support parameter-specific value completion for optimization levels - Add support for PHP version, C++ standard, and WASM target completion - Enable filename completion for input/output files and directories --- completions/tpc.bash | 151 +++++++++++++++++++++++++++++++++++++++++++ package.php | 4 ++ 2 files changed, 155 insertions(+) create mode 100644 completions/tpc.bash diff --git a/completions/tpc.bash b/completions/tpc.bash new file mode 100644 index 00000000..f6ad74ca --- /dev/null +++ b/completions/tpc.bash @@ -0,0 +1,151 @@ +# Bash completion for the TypePHP compiler. +# Generated by: tpc --generate-completion=bash + +_typephp_tpc_complete_files() +{ + local candidate + COMPREPLY=() + while IFS= read -r candidate; do + if [[ -d "$candidate" || "$candidate" == *.php || "$candidate" == *.yml || "$candidate" == *.yaml || "$candidate" == *.prof ]]; then + COMPREPLY+=("$candidate") + fi + done < <(compgen -f -- "$1") +} + +_typephp_tpc_complete_python_files() +{ + local candidate + COMPREPLY=() + while IFS= read -r candidate; do + if [[ -d "$candidate" || "$candidate" == *.py ]]; then + COMPREPLY+=("$candidate") + fi + done < <(compgen -f -- "$1") +} + +_typephp_tpc_complete_paths() +{ + local mode="$1" candidate + COMPREPLY=() + shift + while IFS= read -r candidate; do + COMPREPLY+=("$candidate") + done < <(compgen "$mode" -- "$1") +} + +_typephp_tpc() +{ + local current previous value candidate + current="${COMP_WORDS[COMP_CWORD]}" + previous="" + if (( COMP_CWORD > 0 )); then + previous="${COMP_WORDS[COMP_CWORD - 1]}" + fi + + local index + for ((index = 1; index < COMP_CWORD; index++)); do + if [[ "${COMP_WORDS[index]}" == -- ]]; then + compopt -o default + return + fi + done + + case "$previous" in + -O) + COMPREPLY=( $(compgen -W '0 1 2 3' -- "$current") ) + return + ;; + --optimize) + COMPREPLY=( $(compgen -W '0 1 2 3' -- "$current") ) + return + ;; + -m) + COMPREPLY=( $(compgen -W 'bin lib ext' -- "$current") ) + return + ;; + --mode) + COMPREPLY=( $(compgen -W 'bin lib ext' -- "$current") ) + return + ;; + --php-version) + COMPREPLY=( $(compgen -W '8.2 8.3 8.4 8.5' -- "$current") ) + return + ;; + --cxx-std) + COMPREPLY=( $(compgen -W 'c++17 c++20 c++23' -- "$current") ) + return + ;; + --sanitize) + COMPREPLY=( $(compgen -W 'address undefined' -- "$current") ) + return + ;; + --build-dir|--output-dir|-I|--include-path|-L|--link-path) + compopt -o filenames + _typephp_tpc_complete_paths -d "$current" + return + ;; + --convert-python-to-php) + compopt -o filenames + _typephp_tpc_complete_python_files "$current" + return + ;; + -o|--output) + compopt -o filenames + _typephp_tpc_complete_paths -f "$current" + return + ;; + esac + + case "$current" in + -O[0-3]) + COMPREPLY=("$current") + return + ;; + -O*) + value="${current#-O}" + COMPREPLY=( $(compgen -W '0 1 2 3' -P '-O' -- "$value") ) + return + ;; + --wasm=*) + value="${current#--wasm=}" + COMPREPLY=( $(compgen -W 'component browser' -P '--wasm=' -- "$value") ) + return + ;; + --generate-completion=*) + value="${current#--generate-completion=}" + COMPREPLY=( $(compgen -W 'bash' -P '--generate-completion=' -- "$value") ) + return + ;; + --build-dir=*) + value="${current#--build-dir=}" + compopt -o filenames + COMPREPLY=() + while IFS= read -r candidate; do + COMPREPLY+=("--build-dir=${candidate}") + done < <(compgen -d -- "$value") + return + ;; + --output-dir=*) + value="${current#--output-dir=}" + compopt -o filenames + COMPREPLY=() + while IFS= read -r candidate; do + COMPREPLY+=("--output-dir=${candidate}") + done < <(compgen -d -- "$value") + return + ;; + -* ) + COMPREPLY=( $(compgen -W '-O --optimize -o --output -h --help -v --version --profile --no-literal-strings --php-version -f --force -m --mode -r --run --debug -j --job --no-console --sanitize --cxx-std --march --target-platform --no-color --build-dir --dry -I --include-path -D --define --no-progress --lto --format -l --link-lib -L --link-path --wasm --wasm= --gen-python-helper --convert-python-to-php --output-dir --output-dir= --build-dir= --generate-completion=' -- "$current") ) + return + ;; + esac + + compopt -o filenames + _typephp_tpc_complete_files "$current" +} + +complete -F _typephp_tpc tpc +complete -F _typephp_tpc ./tpc +complete -F _typephp_tpc tpc.php +complete -F _typephp_tpc bin/tpc.php +complete -F _typephp_tpc ./bin/tpc.php diff --git a/package.php b/package.php index c6626d41..7f58f22a 100755 --- a/package.php +++ b/package.php @@ -608,6 +608,7 @@ function packageUnixLike(): void 'README.md', 'LICENSE.md', 'examples/hello.php', + 'completions/tpc.bash', 'wasm/build-program.sh', "{$wasiSdkSourceRoot}/.typephp-wasi-sdk-abi", "{$wasiSdkSourceRoot}/include/php/main/php.h", @@ -703,6 +704,8 @@ function packageUnixLike(): void mustCopy('LICENSE.md', "{$topLevelDir}/LICENSE.md"); mustCreateDirectory("{$topLevelDir}/examples"); mustCopy('examples/hello.php', "{$topLevelDir}/examples/hello.php"); + mustCreateDirectory("{$topLevelDir}/completions"); + mustCopy('completions/tpc.bash', "{$topLevelDir}/completions/tpc.bash"); copyDirectory('wasm', "{$topLevelDir}/wasm"); copyDirectory("{$phpxSourceDir}/wasm", "{$topLevelDir}/vendor/swoole/phpx/wasm"); @@ -740,6 +743,7 @@ function packageUnixLike(): void "{$topLevelDir}/README.md", "{$topLevelDir}/LICENSE.md", "{$topLevelDir}/examples/hello.php", + "{$topLevelDir}/completions/tpc.bash", "{$topLevelDir}/wasm/build-program.sh", "{$topLevelDir}/{$wasiSdkPackageRoot}/.typephp-wasi-sdk-abi", "{$topLevelDir}/{$wasiSdkPackageRoot}/include/php/main/php.h",