2013-06-14 03:45:12 +00:00
module Metadata.LoadLibraries ( docs ) where
2013-03-17 05:24:18 +00:00
2013-03-22 15:52:40 +00:00
import Control.DeepSeq ( force )
import qualified Control.Exception as E
2013-03-17 05:24:18 +00:00
import Paths_Elm
2013-03-21 09:29:23 +00:00
import System.Directory
2013-03-21 13:31:55 +00:00
import System.FilePath
2013-03-22 15:52:40 +00:00
import System.IO.Unsafe ( unsafePerformIO )
-- See stackoverflow discussion for trade-off between using unsafeIO or TemplateHaskell:
-- http://stackoverflow.com/questions/12716215/load-pure-global-variable-from-file
2013-03-17 05:24:18 +00:00
2013-03-22 15:52:40 +00:00
-- Given the awkwardness of including a compile-time generated file
-- vs loading static data, then the unsafeIO seems better.
2013-03-17 05:24:18 +00:00
2013-03-22 15:52:40 +00:00
{- # NOINLINE docs # -}
docs :: String
2013-05-05 01:19:54 +00:00
docs = force $ unsafePerformIO ( safeReadDocs =<< getDataFileName " docs.json " )
2013-03-21 13:31:55 +00:00
2013-05-05 01:19:54 +00:00
safeReadDocs :: FilePath -> IO String
safeReadDocs name = E . catch ( readDocs name ) ( emitError name )
2013-03-21 13:31:55 +00:00
2013-05-05 01:19:54 +00:00
readDocs :: FilePath -> IO String
readDocs name = do
2013-03-21 09:29:23 +00:00
exists <- doesFileExist name
if exists then readFile name
2013-03-22 15:52:40 +00:00
else ioError . userError $ " File Not Found "
2013-03-21 09:29:23 +00:00
2013-05-05 01:19:54 +00:00
emitError :: FilePath -> IOError -> IO String
emitError name err = do
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 "
2013-03-22 15:52:40 +00:00
putStrLn ( show err )
return " { \ " modules \ " :[]} "