Skip to content

Commit

Permalink
Moved initializeGititState to Initialize.
Browse files Browse the repository at this point in the history
Also folded user file reading and plugin loading into it.
  • Loading branch information
jgm committed Jun 16, 2009
1 parent a546834 commit ecd8882
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 26 deletions.
3 changes: 0 additions & 3 deletions Network/Gitit.hs
Expand Up @@ -25,8 +25,6 @@ module Network.Gitit ( initializeGititState
, User(..)
, Cache(..)
, emptyCache
, loadPlugin
, loadPlugins
, wikiHandler
, readMimeTypesFile
, createRepoIfMissing
Expand All @@ -38,7 +36,6 @@ import Network.Gitit.Types
import Network.Gitit.Framework
import Network.Gitit.State
import Network.Gitit.Server
import Network.Gitit.Plugins (loadPlugin, loadPlugins)
import Network.Gitit.Handlers
import Network.Gitit.Initialize
import Network.Gitit.Config (readMimeTypesFile, getDefaultConfig)
Expand Down
23 changes: 22 additions & 1 deletion Network/Gitit/Initialize.hs
Expand Up @@ -16,12 +16,18 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
{- Functions for initializing a Gitit wiki.
-}

module Network.Gitit.Initialize ( createStaticIfMissing, createRepoIfMissing, createTemplateIfMissing )
module Network.Gitit.Initialize ( initializeGititState
, createStaticIfMissing
, createRepoIfMissing
, createTemplateIfMissing )
where
import System.FilePath ((</>), (<.>), takeExtension)
import Data.FileStore
import qualified Data.Map as M
import Network.Gitit.Types
import Network.Gitit.State
import Network.Gitit.Framework
import Network.Gitit.Plugins
import Paths_gitit (getDataFileName)
import Control.Exception (throwIO, try)
import System.Directory (copyFile, createDirectoryIfMissing, doesDirectoryExist, getDirectoryContents, doesFileExist)
Expand All @@ -32,6 +38,21 @@ import Text.Pandoc
import Text.Pandoc.Shared (HTMLMathMethod(..))
import System.Log.Logger (logM, Priority(..))

-- | Initialize Gitit State.
initializeGititState :: FilePath
-> [FilePath]
-> IO ()
initializeGititState userFile' pluginModules' = do
plugins' <- loadPlugins pluginModules'
userFileExists <- doesFileExist userFile'
users' <- if userFileExists
then liftM (M.fromList . read) $ readFile userFile'
else return M.empty
updateAppState $ \s -> s { sessions = Sessions M.empty
, users = users'
, cache = emptyCache
, plugins = plugins' }

-- | Create template file if it doesn't exist.
createTemplateIfMissing :: Config -> IO ()
createTemplateIfMissing conf' = do
Expand Down
10 changes: 0 additions & 10 deletions Network/Gitit/State.hs
Expand Up @@ -45,16 +45,6 @@ appstate = unsafePerformIO $ newIORef AppState { sessions = undefined
, cache = undefined
, plugins = undefined }

initializeGititState :: MonadIO m
=> M.Map String User
-> [Plugin]
-> m ()
initializeGititState users' plugins' = do
updateAppState $ \s -> s { sessions = Sessions M.empty
, users = users'
, cache = emptyCache
, plugins = plugins' }

updateAppState :: MonadIO m => (AppState -> AppState) -> m ()
updateAppState fn = liftIO $! atomicModifyIORef appstate $ \st -> (fn st, ())

Expand Down
13 changes: 1 addition & 12 deletions gitit.hs
Expand Up @@ -28,8 +28,6 @@ import System.FilePath ((</>))
import Control.Concurrent
import Network.Gitit.Config (getConfigFromOpts)
import Data.Maybe (isNothing)
import qualified Data.Map as M
import System.IO.UTF8 (readFile)
import Control.Monad.Reader
import System.Log.Logger (logM, Priority(..), setLevel, setHandlers,
getLogger, saveGlobalLogger)
Expand All @@ -50,12 +48,6 @@ main = do
when (isNothing mbFind) $ error $
"Required program '" ++ prog ++ "' not found in system path."

-- read user file
userFileExists <- doesFileExist $ userFile conf
users' <- if userFileExists
then liftM (M.fromList . read) $ readFile $ userFile conf
else return M.empty

-- set up logging
let level = if debugMode conf then DEBUG else logLevel conf
logFileHandler <- fileHandler (logFile conf) level
Expand All @@ -74,11 +66,8 @@ main = do

let conf' = conf{jsMath = jsMathExists, logLevel = level}

-- load plugins
plugins' <- loadPlugins $ pluginModules conf'

-- initialize state
initializeGititState users' plugins'
initializeGititState (userFile conf') (pluginModules conf')

-- setup the page repository, template, and static files, if they don't exist
createRepoIfMissing conf'
Expand Down

0 comments on commit ecd8882

Please sign in to comment.