@@ -8,18 +8,18 @@ import * as restm from "typed-rest-client/RestClient";
8
8
import * as semver from "semver" ;
9
9
10
10
if ( ! tempDirectory ) {
11
- let baseLocation ;
12
- if ( process . platform === "win32" ) {
13
- // On windows use the USERPROFILE env variable
14
- baseLocation = process . env [ "USERPROFILE" ] || "C:\\" ;
11
+ let baseLocation ;
12
+ if ( process . platform === "win32" ) {
13
+ // On windows use the USERPROFILE env variable
14
+ baseLocation = process . env [ "USERPROFILE" ] || "C:\\" ;
15
+ } else {
16
+ if ( process . platform === "darwin" ) {
17
+ baseLocation = "/Users" ;
15
18
} else {
16
- if ( process . platform === "darwin" ) {
17
- baseLocation = "/Users" ;
18
- } else {
19
- baseLocation = "/home" ;
20
- }
19
+ baseLocation = "/home" ;
21
20
}
22
- tempDirectory = path . join ( baseLocation , "actions" , "temp" ) ;
21
+ }
22
+ tempDirectory = path . join ( baseLocation , "actions" , "temp" ) ;
23
23
}
24
24
25
25
import * as core from "@actions/core" ;
@@ -30,156 +30,156 @@ let osPlat: string = os.platform();
30
30
let osArch : string = os . arch ( ) ;
31
31
32
32
interface ITaskRef {
33
- ref : string ;
33
+ ref : string ;
34
34
}
35
35
36
36
export async function getArduinoCli ( version : string ) {
37
- // resolve the version number
38
- const targetVersion = await computeVersion ( version ) ;
39
- if ( targetVersion ) {
40
- version = targetVersion ;
41
- }
42
-
43
- // look if the binary is cached
44
- let toolPath : string ;
45
- toolPath = tc . find ( "arduino-cli" , version ) ;
46
-
47
- // if not: download, extract and cache
48
- if ( ! toolPath ) {
49
- toolPath = await downloadRelease ( version ) ;
50
- core . debug ( "CLI cached under " + toolPath ) ;
51
- }
52
-
53
- core . addPath ( toolPath ) ;
37
+ // resolve the version number
38
+ const targetVersion = await computeVersion ( version ) ;
39
+ if ( targetVersion ) {
40
+ version = targetVersion ;
41
+ }
42
+
43
+ // look if the binary is cached
44
+ let toolPath : string ;
45
+ toolPath = tc . find ( "arduino-cli" , version ) ;
46
+
47
+ // if not: download, extract and cache
48
+ if ( ! toolPath ) {
49
+ toolPath = await downloadRelease ( version ) ;
50
+ core . debug ( "CLI cached under " + toolPath ) ;
51
+ }
52
+
53
+ core . addPath ( toolPath ) ;
54
54
}
55
55
56
56
async function downloadRelease ( version : string ) : Promise < string > {
57
- // Download
58
- let fileName : string = getFileName ( version ) ;
59
- let downloadUrl : string = util . format (
60
- "https://github.com/Arduino/arduino-cli/releases/download/%s/%s" ,
61
- version ,
62
- fileName
63
- ) ;
64
- let downloadPath : string | null = null ;
65
- try {
66
- downloadPath = await tc . downloadTool ( downloadUrl ) ;
67
- } catch ( error ) {
68
- core . debug ( error ) ;
69
- throw `Failed to download version ${ version } : ${ error } ` ;
70
- }
71
-
72
- // Extract
73
- let extPath : string | null = null ;
74
- if ( osPlat == "win32" ) {
75
- extPath = await tc . extractZip ( downloadPath ) ;
76
- } else {
77
- extPath = await tc . extractTar ( downloadPath ) ;
78
- }
79
-
80
- // Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded
81
- return await tc . cacheDir ( extPath , "arduino-cli" , version ) ;
57
+ // Download
58
+ let fileName : string = getFileName ( version ) ;
59
+ let downloadUrl : string = util . format (
60
+ "https://github.com/Arduino/arduino-cli/releases/download/%s/%s" ,
61
+ version ,
62
+ fileName
63
+ ) ;
64
+ let downloadPath : string | null = null ;
65
+ try {
66
+ downloadPath = await tc . downloadTool ( downloadUrl ) ;
67
+ } catch ( error ) {
68
+ core . debug ( error ) ;
69
+ throw `Failed to download version ${ version } : ${ error } ` ;
70
+ }
71
+
72
+ // Extract
73
+ let extPath : string | null = null ;
74
+ if ( osPlat == "win32" ) {
75
+ extPath = await tc . extractZip ( downloadPath ) ;
76
+ } else {
77
+ extPath = await tc . extractTar ( downloadPath ) ;
78
+ }
79
+
80
+ // Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded
81
+ return await tc . cacheDir ( extPath , "arduino-cli" , version ) ;
82
82
}
83
83
84
84
function getFileName ( version : string ) : string {
85
- const arch : string = osArch == "x64" ? "64bit" : "32bit" ;
86
- let platform = "" ;
87
- let ext = "" ;
88
- switch ( osPlat ) {
89
- case "win32" :
90
- platform = "Windows" ;
91
- ext = "zip" ;
92
- break ;
93
- case "linux" :
94
- platform = "Linux" ;
95
- ext = "tar.gz" ;
96
- break ;
97
- case "darwin" :
98
- platform = "macOS" ;
99
- ext = "tar.gz" ;
100
- break ;
101
- }
102
-
103
- return util . format ( "arduino-cli_%s_%s_%s.%s" , version , platform , arch , ext ) ;
85
+ const arch : string = osArch == "x64" ? "64bit" : "32bit" ;
86
+ let platform = "" ;
87
+ let ext = "" ;
88
+ switch ( osPlat ) {
89
+ case "win32" :
90
+ platform = "Windows" ;
91
+ ext = "zip" ;
92
+ break ;
93
+ case "linux" :
94
+ platform = "Linux" ;
95
+ ext = "tar.gz" ;
96
+ break ;
97
+ case "darwin" :
98
+ platform = "macOS" ;
99
+ ext = "tar.gz" ;
100
+ break ;
101
+ }
102
+
103
+ return util . format ( "arduino-cli_%s_%s_%s.%s" , version , platform , arch , ext ) ;
104
104
}
105
105
106
106
// Retrieve a list of versions scraping tags from the Github API
107
107
async function fetchVersions ( ) : Promise < string [ ] > {
108
- let rest : restm . RestClient = new restm . RestClient ( "setup-arduino-cli" ) ;
109
- let tags : ITaskRef [ ] =
110
- ( await rest . get < ITaskRef [ ] > (
111
- "https://api.github.com/repos/Arduino/arduino-cli/git/refs/tags"
112
- ) ) . result || [ ] ;
113
-
114
- return tags
115
- . filter ( tag => tag . ref . match ( / \d + \. [ \w \. ] + / g) )
116
- . map ( tag => tag . ref . replace ( "refs/tags/" , "" ) ) ;
108
+ let rest : restm . RestClient = new restm . RestClient ( "setup-arduino-cli" ) ;
109
+ let tags : ITaskRef [ ] =
110
+ ( await rest . get < ITaskRef [ ] > (
111
+ "https://api.github.com/repos/Arduino/arduino-cli/git/refs/tags"
112
+ ) ) . result || [ ] ;
113
+
114
+ return tags
115
+ . filter ( tag => tag . ref . match ( / \d + \. [ \w \. ] + / g) )
116
+ . map ( tag => tag . ref . replace ( "refs/tags/" , "" ) ) ;
117
117
}
118
118
119
119
// Compute an actual version starting from the `version` configuration param.
120
120
async function computeVersion ( version : string ) : Promise < string > {
121
- // strip trailing .x chars
122
- if ( version . endsWith ( ".x" ) ) {
123
- version = version . slice ( 0 , version . length - 2 ) ;
124
- }
121
+ // strip trailing .x chars
122
+ if ( version . endsWith ( ".x" ) ) {
123
+ version = version . slice ( 0 , version . length - 2 ) ;
124
+ }
125
125
126
- const allVersions = await fetchVersions ( ) ;
127
- const possibleVersions = allVersions . filter ( v => v . startsWith ( version ) ) ;
126
+ const allVersions = await fetchVersions ( ) ;
127
+ const possibleVersions = allVersions . filter ( v => v . startsWith ( version ) ) ;
128
128
129
- const versionMap = new Map ( ) ;
130
- possibleVersions . forEach ( v => versionMap . set ( normalizeVersion ( v ) , v ) ) ;
129
+ const versionMap = new Map ( ) ;
130
+ possibleVersions . forEach ( v => versionMap . set ( normalizeVersion ( v ) , v ) ) ;
131
131
132
- const versions = Array . from ( versionMap . keys ( ) )
133
- . sort ( semver . rcompare )
134
- . map ( v => versionMap . get ( v ) ) ;
132
+ const versions = Array . from ( versionMap . keys ( ) )
133
+ . sort ( semver . rcompare )
134
+ . map ( v => versionMap . get ( v ) ) ;
135
135
136
- core . debug ( `evaluating ${ versions . length } versions` ) ;
136
+ core . debug ( `evaluating ${ versions . length } versions` ) ;
137
137
138
- if ( versions . length === 0 ) {
139
- throw new Error ( "unable to get latest version" ) ;
140
- }
138
+ if ( versions . length === 0 ) {
139
+ throw new Error ( "unable to get latest version" ) ;
140
+ }
141
141
142
- core . debug ( `matched: ${ versions [ 0 ] } ` ) ;
142
+ core . debug ( `matched: ${ versions [ 0 ] } ` ) ;
143
143
144
- return versions [ 0 ] ;
144
+ return versions [ 0 ] ;
145
145
}
146
146
147
147
// Make partial versions semver compliant.
148
148
function normalizeVersion ( version : string ) : string {
149
- const preStrings = [ "beta" , "rc" , "preview" ] ;
150
-
151
- const versionPart = version . split ( "." ) ;
152
- if ( versionPart [ 1 ] == null ) {
153
- //append minor and patch version if not available
154
- // e.g. 2 -> 2.0.0
155
- return version . concat ( ".0.0" ) ;
156
- } else {
157
- // handle beta and rc
158
- // e.g. 1.10beta1 -? 1.10.0-beta1, 1.10rc1 -> 1.10.0-rc1
159
- if ( preStrings . some ( el => versionPart [ 1 ] . includes ( el ) ) ) {
160
- versionPart [ 1 ] = versionPart [ 1 ]
161
- . replace ( "beta" , ".0-beta" )
162
- . replace ( "rc" , ".0-rc" )
163
- . replace ( "preview" , ".0-preview" ) ;
164
- return versionPart . join ( "." ) ;
165
- }
149
+ const preStrings = [ "beta" , "rc" , "preview" ] ;
150
+
151
+ const versionPart = version . split ( "." ) ;
152
+ if ( versionPart [ 1 ] == null ) {
153
+ //append minor and patch version if not available
154
+ // e.g. 2 -> 2.0.0
155
+ return version . concat ( ".0.0" ) ;
156
+ } else {
157
+ // handle beta and rc
158
+ // e.g. 1.10beta1 -? 1.10.0-beta1, 1.10rc1 -> 1.10.0-rc1
159
+ if ( preStrings . some ( el => versionPart [ 1 ] . includes ( el ) ) ) {
160
+ versionPart [ 1 ] = versionPart [ 1 ]
161
+ . replace ( "beta" , ".0-beta" )
162
+ . replace ( "rc" , ".0-rc" )
163
+ . replace ( "preview" , ".0-preview" ) ;
164
+ return versionPart . join ( "." ) ;
166
165
}
167
-
168
- if ( versionPart [ 2 ] == null ) {
169
- //append patch version if not available
170
- // e.g. 2.1 -> 2.1.0
171
- return version . concat ( ".0" ) ;
172
- } else {
173
- // handle beta and rc
174
- // e.g. 1.8.5beta1 -> 1.8.5-beta1, 1.8.5rc1 -> 1.8.5-rc1
175
- if ( preStrings . some ( el => versionPart [ 2 ] . includes ( el ) ) ) {
176
- versionPart [ 2 ] = versionPart [ 2 ]
177
- . replace ( "beta" , "-beta" )
178
- . replace ( "rc " , "-rc " )
179
- . replace ( "preview " , "-preview" ) ;
180
- return versionPart . join ( ". ") ;
181
- }
166
+ }
167
+
168
+ if ( versionPart [ 2 ] == null ) {
169
+ //append patch version if not available
170
+ // e.g. 2.1 -> 2.1.0
171
+ return version . concat ( ".0" ) ;
172
+ } else {
173
+ // handle beta and rc
174
+ // e.g. 1.8.5beta1 -> 1.8.5-beta1, 1.8.5rc1 -> 1.8.5-rc1
175
+ if ( preStrings . some ( el => versionPart [ 2 ] . includes ( el ) ) ) {
176
+ versionPart [ 2 ] = versionPart [ 2 ]
177
+ . replace ( "beta " , "-beta " )
178
+ . replace ( "rc " , "-rc" )
179
+ . replace ( "preview" , "-preview ") ;
180
+ return versionPart . join ( "." ) ;
182
181
}
182
+ }
183
183
184
- return version ;
184
+ return version ;
185
185
}
0 commit comments