git.iko.soy/typeable/pandoc
on 3ecbf49 help sign in
typeable/pandoc/Setup.hs
103 lines 4.0 kB history raw
1{-# LANGUAGE CPP #-}
3import Distribution.Simple
4import Distribution.Simple.Setup
5 (copyDest, copyVerbosity, fromFlag, installVerbosity, BuildFlags(..))
6import Distribution.PackageDescription
7 (PackageDescription(..), Executable(..), BuildInfo(..))
8import Distribution.Simple.LocalBuildInfo
9 (LocalBuildInfo(..), absoluteInstallDirs)
10import Distribution.Verbosity ( Verbosity, silent )
11import Distribution.Simple.GHC (ghcPackageDbOptions)
12import Distribution.Simple.InstallDirs (mandir, bindir, CopyDest (NoCopyDest))
13import Distribution.Simple.Utils (installOrdinaryFiles)
14import Prelude hiding (catch)
15import Control.Exception ( bracket_, catch )
16import Control.Monad ( unless )
17import System.Process ( rawSystem, runCommand, waitForProcess )
18import System.FilePath ( (</>) )
19import System.Directory
20import System.Exit
21import System.Time
22import System.IO.Error ( isDoesNotExistError )
23import Data.Maybe ( catMaybes )
24import Data.List ( (\\) )
25import Data.Time.Clock (UTCTime(..))
27main :: IO ()
28main = do
29 defaultMainWithHooks $ simpleUserHooks {
30 postBuild = makeManPages
31 , postCopy = \ _ flags pkg lbi ->
32 installManpages pkg lbi (fromFlag $ copyVerbosity flags)
33 (fromFlag $ copyDest flags)
34 , postInst = \ _ flags pkg lbi ->
35 installManpages pkg lbi (fromFlag $ installVerbosity flags) NoCopyDest
36 }
37 exitWith ExitSuccess
39-- | Build man pages from markdown sources in man/
40makeManPages :: Args -> BuildFlags -> PackageDescription -> LocalBuildInfo -> IO ()
41makeManPages _ flags _ lbi = do
42 ds1 <- modifiedDependencies (manDir </> "man1" </> "pandoc.1")
43 ["README", manDir </> "man1" </> "pandoc.1.template"]
44 ds2 <- modifiedDependencies (manDir </> "man5" </> "pandoc_markdown.5")
45 ["README", manDir </> "man5" </> "pandoc_markdown.5.template"]
47 let distPref = fromFlag (buildDistPref flags)
48 packageDB =
49 withPackageDB lbi
50 ++ [SpecificPackageDB $ distPref </> "package.conf.inplace"]
52 verbosity = fromFlag $ buildVerbosity flags
54 args = makeGhcArgs (ghcPackageDbOptions packageDB)
55 ++ ["MakeManPage.hs"]
56 args' = if verbosity == silent
57 then args
58 else args ++ ["--verbose"]
59 -- Don't run MakeManPage.hs unless we have to
60 unless (null ds1 && null ds2) $ do
61 rawSystem "runghc" args' >>= exitWith
63-- format arguments to runghc that we wish to pass to ghc
64-- normally runghc gets it right, unless the argument does
65-- not begin with a '-' charecter, so we need to give clear
66-- directions.
67makeGhcArgs :: [String] -> [String]
68makeGhcArgs = map ("--ghc-arg="++)
70manpages :: [FilePath]
71manpages = ["man1" </> "pandoc.1"
72 ,"man5" </> "pandoc_markdown.5"]
74manDir :: FilePath
75manDir = "man"
77installManpages :: PackageDescription -> LocalBuildInfo
78 -> Verbosity -> CopyDest -> IO ()
79installManpages pkg lbi verbosity copy =
80 installOrdinaryFiles verbosity (mandir (absoluteInstallDirs pkg lbi copy))
81 (zip (repeat manDir) manpages)
83-- | Returns a list of 'dependencies' that have been modified after 'file'.
84modifiedDependencies :: FilePath -> [FilePath] -> IO [FilePath]
85modifiedDependencies file dependencies = do
86 fileModTime <- catch (getModificationTime file) $
87 \e -> if isDoesNotExistError e
88#if __GLASGOW_HASKELL__ >= 706
89 then return (UTCTime (toEnum 0) 0) -- the minimum ClockTime
90#else
91 then return (TOD 0 0) -- the minimum ClockTime
92#endif
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