on 22042e2 help sign in
elk/compiler/Model/LoadLibraries.hs
34 lines 1.2 kB history raw
1module LoadLibraries (docs) where
3import Control.DeepSeq (force)
4import qualified Control.Exception as E
5import Paths_Elm
6import System.Directory
7import System.FilePath
8import System.IO.Unsafe (unsafePerformIO)
10-- See stackoverflow discussion for trade-off between using unsafeIO or TemplateHaskell:
11-- http://stackoverflow.com/questions/12716215/load-pure-global-variable-from-file
13-- Given the awkwardness of including a compile-time generated file
14-- vs loading static data, then the unsafeIO seems better.
16{-# NOINLINE docs #-}
17docs :: String
18docs = force $ unsafePerformIO (safeReadDocs =<< getDataFileName "docs.json")
20safeReadDocs :: FilePath -> IO String
21safeReadDocs name = E.catch (readDocs name) (emitError name)
23readDocs :: FilePath -> IO String
24readDocs name = do
25 exists <- doesFileExist name
26 if exists then readFile name
27 else ioError . userError $ "File Not Found"
30emitError :: FilePath -> IOError -> IO String
31emitError 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\":[]}"