1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
import Distribution.Simple
import Distribution.Simple.Setup
(copyDest, copyVerbosity, fromFlag, installVerbosity)
import Distribution.PackageDescription ( PackageDescription )
import Distribution.Simple.LocalBuildInfo
( LocalBuildInfo(..), absoluteInstallDirs )
import Distribution.Verbosity ( Verbosity )
import Distribution.Simple.InstallDirs (mandir, CopyDest (NoCopyDest))
import Distribution.Simple.Utils (copyFiles)
import Control.Exception ( bracket_ )
import Control.Monad ( unless )
import System.Process ( runCommand, runProcess, waitForProcess )
import System.FilePath ( (</>), (<.>) )
import System.Directory
import System.IO ( stderr, openTempFile )
import System.Exit
import System.Time
import System.IO.Error ( isDoesNotExistError )
import Data.Maybe ( fromJust, isNothing, catMaybes )
import Data.List ( isInfixOf )
main = do
defaultMainWithHooks $ simpleUserHooks {
runTests = runTestSuite
, postBuild = makeManPages
, postCopy = \ _ flags pkg lbi -> installManpages pkg lbi
(fromFlag $ copyVerbosity flags)
(fromFlag $ copyDest flags)
, postInst = \ _ flags pkg lbi -> installManpages pkg lbi
(fromFlag $ installVerbosity flags) NoCopyDest
}
exitWith ExitSuccess
-- | Run test suite.
runTestSuite _ _ _ _ = do
tempPath <- catch getTemporaryDirectory (\_ -> return ".")
(outputPath, hOut) <- openTempFile tempPath "out"
runProcess "pandoc" ["--version"] Nothing Nothing Nothing (Just hOut) Nothing >>= waitForProcess
output <- readFile outputPath
let highlightingSupport = "with syntax highlighting" `isInfixOf` output
let testArgs = if highlightingSupport then ["lhs"] else []
let testCmd = "runhaskell -i.. RunTests.hs " ++ unwords testArgs
inDirectory "tests" $ runCommand testCmd >>= waitForProcess >>= exitWith
-- | Build man pages from markdown sources in man/man1/.
makeManPages _ _ _ _ = do
mapM_ makeManPage manpages
manpages :: [FilePath]
manpages = ["pandoc.1", "hsmarkdown.1", "html2markdown.1", "markdown2pdf.1"]
manDir :: FilePath
manDir = "man" </> "man1"
-- | Build a man page from markdown source in man/man1.
makeManPage manpage = do
let pandoc = "dist" </> "build" </> "pandoc" </> "pandoc"
let page = manDir </> manpage
let source = manDir </> manpage <.> "md"
modifiedDeps <- modifiedDependencies page [source]
unless (null modifiedDeps) $ do
ec <- runProcess pandoc ["-s", "-S", "-r", "markdown", "-w", "man",
"--template=templates/man.template", "-o", page, source]
Nothing Nothing Nothing Nothing (Just stderr) >>= waitForProcess
case ec of
ExitSuccess -> putStrLn $ "Created " ++ manDir </> manpage
_ -> do putStrLn $ "Error creating " ++ manDir </> manpage
exitWith ec
installManpages pkg lbi verbosity copy =
mapM_ (installManpage pkg lbi verbosity copy) manpages
installManpage :: PackageDescription -> LocalBuildInfo
-> Verbosity -> CopyDest -> FilePath -> IO ()
installManpage pkg lbi verbosity copy manpage =
copyFiles verbosity (mandir (absoluteInstallDirs pkg lbi copy) </> "man1")
(zip (repeat manDir) manpages)
-- | 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
-- | 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
|