on cb609ea help sign in
elk/elm/src/Compiler.hs
83 lines 3.4 kB history raw
1{-# LANGUAGE DeriveDataTypeable #-}
2module Main where
4import Data.Either (lefts, rights)
5import Data.List (intersect, intercalate)
6import Data.Maybe (fromMaybe)
7import Data.Version (showVersion)
8import System.Console.CmdArgs
9import System.FilePath
10import Text.Blaze.Html.Renderer.String (renderHtml)
12import qualified Text.Jasmine as JS
13import qualified Data.ByteString.Lazy.Char8 as BS
15import Ast
16import Initialize
17import CompileToJS
18import GenerateHtml
19import Paths_Elm
21data ELM =
22 ELM { make :: Bool
23 , files :: [FilePath]
24 , runtime :: Maybe FilePath
25 , separate_js :: Bool
26 , only_js :: Bool
27 , import_js :: [FilePath]
28 , generate_noscript :: Bool
29 , minify :: Bool
30 , output_directory :: Maybe FilePath
31 }
32 deriving (Data,Typeable,Show,Eq)
34elm = ELM { make = False &= help "automatically compile dependencies."
35 , files = def &= args &= typ "FILES"
36 , runtime = Nothing &= typFile &=
37 help "Specify a custom location for Elm's runtime system."
38 , separate_js = False &= help "Compile to separate HTML and JS files."
39 , only_js = False &= help "Compile only to JavaScript."
40 , 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."
41 , generate_noscript = True &= help "Add generated <noscript> tag to HTML output."
42 , minify = False &= help "Minify generated JavaScript"
43 , output_directory = Nothing &= typFile &= help "Output files to directory specified. Defaults to the location of original file."
44 } &=
45 help "Compile Elm programs to HTML, CSS, and JavaScript." &=
46 summary ("The Elm Compiler " ++ showVersion version ++ ", (c) Evan Czaplicki")
48main = do
49 args <- cmdArgs elm
50 mini <- getDataFileName ("elm-runtime-" ++ showVersion version ++ ".js")
51 compileArgs mini args
53compileArgs mini (ELM _ [] _ _ _ _ _ _ _) =
54 putStrLn "Usage: elm [OPTIONS] [FILES]\nFor more help: elm --help"
55compileArgs mini (ELM make files rtLoc split only js nscrpt isMini outputDir) =
56 mapM_ (fileTo isMini make what js nscrpt outputDir loc) files
57 where loc = fromMaybe mini rtLoc
58 what | only = JS
59 | split = Split
60 | otherwise = HTML
62data What = JS | HTML | Split
64fileTo isMini make what jsFiles noscript outputDir rtLoc file = do
65 let jsStyle = if isMini then Minified else Readable
66 formatJS = if isMini then BS.unpack . JS.minify . BS.pack else id
67 ems <- build make file
68 jss <- concat `fmap` mapM readFile jsFiles
69 case ems of
70 Left err -> putStrLn $ "Error while compiling " ++ file ++ ":\n" ++ err
71 Right ms ->
72 let path = fromMaybe "" outputDir </> file
73 js = replaceExtension path ".js"
74 html = replaceExtension path ".html"
75 pairs = map ((,) (fst ms)) (snd ms)
76 txt = jss ++ concatMap jsModule pairs
77 in case what of
78 JS -> writeFile js (formatJS txt)
79 HTML -> writeFile html . renderHtml $
80 modulesToHtml jsStyle "" rtLoc jss noscript pairs
81 Split ->
82 do writeFile html . renderHtml $ linkedHtml rtLoc js pairs
83 writeFile js (formatJS txt)