175f268622
See: 14e32add30
for discussion on first attempt
Changed Librarys.hs and LoadLibraries.hs from TemplateHaskell to usafeIO.
Avoids needing to touch LoadLibraries after we change docs.json (as the file is loaded at runtime).
Also removes need for using CPP
Drawback of using unsafeIO is fairly well known/understood + this is a classic use-case.
Added error handling to LoadLibraries.hs
Setup.hs now uses "runProcess" instead of "system" to invoke elm compiler.
This allows us to set the environment variable "Elm_datadir" which is picked up by LoadLibraries and makes sure we get the just-built version in dist/data.
Also explicitly sets the RTS file to make sure we're not using one (as we're currently building it!)
Also change the folder the compiled JavaScript ends up in to dist/js. Writing to the source tree, then deleting isn't very nice + this should make it easier to find elm compilation errors. cabal clean handles the file deletion, too.
I think this fixes all the defects of the first version:
1. CPP is not needed
2. touch is not needed
3. building elm-runtime.js uses dist/data/docs.json
4. running elm after install uses the installed elm-runtime.js and docs.json
5. LoadLibraries.hs is simpler. Doesn't use CPP or TemplateHaskell. Has error handling and a decent error message
6. cabal clean && cabal install builds everything with the right data in the right order
(tested on ghc 7.4.1)
36 lines
1.2 KiB
Haskell
36 lines
1.2 KiB
Haskell
module LoadLibraries (docs) where
|
|
|
|
import Control.DeepSeq (force)
|
|
import qualified Control.Exception as E
|
|
import Paths_Elm
|
|
import System.Directory
|
|
import System.FilePath
|
|
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
|
|
|
|
-- Given the awkwardness of including a compile-time generated file
|
|
-- vs loading static data, then the unsafeIO seems better.
|
|
|
|
{-# NOINLINE docs #-}
|
|
docs :: String
|
|
docs = force . unsafePerformIO . safeReadDocs . getDataFileName $ "docs.json"
|
|
|
|
safeReadDocs :: IO FilePath -> IO String
|
|
safeReadDocs path = E.catch (readDocs path) (emitError path)
|
|
|
|
readDocs :: IO FilePath -> IO String
|
|
readDocs path = do
|
|
name <- path
|
|
exists <- doesFileExist name
|
|
if exists then readFile name
|
|
else ioError . userError $ "File Not Found"
|
|
|
|
|
|
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\":[]}"
|