on 0db809f help sign in
37 lines 1.4 kB history raw
1module Metadata.Load (interfaces) where
3import qualified Control.Exception as E
4import Paths_Elm
5import System.Directory
6import System.Exit
7import System.FilePath
8import System.IO.Unsafe (unsafePerformIO)
9import SourceSyntax.Module
10import Data.Binary
12-- See stackoverflow discussion for trade-off between using unsafeIO or TemplateHaskell:
13-- http://stackoverflow.com/questions/12716215/load-pure-global-variable-from-file
15-- Given the awkwardness of including a compile-time generated file
16-- vs loading static data, then the unsafeIO seems better.
18{-# NOINLINE interfaces #-}
19interfaces :: [ModuleInterface]
20interfaces = unsafePerformIO (safeReadDocs =<< getDataFileName "interfaces.data")
22safeReadDocs :: FilePath -> IO [ModuleInterface]
23safeReadDocs 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
33readDocs :: FilePath -> IO [ModuleInterface]
34readDocs name = do
35 exists <- doesFileExist name
36 if exists then decodeFile name
37 else ioError . userError $ "File Not Found"