on master help sign in
Combine two Metadata files into one
commita4468d2eacd09f4005b6ac23e8b0d87fdf594d07 authorEvan Czaplicki <info@elm-lang.org> dateThu 25 Jul 2013, 20:39 (13 years ago) parent0db809f
2 files changed+37−49
compiler/Metadata/Libraries.hs+37−12 compiler/Metadata/Load.hs−37
compiler/Metadata/Libraries.hs+37−12
@@ -1,26 +1,51 @@
1−module Metadata.Libraries (libraries, addPrelude) where
1+module Metadata.Libraries (interfaces, addPrelude) where
22
3−import Control.Applicative ((<$>),(<*>))
43import qualified Data.Map as Map
5−import Data.List (inits)
6−import Text.JSON
7−import Metadata.Load as Load
4+import qualified Control.Exception as E
5+import qualified Paths_Elm as Path
6+import System.Directory
7+import System.Exit
8+import System.FilePath
9+import System.IO.Unsafe (unsafePerformIO)
810import SourceSyntax.Module
11+import Data.Binary
12+
913
1014addPrelude :: Module t v -> Module t v
1115addPrelude (Module name exs ims stmts) = Module name exs (customIms ++ ims) stmts
12− where customIms = concatMap addModule prelude
16+ where
17+ customIms = concatMap addModule prelude
1318
14− addModule (n, method) = case lookup n ims of
15− Nothing -> [(n, method)]
16− Just (As m) -> [(n, method)]
17− Just _ -> []
19+ addModule (n, method) = case lookup n ims of
20+ Nothing -> [(n, method)]
21+ Just (As m) -> [(n, method)]
22+ Just _ -> []
1823
24+prelude :: [(String, ImportMethod)]
1925prelude = text : map (\n -> (n, Hiding [])) modules
2026 where
2127 text = ("Text", Hiding ["link", "color", "height"])
2228 modules = [ "Prelude", "Signal", "List", "Maybe", "Time"
2329 , "Graphics.Element", "Color", "Graphics.Collage" ]
2430
25−libraries :: Map.Map String (Map.Map String String)
26−libraries = Map.empty
31+{-# NOINLINE interfaces #-}
32+interfaces :: Interfaces
33+interfaces =
34+ unsafePerformIO (safeReadDocs =<< Path.getDataFileName "interfaces.data")
35+
36+safeReadDocs :: FilePath -> IO Interfaces
37+safeReadDocs name =
38+ E.catch (readDocs name) $ \err -> do
39+ putStrLn $ unlines [ "Error reading types for standard library!"
40+ , " " ++ show (err :: IOError)
41+ , "The file should be located here: " ++ name
42+ , " If you are using a stable version of Elm,"
43+ , " please report an issue at github.com/evancz/Elm"
44+ , " and specify your versions of Elm and your OS" ]
45+ exitFailure
46+
47+readDocs :: FilePath -> IO Interfaces
48+readDocs name = do
49+ exists <- doesFileExist name
50+ if exists then decodeFile name
51+ else ioError . userError $ "File Not Found"
compiler/Metadata/Load.hsdeleted−37
@@ -1,37 +0,0 @@
1−module Metadata.Load (interfaces) where
2−
3−import qualified Control.Exception as E
4−import Paths_Elm
5−import System.Directory
6−import System.Exit
7−import System.FilePath
8−import System.IO.Unsafe (unsafePerformIO)
9−import SourceSyntax.Module
10−import Data.Binary
11−
12−-- See stackoverflow discussion for trade-off between using unsafeIO or TemplateHaskell:
13−-- http://stackoverflow.com/questions/12716215/load-pure-global-variable-from-file
14−
15−-- Given the awkwardness of including a compile-time generated file
16−-- vs loading static data, then the unsafeIO seems better.
17−
18−{-# NOINLINE interfaces #-}
19−interfaces :: [ModuleInterface]
20−interfaces = unsafePerformIO (safeReadDocs =<< getDataFileName "interfaces.data")
21−
22−safeReadDocs :: FilePath -> IO [ModuleInterface]
23−safeReadDocs name =
24− E.catch (readDocs name) $ \err -> do
25− putStrLn $ unlines [ "Error reading types for standard library!"
26− , " The file should be located here: " ++ name
27− , " If you are using a stable version of Elm,"
28− , " please report an issue at github.com/evancz/Elm"
29− , " and specify your versions of Elm and your OS" ]
30− print (err :: IOError)
31− exitFailure
32−
33−readDocs :: FilePath -> IO [ModuleInterface]
34−readDocs name = do
35− exists <- doesFileExist name
36− if exists then decodeFile name
37− else ioError . userError $ "File Not Found"