{-# LANGUAGE PackageImports #-}
{-# LANGUAGE TemplateHaskell #-}
module Language.PureScript.Ide.Rebuild
( rebuildFileSync
, rebuildFileAsync
, rebuildFile
) where
import Protolude
import "monad-logger" Control.Monad.Logger
import qualified Data.List as List
import qualified Data.Map.Lazy as M
import Data.Maybe (fromJust)
import qualified Data.Set as S
import qualified Language.PureScript as P
import Language.PureScript.Ide.Error
import Language.PureScript.Ide.Logging
import Language.PureScript.Ide.State
import Language.PureScript.Ide.Types
import Language.PureScript.Ide.Util
rebuildFile
:: (Ide m, MonadLogger m, MonadError IdeError m)
=> FilePath
-> Maybe FilePath
-> (ReaderT IdeEnvironment (LoggingT IO) () -> m ())
-> m Success
rebuildFile file actualFile runOpenBuild = do
input <- ideReadFile file
m <- case snd <$> P.parseModuleFromFile (maybe identity const actualFile) input of
Left parseError ->
throwError (RebuildError (P.MultipleErrors [P.toPositionedError parseError]))
Right m -> pure m
externs <- logPerf (labelTimespec "Sorting externs") (sortExterns m =<< getExternFiles)
outputDirectory <- confOutputPath . ideConfiguration <$> ask
let filePathMap = M.singleton (P.getModuleName m) (Left P.RebuildAlways)
foreigns <- P.inferForeignModules (M.singleton (P.getModuleName m) (Right file))
let makeEnv = MakeActionsEnv outputDirectory filePathMap foreigns False
(result, warnings) <- logPerf (labelTimespec "Rebuilding Module") $
liftIO
. P.runMake P.defaultOptions
. P.rebuildModule (buildMakeActions
>>= shushProgress $ makeEnv) externs $ m
case result of
Left errors -> throwError (RebuildError errors)
Right newExterns -> do
whenM isEditorMode $ do
insertModule (fromMaybe file actualFile, m)
insertExterns newExterns
void populateVolatileState
runOpenBuild (rebuildModuleOpen makeEnv externs m)
pure (RebuildSuccess warnings)
isEditorMode :: Ide m => m Bool
isEditorMode = asks (confEditorMode . ideConfiguration)
rebuildFileAsync
:: forall m. (Ide m, MonadLogger m, MonadError IdeError m)
=> FilePath -> Maybe FilePath -> m Success
rebuildFileAsync fp fp' = rebuildFile fp fp' asyncRun
where
asyncRun :: ReaderT IdeEnvironment (LoggingT IO) () -> m ()
asyncRun action = do
env <- ask
let ll = confLogLevel (ideConfiguration env)
void (liftIO (async (runLogger ll (runReaderT action env))))
rebuildFileSync
:: forall m. (Ide m, MonadLogger m, MonadError IdeError m)
=> FilePath -> Maybe FilePath -> m Success
rebuildFileSync fp fp' = rebuildFile fp fp' syncRun
where
syncRun :: ReaderT IdeEnvironment (LoggingT IO) () -> m ()
syncRun action = do
env <- ask
let ll = confLogLevel (ideConfiguration env)
void (liftIO (runLogger ll (runReaderT action env)))
rebuildModuleOpen
:: (Ide m, MonadLogger m)
=> MakeActionsEnv
-> [P.ExternsFile]
-> P.Module
-> m ()
rebuildModuleOpen makeEnv externs m = void $ runExceptT $ do
(openResult, _) <- liftIO
. P.runMake P.defaultOptions
. P.rebuildModule (buildMakeActions
>>= shushProgress
>>= shushCodegen
$ makeEnv) externs $ openModuleExports m
case openResult of
Left _ ->
throwError (GeneralError "Failed when rebuilding with open exports")
Right result -> do
$(logDebug)
("Setting Rebuild cache: " <> P.runModuleName (P.efModuleName result))
cacheRebuild result
data MakeActionsEnv =
MakeActionsEnv
{ maeOutputDirectory :: FilePath
, maeFilePathMap :: ModuleMap (Either P.RebuildPolicy FilePath)
, maeForeignPathMap :: ModuleMap FilePath
, maePrefixComment :: Bool
}
buildMakeActions :: MakeActionsEnv -> P.MakeActions P.Make
buildMakeActions MakeActionsEnv{..} =
P.buildMakeActions
maeOutputDirectory
maeFilePathMap
maeForeignPathMap
maePrefixComment
shushProgress :: P.MakeActions P.Make -> MakeActionsEnv -> P.MakeActions P.Make
shushProgress ma _ =
ma { P.progress = \_ -> pure () }
shushCodegen :: P.MakeActions P.Make -> MakeActionsEnv -> P.MakeActions P.Make
shushCodegen ma MakeActionsEnv{..} =
ma { P.codegen = \_ _ _ -> pure () }
sortExterns
:: (Ide m, MonadError IdeError m)
=> P.Module
-> ModuleMap P.ExternsFile
-> m [P.ExternsFile]
sortExterns m ex = do
sorted' <- runExceptT
. P.sortModules
. (:) m
. map mkShallowModule
. M.elems
. M.delete (P.getModuleName m) $ ex
case sorted' of
Left err ->
throwError (RebuildError err)
Right (sorted, graph) -> do
let deps = fromJust (List.lookup (P.getModuleName m) graph)
pure $ mapMaybe getExtern (deps `inOrderOf` map P.getModuleName sorted)
where
mkShallowModule P.ExternsFile{..} =
P.Module (P.internalModuleSourceSpan "<rebuild>") [] efModuleName (map mkImport efImports) Nothing
mkImport (P.ExternsImport mn it iq) =
P.ImportDeclaration (P.internalModuleSourceSpan "<rebuild>", []) mn it iq
getExtern mn = M.lookup mn ex
inOrderOf :: (Ord a) => [a] -> [a] -> [a]
inOrderOf xs ys = let s = S.fromList xs in filter (`S.member` s) ys
openModuleExports :: P.Module -> P.Module
openModuleExports (P.Module ss cs mn decls _) = P.Module ss cs mn decls Nothing