1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE FlexibleContexts #-}
3module Elm.Utils ((|>), (<|), getAsset, run, unwrappedRun, CommandError(..)) where
5import Control.Monad.Error (MonadError, MonadIO, liftIO, throwError)
6import System.Directory (doesFileExist)
7import System.Environment (getEnv)
8import System.Exit (ExitCode(ExitSuccess, ExitFailure))
9import System.FilePath ((</>))
10import System.IO.Error (tryIOError)
11import System.Process (readProcessWithExitCode)
13import qualified Elm.Compiler.Version as Version
16{-| Forward function application `x |> f == f x`. This function is useful
17for avoiding parenthesis and writing code in a more natural way.
19(|>) :: a -> (a -> b) -> b
23{-| Backward function application `f <| x == f x`. This function is useful for
26(<|) :: (a -> b) -> a -> b
38 | CommandFailed String String
41{-| Run a command, throw an error if the command is not found or if something
44run :: (MonadError String m, MonadIO m) => String -> [String] -> m String
46 do result <- liftIO (unwrappedRun command args)
48 Right out -> return out
50 throwError (context (message err))
53 "failure when running:" ++ concatMap (' ':) (command:args) ++ "\n" ++ msg
57 CommandFailed stderr stdout ->
63unwrappedRun :: String -> [String] -> IO (Either CommandError String)
64unwrappedRun command args =
65 do (exitCode, stdout, stderr) <- readProcessWithExitCode command args ""
68 ExitSuccess -> Right stdout
70 | code == 127 -> Left (missingExe command) -- UNIX
71 | code == 9009 -> Left (missingExe command) -- Windows
72 | otherwise -> Left (CommandFailed stdout stderr)
75missingExe :: String -> CommandError
78 "Could not find command '" ++ command ++ "'. Do you have it installed?\n\
79 \ Can it be run from anywhere? Is it on your PATH?"
85{-| Get the absolute path to a data file. If you install with cabal it will look
87getAsset :: String -> (FilePath -> IO FilePath) -> FilePath -> IO FilePath
88getAsset project getDataFileName name =
89 do path <- getDataFileName name
90 exists <- doesFileExist path
94 environment <- tryIOError (getEnv "ELM_HOME")
97 return (env </> project </> name)
100 fail (errorNotFound name)
103errorNotFound :: FilePath -> String
106 [ "Unable to find the ELM_HOME environment variable when searching"
107 , "for the " ++ name ++ " file."
109 , "If you installed Elm Platform with the Mac or Windows installer, it looks like"
110 , "ELM_HOME was not set automatically. Look up how to set environment variables"
111 , "on your platform and set ELM_HOME to the directory that contains Elm's static"
114 , " * On Mac it is /usr/local/share/elm"
115 , " * On Windows it is one of the following:"
116 , " C:/Program Files/Elm Platform/" ++ Version.version ++ "/share"
117 , " C:/Program Files (x86)/Elm Platform/" ++ Version.version ++ "/share"
119 , "If it seems like a more complex issue, please report it here:"
120 , " <https://github.com/elm-lang/elm-platform/issues>"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list