@@ -0,0 +1,64 @@
1+{-# OPTIONS_GHC -Wall #-}
2+module Elm.Utils ((|>), (<|), getAsset) where
3+
4+import System.Directory (doesFileExist)
5+import System.Environment (getEnv)
6+import System.FilePath ((</>))
7+import System.IO.Error (tryIOError)
8+
9+
10+{-| Forward function application `x |> f == f x`. This function is useful
11+for avoiding parenthesis and writing code in a more natural way.
12+-}
13+(|>) :: a -> (a -> b) -> b
14+x |> f = f x
15+
16+
17+{-| Backward function application `f <| x == f x`. This function is useful for
18+avoiding parenthesis.
19+-}
20+(<|) :: (a -> b) -> a -> b
21+f <| x = f x
22+
23+
24+infixr 0 <|
25+infixl 0 |>
26+
27+
28+{-| Get the absolute path to a data file. If you install with cabal it will look
29+-}
30+getAsset :: String -> (FilePath -> IO FilePath) -> FilePath -> IO FilePath
31+getAsset project getDataFileName name =
32+ do path <- getDataFileName name
33+ exists <- doesFileExist path
34+ if exists
35+ then return path
36+ else do
37+ environment <- tryIOError (getEnv "ELM_HOME")
38+ case environment of
39+ Right env ->
40+ return (env </> project </> name)
41+
42+ Left _ ->
43+ fail (errorNotFound name)
44+
45+
46+errorNotFound :: FilePath -> String
47+errorNotFound name =
48+ unlines
49+ [ "Unable to find the ELM_HOME environment variable when searching"
50+ , "for the " ++ name ++ " file."
51+ , ""
52+ , "If you installed Elm Platform with the Mac or Windows installer, it looks like"
53+ , "ELM_HOME was not set automatically. Look up how to set environment variables"
54+ , "on your platform and set ELM_HOME to the directory that contains Elm's static"
55+ , "files:"
56+ , ""
57+ , " * On Mac it is /usr/local/share/elm"
58+ , " * On Windows it is one of the following:"
59+ , " C:/Program Files/Elm Platform/0.13/share"
60+ , " C:/Program Files (x86)/Elm Platform/0.13/share"
61+ , ""
62+ , "If it seems like a more complex issue, please report it here:"
63+ , " <https://github.com/elm-lang/elm-platform/issues>"
64+ ]