{-# LANGUAGE CPP #-}
module Network.Gitit.Plugins ( loadPlugin, loadPlugins )
where
import Network.Gitit.Types
import System.FilePath (takeBaseName)
import Control.Monad (unless)
import System.Log.Logger (logM, Priority(..))
#ifdef _PLUGINS
import Data.List (isInfixOf, isPrefixOf)
import GHC
import GHC.Paths
import Unsafe.Coerce
loadPlugin :: FilePath -> IO Plugin
loadPlugin pluginName = do
logM "gitit" WARNING ("Loading plugin '" ++ pluginName ++ "'...")
runGhc (Just libdir) $ do
dflags <- getSessionDynFlags
setSessionDynFlags dflags
defaultCleanupHandler dflags $ do
unless ("Network.Gitit.Plugin." `isPrefixOf` pluginName)
$ do
addTarget =<< guessTarget pluginName Nothing
r <- load LoadAllTargets
case r of
Failed -> error $ "Error loading plugin: " ++ pluginName
Succeeded -> return ()
let modName =
if "Network.Gitit.Plugin" `isPrefixOf` pluginName
then pluginName
else if "Network/Gitit/Plugin/" `isInfixOf` pluginName
then "Network.Gitit.Plugin." ++ takeBaseName pluginName
else takeBaseName pluginName
pr <- parseImportDecl "import Prelude"
i <- parseImportDecl "import Network.Gitit.Interface"
m <- parseImportDecl ("import " ++ modName)
setContext [IIDecl m, IIDecl i, IIDecl pr]
value <- compileExpr (modName ++ ".plugin :: Plugin")
let value' = (unsafeCoerce value) :: Plugin
return value'
#else
loadPlugin :: FilePath -> IO Plugin
loadPlugin :: FilePath -> IO Plugin
loadPlugin pluginName :: FilePath
pluginName = do
FilePath -> IO Any
forall a. HasCallStack => FilePath -> a
error (FilePath -> IO Any) -> FilePath -> IO Any
forall a b. (a -> b) -> a -> b
$ "Cannot load plugin '" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
pluginName FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++
"'. gitit was not compiled with plugin support."
Plugin -> IO Plugin
forall (m :: * -> *) a. Monad m => a -> m a
return Plugin
forall a. HasCallStack => a
undefined
#endif
loadPlugins :: [FilePath] -> IO [Plugin]
loadPlugins :: [FilePath] -> IO [Plugin]
loadPlugins pluginNames :: [FilePath]
pluginNames = do
[Plugin]
plugins' <- (FilePath -> IO Plugin) -> [FilePath] -> IO [Plugin]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM FilePath -> IO Plugin
loadPlugin [FilePath]
pluginNames
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([FilePath] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FilePath]
pluginNames) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath -> Priority -> FilePath -> IO ()
logM "gitit" Priority
WARNING "Finished loading plugins."
[Plugin] -> IO [Plugin]
forall (m :: * -> *) a. Monad m => a -> m a
return [Plugin]
plugins'