elm/compiler/Model/Libraries.hs

35 lines
1 KiB
Haskell
Raw Normal View History

module Libraries (libraries) where
import qualified Data.Map as Map
import Text.JSON
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 LoadLibraries (docs)
libraries :: Map.Map String (Map.Map String String)
libraries = case getLibs of
Ok libs -> libs
Error err -> error err
getLibs :: Result (Map.Map String (Map.Map String String))
getLibs = do
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
obj <- decodeStrict docs :: Result (JSObject JSValue)
modules <- valFromObj "modules" obj :: Result [JSObject JSValue]
Map.fromList `fmap` mapM getValues modules
getName :: JSObject JSValue -> Result String
getName obj = valFromObj "name" obj
getType :: JSObject JSValue -> Result String
getType obj = valFromObj "type" obj
getValue :: JSObject JSValue -> Result (String,String)
getValue obj = do n <- getName obj
t <- getType obj
return (n,t)
getValues :: JSObject JSValue -> Result (String, Map.Map String String)
getValues obj = do
name <- getName obj
vs <- valFromObj "values" obj
vals <- mapM getValue vs
return (name, Map.fromList vals)