@@ -18,8 +18,8 @@ data ELM =
18 ELM { make :: Bool
18 ELM { make :: Bool
19 , files :: [FilePath]
19 , files :: [FilePath]
20 , runtime :: Maybe FilePath
20 , runtime :: Maybe FilePath
21−-- , separate_js :: Bool
21+ , separate_js :: Bool
22−-- , only_js :: Bool
22+ , only_js :: Bool
23-- , verbose_js :: Bool
23-- , verbose_js :: Bool
25 deriving (Data,Typeable,Show,Eq)
25 deriving (Data,Typeable,Show,Eq)
@@ -28,8 +28,8 @@ elm = ELM { make = False &= help "automatically compile dependencies."
28 , files = def &= args &= typ "FILES"
28 , files = def &= args &= typ "FILES"
29 , runtime = Nothing &= typ "FILE" &=
29 , runtime = Nothing &= typ "FILE" &=
30 help "Specify a custom location for Elm's runtime system."
30 help "Specify a custom location for Elm's runtime system."
31−-- , separate_js = False &= help "Compile to separate HTML and JS files."
31+ , separate_js = False &= help "Compile to separate HTML and JS files."
32−-- , only_js = False &= help "Compile only to JavaScript."
32+ , only_js = False &= help "Compile only to JavaScript."
33-- , verbose_js = False &= help "Produce JavaScript that may be easier to debug."
33-- , verbose_js = False &= help "Produce JavaScript that may be easier to debug."
35 help "Compile Elm programs to HTML, CSS, and JavaScript." &=
35 help "Compile Elm programs to HTML, CSS, and JavaScript." &=
@@ -40,20 +40,30 @@ main = do
40 mini <- getDataFileName "elm-runtime-0.3.0.js"
40 mini <- getDataFileName "elm-runtime-0.3.0.js"
41 compileArgs mini args
41 compileArgs mini args
43−compileArgs mini (ELM _ [] _) =
43+compileArgs mini (ELM _ [] _ _ _) =
44 putStrLn "Usage: elm [OPTIONS] [FILES]\nFor more help: elm --help"
44 putStrLn "Usage: elm [OPTIONS] [FILES]\nFor more help: elm --help"
45−compileArgs mini (ELM make files rtLoc) =
45+compileArgs mini (ELM make files rtLoc split only) =
46− mapM_ (fileToHtml $ fromMaybe mini rtLoc) files
46+ mapM_ (fileTo get what $ fromMaybe mini rtLoc) files
47+ where get = if make then getModules [] else getModule
48+ what = if only then JS else
49+ if split then Split else HTML
51+data What = JS | HTML | Split
48−fileToHtml rtLoc file = do
53+fileTo get what rtLoc file = do
49− ems <- getModules [] file
54+ ems <- get file
50 case ems of
55 case ems of
51 Left err -> putStrLn $ "Error while compiling " ++ file ++ ":\n" ++ err
56 Left err -> putStrLn $ "Error while compiling " ++ file ++ ":\n" ++ err
52 Right ms ->
57 Right ms ->
53− let name = reverse . tail . dropWhile (/='.') $ reverse file in
58+ let name = reverse . tail . dropWhile (/='.') $ reverse file
54− writeFile (name ++ ".html") . renderHtml $
59+ js = name ++ ".js"
60+ html = name ++ ".html"
62+ JS -> writeFile js $ concatMap jsModule ms
55− modulesToHtml rtLoc ms
63+ HTML -> writeFile html . renderHtml $ modulesToHtml rtLoc ms
65+ writeFile html . renderHtml $ linkedHtml rtLoc js ms
66+ writeFile js $ concatMap jsModule ms
58getModules :: [String] -> FilePath -> IO (Either String [Module])
68getModules :: [String] -> FilePath -> IO (Either String [Module])
59getModules uses file = do
69getModules uses file = do
@@ -71,6 +81,11 @@ getModules uses file = do
71 [] -> Right $ concat (rights ems) ++ [modul]
81 [] -> Right $ concat (rights ems) ++ [modul]
72 errs -> Left $ intercalate "\n" errs
82 errs -> Left $ intercalate "\n" errs
84+getModule :: FilePath -> IO (Either String [Module])
86+ code <- readFile file
87+ return . fmap (:[]) $ initialize code
74toFilePath :: String -> FilePath
89toFilePath :: String -> FilePath
75toFilePath modul = map (\c -> if c == '.' then '/' else c) modul ++ ".elm"
90toFilePath modul = map (\c -> if c == '.' then '/' else c) modul ++ ".elm"