Permalink
Browse files

Code cleanup/refactoring: initializeWiki is now two functions.

  • Loading branch information...
1 parent 2ff8918 commit d789539e3a7320017e714248ba1a1dd88f5d7c37 @jgm committed Jan 29, 2009
Showing with 47 additions and 41 deletions.
  1. +9 −6 Gitit.hs
  2. +38 −35 Gitit/Initialize.hs
View
@@ -22,7 +22,7 @@ module Main where
import HAppS.Server hiding (look, lookRead, lookCookieValue, mkCookie)
import Gitit.HAppS (look, lookRead, lookCookieValue, mkCookie)
import Gitit.Util (withTempDir, orIfNull, consolidateHeads)
-import Gitit.Initialize (initializeWiki)
+import Gitit.Initialize (createStaticIfMissing, createRepoIfMissing)
import System.Environment
import System.IO.UTF8
import System.IO (stderr)
@@ -73,7 +73,8 @@ main = do
Darcs _ -> ["darcs"]
forM_ prereqs $ \prog ->
findExecutable prog >>= \mbFind ->
- when (isNothing mbFind) $ error $ "Required program '" ++ prog ++ "' not found in system path."
+ when (isNothing mbFind) $ error $
+ "Required program '" ++ prog ++ "' not found in system path."
-- read user file and update state
userFileExists <- doesFileExist $ userFile conf
@@ -96,16 +97,18 @@ main = do
initializeAppState conf users' templ
-- setup the page repository and static files, if they don't exist
- initializeWiki conf
+ createRepoIfMissing conf
+ let staticdir = staticDir conf
+ createStaticIfMissing staticdir
-- start the server
hPutStrLn stderr $ "Starting server on port " ++ show (portNumber conf)
let debugger = if debugMode conf then debugFilter else id
tid <- forkIO $ simpleHTTP (Conf { validator = Nothing, port = portNumber conf }) $
debugger $
- [ dir "css" [ withExpiresHeaders $ fileServe [] $ staticDir conf </> "css" ]
- , dir "img" [ withExpiresHeaders $ fileServe [] $ staticDir conf </> "img" ]
- , dir "js" [ withExpiresHeaders $ fileServe [] $ staticDir conf </> "js" ]
+ [ dir "css" [ withExpiresHeaders $ fileServe [] $ staticdir </> "css" ]
+ , dir "img" [ withExpiresHeaders $ fileServe [] $ staticdir </> "img" ]
+ , dir "js" [ withExpiresHeaders $ fileServe [] $ staticdir </> "js" ]
] ++
(if debugMode conf then debugHandlers else []) ++
map (filterIf acceptsZip gzipBinary) wikiHandlers
View
@@ -16,26 +16,27 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
{- Functions for initializing a Gitit wiki.
-}
-module Gitit.Initialize ( initializeWiki )
+module Gitit.Initialize ( createStaticIfMissing, createRepoIfMissing )
where
-import System.FilePath ((</>), (<.>))
+import System.FilePath ((</>), (<.>), takeExtension)
import Data.FileStore
import Gitit.State
import Paths_gitit (getDataFileName)
import qualified Data.ByteString.Lazy as B
-import Prelude hiding (catch)
-import Control.Exception (throwIO, catch)
-import System.Directory (copyFile, createDirectoryIfMissing, doesDirectoryExist)
+import Control.Exception (throwIO, try)
+import System.Directory (copyFile, createDirectoryIfMissing, doesDirectoryExist, getDirectoryContents)
import System.IO (stderr, hPutStrLn)
-import Control.Monad (unless, zipWithM_)
+import Control.Monad (unless, forM_, liftM)
--- | Create repository and public directories, unless they already exist.
-initializeWiki :: Config -> IO ()
-initializeWiki conf = do
- let staticdir = staticDir conf
+-- | Create page repository unless it exists.
+createRepoIfMissing :: Config -> IO ()
+createRepoIfMissing conf = do
fs <- getFileStore
- repoExists <- catch (initialize fs >> return False)
- (\e -> if e == RepositoryExists then return True else throwIO e >> return False)
+ repoExists <- try (initialize fs) >>= \res ->
+ case res of
+ Right _ -> return False
+ Left RepositoryExists -> return True
+ Left e -> throwIO e >> return False
unless repoExists $ do
welcomepath <- getDataFileName $ "data" </> "FrontPage.page"
welcomecontents <- B.readFile welcomepath
@@ -45,33 +46,35 @@ initializeWiki conf = do
create fs (frontPage conf <.> "page") (Author "Gitit" "") "Default front page" welcomecontents
create fs "Help.page" (Author "Gitit" "") "Default front page" helpcontents
hPutStrLn stderr "Created repository"
+
+
+-- | Create static directory unless it exists.
+createStaticIfMissing :: FilePath -> IO ()
+createStaticIfMissing staticdir = do
staticExists <- doesDirectoryExist staticdir
unless staticExists $ do
- createDirectoryIfMissing True $ staticdir </> "css"
- let stylesheets = map ("css" </>) ["screen.css", "print.css", "ie.css", "hk-pyg.css"]
- stylesheetpaths <- mapM getDataFileName stylesheets
- zipWithM_ copyFile stylesheetpaths (map (staticdir </>) stylesheets)
- createDirectoryIfMissing True $ staticdir </> "img" </> "icons"
- let imgs = map (("img" </>) . ("icons" </>))
- ["cross.png", "doc.png", "email.png", "external.png", "feed.png", "folder.png",
- "im.png", "key.png", "page.png", "pdf.png", "tick.png", "xls.png"]
- imgpaths <- mapM getDataFileName imgs
- zipWithM_ copyFile imgpaths (map (staticdir </>) imgs)
+
+ let cssdir = staticdir </> "css"
+ createDirectoryIfMissing True cssdir
+ cssDataDir <- getDataFileName "css"
+ cssFiles <- liftM (filter (\f -> takeExtension f == ".css")) $ getDirectoryContents cssDataDir
+ forM_ cssFiles $ \f -> copyFile (cssDataDir </> f) (cssdir </> f)
+
+ let icondir = staticdir </> "img" </> "icons"
+ createDirectoryIfMissing True icondir
+ iconDataDir <- getDataFileName ("img" </> "icons")
+ iconFiles <- liftM (filter (\f -> takeExtension f == ".png")) $ getDirectoryContents iconDataDir
+ forM_ iconFiles $ \f -> copyFile (iconDataDir </> f) (icondir </> f)
+
logopath <- getDataFileName $ "img" </> "gitit-dog.png"
- copyFile logopath (staticdir </> "img" </> "logo.png")
- createDirectoryIfMissing True $ staticdir </> "js"
+ copyFile logopath $ staticdir </> "img" </> "logo.png"
+
+ let jsdir = staticdir </> "js"
+ createDirectoryIfMissing True jsdir
let javascripts = ["jquery.min.js", "jquery-ui.packed.js",
"folding.js", "dragdiff.js", "preview.js", "search.js", "uploadForm.js"]
- javascriptpaths <- mapM getDataFileName $ map ("js" </>) javascripts
- zipWithM_ copyFile javascriptpaths $ map ((staticdir </> "js") </>) javascripts
- hPutStrLn stderr $ "Created " ++ staticdir ++ " directory"
- jsMathExists <- doesDirectoryExist (staticdir </> "js" </> "jsMath")
- updateAppState $ \s -> s{ jsMath = jsMathExists }
- unless jsMathExists $ do
- hPutStrLn stderr $ replicate 80 '*' ++
- "\nWarning: jsMath not found.\n" ++
- "If you want support for math, copy the jsMath directory into " ++ staticdir ++ "/js/\n" ++
- "jsMath can be obtained from http://www.math.union.edu/~dpvc/jsMath/\n" ++
- replicate 80 '*'
+ jsDataDir <- getDataFileName "js"
+ forM_ javascripts $ \f -> copyFile (jsDataDir </> f) (jsdir </> f)
+ hPutStrLn stderr $ "Created " ++ staticdir ++ " directory"

0 comments on commit d789539

Please sign in to comment.