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 )
|
2011-12-20 07:09:08 +01:00
|
|
|
import Distribution.Simple.GHC (ghcPackageDbOptions)
|
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-31 18:34:15 +01:00
|
|
|
import Control.Monad ( unless )
|
2011-01-28 20:55:11 +01:00
|
|
|
import System.Process ( rawSystem, runCommand, waitForProcess )
|
|
|
|
import System.FilePath ( (</>) )
|
2008-08-10 01:45:40 +02:00
|
|
|
import System.Directory
|
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-02-05 01:53:21 +01:00
|
|
|
let testArgs = "--timeout=5" : 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
|
|
|
|
2011-01-28 20:55:11 +01:00
|
|
|
-- | Build man pages from markdown sources in man/
|
2010-01-17 01:28:36 +01:00
|
|
|
makeManPages :: Args -> BuildFlags -> PackageDescription -> LocalBuildInfo -> IO ()
|
2011-12-20 07:09:08 +01:00
|
|
|
makeManPages _ flags _ lbi = do
|
2011-01-31 18:34:15 +01:00
|
|
|
ds1 <- modifiedDependencies (manDir </> "man1" </> "pandoc.1")
|
|
|
|
["README", manDir </> "man1" </> "pandoc.1.template"]
|
|
|
|
ds2 <- modifiedDependencies (manDir </> "man1" </> "markdown2pdf.1")
|
|
|
|
[manDir </> "man1" </> "markdown2pdf.1.md"]
|
|
|
|
ds3 <- modifiedDependencies (manDir </> "man5" </> "pandoc_markdown.5")
|
|
|
|
["README", manDir </> "man5" </> "pandoc_markdown.5.template"]
|
2011-12-20 07:09:08 +01:00
|
|
|
|
|
|
|
let distPref = fromFlag (buildDistPref flags)
|
|
|
|
packageDB =
|
|
|
|
withPackageDB lbi
|
|
|
|
++ [SpecificPackageDB $ distPref </> "package.conf.inplace"]
|
|
|
|
|
|
|
|
verbosity = fromFlag $ buildVerbosity flags
|
|
|
|
|
|
|
|
args = makeGhcArgs (ghcPackageDbOptions packageDB)
|
|
|
|
++ ["MakeManPage.hs"]
|
|
|
|
args' = if verbosity == silent
|
|
|
|
then args
|
|
|
|
else args ++ ["--verbose"]
|
2011-01-31 18:34:15 +01:00
|
|
|
-- Don't run MakeManPage.hs unless we have to
|
2011-12-20 07:09:08 +01:00
|
|
|
unless (null ds1 && null ds2 && null ds3) $ do
|
|
|
|
rawSystem "runghc" args' >>= exitWith
|
|
|
|
|
|
|
|
-- format arguments to runghc that we wish to pass to ghc
|
|
|
|
-- normally runghc gets it right, unless the argument does
|
|
|
|
-- not begin with a '-' charecter, so we need to give clear
|
|
|
|
-- directions.
|
|
|
|
makeGhcArgs :: [String] -> [String]
|
|
|
|
makeGhcArgs = map ("--ghc-arg="++)
|
2010-01-12 08:02:30 +01:00
|
|
|
|
|
|
|
manpages :: [FilePath]
|
2011-01-28 20:55:11 +01:00
|
|
|
manpages = ["man1" </> "pandoc.1"
|
|
|
|
,"man1" </> "markdown2pdf.1"
|
|
|
|
,"man5" </> "pandoc_markdown.5"]
|
2010-01-12 08:02:30 +01:00
|
|
|
|
|
|
|
manDir :: FilePath
|
2011-01-28 20:55:11 +01:00
|
|
|
manDir = "man"
|
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 =
|
2011-01-28 20:55:11 +01:00
|
|
|
copyFiles verbosity (mandir (absoluteInstallDirs pkg lbi copy))
|
2010-01-12 08:02:30 +01:00
|
|
|
(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
|
|
|
|
|