1+ #!/usr/bin/env -S deno run -A
2+ import { Octokit } from "https://esm.sh/[email protected] ?dts" ; 3+ // --allow-env=GITHUB_TOKEN --allow-write --allow-net
4+ const token = Deno . env . get ( "GITHUB_TOKEN" ) ;
5+ if ( ! token ) {
6+ console . error ( "GITHUB_TOKEN not set" ) ;
7+ Deno . exit ( 1 ) ;
8+ }
9+
10+ const [ owner , repo ] = "indygreg/apple-platform-rs" . split ( "/" ) ;
11+ const octokit = new Octokit ( { auth : token } ) ;
12+
13+ const getTag = async ( version : string ) => {
14+ if ( version === "latest" ) {
15+ const latestRelease = await octokit . request ( "GET /repos/{owner}/{repo}/releases/latest" , {
16+ owner,
17+ repo,
18+ } ) ;
19+ if ( ! latestRelease . data . tag_name )
20+ throw new Error ( "No tag name found in latest release" ) ;
21+ return latestRelease . data . tag_name ;
22+ }
23+ return `apple-codesign/${ version } ` ;
24+ }
25+ const getArtifactName = ( version : string ) => {
26+ const prefix = `apple-codesign-${ version } -${ Deno . build . arch } -` ;
27+ const platform = Deno . build . os ;
28+ if ( platform === "darwin" ) {
29+ return `${ prefix } apple-${ platform } .tar.gz` ;
30+ } else if ( platform === "linux" ) {
31+ return `${ prefix } unknown-linux-musl.tar.gz` ;
32+ } else if ( platform === "windows" ) {
33+ return `${ prefix } pc-windows-msvc.zip` ;
34+ } else {
35+ throw new Error ( `Unsupported platform: ${ platform } ` ) ;
36+ }
37+ }
38+
39+ const version = Deno . args [ 0 ] || "latest" ;
40+ const tag = await getTag ( version ) ;
41+
42+ const release = await octokit . request (
43+ "GET /repos/{owner}/{repo}/releases/tags/{tag}" ,
44+ {
45+ owner,
46+ repo,
47+ tag,
48+ }
49+ ) ;
50+ console . info ( `Found release: ${ tag } (${ release . data . id } )` ) ;
51+
52+ if ( ! release . data . assets || release . data . assets . length === 0 ) {
53+ console . error ( "No assets found in release" ) ;
54+ Deno . exit ( 1 ) ;
55+ }
56+ const artifactName = getArtifactName ( tag . replace ( "apple-codesign/" , "" ) ) ;
57+ const asset = release . data . assets . find (
58+ ( a ) => a . name === artifactName
59+ ) ;
60+ if ( ! asset ) {
61+ console . error ( `No asset found with name ${ artifactName } ` ) ;
62+ Deno . exit ( 1 ) ;
63+ }
64+ console . info ( `Found asset: ${ asset . name } (${ asset . id } )` ) ;
65+ const downloadUrl = asset . browser_download_url ;
66+ console . info ( `Downloading from ${ downloadUrl } ` ) ;
67+ const response = await fetch ( downloadUrl ) ;
68+ const compressedFile = await Deno . open ( artifactName , { create : true , write : true } )
69+ await response . body ! . pipeTo ( compressedFile . writable ) ;
70+
71+ const outDir = "./bin/" ;
72+ await Deno . mkdir ( outDir , { recursive : true } ) ;
73+
74+ const extractCmd = artifactName . endsWith ( ".zip" )
75+ ? new Deno . Command ( "unzip" , { args : [ "-o" , artifactName , "-d" , outDir ] } )
76+ : new Deno . Command ( "tar" , { args : [ "-xzf" , artifactName , "-C" , outDir ] } ) ;
77+
78+ const { success } = await extractCmd . output ( ) ;
79+ if ( ! success ) {
80+ console . error ( "Extraction failed" ) ;
81+ Deno . exit ( 1 ) ;
82+ }
83+
84+ const binPath = `${ outDir } /${ artifactName . replace ( ".tar.gz" , "" ) . replace ( ".zip" , "" ) } /rcodesign` ;
85+ await Deno . chmod ( binPath , 0o755 ) ;
86+
87+ const githubOutput = Deno . env . get ( "GITHUB_OUTPUT" ) ;
88+ if ( githubOutput ) {
89+ const output = `rcodesign_path=${ Deno . cwd ( ) } /${ binPath } \n` ;
90+ await Deno . writeTextFile ( githubOutput , output , { append : true } ) ;
91+ }
92+
93+ Deno . exit ( 0 ) ;
0 commit comments