on master help sign in
Make reactor more reliable when compiling Elm code
8a0fb69Evan Czaplicki
builder/src/Generate/Output.hs+14−12
@@ -11,6 +11,7 @@ module Generate.Output
11 where
11 where
12
12
13
13
14+import Control.Concurrent.MVar (MVar, putMVar)
14import Control.Monad.Trans (liftIO)
15import Control.Monad.Trans (liftIO)
15import qualified Data.ByteString.Builder as B
16import qualified Data.ByteString.Builder as B
16import qualified Data.Map as Map
17import qualified Data.Map as Map
@@ -81,7 +82,7 @@ generateMonolith (Options mode target maybeOutput) (Summary.Summary _ project _
81 Obj.None ->
82 Obj.None ->
82 return ()
83 return ()
83
84
84− Obj.Some name names builder ->
85+ Obj.Some name _names builder ->
85 let
86 let
86 monolith =
87 monolith =
87 "(function(scope){\n'use strict';"
88 "(function(scope){\n'use strict';"
@@ -92,22 +93,22 @@ generateMonolith (Options mode target maybeOutput) (Summary.Summary _ project _
92 Nothing ->
93 Nothing ->
93 IO.writeBuilder "elm.js" monolith
94 IO.writeBuilder "elm.js" monolith
94
95
96+ Just output_ ->
97+ case output_ of
95− Just None ->
98+ None ->
96− return ()
99+ return ()
100+
101+ JavaScript maybeDir fileName ->
102+ do path <- toWritablePath maybeDir fileName
103+ IO.writeBuilder path monolith
97
104
98− Just (JavaScript maybeDir fileName) ->
105+ HtmlBuilder mvar ->
99− do path <- toWritablePath maybeDir fileName
106+ putMVar mvar (Html.sandwich name monolith)
100− IO.writeBuilder path monolith
101
107
102− Just (Html maybeDir fileName) ->
108+ Html maybeDir fileName ->
103− case names of
104− [] ->
105 do path <- toWritablePath maybeDir fileName
109 do path <- toWritablePath maybeDir fileName
106 IO.writeBuilder path (Html.sandwich name monolith)
110 IO.writeBuilder path (Html.sandwich name monolith)
107
111
108− _:_ ->
109− return ()
110−
111
112
112
113
113-- GENERATE REPL MONOLITH
114-- GENERATE REPL MONOLITH
@@ -181,6 +182,7 @@ data Output
181 = None
182 = None
182 | Html (Maybe FilePath) FilePath
183 | Html (Maybe FilePath) FilePath
183 | JavaScript (Maybe FilePath) FilePath
184 | JavaScript (Maybe FilePath) FilePath
185+ | HtmlBuilder (MVar B.Builder)
184
186
185
187
186toWritablePath :: Maybe FilePath -> FilePath -> IO FilePath
188toWritablePath :: Maybe FilePath -> FilePath -> IO FilePath
ui/terminal/src/Develop.hs+14−20
@@ -156,35 +156,29 @@ serveCode file =
156serveElm :: FilePath -> Snap ()
156serveElm :: FilePath -> Snap ()
157serveElm file =
157serveElm file =
158 do guard (takeExtension file == ".elm")
158 do guard (takeExtension file == ".elm")
159+ modifyResponse (setContentType "text/html")
160+ writeBuilder =<< liftIO (compileToHtmlBuilder file)
159
161
160− result <- liftIO $
161− do mvar <- newEmptyMVar
162
162
163− let reporter = Progress.Reporter (\_ -> return ()) (putMVar mvar)
163+compileToHtmlBuilder :: FilePath -> IO B.Builder
164+compileToHtmlBuilder file =
165+ do mvar1 <- newEmptyMVar
166+ mvar2 <- newEmptyMVar
164
167
165− void $ Task.run reporter $
168+ let reporter = Progress.Reporter (\_ -> return ()) (putMVar mvar1)
166− do summary <- Project.getRoot
169+ let options = Output.Options Obj.Dev Obj.Client (Just (Output.HtmlBuilder mvar2))
167− Project.compile options Nothing summary [file]
168
170
169− takeMVar mvar
171+ void $ Task.run reporter $
172+ do summary <- Project.getRoot
173+ Project.compile options Nothing summary [file]
170
174
175+ result <- takeMVar mvar1
171 case result of
176 case result of
172 Just exit ->
177 Just exit ->
173− do modifyResponse (setContentType "text/html")
174− writeBuilder $
175− Generate.makePageHtml "Errors" (Encode.encode (Exit.toJson exit))
178+ return $ Generate.makePageHtml "Errors" (Encode.encode (Exit.toJson exit))
176
179
177 Nothing ->
180 Nothing ->
178− serveFileAs "text/html" (Path.temp "html")
181+ takeMVar mvar2
179−
180−
181−options :: Output.Options
182−options =
183− let
184− (directory, filePath) = FP.splitFileName (Path.temp "html")
185− output = Output.Html (Just directory) filePath
186− in
187− Output.Options Obj.Dev Obj.Client (Just output)
188
182
189
183
190
184