Skip to content

Use purescript-github-actions-toolkit library #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages.dhall
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let upstream =
https://github.com/purescript/package-sets/releases/download/psc-0.13.8-20200724/packages.dhall sha256:bb941d30820a49345a0e88937094d2b9983d939c9fd3a46969b85ce44953d7d9
https://github.com/purescript/package-sets/releases/download/psc-0.13.8-20200909/packages.dhall sha256:b899488adf6f02a92bbaae88039935bbc61bcba4cf4462f6d915fc3d0e094604

in upstream
with versions =
Expand Down
1 change: 1 addition & 0 deletions spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
, "argonaut-core"
, "console"
, "effect"
, "github-actions-toolkit"
, "node-fs"
, "node-path"
, "node-process"
Expand Down
22 changes: 0 additions & 22 deletions src/Actions/Core.js

This file was deleted.

47 changes: 0 additions & 47 deletions src/Actions/Core.purs

This file was deleted.

3 changes: 0 additions & 3 deletions src/Actions/Exec.js

This file was deleted.

20 changes: 0 additions & 20 deletions src/Actions/Exec.purs

This file was deleted.

15 changes: 0 additions & 15 deletions src/Actions/ToolCache.js

This file was deleted.

95 changes: 0 additions & 95 deletions src/Actions/ToolCache.purs

This file was deleted.

17 changes: 13 additions & 4 deletions src/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@ module Main where

import Prelude

import Control.Monad.Except.Trans (mapExceptT, runExceptT)
import Data.Argonaut.Core (Json)
import Data.Either (Either(..))
import Data.Foldable (traverse_)
import Effect (Effect)
import Effect.Aff (launchAff_)
import Effect.Aff (launchAff_, runAff_)
import Effect.Class (liftEffect)
import Effect.Exception (message)
import GitHub.Actions.Core as Core
import Setup.BuildPlan (constructBuildPlan)
import Setup.GetTool (getTool)
import Setup.UpdateVersions (updateVersions)

main :: Json -> Effect Unit
main json = do
tools <- constructBuildPlan json
launchAff_ $ traverse_ getTool tools
main json = runAff_ go $ runExceptT do
tools <- mapExceptT liftEffect $ constructBuildPlan json
traverse_ getTool tools
where
go res = case join res of
Left err -> Core.setFailed (message err)
Right a -> pure unit

update :: Effect Unit
update = launchAff_ updateVersions
48 changes: 21 additions & 27 deletions src/Setup/BuildPlan.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ module Setup.BuildPlan (constructBuildPlan, BuildPlan) where

import Prelude

import Actions.Core as Core
import Control.Monad.Except.Trans (ExceptT)
import Data.Argonaut.Core (Json)
import Data.Argonaut.Decode (decodeJson, printJsonDecodeError, (.:))
import Data.Array as Array
import Data.Bifunctor (bimap, lmap)
import Data.Bifunctor (lmap)
import Data.Either (Either(..))
import Data.Foldable (fold)
import Data.Maybe (Maybe(..))
import Data.Newtype (unwrap)
import Data.Traversable (traverse)
import Data.Version (Version)
import Data.Version as Version
import Effect (Effect)
import Effect.Aff (error, throwError)
import Effect.Class (liftEffect)
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, required)
import Setup.Data.Tool (Tool)
import Setup.Data.Tool as Tool
import Text.Parsing.Parser (parseErrorMessage)
import Text.Parsing.Parser as ParseError
Expand All @@ -26,42 +28,34 @@ import Text.Parsing.Parser as ParseError
type BuildPlan = Array { tool :: Tool, version :: Version }

-- | Construct the list of tools that sholud be downloaded and cached by the action
constructBuildPlan :: Json -> Effect BuildPlan
constructBuildPlan json = map Array.catMaybes $ traverse (resolve json) Tool.allTools
constructBuildPlan :: Json -> ExceptT Error Effect BuildPlan
constructBuildPlan json = 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 -> Effect (Maybe (Either String VersionField))
getVersionField = map (map parse) <<< Core.getInput
where
parse = case _ of
getVersionField :: Key -> ExceptT Error Effect VersionField
getVersionField key = do
value <- Core.getInput' (unwrap key)
case value of
"latest" -> pure Latest
value -> bimap ParseError.parseErrorMessage Exact (Version.parseVersion value)
val -> case Version.parseVersion val of
Left msg -> throwError (error (ParseError.parseErrorMessage msg))
Right version -> pure (Exact version)

-- | Resolve the exact version to provide for a tool in the environment, based
-- | on the action.yml file.
resolve :: Json -> Tool -> Effect (Maybe { tool :: Tool, version :: Version })
resolve :: Json -> Tool -> ExceptT Error Effect { tool :: Tool, version :: Version }
resolve versionsContents tool = do
let key = Key.fromTool tool
getVersionField key >>= case _ of
Nothing | required tool -> throwError $ error "No input received for required key."
Nothing -> pure Nothing
Just field -> map Just $ getVersion field

where
getVersion :: Either String VersionField -> Effect { tool :: Tool, version :: Version }
getVersion = case _ of
Left err -> do
Core.setFailed $ fold [ "Unable to parse version: ", err ]
throwError $ error "Unable to complete fetching version."

Right (Exact v) -> do
field <- getVersionField key
case field of
Exact v -> liftEffect do
Core.info "Found exact version"
pure { tool, version: v }

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

let
Expand Down
3 changes: 3 additions & 0 deletions src/Setup/Data/Key.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ module Setup.Data.Key
, fromTool
) where

import Data.Newtype (class Newtype)
import Setup.Data.Tool (Tool(..))

newtype Key = Key String

derive instance newtypeKey :: Newtype Key _

purescriptKey :: Key
purescriptKey = Key "purescript"

Expand Down
Loading