@@ -1,49 +1,83 @@
1+{-# LANGUAGE DeriveDataTypeable #-}
1module Main where
2module Main where
3−import Data.List (isPrefixOf)
4+import Data.Either (lefts, rights)
5+import Data.List (intersect, intercalate)
6+import Data.Maybe (fromMaybe)
7+import System.Console.CmdArgs
8+import Text.Blaze.Html.Renderer.String (renderHtml)
4import CompileToJS
12import CompileToJS
5import GenerateHtml
13import GenerateHtml
6−import System.Environment
14+import Paths_Elm
7−import Text.Blaze.Html.Renderer.String (renderHtml)
15+
19+ , files :: [FilePath]
20+ , runtime :: Maybe FilePath
21+-- , separate_js :: Bool
23+-- , verbose_js :: Bool
25+ deriving (Data,Typeable,Show,Eq)
27+elm = ELM { make = False &= help "automatically compile dependencies."
28+ , files = def &= args &= typ "FILES"
29+ , runtime = Nothing &= typ "FILE" &=
30+ help "Specify a custom location for Elm's runtime system."
31+-- , separate_js = False &= help "Compile to separate HTML and JS files."
32+-- , only_js = False &= help "Compile only to JavaScript."
33+-- , verbose_js = False &= help "Produce JavaScript that may be easier to debug."
35+ help "Compile Elm programs to HTML, CSS, and JavaScript." &=
36+ summary "The Elm Compiler v0.3.0, (c) Evan Czaplicki"
9−main :: IO ()
38+main = do
10−main = getArgs >>= parse
39+ args <- cmdArgs elm
11−
40+ mini <- getDataFileName "elm-runtime-0.3.0.js"
12−parse :: [String] -> IO ()
41+ compileArgs mini args
13−parse ("--help":_) = putStrLn usage
42+
14−parse ("--version":_) = putStrLn "The Elm Compiler 0.2.0.1"
43+compileArgs mini (ELM _ [] _) =
15−parse [loc,file]
44+ putStrLn "Usage: elm [OPTIONS] [FILES]\nFor more help: elm --help"
16− | "--runtime-location=" `isPrefixOf` loc =
45+compileArgs mini (ELM make files rtLoc) =
46+ mapM_ (fileToHtml $ fromMaybe mini rtLoc) files
48+fileToHtml rtLoc file = do
49+ ems <- getModules [] file
51+ Left err -> putStrLn $ "Error while compiling " ++ file ++ ":\n" ++ err
17− produceHtml (tail $ dropWhile (/='=') loc) file
53+ let name = reverse . tail . dropWhile (/='.') $ reverse file in
18− | otherwise = putStrLn usageMini
54+ writeFile (name ++ ".html") . renderHtml $
19−parse [file] = produceHtml "elm-mini.js" file
55+ modulesToHtml rtLoc ms
20−parse _ = putStrLn usageMini
56+
22−produceHtml :: String -> FilePath -> IO ()
58+getModules :: [String] -> FilePath -> IO (Either String [Module])
23−produceHtml libLoc file = do
59+getModules uses file = do
24 code <- readFile file
60 code <- readFile file
25− let name = takeWhile (/='.') file
61+ case initialize code of
26− case compile code of
62+ Left err -> return . Left $ "Error in " ++ file ++ ":\n" ++ err
27− Left err -> putStrLn err
63+ Right modul@(Module _ _ imports _) ->
28− Right jsCode -> writeFile (name ++ ".html") . renderHtml $
64+ let imps = filter (`notElem` builtInModules) $ map fst imports in
29− generateHtml libLoc name code
65+ case intersect uses imps of
30−
66+ x:_ -> return . Left $ "Error: Cyclic dependency. Module " ++
31−usageMini :: String
67+ x ++ " depends on itself."
32−usageMini =
68+ [] -> do
33− "Usage: elm [OPTIONS] FILE\n\
69+ ems <- mapM (getModules (uses ++ imps) . toFilePath) imps
34− \Try `elm --help' for more information."
70+ return $ case lefts ems of
35−
71+ [] -> Right $ concat (rights ems) ++ [modul]
36−usage :: String
72+ errs -> Left $ intercalate "\n" errs
38− "Usage: elm [OPTIONS] FILE\n\
74+toFilePath :: String -> FilePath
39− \Compile .elm files to .html files.\n\
75+toFilePath modul = map (\c -> if c == '.' then '/' else c) modul ++ ".elm"
40− \Example: elm --runtime-location=../elm-mini.js main.elm\n\
76+
41− \\n\
77+builtInModules =
42− \Resource Locations:\n\
78+ concat [ map ("Data."++) [ "List", "Char", "Maybe" ]
43− \ --runtime-location set the location of the Elm runtime (elm-mini.js)\n\
79+ , map ("Signal."++) [ "Mouse", "Keyboard.Raw"
44− \\n\
80+ , "Window", "Time", "HTTP", "Input", "Random" ]
45− \Compiler Information:\n\
81+ , [ "Element", "Text", "Color", "Line" ]
46− \ --version print the version information and exit\n\
82+ , [ "Prelude" ]
47− \ --help display this help and exit\n\
83+ ]
49− \Elm home page: <http://elm-lang.org>"