|
2 | 2 |
|
3 | 3 | # phpvm - A PHP Version Manager for macOS and Linux |
4 | 4 | # Author: Jerome Thayananthajothy (tjthavarshan@gmail.com) |
5 | | -# Version: 1.10.0 |
| 5 | +# Version: 1.12.1 |
6 | 6 | # |
7 | 7 | # IMPORTANT: This script is written for bash and uses bashisms (arrays, process substitution, etc.) |
8 | 8 | # For sourcing into your shell, use bash only. Zsh users should run phpvm via: |
|
11 | 11 |
|
12 | 12 | # shellcheck disable=SC2155 # Allow declare and assign on same line for better readability |
13 | 13 |
|
14 | | -PHPVM_VERSION="1.12.0" |
| 14 | +PHPVM_VERSION="1.12.1" |
15 | 15 |
|
16 | 16 | # Test mode flag |
17 | 17 | PHPVM_TEST_MODE="${PHPVM_TEST_MODE:-false}" |
@@ -2623,6 +2623,7 @@ Usage: |
2623 | 2623 | phpvm alias [name] [ver] Create, update, or list version aliases |
2624 | 2624 | phpvm unalias <name> Remove version alias |
2625 | 2625 | phpvm cache <dir|clear> Manage cache directory |
| 2626 | + phpvm self-update Update phpvm to the latest stable version |
2626 | 2627 | phpvm help Show this help message |
2627 | 2628 | phpvm info Show system information for debugging |
2628 | 2629 | phpvm version Show version information |
@@ -2678,6 +2679,129 @@ Usage: phpvm help |
2678 | 2679 | EOF |
2679 | 2680 | } |
2680 | 2681 |
|
| 2682 | +phpvm_get_self_update_target() { |
| 2683 | + local script_path |
| 2684 | + if [ -n "${PHPVM_SELF_UPDATE_DEST:-}" ]; then |
| 2685 | + script_path="$PHPVM_SELF_UPDATE_DEST" |
| 2686 | + else |
| 2687 | + script_path="${BASH_SOURCE[0]:-$0}" |
| 2688 | + fi |
| 2689 | + |
| 2690 | + if [ -L "$script_path" ] && command_exists readlink; then |
| 2691 | + local link |
| 2692 | + link=$(command readlink "$script_path") |
| 2693 | + if [ "${link#/}" != "$link" ]; then |
| 2694 | + script_path="$link" |
| 2695 | + else |
| 2696 | + local dir |
| 2697 | + dir=$(cd "$(dirname "$script_path")" 2> /dev/null && pwd) |
| 2698 | + script_path="$dir/$link" |
| 2699 | + fi |
| 2700 | + fi |
| 2701 | + |
| 2702 | + printf '%s |
| 2703 | +' "$script_path" |
| 2704 | +} |
| 2705 | + |
| 2706 | +phpvm_download_url_to_file() { |
| 2707 | + local url="$1" |
| 2708 | + local output="$2" |
| 2709 | + |
| 2710 | + if command_exists curl; then |
| 2711 | + command curl --fail --silent --location "$url" > "$output" |
| 2712 | + return "$?" |
| 2713 | + fi |
| 2714 | + |
| 2715 | + if command_exists wget; then |
| 2716 | + command wget --quiet --output-document="$output" "$url" |
| 2717 | + return "$?" |
| 2718 | + fi |
| 2719 | + |
| 2720 | + phpvm_err "curl or wget is required for self-update." |
| 2721 | + return "$PHPVM_EXIT_ERROR" |
| 2722 | +} |
| 2723 | + |
| 2724 | +phpvm_extract_version_from_file() { |
| 2725 | + local file="$1" |
| 2726 | + local version |
| 2727 | + |
| 2728 | + version=$(command grep -Eo 'PHPVM_VERSION="[0-9]+\.[0-9]+\.[0-9]+"' "$file" 2> /dev/null | command head -1 | command sed 's/PHPVM_VERSION="//;s/"$//') |
| 2729 | + if [ -z "$version" ]; then |
| 2730 | + return "$PHPVM_EXIT_ERROR" |
| 2731 | + fi |
| 2732 | + |
| 2733 | + printf '%s |
| 2734 | +' "$version" |
| 2735 | + return "$PHPVM_EXIT_SUCCESS" |
| 2736 | +} |
| 2737 | + |
| 2738 | +phpvm_self_update() { |
| 2739 | + local source_url="${PHPVM_SELF_UPDATE_URL:-https://raw.githubusercontent.com/Thavarshan/phpvm/main/phpvm.sh}" |
| 2740 | + local tmp_file |
| 2741 | + local remote_version |
| 2742 | + local target_script |
| 2743 | + |
| 2744 | + target_script=$(phpvm_get_self_update_target) || return "$PHPVM_EXIT_ERROR" |
| 2745 | + if [ -z "$target_script" ]; then |
| 2746 | + phpvm_err "Unable to determine phpvm script path for self-update." |
| 2747 | + return "$PHPVM_EXIT_ERROR" |
| 2748 | + fi |
| 2749 | + |
| 2750 | + tmp_file=$(mktemp) || { |
| 2751 | + phpvm_err "Failed to create temporary file for self-update." |
| 2752 | + return "$PHPVM_EXIT_FILE_ERROR" |
| 2753 | + } |
| 2754 | + |
| 2755 | + if phpvm_is_test_mode && [ -n "${PHPVM_SELF_UPDATE_TEST_SOURCE:-}" ]; then |
| 2756 | + if [ ! -f "$PHPVM_SELF_UPDATE_TEST_SOURCE" ]; then |
| 2757 | + rm -f "$tmp_file" |
| 2758 | + phpvm_err "Self-update test source not found: $PHPVM_SELF_UPDATE_TEST_SOURCE" |
| 2759 | + return "$PHPVM_EXIT_FILE_ERROR" |
| 2760 | + fi |
| 2761 | + command cp "$PHPVM_SELF_UPDATE_TEST_SOURCE" "$tmp_file" |
| 2762 | + else |
| 2763 | + if ! phpvm_download_url_to_file "$source_url" "$tmp_file"; then |
| 2764 | + rm -f "$tmp_file" |
| 2765 | + phpvm_err "Failed to download phpvm update from $source_url" |
| 2766 | + return "$PHPVM_EXIT_ERROR" |
| 2767 | + fi |
| 2768 | + fi |
| 2769 | + |
| 2770 | + remote_version=$(phpvm_extract_version_from_file "$tmp_file") || { |
| 2771 | + rm -f "$tmp_file" |
| 2772 | + phpvm_err "Failed to determine remote phpvm version." |
| 2773 | + return "$PHPVM_EXIT_ERROR" |
| 2774 | + } |
| 2775 | + |
| 2776 | + if [ "$remote_version" = "$PHPVM_VERSION" ]; then |
| 2777 | + rm -f "$tmp_file" |
| 2778 | + phpvm_echo "You are already on the latest version: v$PHPVM_VERSION." |
| 2779 | + return "$PHPVM_EXIT_SUCCESS" |
| 2780 | + fi |
| 2781 | + |
| 2782 | + phpvm_echo "Updating phpvm..." |
| 2783 | + |
| 2784 | + if [ ! -w "$target_script" ] && command_exists sudo; then |
| 2785 | + if ! run_with_sudo cp "$tmp_file" "$target_script"; then |
| 2786 | + rm -f "$tmp_file" |
| 2787 | + phpvm_err "Failed to update phpvm." |
| 2788 | + return "$PHPVM_EXIT_ERROR" |
| 2789 | + fi |
| 2790 | + else |
| 2791 | + if ! command cp "$tmp_file" "$target_script"; then |
| 2792 | + rm -f "$tmp_file" |
| 2793 | + phpvm_err "Failed to update phpvm." |
| 2794 | + return "$PHPVM_EXIT_ERROR" |
| 2795 | + fi |
| 2796 | + fi |
| 2797 | + |
| 2798 | + chmod +x "$target_script" 2> /dev/null || true |
| 2799 | + rm -f "$tmp_file" |
| 2800 | + |
| 2801 | + phpvm_echo "phpvm successfully updated to the latest version: v$remote_version." |
| 2802 | + return "$PHPVM_EXIT_SUCCESS" |
| 2803 | +} |
| 2804 | + |
2681 | 2805 | # Print system information for debugging |
2682 | 2806 | print_system_info() { |
2683 | 2807 | phpvm_echo "System Information:" |
@@ -3283,6 +3407,10 @@ main() { |
3283 | 3407 | print_version |
3284 | 3408 | exit "$PHPVM_EXIT_SUCCESS" |
3285 | 3409 | ;; |
| 3410 | + self-update) |
| 3411 | + phpvm_self_update |
| 3412 | + exit "$?" |
| 3413 | + ;; |
3286 | 3414 | unload) |
3287 | 3415 | phpvm_unload |
3288 | 3416 | exit "$?" |
@@ -3405,6 +3533,10 @@ main() { |
3405 | 3533 | phpvm_cache "$@" |
3406 | 3534 | exit "$?" |
3407 | 3535 | ;; |
| 3536 | + self-update) |
| 3537 | + phpvm_self_update |
| 3538 | + exit "$?" |
| 3539 | + ;; |
3408 | 3540 | *) |
3409 | 3541 | phpvm_err "Unknown command: $command" |
3410 | 3542 | print_help |
|
0 commit comments