on be72e86 help sign in
elk/compiler/Model/LoadLibraries.hs
36 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 :: IO FilePath -> IO String
21safeReadDocs path = E.catch (readDocs path) (emitError path)
23readDocs :: IO FilePath -> IO String
24readDocs path = do
25 name <- path
26 exists <- doesFileExist name
27 if exists then readFile name
28 else ioError . userError $ "File Not Found"
31emitError :: IO FilePath -> IOError -> IO String
32emitError path err = do
33 name <- path
34 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"
35 putStrLn (show err)
36 return "{\"modules\":[]}"