1- const { execSync, spawn } = require ( 'child_process' )
2- const https = require ( 'https' )
1+ const { prefab, hooks, Path, utils, semver, SemVer } = require ( "@teaxyz/lib" )
2+ const { install, link, resolve, hydrate } = prefab
3+ const { getExecOutput, exec } = require ( "@actions/exec" )
4+ const { useConfig, useSync, useCellar } = hooks
5+ const core = require ( '@actions/core' )
36const path = require ( 'path' )
4- const fs = require ( 'fs' )
57const os = require ( "os" )
68
79async function go ( ) {
8- process . stderr . write ( "determining latest tea version…\n" )
10+ const TEA_PREFIX = core . getInput ( 'prefix' ) || ` ${ os . homedir ( ) } /.tea`
911
10- const PREFIX = process . env [ 'INPUT_PREFIX' ] || `${ os . homedir ( ) } /.tea`
1112 const TEA_DIR = ( ( ) => {
12- let TEA_DIR = process . env [ 'INPUT_SRCROOT' ]
13- if ( ! TEA_DIR ) return
14- TEA_DIR = TEA_DIR . trim ( )
13+ let TEA_DIR = core . getInput ( 'srcroot' ) . trim ( )
1514 if ( ! TEA_DIR ) return
1615 if ( ! TEA_DIR . startsWith ( "/" ) ) {
1716 // for security this must be an absolute path
@@ -20,149 +19,115 @@ async function go() {
2019 return path . normalize ( TEA_DIR )
2120 } ) ( )
2221
23- const additional_pkgs = [ ]
22+ let vtea = core . getInput ( 'version' ) ?? ""
23+ if ( vtea && ! / ^ [ * ^ ~ @ = ] / . test ( vtea ) ) {
24+ vtea = `@${ vtea } `
25+ }
26+
27+ const pkgs = [ `tea.xyz${ vtea } ` ]
2428 for ( let key in process . env ) {
2529 if ( key . startsWith ( "INPUT_+" ) ) {
2630 const value = process . env [ key ]
2731 if ( key == 'INPUT_+' ) {
2832 for ( const item of value . split ( / \s + / ) ) {
2933 if ( item . trim ( ) ) {
30- additional_pkgs . push ( `+ ${ item } ` )
34+ pkgs . push ( item )
3135 } } } else {
32- key = key . slice ( 6 ) . toLowerCase ( )
33- additional_pkgs . push ( key + value )
36+ key = key . slice ( 7 ) . toLowerCase ( )
37+ pkgs . push ( key + value )
3438 } } }
3539
3640 // we build to /opt and special case this action so people new to
37- // building aren’t immediatelyt flumoxed
38- if ( PREFIX == '/opt' && os . platform == 'darwin' ) {
39- execSync ( 'sudo chown $(whoami) :staff /opt' )
41+ // building aren’t immediately flumoxed
42+ if ( TEA_PREFIX == '/opt' && os . platform == 'darwin' ) {
43+ await exec ( 'sudo' , [ ' chown' , ` ${ os . userInfo ( ) . username } :staff` , ' /opt'] )
4044 }
4145
42- const midfix = ( ( ) => {
43- switch ( process . arch ) {
44- case 'arm64' :
45- return `${ process . platform } /aarch64`
46- case 'x64' :
47- return `${ process . platform } /x86-64`
48- default :
49- throw new Error ( `unsupported platform: ${ process . platform } /${ process . arch } ` )
50- }
51- } ) ( )
52-
53- const v = process . env [ 'INPUT_VERSION' ] || await new Promise ( ( resolve , reject ) => {
54- https . get ( `https://dist.tea.xyz/tea.xyz/${ midfix } /versions.txt` , rsp => {
55- if ( rsp . statusCode != 200 ) return reject ( rsp . statusCode )
56- rsp . setEncoding ( 'utf8' )
57- const chunks = [ ]
58- rsp . on ( "data" , x => chunks . push ( x ) )
59- rsp . on ( "end" , ( ) => {
60- resolve ( chunks . join ( "" ) . trim ( ) . split ( "\n" ) . at ( - 1 ) )
61- } )
62- } ) . on ( 'error' , reject )
63- } )
64-
65- process . stderr . write ( `fetching tea.xyz@${ v } \n` )
66-
67- fs . mkdirSync ( PREFIX , { recursive : true } )
46+ core . info ( `fetching ${ pkgs . join ( ", " ) } …` )
6847
69- const exitcode = await new Promise ( ( resolve , reject ) => {
70- https . get ( `https://dist.tea.xyz/tea.xyz/${ midfix } /v${ v } .tar.gz` , rsp => {
71- if ( rsp . statusCode != 200 ) return reject ( rsp . statusCode )
72- const tar = spawn ( 'tar' , [ 'xzf' , '-' ] , { stdio : [ 'pipe' , 'inherit' , 'inherit' ] , cwd : PREFIX } )
73- rsp . pipe ( tar . stdin )
74- tar . on ( "close" , resolve )
75- } ) . on ( 'error' , reject )
48+ useConfig ( {
49+ prefix : new Path ( TEA_PREFIX ) ,
50+ pantries : [ ] ,
51+ cache : new Path ( TEA_PREFIX ) . join ( 'tea.xyz/var/www' ) ,
52+ UserAgent : 'tea.setup/0.1.0' , //TODO version
53+ options : { compression : 'gz' }
7654 } )
77-
78- if ( exitcode != 0 ) {
79- throw new Error ( `tar: ${ exitcode } ` )
55+ await useSync ( )
56+ const { pkgs : tree } = await hydrate ( pkgs . map ( utils . pkg . parse ) )
57+ const { pending } = await resolve ( tree )
58+ for ( const pkg of pending ) {
59+ core . info ( `installing ${ utils . pkg . str ( pkg ) } ` )
60+ const installation = await install ( pkg )
61+ await link ( installation )
8062 }
8163
82- const oldwd = process . cwd ( )
83- process . chdir ( `${ PREFIX } /tea.xyz` )
84- if ( fs . existsSync ( `v*` ) ) fs . unlinkSync ( `v*` )
85- fs . symlinkSync ( `v${ v } ` , `v*` , 'dir' )
86- if ( fs . existsSync ( `v0` ) ) fs . unlinkSync ( `v0` )
87- fs . symlinkSync ( `v${ v } ` , `v0` , 'dir' ) //FIXME
88- process . chdir ( oldwd )
89-
90- const GITHUB_PATH = process . env [ 'GITHUB_PATH' ]
91- const bindir = `${ PREFIX } /tea.xyz/v${ v } /bin`
92- fs . appendFileSync ( GITHUB_PATH , `${ bindir } \n` , { encoding : 'utf8' } )
64+ const tea = await useCellar ( ) . resolve ( { project : 'tea.xyz' , constraint : new semver . Range ( '*' ) } )
65+ const teafile = tea . path . join ( 'bin/tea' ) . string
66+ const env_args = [ '--env' ]
9367
94- const teafile = `${ bindir } /tea`
95-
96- const env = {
97- TEA_DIR ,
98- ...process . env
68+ if ( TEA_DIR && tea . pkg . version . gte ( new SemVer ( "0.19" ) ) ) {
69+ env_args . push ( '--keep-going' )
9970 }
10071
101- const GITHUB_ENV = process . env [ 'GITHUB_ENV' ]
102- const GITHUB_OUTPUT = process . env [ 'GITHUB_OUTPUT' ]
103-
104- const vv = parseFloat ( v )
105- const env_flag = TEA_DIR ? vv >= 0.19 ? '--env --keep-going' : '--env' : ''
72+ let args = tea . pkg . version . gte ( new SemVer ( "0.21" ) )
73+ ? [ ]
74+ : tea . pkg . version . gte ( new SemVer ( "0.19" ) )
75+ ? [ "--dry-run" ]
76+ : [ "--dump" ]
10677
107- // get env FIXME one call should do init
108-
109- let args = vv >= 0.21
110- ? ""
111- : vv >= 0.19
112- ? "--dry-run"
113- : "--dump"
114-
115- if ( process . env [ "INPUT_CHASTE" ] == "true" ) {
116- args += " --chaste"
78+ if ( core . getBooleanInput ( "chaste" ) ) {
79+ args . push ( '--chaste' )
11780 }
11881
119- out = execSync ( `${ teafile } --sync ${ env_flag } ${ args } ${ additional_pkgs . join ( " " ) } ` , { env} ) . toString ( )
82+ //FIXME we’re running tea/cli since dev-envs are not in libtea
83+ // and we don’t want them in libtea, but we may need a libteacli as a result lol
84+ const { stdout : out } = await getExecOutput (
85+ teafile ,
86+ [ ...env_args , ...args , ...pkgs . map ( x => `+${ x } ` ) ] ,
87+ { env : { ...process . env , TEA_DIR , TEA_PREFIX } } )
12088
12189 const lines = out . split ( "\n" )
12290 for ( const line of lines ) {
12391 const match = line . match ( / ( e x p o r t ) ? ( [ A - Z a - z 0 - 9 _ ] + ) = [ ' " ] ? ( .* ) / )
12492 if ( ! match ) continue
12593 const [ , , key , value ] = match
126- if ( key == 'VERSION' ) {
127- fs . appendFileSync ( GITHUB_OUTPUT , `version=${ value } \n` , { encoding : 'utf8' } )
128- }
12994 if ( key == 'PATH' ) {
13095 for ( const part of value . split ( ":" ) . reverse ( ) ) {
131- fs . appendFileSync ( GITHUB_PATH , ` ${ part } \n` , { encoding : 'utf8' } )
96+ core . addPath ( part )
13297 }
13398 } else {
134- fs . appendFileSync ( GITHUB_ENV , `${ key } =${ value } \n` , { encoding : 'utf8' } )
99+ core . exportVariable ( key , value )
100+ if ( key == 'VERSION' ) {
101+ core . setOutput ( 'version' , value )
102+ }
135103 }
136104 }
137105
138106 if ( TEA_DIR ) {
139- fs . appendFileSync ( GITHUB_OUTPUT , ` srcroot= ${ TEA_DIR } \n` , { encoding : 'utf8' } )
140- fs . appendFileSync ( GITHUB_ENV , ` TEA_DIR= ${ TEA_DIR } \n` , { encoding : 'utf8' } )
107+ core . setOutput ( ' srcroot' , TEA_DIR )
108+ core . exportVariable ( ' TEA_DIR' , TEA_DIR )
141109 }
142110
143111 if ( os . platform ( ) != 'darwin' ) {
144112 const sh = path . join ( path . dirname ( __filename ) , "install-pre-reqs.sh" )
145113 if ( process . getuid ( ) == 0 ) {
146- execSync ( sh )
114+ await exec ( sh )
147115 } else {
148- execSync ( ` sudo ${ sh } ` )
116+ await exec ( ' sudo' , [ sh ] )
149117 }
150118 }
151119
152120 //TODO deprecated exe/md
153121 //NOTE BUT LEAVE BECAUSE WE ONCE SUPPORTED THIS
154- const target = process . env [ 'INPUT_TARGET' ]
122+ const target = core . getInput ( 'target' )
155123 if ( target ) {
156- execSync ( ` ${ teafile } ${ target } ` , { stdio : "inherit" , env} )
124+ await exec ( teafile , [ target ] , { env : { ... process . env , TEA_DIR , TEA_PREFIX } } )
157125 }
158126
159- fs . appendFileSync ( GITHUB_ENV , ` TEA_PREFIX= ${ PREFIX } \n` , { encoding : 'utf8' } )
160- fs . appendFileSync ( GITHUB_OUTPUT , ` prefix= ${ PREFIX } \n` , { encoding : 'utf8' } )
127+ core . exportVariable ( ' TEA_PREFIX' , TEA_PREFIX )
128+ core . setOutput ( ' prefix' , TEA_PREFIX )
161129
162- process . stderr . write ( `installed ${ PREFIX } / tea.xyz/v ${ v } \n ` )
130+ core . info ( `installed ${ tea . path } ` )
163131}
164132
165- go ( ) . catch ( err => {
166- console . error ( err )
167- process . exitCode = 1
168- } )
133+ go ( ) . catch ( core . setFailed )
0 commit comments