{-# LANGUAGE CPP #-}
{-
Copyright (C) 2009 John MacFarlane <jgm@berkeley.edu>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-}

{- Functions for loading plugins.
-}

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 :: FilePath -> IO Plugin
loadPlugin FilePath
pluginName = do
  FilePath -> Priority -> FilePath -> IO ()
logM FilePath
"gitit" Priority
WARNING (FilePath
"Loading plugin '" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
pluginName FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"'...")
  Maybe FilePath -> Ghc Plugin -> IO Plugin
forall a. Maybe FilePath -> Ghc a -> IO a
runGhc (FilePath -> Maybe FilePath
forall a. a -> Maybe a
Just FilePath
libdir) (Ghc Plugin -> IO Plugin) -> Ghc Plugin -> IO Plugin
forall a b. (a -> b) -> a -> b
$ do
    DynFlags
dflags <- Ghc DynFlags
forall (m :: * -> *). GhcMonad m => m DynFlags
getSessionDynFlags
    DynFlags -> Ghc [InstalledUnitId]
forall (m :: * -> *). GhcMonad m => DynFlags -> m [InstalledUnitId]
setSessionDynFlags DynFlags
dflags
    -- initDynFlags
    Bool -> Ghc () -> Ghc ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (FilePath
"Network.Gitit.Plugin." FilePath -> FilePath -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` FilePath
pluginName)
      (Ghc () -> Ghc ()) -> Ghc () -> Ghc ()
forall a b. (a -> b) -> a -> b
$ do
          Target -> Ghc ()
forall (m :: * -> *). GhcMonad m => Target -> m ()
addTarget (Target -> Ghc ()) -> Ghc Target -> Ghc ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< FilePath -> Maybe Phase -> Ghc Target
forall (m :: * -> *).
GhcMonad m =>
FilePath -> Maybe Phase -> m Target
guessTarget FilePath
pluginName Maybe Phase
forall a. Maybe a
Nothing
          SuccessFlag
r <- LoadHowMuch -> Ghc SuccessFlag
forall (m :: * -> *). GhcMonad m => LoadHowMuch -> m SuccessFlag
load LoadHowMuch
LoadAllTargets
          case SuccessFlag
r of
            SuccessFlag
Failed -> FilePath -> Ghc ()
forall a. HasCallStack => FilePath -> a
error (FilePath -> Ghc ()) -> FilePath -> Ghc ()
forall a b. (a -> b) -> a -> b
$ FilePath
"Error loading plugin: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
pluginName
            SuccessFlag
Succeeded -> () -> Ghc ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    let modName :: FilePath
modName =
          if FilePath
"Network.Gitit.Plugin" FilePath -> FilePath -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` FilePath
pluginName
             then FilePath
pluginName
             else if FilePath
"Network/Gitit/Plugin/" FilePath -> FilePath -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isInfixOf` FilePath
pluginName
                     then FilePath
"Network.Gitit.Plugin." FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath -> FilePath
takeBaseName FilePath
pluginName
                     else FilePath -> FilePath
takeBaseName FilePath
pluginName
    ImportDecl GhcPs
pr <- FilePath -> Ghc (ImportDecl GhcPs)
forall (m :: * -> *).
GhcMonad m =>
FilePath -> m (ImportDecl GhcPs)
parseImportDecl FilePath
"import Prelude"
    ImportDecl GhcPs
i <- FilePath -> Ghc (ImportDecl GhcPs)
forall (m :: * -> *).
GhcMonad m =>
FilePath -> m (ImportDecl GhcPs)
parseImportDecl FilePath
"import Network.Gitit.Interface"
    ImportDecl GhcPs
m <- FilePath -> Ghc (ImportDecl GhcPs)
forall (m :: * -> *).
GhcMonad m =>
FilePath -> m (ImportDecl GhcPs)
parseImportDecl (FilePath
"import " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
modName)
    [InteractiveImport] -> Ghc ()
forall (m :: * -> *). GhcMonad m => [InteractiveImport] -> m ()
setContext [ImportDecl GhcPs -> InteractiveImport
IIDecl ImportDecl GhcPs
m, ImportDecl GhcPs -> InteractiveImport
IIDecl  ImportDecl GhcPs
i, ImportDecl GhcPs -> InteractiveImport
IIDecl ImportDecl GhcPs
pr]
    HValue
value <- FilePath -> Ghc HValue
forall (m :: * -> *). GhcMonad m => FilePath -> m HValue
compileExpr (FilePath
modName FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
".plugin :: Plugin")
    let value' :: Plugin
value' = (HValue -> Plugin
forall a b. a -> b
unsafeCoerce HValue
value) :: Plugin
    Plugin -> Ghc Plugin
forall (m :: * -> *) a. Monad m => a -> m a
return Plugin
value'

#else

loadPlugin :: FilePath -> IO Plugin
loadPlugin pluginName = do
  error $ "Cannot load plugin '" ++ pluginName ++
          "'. gitit was not compiled with plugin support."
  return undefined

#endif

loadPlugins :: [FilePath] -> IO [Plugin]
loadPlugins :: [FilePath] -> IO [Plugin]
loadPlugins [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 FilePath
"gitit" Priority
WARNING FilePath
"Finished loading plugins."
  [Plugin] -> IO [Plugin]
forall (m :: * -> *) a. Monad m => a -> m a
return [Plugin]
plugins'