on master help sign in
Expose runtime, add Elm.Utils for all projects to share
commit10a84b4d6c2db10ed8536d2473097d6906a1d081 authorEvan Czaplicki <info@elm-lang.org> dateTue 4 Nov 2014, 17:36 (12 years ago) parent6d50056
3 files changed+81−3
compiler/Elm/Compiler.hs+12−1 compiler/Elm/Utils.hs+64 elm-compiler.cabal+5−2
compiler/Elm/Compiler.hs+12−1
@@ -1,6 +1,6 @@
11{-# OPTIONS_GHC -Wall #-}
22{-# LANGUAGE FlexibleContexts #-}
3−module Elm.Compiler (version, parseDependencies, compile) where
3+module Elm.Compiler (version, parseDependencies, compile, runtimePath) where
44
55import Control.Monad.Error (MonadError, throwError)
66import qualified Data.List as List
@@ -11,9 +11,11 @@ import qualified AST.Module as Module (HeaderAndImports(HeaderAndImports), toInt
1111import qualified Build.Source as Source
1212import qualified Elm.Compiler.Module as PublicModule
1313import qualified Elm.Compiler.Version as Version
14+import qualified Elm.Utils as Utils
1415import qualified Generate.JavaScript as JS
1516import qualified Parse.Helpers as Help
1617import qualified Parse.Module as Parse
18+import qualified Paths_elm_compiler as Compiler
1719
1820
1921-- VERSION
@@ -55,3 +57,12 @@ compile source interfaces =
5557 where
5658 unwrappedInterfaces =
5759 Map.mapKeysMonotonic (\(PublicModule.Name name) -> name) interfaces
60+
61+
62+-- DATA FILES
63+
64+{-| Path to the runtime.
65+-}
66+runtimePath :: IO FilePath
67+runtimePath =
68+ Utils.getAsset "compiler" Compiler.getDataFileName "runtime/core.js"
compiler/Elm/Utils.hsnew file+64
@@ -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+ ]
elm-compiler.cabal+5−2
@@ -27,11 +27,13 @@ Extra-source-files: changelog.md
2727Cabal-version: >=1.9
2828Build-type: Simple
2929
30+Data-files:
31+ runtime/core.js
32+
3033source-repository head
3134 type: git
3235 location: git://github.com/elm-lang/Elm.git
3336
34−
3537Library
3638 ghc-options:
3739 -threaded -O2 -W
@@ -42,7 +44,8 @@ Library
4244 exposed-modules:
4345 Elm.Compiler,
4446 Elm.Compiler.Module,
45− Elm.Docs
47+ Elm.Docs,
48+ Elm.Utils
4649
4750 other-modules:
4851 Elm.Compiler.Version,