git.iko.soy/typeable/pandoc
on 1.5.0.1 help sign in
typeable/pandoc/Setup.hs
107 lines 4.7 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.InstallDirs (mandir, bindir, CopyDest (NoCopyDest))
10import Distribution.Simple.Utils (copyFiles)
11import Control.Exception ( bracket_ )
12import Control.Monad ( unless )
13import System.Process ( runCommand, runProcess, waitForProcess )
14import System.FilePath ( (</>), (<.>) )
15import System.Directory
16import System.IO ( stderr )
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 -> do
29 installManpages pkg lbi (fromFlag $ copyVerbosity flags)
30 (fromFlag $ copyDest flags)
31 installScripts pkg lbi (fromFlag $ copyVerbosity flags)
32 (fromFlag $ copyDest flags)
33 , postInst = \ _ flags pkg lbi -> do
34 installManpages pkg lbi (fromFlag $ installVerbosity flags) NoCopyDest
35 installScripts pkg lbi (fromFlag $ installVerbosity flags) NoCopyDest
36 }
37 exitWith ExitSuccess
39-- | Run test suite.
40runTestSuite :: Args -> Bool -> PackageDescription -> LocalBuildInfo -> IO a
41runTestSuite _ _ pkg _ = do
42 let isHighlightingKate (Dependency (PackageName "highlighting-kate") _) = True
43 isHighlightingKate _ = False
44 let highlightingSupport = any isHighlightingKate $ buildDepends pkg
45 let testArgs = ["lhs" | highlightingSupport]
46 let testCmd = "runhaskell -i.. RunTests.hs " ++ unwords testArgs
47 inDirectory "tests" $ runCommand testCmd >>= waitForProcess >>= exitWith
49-- | Build man pages from markdown sources in man/man1/.
50makeManPages :: Args -> BuildFlags -> PackageDescription -> LocalBuildInfo -> IO ()
51makeManPages _ flags _ _ = mapM_ (makeManPage (fromFlag $ buildVerbosity flags)) manpages
53manpages :: [FilePath]
54manpages = ["pandoc.1", "markdown2pdf.1"]
56manDir :: FilePath
57manDir = "man" </> "man1"
59-- | Build a man page from markdown source in man/man1.
60makeManPage :: Verbosity -> FilePath -> IO ()
61makeManPage verbosity manpage = do
62 let pandoc = "dist" </> "build" </> "pandoc" </> "pandoc"
63 let page = manDir </> manpage
64 let source = manDir </> manpage <.> "md"
65 modifiedDeps <- modifiedDependencies page [source]
66 unless (null modifiedDeps) $ do
67 ec <- runProcess pandoc ["-s", "-S", "-r", "markdown", "-w", "man",
68 "--template=templates/man.template", "-o", page, source]
69 Nothing Nothing Nothing Nothing (Just stderr) >>= waitForProcess
70 case ec of
71 ExitSuccess -> unless (verbosity == silent) $
72 putStrLn $ "Created " ++ manDir </> manpage
73 _ -> do putStrLn $ "Error creating " ++ manDir </> manpage
74 exitWith ec
76installScripts :: PackageDescription -> LocalBuildInfo
77 -> Verbosity -> CopyDest -> IO ()
78installScripts pkg lbi verbosity copy =
79 copyFiles verbosity (bindir (absoluteInstallDirs pkg lbi copy))
80 (zip (repeat ".") (wrappers \\ exes))
81 where exes = map exeName $ filter isBuildable $ executables pkg
82 isBuildable = buildable . buildInfo
83 wrappers = ["markdown2pdf"]
85installManpages :: PackageDescription -> LocalBuildInfo
86 -> Verbosity -> CopyDest -> IO ()
87installManpages pkg lbi verbosity copy =
88 copyFiles verbosity (mandir (absoluteInstallDirs pkg lbi copy) </> "man1")
89 (zip (repeat manDir) manpages)
91-- | Returns a list of 'dependencies' that have been modified after 'file'.
92modifiedDependencies :: FilePath -> [FilePath] -> IO [FilePath]
93modifiedDependencies file dependencies = do
94 fileModTime <- catch (getModificationTime file) $
95 \e -> if isDoesNotExistError e
96 then return (TOD 0 0) -- the minimum ClockTime
97 else ioError e
98 depModTimes <- mapM getModificationTime dependencies
99 let modified = zipWith (\dep time -> if time > fileModTime then Just dep else Nothing) dependencies depModTimes
100 return $ catMaybes modified
102-- | Perform an IO action in a directory.
103inDirectory :: FilePath -> IO a -> IO a
104inDirectory dir action = do
105 oldDir <- getCurrentDirectory
106 bracket_ (setCurrentDirectory dir) (setCurrentDirectory oldDir) action