11const core = require ( '@actions/core' ) ;
2- const exec = require ( '@actions/exec' )
3- const path = require ( 'path' )
2+ const exec = require ( '@actions/exec' ) ;
3+ const path = require ( 'path' ) ;
44const fs = require ( 'fs' ) ;
55
6+ // Static version - update manually when new func releases are available
7+ const DEFAULT_FUNC_VERSION = 'knative-v1.20.1' ;
68
7- // change this accordingly
8- const DEFAULT_FUNC_VERSION = 'knative-v1.16.1'
9-
10- // detect os system in Github Actions and determine binary name
119function getOsBinName ( ) {
1210 const runnerOS = process . env . RUNNER_OS ;
1311 const runnerArch = process . env . RUNNER_ARCH ;
@@ -23,102 +21,77 @@ function getOsBinName() {
2321 } else if ( runnerOS === 'macOS' ) {
2422 return runnerArch === 'X64' ? 'func_darwin_amd64' : 'func_darwin_arm64' ;
2523 } else if ( runnerOS === 'Windows' ) {
26- return 'func_windows_amd64' ;
24+ return 'func_windows_amd64.exe ' ;
2725 } else {
2826 return 'unknown' ;
2927 }
3028}
3129
32- // smartVersionParse will check validity of given version and fill in the parts
33- // to make it correct if possible.
34- // Ex.: '1.16' or 'v1.16' will return 'v1.16.0'
30+ // Normalizes version to release tag format: knative-vX.Y.Z
31+ // Ex.: '1.16' or 'v1.16' will return 'knative-v1.16.0'
3532function smartVersionUpdate ( version ) {
36- versionRegex = / ^ (?< knprefix > k n a t i v e - ) ? (?< prefix > v ? ) (?< major > \d + ) \. (?< minor > \d + ) ( .(?< patch > \d + ) ) ? $ / ;
33+ const versionRegex = / ^ (?< knprefix > k n a t i v e - ) ? (?< prefix > v ? ) (?< major > \d + ) \. (?< minor > \d + ) ( .(?< patch > \d + ) ) ? $ / ;
3734 let match = version . match ( versionRegex ) ;
38- if ( match ) {
39- if ( match . groups . knprefix == undefined ) {
40- match . groups . knprefix = "" ;
41- }
35+ if ( match ) {
36+ const knprefix = 'knative-' ;
4237 const prefix = 'v' ;
43- if ( match . groups . patch == undefined ) {
44- match . groups . patch = 0 ;
45- }
46- return `${ match . groups . knprefix } ${ prefix } ${ match . groups . major } .${ match . groups . minor } .${ match . groups . patch } ` ;
38+ const patch = match . groups . patch ?? 0 ;
39+ return `${ knprefix } ${ prefix } ${ match . groups . major } .${ match . groups . minor } .${ patch } ` ;
4740 }
4841
4942 core . setFailed ( `Invalid version format (${ version } ). Expected format: "1.16[.X]" or "v1.16[.X]"` ) ;
5043 return undefined ;
5144}
5245
53- /**
54- * @param {string } url - Full url to be curled
55- * @param {string } binPath - Full target path of the binary
56- */
57- // download func, set as executable
58- async function cmdConstructAndRun ( url , binPath ) {
59- const cmd = `curl -L -o "${ binPath } " "${ url } "` ;
60- await exec . exec ( cmd ) ;
46+ async function cmdConstructAndRun ( url , binPath ) {
47+ await exec . exec ( 'curl' , [ '-L' , '-o' , binPath , url ] ) ;
6148
62- //check if downloaded successfully
63- if ( ! fs . existsSync ( binPath ) ) {
49+ if ( ! fs . existsSync ( binPath ) ) {
6450 core . setFailed ( "Download failed, couldn't find the binary on disk" ) ;
51+ return ;
6552 }
6653
67- await exec . exec ( `chmod +x ${ binPath } ` ) ;
54+ if ( process . env . RUNNER_OS !== 'Windows' ) {
55+ await exec . exec ( 'chmod' , [ '+x' , binPath ] ) ;
56+ }
6857}
6958
70- /**
71- * @param {string } binPath - full path to Func binary
72- * */
73- async function addBinToPath ( binPath ) {
74- dir = path . dirname ( binPath )
75- // Write to $GITHUB_PATH, making it available for subsequent steps
59+ async function addBinToPath ( binPath ) {
60+ const dir = path . dirname ( binPath ) ;
7661 fs . appendFileSync ( process . env . GITHUB_PATH , `\n${ dir } ` ) ;
7762
78- // add only if its not in PATH yet
79- if ( ! process . env . PATH . includes ( dir ) ) {
63+ if ( ! process . env . PATH . includes ( dir ) ) {
8064 process . env . PATH = process . env . PATH + path . delimiter + dir ;
81- core . info ( `dir ${ dir } added to $ PATH` ) ;
65+ core . info ( `${ dir } added to PATH` ) ;
8266 }
83- }
67+ }
8468
85- // -------------------------------------------------------------------------- \\
86- async function run ( ) {
69+ async function run ( ) {
8770 try {
88-
89- // Fetch value of inputs specified in action.yml or use defaults
90-
91- // osBin refers to the exact name match of an existing binary available to
92- // download
9371 const osBin = core . getInput ( 'binary' ) || getOsBinName ( ) ;
94- if ( osBin == "unknown" ) {
72+ if ( osBin === "unknown" ) {
9573 core . setFailed ( "Invalid os binary determination, try setting it specifically using 'binary'" ) ;
74+ return ;
9675 }
97- // version to be downloaded
98- let version = core . getInput ( 'version' ) || DEFAULT_FUNC_VERSION
99- // destination is a directory where to download the Func
76+
77+ let version = core . getInput ( 'version' ) || DEFAULT_FUNC_VERSION ;
10078 const destination = core . getInput ( 'destination' ) || process . cwd ( ) ;
101- // bin refers to the name of the binary (Func) that will be downloaded (this
102- // is what it will be called)
103- const bin = core . getInput ( 'name' ) || 'func' ;
79+ let bin = core . getInput ( 'name' ) || 'func' ;
80+ if ( process . env . RUNNER_OS === 'Windows' && ! bin . endsWith ( '.exe' ) ) {
81+ bin += '.exe' ;
82+ }
10483
10584 version = smartVersionUpdate ( version ) ;
85+ if ( ! version ) return ;
10686
107- var url = `https://github.com/knative/func/releases/download/${ version } /${ osBin } ` ;
108- console . log ( `URL: ${ url } ` ) ;
109-
110- fullPathBin = path . resolve ( destination , bin ) ;
87+ const url = `https://github.com/knative/func/releases/download/${ version } /${ osBin } ` ;
88+ core . info ( `URL: ${ url } ` ) ;
11189
112- // download Func
113- await cmdConstructAndRun ( url , fullPathBin ) ;
90+ const fullPathBin = path . resolve ( destination , bin ) ;
11491
115- // add final binary to PATH (directory where the bin is ) and add it to
116- // GITHUB_PATH so it can be used bo subsequent 'runs'
92+ await cmdConstructAndRun ( url , fullPathBin ) ;
11793 await addBinToPath ( fullPathBin ) ;
118-
119- await exec . exec ( "ls -la" )
120- // run 'func version' as a test
121- await exec . exec ( `${ bin } version` ) ;
94+ await exec . exec ( fullPathBin , [ 'version' ] ) ;
12295
12396 } catch ( error ) {
12497 core . setFailed ( error . message ) ;
0 commit comments