@@ -1,158 +0,0 @@
1−module Build.Build (build) where
3−import Control.Monad (when, forM_, foldM)
4−import qualified Data.Map as Map
5−import qualified Data.List as List
6−import qualified Data.Binary as Binary
7−import System.Directory
12−import Text.Blaze.Html.Renderer.Utf8 (renderHtml)
13−import qualified Data.ByteString.Lazy.Char8 as BS
14−import qualified Data.ByteString.Lazy as L
16−import qualified Metadata.Prelude as Prelude
17−import qualified Transform.Canonicalize as Canonical
18−import SourceSyntax.Module
19−import Parse.Module (getModuleName)
20−import qualified Build.FromSource as FromSource
21−import Build.Dependencies (getSortedDependencies)
22−import Generate.JavaScript (jsModule)
23−import Generate.Html (createHtml, JSSource(..))
24−import qualified InterfaceSerialization as IS
26−import SourceSyntax.PrettyPrint (pretty, variable)
27−import Text.PrettyPrint as P
28−import qualified Type.Alias as Alias
30−import qualified Build.Utils as Utils
31−import qualified Build.Info as Info
32−import qualified Build.Flags as Flag
34−buildFile :: Flag.Flags -> Int -> Int -> Interfaces -> FilePath -> IO (String, ModuleInterface)
35−buildFile flags moduleNum numModules interfaces filePath = do
36− compiled <- alreadyCompiled
37− if not compiled then compile else do
38− bytes <- IS.loadInterface (Utils.elmi flags filePath)
39− let binary = IS.interfaceDecode (Utils.elmi flags filePath) =<< bytes
40− case IS.validVersion filePath =<< binary of
45− Right (name, interface) -> do
46− when (Flag.print_types flags) $
47− printTypes interfaces
52− return (name, interface)
55− alreadyCompiled :: IO Bool
57− existsi <- doesFileExist (Utils.elmi flags filePath)
58− existso <- doesFileExist (Utils.elmo flags filePath)
59− if not existsi || not existso
61− else do tsrc <- getModificationTime filePath
62− tint <- getModificationTime (Utils.elmo flags filePath)
63− return (tsrc <= tint)
66− number = "[" ++ show moduleNum ++ " of " ++ show numModules ++ "]"
68− compile :: IO (String,ModuleInterface)
70− source <- readFile filePath
71− let name = case getModuleName source of
74− putStrLn $ concat [ number, " Compiling ", name
75− , replicate (max 1 (20 - length name)) ' '
76− , "( " ++ filePath ++ " )" ]
78− createDirectoryIfMissing True (Flag.cache_dir flags)
79− createDirectoryIfMissing True (Flag.build_dir flags)
82− case FromSource.build (Flag.no_prelude flags) interfaces source of
84− mapM print (List.intersperse (P.text " ") errors)
86− Right modul -> return modul
88− when (Flag.print_types flags)
89− (printTypes interfaces
90− (types metaModule) (aliases metaModule) (imports metaModule))
92− let interface = Canonical.interface name $ ModuleInterface {
93− iVersion = Info.version,
94− iTypes = types metaModule,
95− iImports = imports metaModule,
96− iAdts = datatypes metaModule,
97− iAliases = aliases metaModule,
98− iFixities = fixities metaModule
101− createDirectoryIfMissing True . dropFileName $ Utils.elmi flags filePath
102− handle <- openBinaryFile (Utils.elmi flags filePath) WriteMode
103− L.hPut handle (Binary.encode (name,interface))
105− writeFile (Utils.elmo flags filePath) (jsModule metaModule)
106− return (name,interface)
108−printTypes interfaces moduleTypes moduleAliases moduleImports = do
110− let rules = Alias.rules interfaces moduleAliases moduleImports
111− forM_ (Map.toList moduleTypes) $ \(n,t) -> do
112− print $ variable n <+> P.text ":" <+> pretty (Alias.realias rules t)
115−build :: Flag.Flags -> FilePath -> IO ()
116−build flags rootFile = do
117− let noPrelude = Flag.no_prelude flags
118− builtIns <- if noPrelude then return Map.empty else Prelude.interfaces
120− files <- if Flag.make flags
121− then getSortedDependencies (Flag.src_dir flags) builtIns rootFile
122− else return [rootFile]
124− (moduleName, interfaces) <- buildFiles flags (length files) builtIns "" files
126− js <- foldM appendToOutput BS.empty files
128− (extension, code) <- case Flag.only_js flags of
130− putStr "Generating JavaScript ... "
133− putStr "Generating HTML ... "
134− rtsPath <- case Flag.runtime flags of
135− Just fp -> return fp
136− Nothing -> Info.runtime
137− return ("html", renderHtml $
138− createHtml rtsPath (takeBaseName rootFile) (sources js) moduleName "")
140− let targetFile = Utils.buildPath flags rootFile extension
141− createDirectoryIfMissing True (takeDirectory targetFile)
142− BS.writeFile targetFile code
146− appendToOutput :: BS.ByteString -> FilePath -> IO BS.ByteString
147− appendToOutput js filePath =
148− do src <- BS.readFile (Utils.elmo flags filePath)
149− return (BS.append src js)
151− sources js = map Link (Flag.scripts flags) ++ [ Source js ]
153−buildFiles :: Flag.Flags -> Int -> Interfaces -> String -> [FilePath] -> IO (String, Interfaces)
154−buildFiles _ _ interfaces moduleName [] = return (moduleName, interfaces)
155−buildFiles flags numModules interfaces _ (filePath:rest) = do
156− (name,interface) <- buildFile flags (numModules - length rest) numModules interfaces filePath
157− let interfaces' = Map.insert name interface interfaces
158− buildFiles flags numModules interfaces' name rest