git.iko.soy/typeable/pandoc
on 1.9.0.3 help sign in
typeable/pandoc/Setup.hs
108 lines 4.4 kB history raw
1import Distribution.Simple
2import Distribution.Simple.Setup
3 (copyDest, copyVerbosity, fromFlag, installVerbosity, BuildFlags(..))
4import Distribution.PackageDescription
5 (PackageDescription(..), Executable(..), BuildInfo(..))
6import Distribution.Simple.LocalBuildInfo
7 (LocalBuildInfo(..), absoluteInstallDirs)
8import Distribution.Verbosity ( Verbosity, silent )
9import Distribution.Simple.GHC (ghcPackageDbOptions)
10import Distribution.Simple.InstallDirs (mandir, bindir, CopyDest (NoCopyDest))
11import Distribution.Simple.Utils (copyFiles)
12import Control.Exception ( bracket_ )
13import Control.Monad ( unless )
14import System.Process ( rawSystem, runCommand, waitForProcess )
15import System.FilePath ( (</>) )
16import System.Directory
17import System.Exit
18import System.Time
19import System.IO.Error ( isDoesNotExistError )
20import Data.Maybe ( catMaybes )
21import Data.List ( (\\) )
23main :: IO ()
24main = do
25 defaultMainWithHooks $ simpleUserHooks {
26 runTests = runTestSuite
27 , postBuild = makeManPages
28 , postCopy = \ _ flags pkg lbi ->
29 installManpages pkg lbi (fromFlag $ copyVerbosity flags)
30 (fromFlag $ copyDest flags)
31 , postInst = \ _ flags pkg lbi ->
32 installManpages pkg lbi (fromFlag $ installVerbosity flags) NoCopyDest
33 }
34 exitWith ExitSuccess
36-- | Run test suite.
37runTestSuite :: Args -> Bool -> PackageDescription -> LocalBuildInfo -> IO a
38runTestSuite args _ pkg lbi = do
39 let testDir = buildDir lbi </> "test-pandoc"
40 testDir' <- canonicalizePath testDir
41 let testArgs = "--timeout=5" : concatMap (\arg -> ["-t",arg]) args
42 if any id [buildable (buildInfo exe) | exe <- executables pkg, exeName exe == "test-pandoc"]
43 then inDirectory "tests" $ rawSystem (testDir' </> "test-pandoc") testArgs >>= exitWith
44 else do
45 putStrLn "Build pandoc with the 'tests' flag to run tests"
46 exitWith $ ExitFailure 3
48-- | Build man pages from markdown sources in man/
49makeManPages :: Args -> BuildFlags -> PackageDescription -> LocalBuildInfo -> IO ()
50makeManPages _ flags _ lbi = do
51 ds1 <- modifiedDependencies (manDir </> "man1" </> "pandoc.1")
52 ["README", manDir </> "man1" </> "pandoc.1.template"]
53 ds2 <- modifiedDependencies (manDir </> "man5" </> "pandoc_markdown.5")
54 ["README", manDir </> "man5" </> "pandoc_markdown.5.template"]
56 let distPref = fromFlag (buildDistPref flags)
57 packageDB =
58 withPackageDB lbi
59 ++ [SpecificPackageDB $ distPref </> "package.conf.inplace"]
61 verbosity = fromFlag $ buildVerbosity flags
63 args = makeGhcArgs (ghcPackageDbOptions packageDB)
64 ++ ["MakeManPage.hs"]
65 args' = if verbosity == silent
66 then args
67 else args ++ ["--verbose"]
68 -- Don't run MakeManPage.hs unless we have to
69 unless (null ds1 && null ds2) $ do
70 rawSystem "runghc" args' >>= exitWith
72-- format arguments to runghc that we wish to pass to ghc
73-- normally runghc gets it right, unless the argument does
74-- not begin with a '-' charecter, so we need to give clear
75-- directions.
76makeGhcArgs :: [String] -> [String]
77makeGhcArgs = map ("--ghc-arg="++)
79manpages :: [FilePath]
80manpages = ["man1" </> "pandoc.1"
81 ,"man5" </> "pandoc_markdown.5"]
83manDir :: FilePath
84manDir = "man"
86installManpages :: PackageDescription -> LocalBuildInfo
87 -> Verbosity -> CopyDest -> IO ()
88installManpages pkg lbi verbosity copy =
89 copyFiles verbosity (mandir (absoluteInstallDirs pkg lbi copy))
90 (zip (repeat manDir) manpages)
92-- | Returns a list of 'dependencies' that have been modified after 'file'.
93modifiedDependencies :: FilePath -> [FilePath] -> IO [FilePath]
94modifiedDependencies file dependencies = do
95 fileModTime <- catch (getModificationTime file) $
96 \e -> if isDoesNotExistError e
97 then return (TOD 0 0) -- the minimum ClockTime
98 else ioError e
99 depModTimes <- mapM getModificationTime dependencies
100 let modified = zipWith (\dep time -> if time > fileModTime then Just dep else Nothing) dependencies depModTimes
101 return $ catMaybes modified
103-- | Perform an IO action in a directory.
104inDirectory :: FilePath -> IO a -> IO a
105inDirectory dir action = do
106 oldDir <- getCurrentDirectory
107 bracket_ (setCurrentDirectory dir) (setCurrentDirectory oldDir) action