|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Package\Artifact; |
| 6 | + |
| 7 | +use StaticPHP\Artifact\ArtifactDownloader; |
| 8 | +use StaticPHP\Artifact\Downloader\DownloadResult; |
| 9 | +use StaticPHP\Artifact\Downloader\Type\CheckUpdateResult; |
| 10 | +use StaticPHP\Attribute\Artifact\AfterBinaryExtract; |
| 11 | +use StaticPHP\Attribute\Artifact\CustomBinary; |
| 12 | +use StaticPHP\Attribute\Artifact\CustomBinaryCheckUpdate; |
| 13 | +use StaticPHP\Exception\DownloaderException; |
| 14 | +use StaticPHP\Runtime\SystemTarget; |
| 15 | +use StaticPHP\Util\System\LinuxUtil; |
| 16 | + |
| 17 | +class rust |
| 18 | +{ |
| 19 | + #[CustomBinary('rust', [ |
| 20 | + 'linux-x86_64', |
| 21 | + 'linux-aarch64', |
| 22 | + ])] |
| 23 | + public function downBinary(ArtifactDownloader $downloader): DownloadResult |
| 24 | + { |
| 25 | + // determine distro first |
| 26 | + $distro = LinuxUtil::isMuslDist() ? 'musl' : 'gnu'; |
| 27 | + $arch = SystemTarget::getTargetArch(); |
| 28 | + |
| 29 | + // get latest rust version from link |
| 30 | + $toml_config = default_shell()->executeCurl('https://static.rust-lang.org/dist/channel-rust-stable.toml', retries: $downloader->getRetry()); |
| 31 | + // parse toml by regex since we want to avoid adding a toml parser dependency just for this |
| 32 | + $cnt = preg_match_all('/^version = "([^"]+)"$/m', $toml_config ?: '', $matches); |
| 33 | + if (!$cnt) { |
| 34 | + throw new DownloaderException('Failed to parse Rust version from channel config'); |
| 35 | + } |
| 36 | + $versions = $matches[1]; |
| 37 | + // strip version num \d.\d.\d (some version number is like "x.x.x (abcdefg 1970-01-01)" |
| 38 | + $versions = array_filter(array_map(fn ($v) => preg_match('/^(\d+\.\d+\.\d+)/', $v, $m) ? $m[1] : null, $versions)); |
| 39 | + usort($versions, 'version_compare'); |
| 40 | + $latest_version = end($versions); |
| 41 | + if (!$latest_version) { |
| 42 | + throw new DownloaderException('Could not determine latest Rust version'); |
| 43 | + } |
| 44 | + |
| 45 | + // merge download link |
| 46 | + $download_url = "https://static.rust-lang.org/dist/rust-{$latest_version}-{$arch}-unknown-linux-{$distro}.tar.xz"; |
| 47 | + $path = DOWNLOAD_PATH . DIRECTORY_SEPARATOR . basename($download_url); |
| 48 | + default_shell()->executeCurlDownload($download_url, $path, retries: $downloader->getRetry()); |
| 49 | + return DownloadResult::archive(basename($path), ['url' => $download_url, 'version' => $latest_version], extract: PKG_ROOT_PATH . '/rust-install', verified: false, version: $latest_version); |
| 50 | + } |
| 51 | + |
| 52 | + #[CustomBinaryCheckUpdate('rust', [ |
| 53 | + 'linux-x86_64', |
| 54 | + 'linux-aarch64', |
| 55 | + ])] |
| 56 | + public function checkUpdateBinary(?string $old_version, ArtifactDownloader $downloader): CheckUpdateResult |
| 57 | + { |
| 58 | + $toml_config = default_shell()->executeCurl('https://static.rust-lang.org/dist/channel-rust-stable.toml', retries: $downloader->getRetry()); |
| 59 | + $cnt = preg_match_all('/^version = "([^"]+)"$/m', $toml_config ?: '', $matches); |
| 60 | + if (!$cnt) { |
| 61 | + throw new DownloaderException('Failed to parse Rust version from channel config'); |
| 62 | + } |
| 63 | + $versions = array_filter(array_map(fn ($v) => preg_match('/^(\d+\.\d+\.\d+)/', $v, $m) ? $m[1] : null, $matches[1])); |
| 64 | + usort($versions, 'version_compare'); |
| 65 | + $latest_version = end($versions); |
| 66 | + if (!$latest_version) { |
| 67 | + throw new DownloaderException('Could not determine latest Rust version'); |
| 68 | + } |
| 69 | + return new CheckUpdateResult( |
| 70 | + old: $old_version, |
| 71 | + new: $latest_version, |
| 72 | + needUpdate: $old_version === null || $latest_version !== $old_version, |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + #[AfterBinaryExtract('rust', [ |
| 77 | + 'linux-x86_64', |
| 78 | + 'linux-aarch64', |
| 79 | + ])] |
| 80 | + public function postExtractRust(string $target_path): void |
| 81 | + { |
| 82 | + $prefix = PKG_ROOT_PATH . '/rust'; |
| 83 | + shell()->exec("cd {$target_path} && ./install.sh --prefix={$prefix}"); |
| 84 | + } |
| 85 | +} |
0 commit comments