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)
14{-| Forward function application `x |> f == f x`. This function is useful
15for avoiding parenthesis and writing code in a more natural way.
17(|>) :: a -> (a -> b) -> b
21{-| Backward function application `f <| x == f x`. This function is useful for
24(<|) :: (a -> b) -> a -> b
36 | CommandFailed String String
39{-| Run a command, throw an error if the command is not found or if something
42run :: (MonadError String m, MonadIO m) => String -> [String] -> m String
44 do result <- liftIO (unwrappedRun command args)
46 Right out -> return out
48 throwError (context (message err))
51 "failure when running:" ++ concatMap (' ':) (command:args) ++ "\n" ++ msg
55 CommandFailed stderr stdout ->
61unwrappedRun :: String -> [String] -> IO (Either CommandError String)
62unwrappedRun command args =
63 do (exitCode, stdout, stderr) <- readProcessWithExitCode command args ""
66 ExitSuccess -> Right stdout
68 | code == 127 -> Left (missingExe command) -- UNIX
69 | code == 9009 -> Left (missingExe command) -- Windows
70 | otherwise -> Left (CommandFailed stdout stderr)
73missingExe :: String -> CommandError
76 "Could not find command '" ++ command ++ "'. Do you have it installed?\n\
77 \ Can it be run from anywhere? Is it on your PATH?"
83{-| Get the absolute path to a data file. If you install with cabal it will look
85getAsset :: String -> (FilePath -> IO FilePath) -> FilePath -> IO FilePath
86getAsset project getDataFileName name =
87 do path <- getDataFileName name
88 exists <- doesFileExist path
92 environment <- tryIOError (getEnv "ELM_HOME")
95 return (env </> project </> name)
98 fail (errorNotFound name)
101errorNotFound :: FilePath -> String
104 [ "Unable to find the ELM_HOME environment variable when searching"
105 , "for the " ++ name ++ " file."
107 , "If you installed Elm Platform with the Mac or Windows installer, it looks like"
108 , "ELM_HOME was not set automatically. Look up how to set environment variables"
109 , "on your platform and set ELM_HOME to the directory that contains Elm's static"
112 , " * On Mac it is /usr/local/share/elm"
113 , " * On Windows it is one of the following:"
114 , " C:/Program Files/Elm Platform/0.13/share"
115 , " C:/Program Files (x86)/Elm Platform/0.13/share"
117 , "If it seems like a more complex issue, please report it here:"
118 , " <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