|
17 | 17 | return; |
18 | 18 | } |
19 | 19 |
|
20 | | -$today = date( 'Y-m-d' ); |
21 | | -chdir( SLIC_ROOT_DIR ); |
22 | | -$status = passthru( 'git checkout main && git pull' ); |
| 20 | +if ( is_phar() ) { |
| 21 | + phar_self_update(); |
| 22 | +} else { |
| 23 | + git_upgrade(); |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Upgrades slic via git pull on the main branch. |
| 28 | + */ |
| 29 | +function git_upgrade() { |
| 30 | + chdir( SLIC_ROOT_DIR ); |
| 31 | + $status = passthru( 'git checkout main && git pull' ); |
23 | 32 |
|
24 | | -if ( ! $status ) { |
25 | | - unlink( SLIC_ROOT_DIR . '/.remote-version' ); |
| 33 | + if ( ! $status ) { |
| 34 | + $remote_version_file = slic_data_dir() . '/.remote-version'; |
| 35 | + if ( file_exists( $remote_version_file ) ) { |
| 36 | + unlink( $remote_version_file ); |
| 37 | + } |
26 | 38 |
|
27 | | - $status = passthru( 'php slic update' ); |
| 39 | + $status = passthru( PHP_BINARY . ' ' . escapeshellarg( $GLOBALS['argv'][0] ) . ' update' ); |
| 40 | + } |
| 41 | + |
| 42 | + exit( $status ); |
28 | 43 | } |
29 | 44 |
|
30 | | -exit( $status ); |
| 45 | +/** |
| 46 | + * Self-updates the phar archive from GitHub Releases. |
| 47 | + */ |
| 48 | +function phar_self_update() { |
| 49 | + $current_version = CLI_VERSION; |
| 50 | + |
| 51 | + echo colorize( "Checking for updates..." . PHP_EOL ); |
| 52 | + |
| 53 | + $latest_version = fetch_latest_github_release_version(); |
| 54 | + |
| 55 | + if ( null === $latest_version ) { |
| 56 | + echo magenta( "Error: Could not check for updates. Please try again later." . PHP_EOL ); |
| 57 | + exit( 1 ); |
| 58 | + } |
| 59 | + |
| 60 | + if ( version_compare( $latest_version, $current_version, '<=' ) ) { |
| 61 | + echo light_cyan( "You are already running the latest version ({$current_version})." . PHP_EOL ); |
| 62 | + exit( 0 ); |
| 63 | + } |
| 64 | + |
| 65 | + echo colorize( "Updating from <yellow>{$current_version}</yellow> to <yellow>{$latest_version}</yellow>..." . PHP_EOL ); |
| 66 | + |
| 67 | + // Fetch the release to get the slic.phar asset URL. |
| 68 | + $context = stream_context_create( [ |
| 69 | + 'http' => [ |
| 70 | + 'method' => 'GET', |
| 71 | + 'header' => "User-Agent: slic-cli\r\nAccept: application/vnd.github.v3+json\r\n", |
| 72 | + 'timeout' => 10, |
| 73 | + ], |
| 74 | + ] ); |
| 75 | + |
| 76 | + $response = @file_get_contents( 'https://api.github.com/repos/stellarwp/slic/releases/latest', false, $context ); |
| 77 | + |
| 78 | + if ( false === $response ) { |
| 79 | + echo magenta( "Error: Could not fetch release information." . PHP_EOL ); |
| 80 | + exit( 1 ); |
| 81 | + } |
| 82 | + |
| 83 | + $release = json_decode( $response, true ); |
| 84 | + |
| 85 | + if ( ! is_array( $release ) || empty( $release['assets'] ) ) { |
| 86 | + echo magenta( "Error: No assets found in the latest release." . PHP_EOL ); |
| 87 | + exit( 1 ); |
| 88 | + } |
| 89 | + |
| 90 | + // Find the slic.phar asset. |
| 91 | + $phar_url = null; |
| 92 | + foreach ( $release['assets'] as $asset ) { |
| 93 | + if ( $asset['name'] === 'slic.phar' ) { |
| 94 | + $phar_url = $asset['browser_download_url']; |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if ( null === $phar_url ) { |
| 100 | + echo magenta( "Error: slic.phar not found in the latest release assets." . PHP_EOL ); |
| 101 | + exit( 1 ); |
| 102 | + } |
| 103 | + |
| 104 | + // Download the new phar. |
| 105 | + $download_context = stream_context_create( [ |
| 106 | + 'http' => [ |
| 107 | + 'method' => 'GET', |
| 108 | + 'header' => "User-Agent: slic-cli\r\n", |
| 109 | + 'timeout' => 60, |
| 110 | + 'follow_location' => true, |
| 111 | + ], |
| 112 | + ] ); |
| 113 | + |
| 114 | + $new_phar = @file_get_contents( $phar_url, false, $download_context ); |
| 115 | + |
| 116 | + if ( false === $new_phar || empty( $new_phar ) ) { |
| 117 | + echo magenta( "Error: Failed to download the new phar." . PHP_EOL ); |
| 118 | + exit( 1 ); |
| 119 | + } |
| 120 | + |
| 121 | + // Get the path to the currently running phar. |
| 122 | + $phar_path = \Phar::running( false ); |
| 123 | + |
| 124 | + if ( empty( $phar_path ) ) { |
| 125 | + echo magenta( "Error: Could not determine the running phar path." . PHP_EOL ); |
| 126 | + exit( 1 ); |
| 127 | + } |
| 128 | + |
| 129 | + // Write the new phar to a temp file first, then rename for atomicity. |
| 130 | + $tmp_path = $phar_path . '.tmp'; |
| 131 | + |
| 132 | + if ( false === file_put_contents( $tmp_path, $new_phar ) ) { |
| 133 | + echo magenta( "Error: Could not write the new phar to {$tmp_path}." . PHP_EOL ); |
| 134 | + exit( 1 ); |
| 135 | + } |
| 136 | + |
| 137 | + // Make the temp file executable. |
| 138 | + chmod( $tmp_path, 0755 ); |
| 139 | + |
| 140 | + // Replace the old phar with the new one. |
| 141 | + if ( ! rename( $tmp_path, $phar_path ) ) { |
| 142 | + unlink( $tmp_path ); |
| 143 | + echo magenta( "Error: Could not replace the phar at {$phar_path}." . PHP_EOL ); |
| 144 | + exit( 1 ); |
| 145 | + } |
| 146 | + |
| 147 | + // Clear the cached remote version. |
| 148 | + $remote_version_file = slic_data_dir() . '/.remote-version'; |
| 149 | + if ( file_exists( $remote_version_file ) ) { |
| 150 | + unlink( $remote_version_file ); |
| 151 | + } |
| 152 | + |
| 153 | + echo light_cyan( "Successfully updated slic to version {$latest_version}." . PHP_EOL ); |
| 154 | + exit( 0 ); |
| 155 | +} |
0 commit comments