git.iko.soy/typeable/pandoc
on d0879bc help sign in
typeable/pandoc/Setup.hs
103 lines 4.5 kB history raw
1import Distribution.Simple
2import Distribution.Simple.Setup
3 (copyDest, copyVerbosity, fromFlag, installVerbosity)
4import Distribution.PackageDescription
5 ( PackageDescription(..), Executable(..), BuildInfo(..) )
6import Distribution.Simple.LocalBuildInfo
7 ( LocalBuildInfo(..), absoluteInstallDirs )
8import Distribution.Verbosity ( Verbosity )
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, openTempFile )
17import System.Exit
18import System.Time
19import System.IO.Error ( isDoesNotExistError )
20import Data.Maybe ( fromJust, isNothing, catMaybes )
21import Data.List ( isInfixOf, (\\) )
23main = do
24 defaultMainWithHooks $ simpleUserHooks {
25 runTests = runTestSuite
26 , postBuild = makeManPages
27 , postCopy = \ _ flags pkg lbi -> do
28 installManpages pkg lbi (fromFlag $ copyVerbosity flags)
29 (fromFlag $ copyDest flags)
30 installScripts pkg lbi (fromFlag $ copyVerbosity flags)
31 (fromFlag $ copyDest flags)
32 , postInst = \ _ flags pkg lbi -> do
33 installManpages pkg lbi (fromFlag $ installVerbosity flags) NoCopyDest
34 installScripts pkg lbi (fromFlag $ installVerbosity flags) NoCopyDest
35 }
36 exitWith ExitSuccess
38-- | Run test suite.
39runTestSuite _ _ pkg _ = do
40 let isHighlightingKate (Dependency (PackageName "highlighting-kate") _) = True
41 isHighlightingKate _ = False
42 let highlightingSupport = any isHighlightingKate $ buildDepends pkg
43 let testArgs = if highlightingSupport then ["lhs"] else []
44 let testCmd = "runhaskell -i.. RunTests.hs " ++ unwords testArgs
45 inDirectory "tests" $ runCommand testCmd >>= waitForProcess >>= exitWith
47-- | Build man pages from markdown sources in man/man1/.
48makeManPages _ _ _ _ = do
49 mapM_ makeManPage manpages
51manpages :: [FilePath]
52manpages = ["pandoc.1", "hsmarkdown.1", "html2markdown.1", "markdown2pdf.1"]
54manDir :: FilePath
55manDir = "man" </> "man1"
57-- | Build a man page from markdown source in man/man1.
58makeManPage manpage = do
59 let pandoc = "dist" </> "build" </> "pandoc" </> "pandoc"
60 let page = manDir </> manpage
61 let source = manDir </> manpage <.> "md"
62 modifiedDeps <- modifiedDependencies page [source]
63 unless (null modifiedDeps) $ do
64 ec <- runProcess pandoc ["-s", "-S", "-r", "markdown", "-w", "man",
65 "--template=templates/man.template", "-o", page, source]
66 Nothing Nothing Nothing Nothing (Just stderr) >>= waitForProcess
67 case ec of
68 ExitSuccess -> putStrLn $ "Created " ++ manDir </> manpage
69 _ -> do putStrLn $ "Error creating " ++ manDir </> manpage
70 exitWith ec
72installScripts :: PackageDescription -> LocalBuildInfo
73 -> Verbosity -> CopyDest -> IO ()
74installScripts pkg lbi verbosity copy =
75 copyFiles verbosity (bindir (absoluteInstallDirs pkg lbi copy))
76 (zip (repeat ".") (wrappers \\ exes))
77 where exes = map exeName $ filter isBuildable $ executables pkg
78 isBuildable = buildable . buildInfo
79 wrappers = ["html2markdown", "hsmarkdown", "markdown2pdf"]
81installManpages :: PackageDescription -> LocalBuildInfo
82 -> Verbosity -> CopyDest -> IO ()
83installManpages pkg lbi verbosity copy =
84 copyFiles verbosity (mandir (absoluteInstallDirs pkg lbi copy) </> "man1")
85 (zip (repeat manDir) manpages)
87-- | Returns a list of 'dependencies' that have been modified after 'file'.
88modifiedDependencies :: FilePath -> [FilePath] -> IO [FilePath]
89modifiedDependencies file dependencies = do
90 fileModTime <- catch (getModificationTime file) $
91 \e -> if isDoesNotExistError e
92 then return (TOD 0 0) -- the minimum ClockTime
93 else ioError e
94 depModTimes <- mapM getModificationTime dependencies
95 let modified = zipWith (\dep time -> if time > fileModTime then Just dep else Nothing) dependencies depModTimes
96 return $ catMaybes modified
98-- | Perform an IO action in a directory.
99inDirectory :: FilePath -> IO a -> IO a
100inDirectory dir action = do
101 oldDir <- getCurrentDirectory
102 bracket_ (setCurrentDirectory dir) (setCurrentDirectory oldDir) action