@@ -49,26 +49,31 @@ async function treePrint(dir: string, prefix = '') {
4949 }
5050}
5151
52- const commit = process . env . MXD_COMMIT || exit ( 2 )
52+ const commit = process . env . MXD_COMMIT
53+ const releaseTag = process . env . MXD_RELEASE
5354const platform = ( process . env . MXD_PLATFORM || exit ( 2 ) ) as 'windows' | 'linux-gnu' | 'linux-musl' | 'darwin'
5455const arch = ( process . env . MXD_ARCH || exit ( 2 ) ) as 'x86_64' | 'aarch64'
5556
5657const octokit = new Octokit ( {
5758 auth : process . env . GITHUB_TOKEN ,
5859} )
5960
60- console . log ( 'Fetching workflow runs' )
61- const runs = await octokit . rest . actions . listWorkflowRunsForRepo ( {
62- owner : 'koitococo' ,
63- repo : 'mxlite' ,
64- // eslint-disable-next-line camelcase
65- head_sha : commit ,
66- headers : {
67- 'X-GitHub-Api-Version' : '2022-11-28' ,
68- } ,
69- } )
61+ async function downloadFromActions ( ) {
62+ console . log ( 'Fetching workflow runs' )
63+ const runs = await octokit . rest . actions . listWorkflowRunsForRepo ( {
64+ owner : 'koitococo' ,
65+ repo : 'mxlite' ,
66+ // eslint-disable-next-line camelcase
67+ head_sha : commit ! ,
68+ headers : {
69+ 'X-GitHub-Api-Version' : '2022-11-28' ,
70+ } ,
71+ } )
72+
73+ if ( ! ( runs . data . workflow_runs . length > 0 ) ) {
74+ throw new Error ( 'No workflow runs found.' )
75+ }
7076
71- if ( runs . data . workflow_runs . length > 0 ) {
7277 console . log ( 'Fetching artifacts' )
7378 const artifacts = await octokit . rest . actions . listWorkflowRunArtifacts ( {
7479 owner : 'koitococo' ,
@@ -80,74 +85,126 @@ if (runs.data.workflow_runs.length > 0) {
8085 } ,
8186 } )
8287
83- const matchedArtifact = artifacts . data . artifacts . find (
88+ const mxdArtifact = artifacts . data . artifacts . find (
8489 ( artifact ) => artifact . name . match ( / m x d - / ) && artifact . name . includes ( platform ) && artifact . name . includes ( arch ) ,
8590 )
86- if ( matchedArtifact ) {
87- console . log ( 'Downloading mxd artifact:' , matchedArtifact . name )
91+ if ( ! mxdArtifact ) {
92+ throw new Error ( `No mxd artifact found for platform ${ platform } and arch ${ arch } .` )
93+ }
94+ console . log ( 'Downloading mxd artifact:' , mxdArtifact . name )
95+ const response = await octokit . rest . actions . downloadArtifact ( {
96+ owner : 'koitococo' ,
97+ repo : 'mxlite' ,
98+ // eslint-disable-next-line camelcase
99+ artifact_id : mxdArtifact . id ,
100+ // eslint-disable-next-line camelcase
101+ archive_format : 'zip' ,
102+ headers : {
103+ 'X-GitHub-Api-Version' : '2022-11-28' ,
104+ } ,
105+ } )
106+ const binary = response . data as ArrayBuffer
107+ await writeFile ( 'artifact.zip' , new Uint8Array ( binary ) )
108+ console . log ( 'mxd artifact downloaded successfully' )
109+ const targetDir = join ( process . cwd ( ) , 'bin' )
110+ mkdirSync ( targetDir , { recursive : true } )
111+ await extractZip ( 'artifact.zip' , { dir : targetDir } )
112+ rmSync ( 'artifact.zip' )
113+
114+ const mxaArtifacts = artifacts . data . artifacts . filter (
115+ ( artifact ) => artifact . name . match ( / ^ m x a - ( .+ ) / ) && ! artifact . name . includes ( 'gnu' ) ,
116+ )
117+ if ( ! ( mxaArtifacts . length > 0 ) ) {
118+ throw new Error ( 'No mxa artifacts found.' )
119+ }
120+ for ( const artifact of mxaArtifacts ) {
121+ console . log ( `Downloading mxa artifact: ${ artifact . name } ` )
88122 const response = await octokit . rest . actions . downloadArtifact ( {
89123 owner : 'koitococo' ,
90124 repo : 'mxlite' ,
91125 // eslint-disable-next-line camelcase
92- artifact_id : matchedArtifact . id ,
126+ artifact_id : artifact . id ,
93127 // eslint-disable-next-line camelcase
94128 archive_format : 'zip' ,
95129 headers : {
96130 'X-GitHub-Api-Version' : '2022-11-28' ,
97131 } ,
98132 } )
99133 const binary = response . data as ArrayBuffer
100- await writeFile ( 'artifact.zip' , new Uint8Array ( binary ) )
101- console . log ( 'mxd artifact downloaded successfully' )
102- const targetDir = join ( process . cwd ( ) , 'bin' )
134+ await writeFile ( `${ artifact . name } .zip` , new Uint8Array ( binary ) )
135+ console . log ( `mxa artifact ${ artifact . name } downloaded successfully` )
136+ const matchedSegments = artifact . name . match ( / ^ m x a - ( \w + ) - ( \w + ) - ( \w + ) / )
137+ const arch = matchedSegments ?. [ 1 ]
138+ const platform = matchedSegments ?. [ 3 ]
139+ if ( ! arch || ! platform ) {
140+ console . error ( `Failed to parse architecture or platform from artifact name: ${ artifact . name } ` )
141+ continue
142+ }
143+ const targetDir = join ( process . cwd ( ) , 'bin/mxa' , platform , arch )
103144 mkdirSync ( targetDir , { recursive : true } )
104- await extractZip ( 'artifact.zip' , { dir : targetDir } )
105- rmSync ( 'artifact.zip' )
106- } else {
107- console . error ( 'No mxd artifact found.' )
145+ await extractZip ( `${ artifact . name } .zip` , { dir : targetDir } )
146+ rmSync ( `${ artifact . name } .zip` )
108147 }
109148
110- const mxaArtifacts = artifacts . data . artifacts . filter (
111- ( artifact ) => artifact . name . match ( / ^ m x a - ( .+ ) / ) && ! artifact . name . includes ( 'gnu' ) ,
149+ console . log ( 'bin directory structure:' )
150+ await treePrint ( join ( process . cwd ( ) , 'bin' ) )
151+ }
152+
153+ async function downloadByUrl ( url : string , dir : string , filename : string ) {
154+ const response = await fetch ( url )
155+ if ( ! response . ok ) {
156+ throw new Error ( `Failed to download ${ url } : ${ response . statusText } ` )
157+ }
158+ const data = await response . bytes ( )
159+ mkdirSync ( dir , { recursive : true } )
160+ const target = join ( dir , filename )
161+ await writeFile ( target , new Uint8Array ( data ) )
162+ }
163+
164+ async function downloadFromRelease ( ) {
165+ console . log ( 'Fetching release info' )
166+ const releaseInfo = await octokit . rest . repos . getReleaseByTag ( {
167+ owner : 'koitococo' ,
168+ repo : 'mxlite' ,
169+ tag : releaseTag ! ,
170+ headers : {
171+ 'X-GitHub-Api-Version' : '2022-11-28' ,
172+ } ,
173+ } )
174+ console . log ( 'Fetching assets' )
175+ const assets = releaseInfo . data . assets
176+
177+ const mxdAsset = assets . find (
178+ ( asset ) => asset . name . includes ( 'mxd-' ) && asset . name . includes ( platform ) && asset . name . includes ( arch ) ,
112179 )
113- if ( mxaArtifacts . length > 0 ) {
114- for ( const artifact of mxaArtifacts ) {
115- console . log ( `Downloading mxa artifact: ${ artifact . name } ` )
116- const response = await octokit . rest . actions . downloadArtifact ( {
117- owner : 'koitococo' ,
118- repo : 'mxlite' ,
119- // eslint-disable-next-line camelcase
120- artifact_id : artifact . id ,
121- // eslint-disable-next-line camelcase
122- archive_format : 'zip' ,
123- headers : {
124- 'X-GitHub-Api-Version' : '2022-11-28' ,
125- } ,
126- } )
127- const binary = response . data as ArrayBuffer
128- await writeFile ( `${ artifact . name } .zip` , new Uint8Array ( binary ) )
129- console . log ( `mxa artifact ${ artifact . name } downloaded successfully` )
130- const matchedSegments = artifact . name . match ( / ^ m x a - ( \w + ) - ( \w + ) - ( \w + ) / )
131- const arch = matchedSegments ?. [ 1 ]
132- const platform = matchedSegments ?. [ 3 ]
133- if ( ! arch || ! platform ) {
134- console . error ( `Failed to parse architecture or platform from artifact name: ${ artifact . name } ` )
135- continue
136- }
137- const targetDir = join ( process . cwd ( ) , 'bin/mxa' , platform , arch )
138- mkdirSync ( targetDir , { recursive : true } )
139- console . log ( `Extracting ${ artifact . name } .zip to ${ targetDir } ` )
140- await extractZip ( `${ artifact . name } .zip` , { dir : targetDir } )
141- rmSync ( `${ artifact . name } .zip` )
180+ if ( ! mxdAsset ) {
181+ throw new Error ( `No mxd asset found for platform ${ platform } and arch ${ arch } .` )
182+ }
183+
184+ console . log ( 'Downloading mxd asset:' , mxdAsset . name )
185+ await downloadByUrl ( mxdAsset . url , 'bin' , 'mxd' )
186+
187+ for ( const asset of assets ) {
188+ const match = asset . name . match ( / ^ m x a - ( .+ ) - l i n u x - m u s l $ / )
189+ if ( match ) {
190+ const arch = match [ 1 ]
191+
192+ console . log ( `Downloading mxa asset: ${ asset . name } ` )
193+ await downloadByUrl ( asset . url , join ( 'bin/mxa/linux' , arch ) , 'mxa' )
142194 }
143- } else {
144- console . error ( 'No mxa artifacts found.' )
145195 }
196+ }
146197
147- console . log ( 'bin directory structure:' )
148- await treePrint ( join ( process . cwd ( ) , 'bin' ) )
149- } else {
150- console . error ( 'No workflow runs found.' )
151- console . error ( runs )
152- exit ( 1 )
198+ if ( releaseTag ) {
199+ await downloadFromRelease ( )
200+ exit ( 0 )
201+ }
202+
203+ if ( commit ) {
204+ await downloadFromActions ( )
205+ exit ( 0 )
153206}
207+
208+ throw new Error ( 'Either COMMIT or RELEASE environment variable must be set.' )
209+
210+ export { }
0 commit comments