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