on 32a331b help sign in
elk/compiler/Compiler.hs
182 lines 6.9 kB history raw
1{-# LANGUAGE DeriveDataTypeable #-}
2module Main where
4import Control.Monad
5import qualified Data.Map as Map
6import qualified Data.List as List
7import qualified Data.Binary as Binary
8import Data.Version (showVersion)
9import System.Console.CmdArgs hiding (program)
10import System.Directory
11import System.Exit
12import System.FilePath
14import qualified Text.Blaze.Html.Renderer.Pretty as Pretty
15import qualified Text.Blaze.Html.Renderer.String as Normal
16import qualified Text.Jasmine as JS
17import qualified Data.ByteString.Lazy.Char8 as BS
19import qualified Metadata.Prelude as Prelude
20import SourceSyntax.Module
21import Initialize (buildFromSource, getSortedModuleNames, Interfaces)
22import Generate.JavaScript (jsModule)
23import Generate.Html (createHtml, JSStyle(..), JSSource(..))
24import Paths_Elm
26import SourceSyntax.PrettyPrint (pretty, variable)
27import Text.PrettyPrint as P
28import qualified Type.Type as Type
29import qualified Parse.Type as Parse
30import qualified Data.Traversable as Traverse
32data Flags =
33 Flags { make :: Bool
34 , files :: [FilePath]
35 , runtime :: Maybe FilePath
36 , only_js :: Bool
37 , print_types :: Bool
38 , print_program :: Bool
39 , scripts :: [FilePath]
40 , no_prelude :: Bool
41 , minify :: Bool
42 , output_directory :: FilePath
43 }
44 deriving (Data,Typeable,Show,Eq)
46flags = Flags
47 { make = False
48 &= help "automatically compile dependencies."
49 , files = def &= args &= typ "FILES"
50 , runtime = Nothing &= typFile
51 &= help "Specify a custom location for Elm's runtime system."
52 , only_js = False
53 &= help "Compile only to JavaScript."
54 , print_types = False
55 &= help "Print out infered types of top-level definitions."
56 , print_program = False
57 &= help "Print out an internal representation of a program."
58 , scripts = [] &= typFile
59 &= help "Load JavaScript files in generated HTML. Files will be included in the given order."
60 , no_prelude = False
61 &= help "Do not import Prelude by default, used only when compiling standard libraries."
62 , minify = False
63 &= help "Minify generated JavaScript and HTML"
64 , output_directory = "ElmFiles" &= typFile
65 &= help "Output files to directory specified. Defaults to ElmFiles/ directory."
66 } &= help "Compile Elm programs to HTML, CSS, and JavaScript."
67 &= summary ("The Elm Compiler " ++ showVersion version ++ ", (c) Evan Czaplicki")
69main :: IO ()
70main = compileArgs =<< cmdArgs flags
72compileArgs :: Flags -> IO ()
73compileArgs flags =
74 case files flags of
75 [] -> putStrLn "Usage: elm [OPTIONS] [FILES]\nFor more help: elm --help"
76 fs -> mapM_ (build flags) fs
79file :: Flags -> FilePath -> String -> FilePath
80file flags filePath ext = output_directory flags </> replaceExtension filePath ext
82elmo :: Flags -> FilePath -> FilePath
83elmo flags filePath = file flags filePath "elmo"
84elmi flags filePath = file flags filePath "elmi"
87buildFile :: Flags -> Int -> Int -> Interfaces -> FilePath -> IO ModuleInterface
88buildFile flags moduleNum numModules interfaces filePath =
89 do compiled <- alreadyCompiled
90 if compiled then Binary.decodeFile (elmi flags filePath) else compile
92 where
93 alreadyCompiled :: IO Bool
94 alreadyCompiled = do
95 exists <- doesFileExist (elmo flags filePath)
96 if not exists then return False
97 else do tsrc <- getModificationTime filePath
98 tint <- getModificationTime (elmo flags filePath)
99 return (tsrc < tint)
101 number :: String
102 number = "[" ++ show moduleNum ++ " of " ++ show numModules ++ "]"
104 name :: String
105 name = List.intercalate "." (splitDirectories (dropExtensions filePath))
107 compile :: IO ModuleInterface
108 compile = do
109 putStrLn (number ++ " Compiling " ++ name)
110 source <- readFile filePath
111 createDirectoryIfMissing True (output_directory flags)
112 metaModule <-
113 case buildFromSource interfaces source of
114 Left err -> mapM print err >> exitFailure
115 Right modul -> do
116 if print_program flags then print . pretty $ program modul else return ()
117 return modul
119 if print_types flags then printTypes metaModule else return ()
120 tipes <- toSrcTypes (types metaModule)
121 let interface = ModuleInterface {
122 iTypes = tipes,
123 iAdts = datatypes metaModule
124 }
125 Binary.encodeFile (elmi flags filePath) interface
126 let js = jsModule metaModule
127 writeFile (elmo flags filePath) js
128 return interface
130toSrcTypes tipes = Traverse.traverse convert tipes
131 where
132 convert t = fmap (Parse.readType . P.render) (Type.extraPretty t)
134printTypes metaModule = do
135 putStrLn ""
136 forM_ (Map.toList $ types metaModule) $ \(n,t) -> do
137 pt <- Type.extraPretty t
138 print $ variable n <+> P.text ":" <+> pt
139 putStrLn ""
141getRuntime :: Flags -> IO FilePath
142getRuntime flags =
143 case runtime flags of
144 Just fp -> return fp
145 Nothing -> getDataFileName "elm-runtime.js"
147build :: Flags -> FilePath -> IO ()
148build flags rootFile = do
149 files <- if make flags then getSortedModuleNames rootFile else return [rootFile]
150 interfaces <- buildFiles flags (length files) Prelude.interfaces files
151 js <- foldM appendToOutput "" files
152 case only_js flags of
153 True -> do
154 putStr "Generating JavaScript ... "
155 writeFile (file flags rootFile "js") (genJs js)
156 putStrLn "Done"
157 False -> do
158 putStr "Generating HTML ... "
159 runtime <- getRuntime flags
160 let html = genHtml $ createHtml runtime rootFile (sources js) ""
161 writeFile (file flags rootFile "html") html
162 putStrLn "Done"
164 where
165 appendToOutput :: String -> FilePath -> IO String
166 appendToOutput js filePath =
167 do src <- readFile (elmo flags filePath)
168 return (src ++ js)
170 genHtml = if minify flags then Normal.renderHtml else Pretty.renderHtml
171 genJs = if minify flags then BS.unpack . JS.minify . BS.pack else id
172 sources js = map Link (scripts flags) ++
173 [ Source (if minify flags then Minified else Readable) js ]
176buildFiles :: Flags -> Int -> Interfaces -> [FilePath] -> IO Interfaces
177buildFiles _ _ interfaces [] = return interfaces
178buildFiles flags numModules interfaces (filePath:rest) = do
179 interface <- buildFile flags (numModules - length rest) numModules interfaces filePath
180 let moduleName = List.intercalate "." (splitDirectories (dropExtensions filePath))
181 interfaces' = Map.insert moduleName interface interfaces
182 buildFiles flags numModules interfaces' rest