@@ -41,8 +41,7 @@ import Control.Monad
4141import Control.Monad.Error.Class (MonadError(..))
4242import Control.Monad.Reader
4343import Control.Monad.Writer
44−
45−import System.FilePath ((</>))
44+import Control.Monad.Supply.Class (fresh)
4645
4746import Language.PureScript.AST as P
4847import Language.PureScript.Comments as P
@@ -144,11 +143,6 @@ data RebuildPolicy
144143 -- | Always rebuild this module
145144 | RebuildAlways deriving (Show, Eq, Ord)
146145
147−-- Traverse (Either e) instance (base 4.7)
148−traverseEither :: Applicative f => (a -> f b) -> Either e a -> f (Either e b)
149−traverseEither _ (Left x) = pure (Left x)
150−traverseEither f (Right y) = Right <$> f y
151−
152146-- |
153147-- Compiles in "make" mode, compiling each module separately to a js files and an externs file
154148--
@@ -156,67 +150,46 @@ traverseEither f (Right y) = Right <$> f y
156150-- having to typecheck the module again.
157151--
158152make :: forall m. (Functor m, Applicative m, Monad m, MonadMake m)
159− => FilePath -> [(Either RebuildPolicy FilePath, Module)] -> [String] -> m Environment
160−make outputDir ms prefix = do
153+ => (ModuleName -> m (Either RebuildPolicy (Maybe UTCTime)))
154+ -> (ModuleName -> m (Maybe UTCTime))
155+ -> (ModuleName -> m (FilePath, String))
156+ -> (CoreFn.Module CoreFn.Ann -> String -> Environment -> Integer -> m ())
157+ -> [(Either RebuildPolicy FilePath, Module)]
158+ -> m Environment
159+make getInputTimestamp getOutputTimestamp readExterns codegen ms = do
161160 noPrelude <- asks optionsNoPrelude
162161 unless noPrelude (checkPreludeIsDefined (map snd ms))
163− let filePathMap = M.fromList (map (\(fp, Module _ mn _ _) -> (mn, fp)) ms)
164−
165162 (sorted, graph) <- sortModules $ map importPrim $ if noPrelude then map snd ms else map (importPrelude . snd) ms
166−
167163 mapM_ lint sorted
168−
169164 toRebuild <- foldM (\s (Module _ moduleName' _ _) -> do
170− let filePath = runModuleName moduleName'
171−
172− jsFile = outputDir </> filePath </> "index.js"
173− externsFile = outputDir </> filePath </> "externs.purs"
174− inputFile = fromMaybe (error "Module has no filename in 'make'") $ M.lookup moduleName' filePathMap
175−
176− jsTimestamp <- getTimestamp jsFile
177− externsTimestamp <- getTimestamp externsFile
178− inputTimestamp <- traverseEither getTimestamp inputFile
179−
180− return $ case (inputTimestamp, jsTimestamp, externsTimestamp) of
181− (Right (Just t1), Just t2, Just t3) | t1 < min t2 t3 -> s
182− (Left RebuildNever, Just _, Just _) -> s
165+ inputTimestamp <- getInputTimestamp moduleName'
166+ outputTimestamp <- getOutputTimestamp moduleName'
167+ return $ case (inputTimestamp, outputTimestamp) of
168+ (Right (Just t1), Just t2) | t1 < t2 -> s
169+ (Left RebuildNever, Just _) -> s
183170 _ -> S.insert moduleName' s) S.empty sorted
184171
185172 marked <- rebuildIfNecessary (reverseDependencies graph) toRebuild sorted
186−
187173 (desugared, nextVar) <- runSupplyT 0 $ zip (map fst marked) <$> desugar (map snd marked)
188−
189174 evalSupplyT nextVar $ go initEnvironment desugared
190−
191175 where
176+
192177 go :: Environment -> [(Bool, Module)] -> SupplyT m Environment
193178 go env [] = return env
194179 go env ((False, m) : ms') = do
195180 (_, env') <- lift . runCheck' env $ typeCheckModule Nothing m
196−
197181 go env' ms'
198182 go env ((True, m@(Module coms moduleName' _ exps)) : ms') = do
199− let filePath = runModuleName moduleName'
200− jsFile = outputDir </> filePath </> "index.js"
201− externsFile = outputDir </> filePath </> "externs.purs"
202183
203184 lift . progress $ "Compiling " ++ runModuleName moduleName'
204−
205185 (Module _ _ elaborated _, env') <- lift . runCheck' env $ typeCheckModule Nothing m
206−
207186 regrouped <- createBindingGroups moduleName' . collapseBindingGroups $ elaborated
208−
209187 let mod' = Module coms moduleName' regrouped exps
210188 corefn = CoreFn.moduleToCoreFn env' mod'
211189 [renamed] = renameInModules [corefn]
212−
213− pjs <- prettyPrintJS <$> moduleToJs renamed Nothing
214− let js = unlines $ map ("// " ++) prefix ++ [pjs]
215− let exts = unlines $ map ("-- " ++) prefix ++ [moduleToPs mod' env']
216−
217− lift $ writeTextFile jsFile js
218− lift $ writeTextFile externsFile exts
219−
190+ let exts = moduleToPs mod' env'
191+ nextVar <- fresh
192+ lift $ codegen renamed exts env' nextVar
220193 go env' ms'
221194
222195 rebuildIfNecessary :: M.Map ModuleName [ModuleName] -> S.Set ModuleName -> [Module] -> m [(Bool, Module)]
@@ -226,12 +199,11 @@ make outputDir ms prefix = do
226199 toRebuild' = toRebuild `S.union` S.fromList deps
227200 (:) (True, m) <$> rebuildIfNecessary graph toRebuild' ms'
228201 rebuildIfNecessary graph toRebuild (Module _ moduleName' _ _ : ms') = do
229− let externsFile = outputDir </> runModuleName moduleName' </> "externs.purs"
230− externs <- readTextFile externsFile
231− externsModules <- fmap (map snd) . alterErrors $ P.parseModulesFromFiles id [(externsFile, externs)]
202+ (path, externs) <- readExterns moduleName'
203+ externsModules <- fmap (map snd) . alterErrors $ P.parseModulesFromFiles id [(path, externs)]
232204 case externsModules of
233205 [m'@(Module _ moduleName'' _ _)] | moduleName'' == moduleName' -> (:) (False, m') <$> rebuildIfNecessary graph toRebuild ms'
234− _ -> throwError . errorMessage . InvalidExternsFile $ externsFile
206+ _ -> throwError . errorMessage . InvalidExternsFile $ path
235207 where
236208 alterErrors = flip catchError $ \(MultipleErrors errs) ->
237209 throwError . MultipleErrors $ flip map errs $ \e -> case e of