1{-# LANGUAGE DeriveDataTypeable #-}
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
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 qualified Transform.Canonicalize as Canonical
21import SourceSyntax.Module
22import Initialize (buildFromSource, getSortedModuleNames, Interfaces)
23import Generate.JavaScript (jsModule)
24import Generate.Html (createHtml, JSStyle(..), JSSource(..))
27import SourceSyntax.PrettyPrint (pretty, variable)
28import Text.PrettyPrint as P
29import qualified Type.Type as Type
30import qualified Data.Traversable as Traverse
35 , runtime :: Maybe FilePath
38 , print_program :: Bool
39 , scripts :: [FilePath]
42 , output_directory :: FilePath
44 deriving (Data,Typeable,Show,Eq)
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."
53 &= help "Compile only to JavaScript."
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."
61 &= help "Do not import Prelude by default, used only when compiling standard libraries."
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")
70main = compileArgs =<< cmdArgs flags
72compileArgs :: Flags -> IO ()
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 not compiled then compile
91 else getInterface `fmap` Binary.decodeFile (elmi flags filePath)
93 getInterface :: (String, ModuleInterface) -> ModuleInterface
96 alreadyCompiled :: IO Bool
98 existsi <- doesFileExist (elmi flags filePath)
99 existso <- doesFileExist (elmo flags filePath)
100 if not existsi || not existso
102 else do tsrc <- getModificationTime filePath
103 tint <- getModificationTime (elmo flags filePath)
107 number = "[" ++ show moduleNum ++ " of " ++ show numModules ++ "]"
110 name = List.intercalate "." (splitDirectories (dropExtensions filePath))
112 compile :: IO ModuleInterface
114 putStrLn (number ++ " Compiling " ++ name)
115 source <- readFile filePath
116 createDirectoryIfMissing True (output_directory flags)
118 case buildFromSource (no_prelude flags) interfaces source of
119 Left err -> mapM print err >> exitFailure
121 case print_program flags of
123 True -> print . pretty $ program modul
126 if print_types flags then printTypes metaModule else return ()
127 tipes <- Traverse.traverse Type.toSrcType (types metaModule)
128 let interface = Canonical.interface name $ ModuleInterface {
130 iAdts = datatypes metaModule,
131 iAliases = aliases metaModule
133 createDirectoryIfMissing True . dropFileName $ elmi flags filePath
134 Binary.encodeFile (elmi flags filePath) (name,interface)
135 writeFile (elmo flags filePath) (jsModule metaModule)
138printTypes metaModule = do
140 forM_ (Map.toList $ types metaModule) $ \(n,t) -> do
141 pt <- Type.extraPretty t
142 print $ variable n <+> P.text ":" <+> pt
145getRuntime :: Flags -> IO FilePath
147 case runtime flags of
149 Nothing -> getDataFileName "elm-runtime.js"
151build :: Flags -> FilePath -> IO ()
152build flags rootFile = do
153 let noPrelude = no_prelude flags
154 files <- if make flags then getSortedModuleNames noPrelude rootFile else return [rootFile]
155 let ifaces = if noPrelude then Map.empty else Prelude.interfaces
156 interfaces <- buildFiles flags (length files) ifaces files
157 js <- foldM appendToOutput "" files
158 case only_js flags of
160 putStr "Generating JavaScript ... "
161 writeFile (file flags rootFile "js") (genJs js)
164 putStr "Generating HTML ... "
165 runtime <- getRuntime flags
166 let html = genHtml $ createHtml runtime rootFile (sources js) ""
167 writeFile (file flags rootFile "html") html
171 appendToOutput :: String -> FilePath -> IO String
172 appendToOutput js filePath =
173 do src <- readFile (elmo flags filePath)
176 genHtml = if minify flags then Normal.renderHtml else Pretty.renderHtml
177 genJs = if minify flags then BS.unpack . JS.minify . BS.pack else id
178 sources js = map Link (scripts flags) ++
179 [ Source (if minify flags then Minified else Readable) js ]
182buildFiles :: Flags -> Int -> Interfaces -> [FilePath] -> IO Interfaces
183buildFiles _ _ interfaces [] = return interfaces
184buildFiles flags numModules interfaces (filePath:rest) = do
185 interface <- buildFile flags (numModules - length rest) numModules interfaces filePath
186 let moduleName = List.intercalate "." (splitDirectories (dropExtensions filePath))
187 interfaces' = Map.insert moduleName interface interfaces
188 buildFiles flags numModules interfaces' rest
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list