Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
cakekindel committed Sep 17, 2024
1 parent 9814f15 commit bc1f9c4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/Spago/Cmd.purs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type ExecResult =

exitedOk :: Either ExecResult ExecResult -> Boolean
exitedOk = either identity identity >>> case _ of
{exit: Normally 0} -> true
{ exit: Normally 0 } -> true
_ -> false

exit :: Either ExecResult ExecResult -> Exit
Expand Down
74 changes: 42 additions & 32 deletions src/Spago/Psa.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import Spago.Prelude

import Codec.JSON.DecodeError as CJ.DecodeError
import Control.Alternative as Alternative
import Control.Monad.Except.Trans (ExceptT(..), runExceptT)
import Control.Monad.Trans.Class (lift)
import Data.Array as Array
import Data.Array.NonEmpty as NonEmptyArray
import Data.Codec.JSON as CJ
import Data.Either (blush)
import Data.Map as Map
import Data.Set as Set
import Data.String as Str
Expand All @@ -21,6 +24,7 @@ import Data.Tuple as Tuple
import Effect.Ref as Ref
import Foreign.Object as FO
import JSON as JSON
import Node.ChildProcess.Types (Exit(..))
import Node.Encoding as Encoding
import Node.FS.Aff as FSA
import Node.Path as Path
Expand All @@ -38,40 +42,46 @@ import Spago.Purs as Purs
defaultStatVerbosity :: Core.StatVerbosity
defaultStatVerbosity = Core.CompactStats

-- Left decodeErrMsg -> do
-- logWarn $ Array.intercalate "\n"
-- [ "Failed to decode PsaResult at index '" <> show idx <> "': " <> decodeErrMsg
-- , "Json was: " <> err
-- ]
-- -- If we can't decode the error, then there's likely a codec issue on Spago's side.
-- -- So, this shouldn't fail the build.
-- pure true

psaCompile :: forall a. Set.Set FilePath -> Array String -> PsaArgs -> Spago (Purs.PursEnv a) Boolean
psaCompile globs pursArgs psaArgs = do
result <- Purs.compile globs (Array.snoc pursArgs "--json-errors")
let resultStdout = Cmd.getStdout result
arrErrorsIsEmpty <- forWithIndex (Str.split (Str.Pattern "\n") resultStdout) \idx err ->
case JSON.parse err >>= CJ.decode psaResultCodec >>> lmap CJ.DecodeError.print of
Left decodeErrMsg -> do
logWarn $ Array.intercalate "\n"
[ "Failed to decode PsaResult at index '" <> show idx <> "': " <> decodeErrMsg
, "Json was: " <> err
]
-- If we can't decode the error, then there's likely a codec issue on Spago's side.
-- So, this shouldn't fail the build.
pure true
Right out -> do
files <- liftEffect $ Ref.new FO.empty
out' <- buildOutput (loadLines files) psaArgs out

liftEffect $ if psaArgs.jsonErrors then printJsonOutputToOut out' else printDefaultOutputToErr psaArgs out'

pure $ FO.isEmpty out'.stats.allErrors

if Array.all identity arrErrorsIsEmpty && Cmd.exitedOk result then do
logSuccess "Build succeeded."
pure true
else if Array.all identity arrErrorsIsEmpty && not (Cmd.exitedOk result) then do
prepareToDie [ "purs exited with non-ok status code: " <> show (Cmd.exit result) ]
pure false
else do
case result of
Left r -> logDebug $ Cmd.printExecResult r
_ -> pure unit
prepareToDie [ "Failed to build." ]
pure false
purs <- Purs.compile globs (Array.snoc pursArgs "--json-errors")
let
resultStdout = Cmd.getStdout purs
print' = if psaArgs.jsonErrors then printJsonOutputToOut else printDefaultOutputToErr psaArgs

errors <- for (Str.split (Str.Pattern "\n") resultStdout) \err -> runExceptT do
out <- ExceptT $ pure $ JSON.parse err >>= CJ.decode psaResultCodec >>> lmap CJ.DecodeError.print
files <- liftEffect $ Ref.new FO.empty
out' <- lift $ buildOutput (loadLines files) psaArgs out
liftEffect (print' out') $> FO.isEmpty out'.stats.allErrors

let
noErrors = Array.all (either (const true) identity) errors
failedToDecodeMsg (idx /\ err) =
Array.intercalate "\n"
[ "Failed to decode PsaResult at index '" <> show idx <> "': " <> err
, "Json was: " <> err
]
failedToDecode = failedToDecodeMsg <$> Array.catMaybes (Array.mapWithIndex (\idx e -> (idx /\ _) <$> blush e) errors)

case Cmd.exit purs, noErrors of
Normally 0, true -> for failedToDecode logWarn *> logSuccess "Build succeeded." $> true
_, true -> prepareToDie [ "purs exited with non-ok status code: " <> show (Cmd.exit purs) ] $> false
_, _ -> do
case purs of
Left r -> logDebug $ Cmd.printExecResult r
_ -> pure unit
prepareToDie [ "Failed to build." ]
pure false

where
isEmptySpan filename pos =
Expand Down

0 comments on commit bc1f9c4

Please sign in to comment.