elm/compiler/Metadata/LoadLibraries.hs

35 lines
1.2 KiB
Haskell
Raw Normal View History

module Metadata.LoadLibraries (docs) where
Fix cabal install missing docs.json (followup) See: https://github.com/ngunn/Elm/commit/14e32add30c690b1309252d530afbea29cc448ec 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)
2013-03-22 15:52:40 +00:00
import Control.DeepSeq (force)
import qualified Control.Exception as E
import Paths_Elm
import System.Directory
import System.FilePath
Fix cabal install missing docs.json (followup) See: https://github.com/ngunn/Elm/commit/14e32add30c690b1309252d530afbea29cc448ec 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)
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
Fix cabal install missing docs.json (followup) See: https://github.com/ngunn/Elm/commit/14e32add30c690b1309252d530afbea29cc448ec 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)
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.
Fix cabal install missing docs.json (followup) See: https://github.com/ngunn/Elm/commit/14e32add30c690b1309252d530afbea29cc448ec 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)
2013-03-22 15:52:40 +00:00
{-# NOINLINE docs #-}
docs :: String
docs = force $ unsafePerformIO (safeReadDocs =<< getDataFileName "docs.json")
safeReadDocs :: FilePath -> IO String
safeReadDocs name = E.catch (readDocs name) (emitError name)
readDocs :: FilePath -> IO String
readDocs name = do
exists <- doesFileExist name
if exists then readFile name
Fix cabal install missing docs.json (followup) See: https://github.com/ngunn/Elm/commit/14e32add30c690b1309252d530afbea29cc448ec 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)
2013-03-22 15:52:40 +00:00
else ioError . userError $ "File Not Found"
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"
Fix cabal install missing docs.json (followup) See: https://github.com/ngunn/Elm/commit/14e32add30c690b1309252d530afbea29cc448ec 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)
2013-03-22 15:52:40 +00:00
putStrLn (show err)
return "{\"modules\":[]}"