2013-03-21 09:29:23 +00:00
module 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
docs = force . unsafePerformIO . safeReadDocs . getDataFileName $ " docs.json "
2013-03-21 13:31:55 +00:00
2013-03-22 15:52:40 +00:00
safeReadDocs :: IO FilePath -> IO String
safeReadDocs path = E . catch ( readDocs path ) ( emitError path )
2013-03-21 13:31:55 +00:00
readDocs :: IO FilePath -> IO String
readDocs path = do
name <- path
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-03-22 15:52:40 +00:00
emitError :: IO FilePath -> IOError -> IO String
emitError path err = do
name <- path
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 "
putStrLn ( show err )
return " { \ " modules \ " :[]} "