@@ -53,9 +53,10 @@ import qualified Reporting.Render.Code as Code
53main :: IO ()
53main :: IO ()
55 do artifacts <- loadArtifacts
55 do artifacts <- loadArtifacts
56+ errorHtml <- compileErrorViewer artifacts
56 httpServe config $ msum $
57 httpServe config $ msum $
57 [ path "compile" $ compile artifacts
58 [ path "compile" $ compile artifacts
58− , path "compile/error.js" $ serveFile "error.js"
59+ , path "compile/error.js" $ writeBS errorHtml
59 , notFound
60 , notFound
@@ -63,14 +64,14 @@ main =
63config :: Config Snap a
64config :: Config Snap a
65 defaultConfig
66 defaultConfig
66− # setVerbose False
67+ |> setVerbose False
67− # setPort 8000
68+ |> setPort 8000
68− # setAccessLog ConfigNoLog
69+ |> setAccessLog ConfigNoLog
69− # setErrorLog ConfigNoLog
70+ |> setErrorLog ConfigNoLog
72−(#) :: a -> (a -> b) -> b
73+(|>) :: a -> (a -> b) -> b
73−(#) value func =
74+(|>) value func =
74 func value
75 func value
@@ -90,23 +91,17 @@ notFound =
92compile :: Artifacts -> Snap ()
93compile :: Artifacts -> Snap ()
93−compile (Artifacts ifaces objs) =
94+compile artifacts =
94 applyCORS corsOptions $ method POST $
95 applyCORS corsOptions $ method POST $
95 do parts <- handleMultipart defaultUploadPolicy handlePart
96 do parts <- handleMultipart defaultUploadPolicy handlePart
96 case parts of
97 case parts of
97 [Just source] ->
98 [Just source] ->
98− case compileByteString ifaces source of
99+ case compileToBuilder artifacts source of
99 Left err ->
100 Left err ->
100 writeBuilder $ errorToHtmlBuilder source err
101 writeBuilder $ errorToHtmlBuilder source err
102− Right (home, locals, main) ->
103+ Right builder ->
103− let
104+ writeBuilder builder
104− mode = Mode.Dev Nothing
105− name = ModuleName._module home
106− mains = Map.singleton home main
107− graph = Opt.addLocalGraph locals objs
109− writeBuilder $ Html.sandwich name $ Generate.generate mode graph mains
@@ -119,8 +114,26 @@ handlePart info stream =
119 else return Nothing
114 else return Nothing
122−compileByteString :: Map.Map ModuleName.Raw I.Interface -> B.ByteString -> Either Error.Error (ModuleName.Canonical, Opt.LocalGraph, Opt.Main)
117+errorToHtmlBuilder :: B.ByteString -> Error.Error -> B.Builder
123−compileByteString interfaces source =
118+errorToHtmlBuilder source err =
120+ json = Encode.encodeUgly (Error.errorToJson (Code.toSource source) err)
125+ <meta charset="UTF-8">
126+ <style>body { padding: 0; margin: 0; }</style>
127+ <script src="https://worker.elm-lang.org/compile/error.js"></script>
128+ <script>Elm.Error.init({flags:|] <> json <> [r|})</script>
135+compileToBuilder :: Artifacts -> B.ByteString -> Either Error.Error B.Builder
136+compileToBuilder (Artifacts interfaces objects) source =
124 case Parse.fromByteString Pkg.dummyName source of
137 case Parse.fromByteString Pkg.dummyName source of
125 Left err ->
138 Left err ->
126 Left (Error.BadSyntax err)
139 Left (Error.BadSyntax err)
@@ -137,8 +150,18 @@ compileByteString interfaces source =
138 Right artifacts@(Compile.Artifacts modul _ locals@(Opt.LocalGraph maybeMain _ _)) ->
151 Right artifacts@(Compile.Artifacts modul _ locals@(Opt.LocalGraph maybeMain _ _)) ->
139 case maybeMain of
152 case maybeMain of
154+ Left (error "TODO no main")
158+ mode = Mode.Dev Nothing
140− Just main -> Right (Can._name modul, locals, main)
159+ home = Can._name modul
141− Nothing -> Left (error "TODO no main")
160+ name = ModuleName._module home
161+ mains = Map.singleton home main
162+ graph = Opt.addLocalGraph locals objects
164+ Right $ Html.sandwich name $ Generate.generate mode graph mains
144checkImports :: Map.Map ModuleName.Raw I.Interface -> [Src.Import] -> Either Error.Error (Map.Map ModuleName.Raw I.Interface)
167checkImports :: Map.Map ModuleName.Raw I.Interface -> [Src.Import] -> Either Error.Error (Map.Map ModuleName.Raw I.Interface)
@@ -160,21 +183,46 @@ checkImports interfaces imports =
160 Left (Error.BadImports (fmap toError (NE.List i is)))
183 Left (Error.BadImports (fmap toError (NE.List i is)))
163−errorToHtmlBuilder :: B.ByteString -> Error.Error -> B.Builder
186+
164−errorToHtmlBuilder source err =
187+-- COMPILE ERROR VIEWER
190+compileErrorViewer :: Artifacts -> B.ByteString
191+compileErrorViewer artifacts =
192+ do source <- File.readUtf8 "src/Error.elm"
193+ case compileToBuilder artifacts source of
195+ error "problem compiling src/Error.elm"
198+ return (LBS.toStrict (B.toLazyByteString builder))
205+corsOptions :: (Monad m) => CORSOptions m
166− json = Encode.encodeUgly (Error.errorToJson (Code.toSource source) err)
208+ allowedOrigins = toOriginList [ "https://elm-lang.org", "https://package.elm-lang.org" ]
209+ allowedMethods = HashSet.singleton (HashableMethod POST)
168− [r|<!DOCTYPE HTML>
211+ CORSOptions
169−<html>
212+ { corsAllowOrigin = return allowedOrigins
170−<head>
213+ , corsAllowCredentials = return True
171− <meta charset="UTF-8">
214+ , corsExposeHeaders = return HashSet.empty
172− <style>body { padding: 0; margin: 0; }</style>
215+ , corsAllowedMethods = return allowedMethods
173− <script src="https://worker.elm-lang.org/compile/error.js"></script>
216+ , corsAllowedHeaders = return
174− <script>Elm.Error.init({flags:|] <> json <> [r|})</script>
217+ }
177−</html>|]
220+toOriginList :: [String] -> OriginList
221+toOriginList origins =
222+ Origins $ mkOriginSet $
223+ case traverse parseURI origins of
225+ Nothing -> error "invalid entry given to toOriginList list"
@@ -228,39 +276,3 @@ toUnique oneOrMore =
228 case oneOrMore of
276 case oneOrMore of
229 OneOrMore.One value -> Just value
277 OneOrMore.One value -> Just value
230 OneOrMore.More _ _ -> Nothing
278 OneOrMore.More _ _ -> Nothing
237−corsOptions :: (Monad m) => CORSOptions m
240− { corsAllowOrigin = return allowedOrigins
241− , corsAllowCredentials = return True
242− , corsExposeHeaders = return HashSet.empty
243− , corsAllowedMethods = return allowedMethods
244− , corsAllowedHeaders = return
248−{-# NOINLINE allowedMethods #-}
249−allowedMethods :: HashSet.HashSet HashableMethod
251− HashSet.singleton (HashableMethod POST)
254−{-# NOINLINE allowedOrigins #-}
255−allowedOrigins :: OriginList
259− [ "https://elm-lang.org"
260− , "https://package.elm-lang.org"
263− Origins $ mkOriginSet $
264− case traverse parseURI origins of
266− Nothing -> error "Invalid entry in allowedOrigins list"