on 45cfc8f help sign in
elk/compiler/Compiler.hs
91 lines 3.7 kB history raw
1{-# LANGUAGE DeriveDataTypeable #-}
2module Main where
4import Data.Either (lefts, rights)
5import Data.List (intersect, intercalate,lookup)
6import Data.Maybe (fromMaybe)
7import Data.Version (showVersion)
8import System.Console.CmdArgs
9import System.Exit
10import System.FilePath
11import Text.Blaze.Html.Renderer.String (renderHtml)
13import qualified Text.Jasmine as JS
14import qualified Data.ByteString.Lazy.Char8 as BS
16import Ast
17import Initialize (build,buildFromSource)
18import CompileToJS
19import GenerateHtml
20import qualified Libraries as Libraries
21import Paths_Elm
23data ELM =
24 ELM { make :: Bool
25 , files :: [FilePath]
26 , runtime :: Maybe FilePath
27 , separate_js :: Bool
28 , only_js :: Bool
29 , import_js :: [FilePath]
30 , no_prelude :: Bool
31 , noscript :: Bool
32 , minify :: Bool
33 , output_directory :: Maybe FilePath
34 }
35 deriving (Data,Typeable,Show,Eq)
37elm = ELM { make = False &= help "automatically compile dependencies."
38 , files = def &= args &= typ "FILES"
39 , runtime = Nothing &= typFile &=
40 help "Specify a custom location for Elm's runtime system."
41 , separate_js = False &= help "Compile to separate HTML and JS files."
42 , only_js = False &= help "Compile only to JavaScript."
43 , import_js = [] &= typFile &= help "Include a JavaScript file before the body of the Elm program. Can be used many times. Files will be included in the given order."
44 , no_prelude = False &= help "Do not import Prelude by default, used only when compiling standard libraries."
45 , noscript = True &= help "Add generated <noscript> tag to HTML output."
46 , minify = False &= help "Minify generated JavaScript"
47 , output_directory = Nothing &= typFile &= help "Output files to directory specified. Defaults to the location of original file."
48 } &=
49 help "Compile Elm programs to HTML, CSS, and JavaScript." &=
50 summary ("The Elm Compiler " ++ showVersion version ++ ", (c) Evan Czaplicki")
52main = do
53 args <- cmdArgs elm
54 mini <- getDataFileName "elm-runtime.js"
55 compileArgs mini args
57compileArgs mini flags =
58 case files flags of
59 [] -> putStrLn "Usage: elm [OPTIONS] [FILES]\nFor more help: elm --help"
60 fs -> mapM_ (fileTo flags what loc) fs
61 where loc = fromMaybe mini (runtime flags)
62 what | only_js flags = JS
63 | separate_js flags = Split
64 | otherwise = HTML
66data What = JS | HTML | Split
68fileTo flags what rtLoc file = do
69 let jsStyle = if minify flags then Minified else Readable
70 formatJS = if minify flags then BS.unpack . JS.minify . BS.pack else id
71 prelude = not (no_prelude flags)
72 ems <- if make flags then build prelude file
73 else do src <- readFile file
74 return (fmap (:[]) (buildFromSource prelude src))
75 jss <- concat `fmap` mapM readFile (import_js flags)
76 case ems of
77 Left err -> do putStrLn $ "Error while compiling " ++ file ++ ":\n" ++ err
78 exitFailure
79 Right ms' ->
80 let path = fromMaybe "" (output_directory flags) </> file
81 js = replaceExtension path ".js"
82 html = replaceExtension path ".html"
83 ms = if no_prelude flags then ms' else map Libraries.addPrelude ms'
84 txt = jss ++ concatMap jsModule ms
85 in case what of
86 JS -> writeFile js (formatJS txt)
87 HTML -> writeFile html . renderHtml $
88 modulesToHtml jsStyle "" rtLoc jss (noscript flags) ms
89 Split ->
90 do writeFile html . renderHtml $ linkedHtml rtLoc js ms
91 writeFile js (formatJS txt)