Skip to content

Commit

Permalink
Not use the versions file for NPM tools
Browse files Browse the repository at this point in the history
  • Loading branch information
pete-murphy committed Aug 10, 2021
1 parent 915bfb6 commit a5436b3
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 46 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/update.js

Large diffs are not rendered by default.

32 changes: 17 additions & 15 deletions src/Setup/BuildPlan.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import Data.Argonaut.Decode (decodeJson, printJsonDecodeError, (.:))
import Data.Array as Array
import Data.Bifunctor (lmap)
import Data.Either (Either(..))
import Data.Foldable (fold)
import Data.Foldable (elem, fold)
import Data.Maybe (Maybe(..))
import Data.Traversable (traverse)
import Data.Version (Version)
import Data.Version as Version
import Effect (Effect)
import Effect.Aff (error, throwError)
Expand All @@ -20,21 +19,19 @@ import Effect.Exception (Error)
import GitHub.Actions.Core as Core
import Setup.Data.Key (Key)
import Setup.Data.Key as Key
import Setup.Data.Tool (Tool)
import Setup.Data.Tool (Tool(..))
import Setup.Data.Tool as Tool
import Setup.Data.VersionField (VersionField(..))
import Text.Parsing.Parser (parseErrorMessage)
import Text.Parsing.Parser as ParseError

-- | The list of tools that should be downloaded and cached by the action
type BuildPlan = Array { tool :: Tool, version :: Version }
type BuildPlan = Array { tool :: Tool, versionField :: VersionField }

-- | Construct the list of tools that sholud be downloaded and cached by the action
constructBuildPlan :: Json -> ExceptT Error Effect BuildPlan
constructBuildPlan json = map Array.catMaybes $ traverse (resolve json) Tool.allTools

-- | The parsed value of an input field that specifies a version
data VersionField = Latest | Exact Version

-- | Attempt to read the value of an input specifying a tool version
getVersionField :: Key -> ExceptT Error Effect (Maybe VersionField)
getVersionField key = do
Expand All @@ -52,19 +49,24 @@ getVersionField key = do
pure (pure (Exact version))

-- | Resolve the exact version to provide for a tool in the environment, based
-- | on the action.yml file.
resolve :: Json -> Tool -> ExceptT Error Effect (Maybe { tool :: Tool, version :: Version })
-- | on the action.yml file. In the case of NPM packages, bypass the action.yml
-- | file and use 'Latest'.
resolve :: Json -> Tool -> ExceptT Error Effect (Maybe { tool :: Tool, versionField :: VersionField })
resolve versionsContents tool = do
let key = Key.fromTool tool
field <- getVersionField key
case field of
Nothing -> pure Nothing
case field, tool `elem` [ Psa, PursTidy ] of
Nothing, _ -> pure Nothing

Just (Exact v) -> liftEffect do
Just (Exact v), _ -> liftEffect do
Core.info "Found exact version"
pure (pure { tool, version: v })
pure (pure { tool, versionField: Exact v })

Just Latest, true -> liftEffect do
Core.info $ fold [ "Fetching latest tag for ", Tool.name tool ]
pure (pure { tool, versionField: Latest })

Just Latest -> liftEffect do
Just Latest, false -> liftEffect do
Core.info $ fold [ "Fetching latest tag for ", Tool.name tool ]

let
Expand All @@ -77,4 +79,4 @@ resolve versionsContents tool = do
throwError $ error "Unable to complete fetching version."

Right v -> do
pure (pure { tool, version: v })
pure (pure { tool, versionField: Exact v })
54 changes: 32 additions & 22 deletions src/Setup/Data/Tool.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ module Setup.Data.Tool where
import Prelude

import Affjax (URL)
import Data.Bounded.Generic (genericBottom, genericTop)
import Data.Either (fromRight')
import Data.Enum (class Enum, upFromIncluding)
import Data.Enum.Generic (genericPred, genericSucc)
import Data.Foldable (elem, fold)
import Data.Generic.Rep (class Generic)
import Data.Bounded.Generic (genericBottom, genericTop)
import Data.Enum.Generic (genericPred, genericSucc)
import Data.Version (Version, parseVersion)
import Data.Version as Version
import Data.Version (parseVersion)
import Node.Path (FilePath)
import Node.Path as Path
import Partial.Unsafe (unsafeCrashWith)
import Setup.Data.Platform (Platform(..), platform)
import Setup.Data.VersionField (VersionField(..))
import Setup.Data.VersionField as VersionField

data Tool
= PureScript
Expand Down Expand Up @@ -75,6 +76,9 @@ repository = case _ of
Zephyr ->
{ owner: "coot", name: "zephyr" }

isNPMTool :: Tool -> Boolean
isNPMTool = (_ == Psa) || (_ == PursTidy)

-- | How a tool will be installed: either a tarball from a URL, or an NPM package
-- | at a particular version.
data InstallMethod = Tarball TarballOpts | NPM NPMPackage
Expand All @@ -91,8 +95,8 @@ type NPMPackage = String

-- | The installation method for a tool, which includes the source path necessary
-- | to download or install the tool.
installMethod :: Tool -> Version -> InstallMethod
installMethod tool version = do
installMethod :: Tool -> VersionField -> InstallMethod
installMethod tool versionField = do
let
toolName = name tool
toolRepo = repository tool
Expand All @@ -118,26 +122,32 @@ installMethod tool version = do
Spago -> Tarball
{ source: formatGitHub'
-- Spago has changed naming conventions from version to version
if version >= unsafeVersion "0.18.1" then case platform of
Windows -> "Windows"
Mac -> "macOS"
Linux -> "Linux"
else if version == unsafeVersion "0.18.0" then case platform of
Windows -> "windows-latest"
Mac -> "macOS-latest"
Linux -> "linux-latest"
else case platform of
Windows -> "windows"
Mac -> "osx"
Linux -> "linux"
case versionField of
Exact version
| version >= unsafeVersion "0.18.1" ->
case platform of
Windows -> "Windows"
Mac -> "macOS"
Linux -> "Linux"
| version == unsafeVersion "0.18.1" ->
case platform of
Windows -> "windows-latest"
Mac -> "macOS-latest"
Linux -> "linux-latest"
_ ->
case platform of
Windows -> "windows"
Mac -> "osx"
Linux -> "linux"

, getExecutablePath: \p -> Path.concat [ p, executableName ]
}

Psa ->
NPM ("purescript-psa@" <> Version.showVersion version)
NPM ("purescript-psa@" <> VersionField.showVersionField versionField)

PursTidy ->
NPM ("purs-tidy@" <> Version.showVersion version)
NPM ("purs-tidy@" <> VersionField.showVersionField versionField)

Zephyr -> Tarball
{ source: formatGitHub' $ case platform of
Expand All @@ -153,8 +163,8 @@ installMethod tool version = do
-- Example: "v0.13.2", "0.15.2"
formatTag :: String
formatTag = do
let versionStr = Version.showVersion version
if tool `elem` [ PureScript, Zephyr, Psa ] then
let versionStr = VersionField.showVersionField versionField
if tool `elem` [ PureScript, Zephyr ] then
fold [ "v", versionStr ]
else
versionStr
Expand Down
12 changes: 12 additions & 0 deletions src/Setup/Data/VersionField.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Setup.Data.VersionField (VersionField(..), showVersionField) where

import Data.Version (Version)
import Data.Version as Version

-- | The parsed value of an input field that specifies a version
data VersionField = Latest | Exact Version

showVersionField :: VersionField -> String
showVersionField = case _ of
Latest -> "latest"
Exact v -> Version.showVersion v
14 changes: 7 additions & 7 deletions src/Setup/GetTool.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import Prelude
import Control.Monad.Except.Trans (ExceptT, mapExceptT)
import Data.Foldable (fold)
import Data.Maybe (Maybe(..))
import Data.Version (Version)
import Data.Version as Version
import Effect.Aff (Aff)
import Effect.Class (liftEffect)
import Effect.Exception (Error)
Expand All @@ -16,18 +14,20 @@ import GitHub.Actions.ToolCache as ToolCache
import Setup.Data.Platform (Platform(..), platform)
import Setup.Data.Tool (InstallMethod(..), Tool)
import Setup.Data.Tool as Tool
import Setup.Data.VersionField (VersionField)
import Setup.Data.VersionField as VersionField

getTool :: { tool :: Tool, version :: Version } -> ExceptT Error Aff Unit
getTool { tool, version } = do
getTool :: { tool :: Tool, versionField :: VersionField } -> ExceptT Error Aff Unit
getTool { tool, versionField } = do
let
name = Tool.name tool
installMethod = Tool.installMethod tool version
installMethod = Tool.installMethod tool versionField

liftEffect $ Core.info $ fold [ "Fetching ", name ]

case installMethod of
Tarball opts -> do
mbPath <- mapExceptT liftEffect $ ToolCache.find { arch: Nothing, toolName: name, versionSpec: Version.showVersion version }
mbPath <- mapExceptT liftEffect $ ToolCache.find { arch: Nothing, toolName: name, versionSpec: VersionField.showVersionField versionField }
case mbPath of
Just path -> liftEffect do
Core.info $ fold [ "Found cached version of ", name ]
Expand All @@ -36,7 +36,7 @@ getTool { tool, version } = do
Nothing -> do
downloadPath <- ToolCache.downloadTool' opts.source
extractedPath <- ToolCache.extractTar' downloadPath
cached <- ToolCache.cacheFile { sourceFile: opts.getExecutablePath extractedPath, tool: name, version: Version.showVersion version, targetFile: name, arch: Nothing }
cached <- ToolCache.cacheFile { sourceFile: opts.getExecutablePath extractedPath, tool: name, version: VersionField.showVersionField versionField, targetFile: name, arch: Nothing }

liftEffect do
Core.info $ fold [ "Cached path ", cached, ", adding to PATH" ]
Expand Down

0 comments on commit a5436b3

Please sign in to comment.