99use StaticPHP \Artifact \Downloader \DownloadResult ;
1010use StaticPHP \Attribute \Artifact \AfterBinaryExtract ;
1111use StaticPHP \Attribute \Artifact \CustomBinary ;
12- use StaticPHP \Exception \ValidationException ;
12+ use StaticPHP \Exception \DownloaderException ;
1313use StaticPHP \Runtime \SystemTarget ;
1414use StaticPHP \Util \System \LinuxUtil ;
1515
@@ -28,29 +28,41 @@ public function downBinary(ArtifactDownloader $downloader): DownloadResult
2828 $ arch = match (explode ('- ' , $ name )[1 ]) {
2929 'x86_64 ' => 'amd64 ' ,
3030 'aarch64 ' => 'arm64 ' ,
31- default => throw new ValidationException ('Unsupported architecture: ' . $ name ),
31+ default => throw new DownloaderException ('Unsupported architecture: ' . $ name ),
3232 };
3333 $ os = match (explode ('- ' , $ name )[0 ]) {
3434 'linux ' => 'linux ' ,
3535 'macos ' => 'darwin ' ,
36- default => throw new ValidationException ('Unsupported OS: ' . $ name ),
36+ default => throw new DownloaderException ('Unsupported OS: ' . $ name ),
3737 };
38- $ hash = match ("{$ os }- {$ arch }" ) {
39- 'linux-amd64 ' => '2852af0cb20a13139b3448992e69b868e50ed0f8a1e5940ee1de9e19a123b613 ' ,
40- 'linux-arm64 ' => '05de75d6994a2783699815ee553bd5a9327d8b79991de36e38b66862782f54ae ' ,
41- 'darwin-amd64 ' => '5bd60e823037062c2307c71e8111809865116714d6f6b410597cf5075dfd80ef ' ,
42- 'darwin-arm64 ' => '544932844156d8172f7a28f77f2ac9c15a23046698b6243f633b0a0b00c0749c ' ,
43- };
44- $ go_version = '1.25.0 ' ;
45- $ url = "https://go.dev/dl/go {$ go_version }. {$ os }- {$ arch }.tar.gz " ;
46- $ path = DOWNLOAD_PATH . DIRECTORY_SEPARATOR . "go {$ go_version }. {$ os }- {$ arch }.tar.gz " ;
38+
39+ // get version and hash
40+ [$ version ] = explode ("\n" , default_shell ()->executeCurl ('https://go.dev/VERSION?m=text ' ) ?: '' );
41+ if ($ version === '' ) {
42+ throw new DownloaderException ('Failed to get latest Go version from https://go.dev/VERSION?m=text ' );
43+ }
44+ $ page = default_shell ()->executeCurl ('https://go.dev/dl/ ' );
45+ if ($ page === '' || $ page === false ) {
46+ throw new DownloaderException ('Failed to get Go download page from https://go.dev/dl/ ' );
47+ }
48+
49+ $ version_regex = str_replace ('. ' , '\. ' , $ version );
50+ $ pattern = "/href= \"\\/dl \\/ {$ version_regex }\\. {$ os }- {$ arch }\\.tar \\.gz \">.*?<tt>([a-f0-9]{64})< \\/tt>/s " ;
51+ if (preg_match ($ pattern , $ page , $ matches )) {
52+ $ hash = $ matches [1 ];
53+ } else {
54+ throw new DownloaderException ("Failed to find download hash for Go {$ version } {$ os }- {$ arch }" );
55+ }
56+
57+ $ url = "https://go.dev/dl/ {$ version }. {$ os }- {$ arch }.tar.gz " ;
58+ $ path = DOWNLOAD_PATH . DIRECTORY_SEPARATOR . "{$ version }. {$ os }- {$ arch }.tar.gz " ;
4759 default_shell ()->executeCurlDownload ($ url , $ path , retries: $ downloader ->getRetry ());
4860 // verify hash
4961 $ file_hash = hash_file ('sha256 ' , $ path );
5062 if ($ file_hash !== $ hash ) {
51- throw new ValidationException ("Hash mismatch for downloaded go-xcaddy binary. Expected {$ hash }, got {$ file_hash }" );
63+ throw new DownloaderException ("Hash mismatch for downloaded go-xcaddy binary. Expected {$ hash }, got {$ file_hash }" );
5264 }
53- return DownloadResult::archive (basename ($ path ), ['url ' => $ url , 'version ' => $ go_version ], extract: "{$ pkgroot }/go-xcaddy " , verified: true , version: $ go_version );
65+ return DownloadResult::archive (basename ($ path ), ['url ' => $ url , 'version ' => $ version ], extract: "{$ pkgroot }/go-xcaddy " , verified: true , version: $ version );
5466 }
5567
5668 #[AfterBinaryExtract('go-xcaddy ' , [
0 commit comments