@@ -1,11 +1,13 @@
1−module Metadata.LoadLibraries (docs) where
1+module Metadata.Load (interfaces) where
22
3−import Control.DeepSeq (force)
43import qualified Control.Exception as E
54import Paths_Elm
65import System.Directory
6+import System.Exit
77import System.FilePath
88import System.IO.Unsafe (unsafePerformIO)
9+import SourceSyntax.Module
10+import Data.Binary
911
1012-- See stackoverflow discussion for trade-off between using unsafeIO or TemplateHaskell:
1113-- http://stackoverflow.com/questions/12716215/load-pure-global-variable-from-file
@@ -13,22 +15,23 @@ import System.IO.Unsafe (unsafePerformIO)
1315-- Given the awkwardness of including a compile-time generated file
1416-- vs loading static data, then the unsafeIO seems better.
1517
16−{-# NOINLINE docs #-}
17−docs :: String
18−docs = force $ unsafePerformIO (safeReadDocs =<< getDataFileName "docs.json")
19−
20−safeReadDocs :: FilePath -> IO String
21−safeReadDocs name = E.catch (readDocs name) (emitError name)
22−
23−readDocs :: FilePath -> IO String
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]
2434readDocs name = do
2535 exists <- doesFileExist name
26− if exists then readFile name
36+ if exists then decodeFile name
2737 else ioError . userError $ "File Not Found"
28−
29−
30−emitError :: FilePath -> IOError -> IO String
31−emitError name err = do
32− putStrLn $ "Error! Types for standard library not loaded properly!\n File should be here:" ++ name ++ "\n The file is created and copied by command: cabal install"
33− putStrLn (show err)
34− return "{\"modules\":[]}"