on c8eeedf help sign in
elk/compiler/Compiler.hs
89 lines 3.2 kB history raw
1{-# OPTIONS_GHC -W #-}
2module Main where
4import Control.Monad (foldM, when)
5import qualified Data.Maybe as Maybe
6import Text.Blaze.Html.Renderer.String (renderHtml)
7import qualified Data.ByteString.Lazy.Char8 as BS
8import qualified System.Console.CmdArgs as CmdArgs
9import System.Directory
10import System.Exit (exitSuccess)
11import System.FilePath
12import GHC.Conc
14import Build.Dependencies (getBuildRecipe, Recipe(..))
15import qualified Generate.Html as Html
16import qualified Metadata.Prelude as Prelude
17import qualified Build.Utils as Utils
18import qualified Build.Flags as Flag
19import qualified Build.File as File
20import qualified Elm.Internal.Paths as Path
22main :: IO ()
23main = do setNumCapabilities =<< getNumProcessors
24 compileArgs =<< CmdArgs.cmdArgs Flag.flags
26compileArgs :: Flag.Flags -> IO ()
27compileArgs flags =
28 do when (Flag.get_runtime flags) $ do
29 putStrLn Path.runtime
30 exitSuccess
31 case Flag.files flags of
32 [] -> putStrLn "Usage: elm [OPTIONS] [FILES]\nFor more help: elm --help"
33 fs -> mapM_ (build flags) fs
35build :: Flag.Flags -> FilePath -> IO ()
36build flags rootFile =
37 do let noPrelude = Flag.no_prelude flags
38 builtIns <- Prelude.interfaces noPrelude
40 (Recipe elmFiles jsFiles) <-
41 if Flag.make flags
42 then Utils.run (getBuildRecipe (Flag.src_dir flags) builtIns rootFile)
43 else return (Recipe [rootFile] [])
45 moduleName <- File.build flags builtIns elmFiles
47 let elmos = map (Utils.elmo flags) elmFiles
48 js <- foldM appendToOutput BS.empty (elmos ++ jsFiles)
50 (extension, code) <-
51 if Flag.only_js flags
52 then do putStr "Generating JavaScript ... "
53 makeJs js
54 else do putStr "Generating HTML ... "
55 makeHtml js moduleName
57 let targetFile = Utils.buildPath flags rootFile extension
58 createDirectoryIfMissing True (takeDirectory targetFile)
59 BS.writeFile targetFile code
60 putStrLn "Done"
62 where
63 appendToOutput :: BS.ByteString -> FilePath -> IO BS.ByteString
64 appendToOutput js filePath = do
65 src <- BS.readFile filePath
66 return (BS.append src js)
68 getRuntime :: IO Html.JSSource
69 getRuntime =
70 let runtimePath = Maybe.fromMaybe Path.runtime (Flag.set_runtime flags) in
71 case Flag.bundle_runtime flags of
72 False -> return (Html.Link runtimePath)
73 True -> Html.Source `fmap` BS.readFile runtimePath
75 makeJs :: BS.ByteString -> IO (String, BS.ByteString)
76 makeJs js =
77 do runtime <- getRuntime
78 case runtime of
79 Html.Link _ -> return ("js", js)
80 Html.Source rts -> return ("js", BS.append rts js)
82 makeHtml :: BS.ByteString -> String -> IO (String, BS.ByteString)
83 makeHtml js moduleName =
84 do runtime <- getRuntime
85 return ("html", BS.pack $ renderHtml (html runtime))
86 where
87 sources js = map Html.Link (Flag.scripts flags) ++ [ Html.Source js ]
88 html runtime =
89 Html.generate runtime (takeBaseName rootFile) (sources js) moduleName ""