module HsInspect.Plugin
( plugin,
)
where
import qualified Config as GHC
import Control.Monad (when)
import Control.Monad.IO.Class (liftIO)
import Data.Foldable (traverse_)
import Data.List (stripPrefix)
import qualified GHC
import qualified GhcPlugins as GHC
import System.Directory (doesDirectoryExist)
import System.Environment
import System.IO.Error (catchIOError)
plugin :: GHC.Plugin
plugin =
GHC.defaultPlugin
{ GHC.installCoreToDos = install
}
install :: [GHC.CommandLineOption] -> [GHC.CoreToDo] -> GHC.CoreM [GHC.CoreToDo]
install _ core = do
dflags <- GHC.getDynFlags
args <- liftIO $ getArgs
let ghcFlags = unwords $ replace ["-fplugin", "HsInspect.Plugin"] [] args
paths = GHC.importPaths dflags
writeGhcFlags path =
whenM (doesDirectoryExist path)
$ writeFile (path <> "/.ghc.flags") ghcFlags
enable = case GHC.hscTarget dflags of
GHC.HscInterpreted -> False
GHC.HscNothing -> False
_ -> True
when enable $ liftIO . ignoreIOExceptions $ do
traverse_ writeGhcFlags paths
writeFile ".ghc.version" GHC.cProjectVersion
pure core
replace :: Eq a => [a] -> [a] -> [a] -> [a]
replace [] _ _ = error "Extra.replace, first argument cannot be empty"
replace from to xs | Just xs' <- stripPrefix from xs = to ++ replace from to xs'
replace from to (x : xs) = x : replace from to xs
replace _ _ [] = []
whenM :: Monad m => m Bool -> m () -> m ()
whenM b t = ifM b t (return ())
ifM :: Monad m => m Bool -> m a -> m a -> m a
ifM b t f = do b' <- b; if b' then t else f
ignoreIOExceptions :: IO () -> IO ()
ignoreIOExceptions io = io `catchIOError` (\_ -> pure ())