on master help sign in
Move executable runner to Elm.Utils
8219913Evan Czaplicki
compiler/Elm/Utils.hs+48−1
@@ -1,10 +1,14 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# OPTIONS_GHC -Wall #-}
2+{-# LANGUAGE FlexibleContexts #-}
2−module Elm.Utils ((|>), (<|), getAsset) where
3+module Elm.Utils ((|>), (<|), getAsset, run, unwrappedRun, CommandError(..)) where
3
4
5+import Control.Monad.Error (MonadError, MonadIO, liftIO, throwError)
4import System.Directory (doesFileExist)
6import System.Directory (doesFileExist)
5import System.Environment (getEnv)
7import System.Environment (getEnv)
8+import System.Exit (ExitCode(ExitSuccess, ExitFailure))
6import System.FilePath ((</>))
9import System.FilePath ((</>))
7import System.IO.Error (tryIOError)
10import System.IO.Error (tryIOError)
11+import System.Process (readProcessWithExitCode)
8
12
9
13
10{-| Forward function application `x |> f == f x`. This function is useful
14{-| Forward function application `x |> f == f x`. This function is useful
@@ -25,6 +29,49 @@ infixr 0 <|
25infixl 0 |>
29infixl 0 |>
26
30
27
31
32+-- RUN EXECUTABLES
33+
34+data CommandError
35+ = MissingExe
36+ | CommandFailed String
37+
38+
39+{-| Run a command, throw an error if the command is not found or if something
40+goes wrong.
41+-}
42+run :: (MonadError String m, MonadIO m) => String -> [String] -> m String
43+run command args =
44+ do result <- liftIO (unwrappedRun command args)
45+ case result of
46+ Right out -> return out
47+ Left err ->
48+ throwError (context (message err))
49+ where
50+ context msg =
51+ "failure when running:" ++ concatMap (' ':) (command:args) ++ "\n" ++ msg
52+
53+ message err =
54+ case err of
55+ CommandFailed msg -> msg
56+ MissingExe ->
57+ "Could not find command '" ++ command ++ "'. Do you have it installed?\n\
58+ \ Can it be run from anywhere? Is it on your PATH?"
59+
60+
61+unwrappedRun :: String -> [String] -> IO (Either CommandError String)
62+unwrappedRun command args =
63+ do (exitCode, stdout, stderr) <- readProcessWithExitCode command args ""
64+ return $
65+ case exitCode of
66+ ExitSuccess -> Right stdout
67+ ExitFailure code
68+ | code == 127 -> Left MissingExe -- UNIX
69+ | code == 9009 -> Left MissingExe -- Windows
70+ | otherwise -> Left (CommandFailed stderr)
71+
72+
73+-- GET STATIC ASSETS
74+
28{-| Get the absolute path to a data file. If you install with cabal it will look
75{-| Get the absolute path to a data file. If you install with cabal it will look
29-}
76-}
30getAsset :: String -> (FilePath -> IO FilePath) -> FilePath -> IO FilePath
77getAsset :: String -> (FilePath -> IO FilePath) -> FilePath -> IO FilePath
elm-compiler.cabal+1
@@ -125,6 +125,7 @@ Library
125 mtl >= 2 && < 3,
125 mtl >= 2 && < 3,
126 parsec >= 3.1.1 && < 3.5,
126 parsec >= 3.1.1 && < 3.5,
127 pretty >= 1.0 && < 2.0,
127 pretty >= 1.0 && < 2.0,
128+ process,
128 text >= 1 && < 2,
129 text >= 1 && < 2,
129 transformers >= 0.2 && < 0.5,
130 transformers >= 0.2 && < 0.5,
130 union-find >= 0.2 && < 0.3,
131 union-find >= 0.2 && < 0.3,