git.iko.soy/typeable/pandoc
on a1eadcd help sign in
typeable/pandoc/Setup.hs
63 lines 2.8 kB history raw
1import Distribution.Simple
2import Distribution.Package ( pkgName )
3import Distribution.Simple.LocalBuildInfo ( packageDeps )
4import Distribution.PackageDescription ( emptyHookedBuildInfo )
5import Control.Exception ( bracket_ )
6import Control.Monad ( unless )
7import System.Process ( runCommand, runProcess, waitForProcess )
8import System.FilePath ( (</>), (<.>) )
9import System.Directory
10import System.IO ( stderr )
11import System.Exit
12import System.Time
13import System.IO.Error ( isDoesNotExistError )
14import Data.Maybe ( fromJust, isNothing, catMaybes )
16main = do
17 defaultMainWithHooks $ simpleUserHooks { runTests = runTestSuite
18 , postBuild = makeManPages }
19 exitWith ExitSuccess
21-- | Run test suite.
22runTestSuite _ _ _ local = do
23 let highlightingSupport = (PackageName "highlighting-kate") `elem` (map pkgName $ packageDeps local)
24 let testArgs = if highlightingSupport then ["lhs"] else []
25 let testCmd = "runhaskell -i.. RunTests.hs " ++ unwords testArgs
26 inDirectory "tests" $ runCommand testCmd >>= waitForProcess >>= exitWith
28-- | Build man pages from markdown sources in man/man1/.
29makeManPages _ _ _ _ = do
30 mapM_ makeManPage ["pandoc.1", "hsmarkdown.1", "html2markdown.1", "markdown2pdf.1"]
32-- | Build a man page from markdown source in man/man1.
33makeManPage manpage = do
34 let manDir = "man" </> "man1"
35 let pandoc = "dist" </> "build" </> "pandoc" </> "pandoc"
36 let page = manDir </> manpage
37 let source = manDir </> manpage <.> "md"
38 modifiedDeps <- modifiedDependencies page [source]
39 unless (null modifiedDeps) $ do
40 ec <- runProcess pandoc ["-s", "-S", "-r", "markdown", "-w", "man", "-o", page, source]
41 Nothing Nothing Nothing Nothing (Just stderr) >>= waitForProcess
42 case ec of
43 ExitSuccess -> putStrLn $ "Created " ++ manDir </> manpage
44 _ -> do putStrLn $ "Error creating " ++ manDir </> manpage
45 exitWith ec
47-- | Returns a list of 'dependencies' that have been modified after 'file'.
48modifiedDependencies :: FilePath -> [FilePath] -> IO [FilePath]
49modifiedDependencies file dependencies = do
50 fileModTime <- catch (getModificationTime file) $
51 \e -> if isDoesNotExistError e
52 then return (TOD 0 0) -- the minimum ClockTime
53 else ioError e
54 depModTimes <- mapM getModificationTime dependencies
55 let modified = zipWith (\dep time -> if time > fileModTime then Just dep else Nothing) dependencies depModTimes
56 return $ catMaybes modified
58-- | Perform an IO action in a directory.
59inDirectory :: FilePath -> IO a -> IO a
60inDirectory dir action = do
61 oldDir <- getCurrentDirectory
62 bracket_ (setCurrentDirectory dir) (setCurrentDirectory oldDir) action