1- const core = require ( "@actions/core" ) ;
2- const tc = require ( "@actions/tool-cache" ) ;
3- const semver = require ( "semver" ) ;
1+ import * as core from "@actions/core" ;
2+ import * as tc from "@actions/tool-cache" ;
3+ import * as semver from "semver" ;
4+ import * as path from "path" ;
5+ import * as axios from "axios" ;
46
57const PureScript = "PureScript" ;
68const Spago = "Spago" ;
@@ -20,25 +22,26 @@ const toolVersionKey = (tool) => {
2022 if ( tool === Spago ) return "spago-version" ;
2123} ;
2224
23- const toolLatestTag = ( tool ) => {
24- // TODO
25- // Get the latest tag automatically:
26- // TAG=$(basename $(curl --location --silent --output /dev/null -w %{url_effective} https://github.com/purescript/purescript/releases/latest))
27- if ( tool === PureScript ) return "0.13.8" ;
28- if ( tool === Spago ) return "0.15.3" ;
29- } ;
30-
31- const toolVersion = ( tool ) => {
25+ const toolVersion = async ( tool ) => {
3226 const key = toolVersionKey ( tool ) ;
33- const input = core . getInput ( tool ) ;
34- if ( input ) {
35- if ( semver . valid ( input ) ) {
36- return input ;
37- } else {
38- core . setFailed ( `${ input } is not valid for ${ key } .` ) ;
27+ const input = core . getInput ( key ) ;
28+
29+ if ( input === "latest" ) {
30+ core . info ( `Fetching latest tag for ${ tool } ` ) ;
31+ const repo = toolRepository ( tool ) ;
32+ const url = `https://api.github.com/repos/${ repo } /releases/latest` ;
33+
34+ try {
35+ const response = await axios . get ( url ) ;
36+ return response . data . tag_name ;
37+ } catch ( err ) {
38+ core . setFailed ( `Failed to get latest tag: ${ err } ` ) ;
3939 }
40+ } else if ( semver . valid ( input ) ) {
41+ if ( tool === PureScript ) return `v${ input } ` ;
42+ return input ;
4043 } else {
41- return toolLatestTag ( tool ) ;
44+ core . setFailed ( ` ${ input } is not valid for ${ key } .` ) ;
4245 }
4346} ;
4447
@@ -65,39 +68,40 @@ const tarballName = (tool, platform) => {
6568} ;
6669
6770const downloadTool = async ( tool ) => {
68- const version = toolVersion ( tool ) ;
71+ const version = await toolVersion ( tool ) ;
6972 const name = toolName ( tool ) ;
7073
7174 // If the tool has previously been downloaded at the provided version, then we
72- // can simply add it to the PATH
75+ // can simply add it to the PATH.
7376 const cached = tc . find ( name , version ) ;
7477 if ( cached ) {
78+ core . info (
79+ `Found cached version of ${ name } at version ${ version } , adding to PATH.`
80+ ) ;
7581 core . addPath ( cached ) ;
76- console . log ( `Found cached version of ${ name } , adding to PATH` ) ;
7782 return ;
7883 }
7984
85+ core . info (
86+ `Did not find cached version of ${ name } at version ${ version } , fetching.`
87+ ) ;
88+
8089 const platform = parsePlatform ( process . platform ) ;
8190 const tarball = tarballName ( tool , platform ) ;
8291 const repo = toolRepository ( tool ) ;
92+ const url = `https://github.com/${ repo } /releases/download/${ version } /${ tarball } .tar.gz` ;
8393
84- const downloadPath = await tc . downloadTool (
85- `https://github.com/${ repo } /releases/download/${ version } /${ tarball } .tar.gz`
86- ) ;
94+ const downloadPath = await tc . downloadTool ( url ) ;
95+ let extracted = await tc . extractTar ( downloadPath ) ;
8796
88- const extracted = await tc . extractTar ( downloadPath ) ;
89-
90- switch ( tool ) {
91- case PureScript :
92- const purescriptPath = await tc . cacheDir ( extracted , name , version ) ;
93- core . addPath ( purescriptPath ) ;
94- return ;
95-
96- case Spago :
97- let spagoPath = await tc . cacheFile ( extracted , name , name , version ) ;
98- core . addPath ( spagoPath ) ;
99- return ;
97+ if ( tool === PureScript ) {
98+ extracted = path . join ( extracted , "purescript" ) ;
10099 }
100+
101+ const cachedPath = await tc . cacheDir ( extracted , name , version ) ;
102+ core . info ( `Cached path ${ cachedPath } .` ) ;
103+ core . addPath ( cachedPath ) ;
104+ return ;
101105} ;
102106
103107const run = async ( ) => {
0 commit comments