2006-10-17 16:22:29 +02:00
|
|
|
import Distribution.Simple
|
2010-01-12 08:02:30 +01:00
|
|
|
import Distribution.Simple.Setup
|
2010-01-17 01:28:36 +01:00
|
|
|
(copyDest, copyVerbosity, fromFlag, installVerbosity, BuildFlags(..))
|
2010-01-12 08:02:36 +01:00
|
|
|
import Distribution.PackageDescription
|
2010-01-17 01:28:36 +01:00
|
|
|
(PackageDescription(..), Executable(..), BuildInfo(..))
|
2010-01-12 08:02:30 +01:00
|
|
|
import Distribution.Simple.LocalBuildInfo
|
2010-01-17 01:28:36 +01:00
|
|
|
(LocalBuildInfo(..), absoluteInstallDirs)
|
2010-01-17 01:28:42 +01:00
|
|
|
import Distribution.Verbosity ( Verbosity, silent )
|
2010-01-12 08:02:36 +01:00
|
|
|
import Distribution.Simple.InstallDirs (mandir, bindir, CopyDest (NoCopyDest))
|
2010-01-12 08:02:30 +01:00
|
|
|
import Distribution.Simple.Utils (copyFiles)
|
2008-08-09 20:14:20 +02:00
|
|
|
import Control.Exception ( bracket_ )
|
2011-01-12 05:34:49 +01:00
|
|
|
import Control.Monad ( unless )
|
2010-12-16 02:52:35 +01:00
|
|
|
import System.Process ( rawSystem, runCommand, runProcess, waitForProcess )
|
2008-08-10 01:45:40 +02:00
|
|
|
import System.FilePath ( (</>), (<.>) )
|
|
|
|
import System.Directory
|
2010-01-17 01:28:36 +01:00
|
|
|
import System.IO ( stderr )
|
2008-08-09 20:14:20 +02:00
|
|
|
import System.Exit
|
2008-08-10 01:45:40 +02:00
|
|
|
import System.Time
|
|
|
|
import System.IO.Error ( isDoesNotExistError )
|
2010-01-17 01:28:36 +01:00
|
|
|
import Data.Maybe ( catMaybes )
|
|
|
|
import Data.List ( (\\) )
|
2008-08-09 18:51:08 +02:00
|
|
|
|
2010-01-17 01:28:36 +01:00
|
|
|
main :: IO ()
|
2008-10-27 22:52:31 +01:00
|
|
|
main = do
|
2010-01-12 08:02:30 +01:00
|
|
|
defaultMainWithHooks $ simpleUserHooks {
|
|
|
|
runTests = runTestSuite
|
2010-01-17 01:28:36 +01:00
|
|
|
, postBuild = makeManPages
|
2010-01-12 08:02:36 +01:00
|
|
|
, postCopy = \ _ flags pkg lbi -> do
|
|
|
|
installManpages pkg lbi (fromFlag $ copyVerbosity flags)
|
|
|
|
(fromFlag $ copyDest flags)
|
|
|
|
installScripts pkg lbi (fromFlag $ copyVerbosity flags)
|
|
|
|
(fromFlag $ copyDest flags)
|
|
|
|
, postInst = \ _ flags pkg lbi -> do
|
|
|
|
installManpages pkg lbi (fromFlag $ installVerbosity flags) NoCopyDest
|
|
|
|
installScripts pkg lbi (fromFlag $ installVerbosity flags) NoCopyDest
|
2010-01-12 08:02:30 +01:00
|
|
|
}
|
2008-10-27 22:52:31 +01:00
|
|
|
exitWith ExitSuccess
|
2008-08-09 18:51:08 +02:00
|
|
|
|
2008-08-09 20:14:20 +02:00
|
|
|
-- | Run test suite.
|
2010-01-17 01:28:36 +01:00
|
|
|
runTestSuite :: Args -> Bool -> PackageDescription -> LocalBuildInfo -> IO a
|
2011-01-12 02:36:58 +01:00
|
|
|
runTestSuite args _ pkg lbi = do
|
2010-12-16 02:52:35 +01:00
|
|
|
let testDir = buildDir lbi </> "test-pandoc"
|
|
|
|
testDir' <- canonicalizePath testDir
|
2011-01-12 02:36:58 +01:00
|
|
|
let testArgs = concatMap (\arg -> ["-t",arg]) args
|
2010-12-16 02:52:35 +01:00
|
|
|
if any id [buildable (buildInfo exe) | exe <- executables pkg, exeName exe == "test-pandoc"]
|
2011-01-12 02:36:58 +01:00
|
|
|
then inDirectory "tests" $ rawSystem (testDir' </> "test-pandoc") testArgs >>= exitWith
|
2010-12-16 02:52:35 +01:00
|
|
|
else do
|
|
|
|
putStrLn "Build pandoc with the 'tests' flag to run tests"
|
|
|
|
exitWith $ ExitFailure 3
|
2008-02-09 04:20:42 +01:00
|
|
|
|
2008-08-10 01:45:40 +02:00
|
|
|
-- | Build man pages from markdown sources in man/man1/.
|
2010-01-17 01:28:36 +01:00
|
|
|
makeManPages :: Args -> BuildFlags -> PackageDescription -> LocalBuildInfo -> IO ()
|
2011-01-12 05:34:49 +01:00
|
|
|
makeManPages _ flags _ bi = do
|
|
|
|
let pandocPath = (buildDir bi) </> "pandoc" </> "pandoc"
|
2010-12-07 21:10:07 +01:00
|
|
|
makeManPage pandocPath (fromFlag $ buildVerbosity flags) "markdown2pdf.1"
|
|
|
|
let testCmd = "runghc -package-conf=dist/package.conf.inplace MakeManPage.hs" -- makes pandoc.1 from README
|
|
|
|
runCommand testCmd >>= waitForProcess >>= exitWith
|
2010-01-12 08:02:30 +01:00
|
|
|
|
|
|
|
manpages :: [FilePath]
|
2010-02-06 19:55:28 +01:00
|
|
|
manpages = ["pandoc.1", "markdown2pdf.1"]
|
2010-01-12 08:02:30 +01:00
|
|
|
|
|
|
|
manDir :: FilePath
|
|
|
|
manDir = "man" </> "man1"
|
2008-08-10 01:45:40 +02:00
|
|
|
|
|
|
|
-- | Build a man page from markdown source in man/man1.
|
2010-03-23 02:43:38 +01:00
|
|
|
makeManPage :: FilePath -> Verbosity -> FilePath -> IO ()
|
|
|
|
makeManPage pandoc verbosity manpage = do
|
2008-08-10 01:45:40 +02:00
|
|
|
let page = manDir </> manpage
|
2010-03-23 02:43:38 +01:00
|
|
|
let source = page <.> "md"
|
2008-08-10 01:45:40 +02:00
|
|
|
modifiedDeps <- modifiedDependencies page [source]
|
2008-10-27 22:52:31 +01:00
|
|
|
unless (null modifiedDeps) $ do
|
2009-12-31 02:47:00 +01:00
|
|
|
ec <- runProcess pandoc ["-s", "-S", "-r", "markdown", "-w", "man",
|
|
|
|
"--template=templates/man.template", "-o", page, source]
|
2008-10-27 22:52:31 +01:00
|
|
|
Nothing Nothing Nothing Nothing (Just stderr) >>= waitForProcess
|
|
|
|
case ec of
|
2010-03-23 02:43:38 +01:00
|
|
|
ExitSuccess -> unless (verbosity == silent) $
|
|
|
|
putStrLn $ "Created " ++ page
|
|
|
|
ExitFailure n -> putStrLn ("Error creating " ++ page ++
|
|
|
|
". Exit code = " ++ show n) >> exitWith ec
|
2008-08-10 01:45:40 +02:00
|
|
|
|
2010-01-12 08:02:36 +01:00
|
|
|
installScripts :: PackageDescription -> LocalBuildInfo
|
|
|
|
-> Verbosity -> CopyDest -> IO ()
|
|
|
|
installScripts pkg lbi verbosity copy =
|
|
|
|
copyFiles verbosity (bindir (absoluteInstallDirs pkg lbi copy))
|
|
|
|
(zip (repeat ".") (wrappers \\ exes))
|
|
|
|
where exes = map exeName $ filter isBuildable $ executables pkg
|
|
|
|
isBuildable = buildable . buildInfo
|
2010-02-06 19:55:28 +01:00
|
|
|
wrappers = ["markdown2pdf"]
|
2010-01-12 08:02:30 +01:00
|
|
|
|
2010-01-12 08:02:36 +01:00
|
|
|
installManpages :: PackageDescription -> LocalBuildInfo
|
|
|
|
-> Verbosity -> CopyDest -> IO ()
|
|
|
|
installManpages pkg lbi verbosity copy =
|
2010-01-12 08:02:30 +01:00
|
|
|
copyFiles verbosity (mandir (absoluteInstallDirs pkg lbi copy) </> "man1")
|
|
|
|
(zip (repeat manDir) manpages)
|
|
|
|
|
2008-08-10 01:45:40 +02:00
|
|
|
-- | Returns a list of 'dependencies' that have been modified after 'file'.
|
|
|
|
modifiedDependencies :: FilePath -> [FilePath] -> IO [FilePath]
|
|
|
|
modifiedDependencies file dependencies = do
|
|
|
|
fileModTime <- catch (getModificationTime file) $
|
|
|
|
\e -> if isDoesNotExistError e
|
|
|
|
then return (TOD 0 0) -- the minimum ClockTime
|
|
|
|
else ioError e
|
|
|
|
depModTimes <- mapM getModificationTime dependencies
|
|
|
|
let modified = zipWith (\dep time -> if time > fileModTime then Just dep else Nothing) dependencies depModTimes
|
|
|
|
return $ catMaybes modified
|
2008-08-09 20:14:20 +02:00
|
|
|
|
|
|
|
-- | Perform an IO action in a directory.
|
|
|
|
inDirectory :: FilePath -> IO a -> IO a
|
|
|
|
inDirectory dir action = do
|
|
|
|
oldDir <- getCurrentDirectory
|
|
|
|
bracket_ (setCurrentDirectory dir) (setCurrentDirectory oldDir) action
|
|
|
|
|