git.iko.soy/typeable/pandoc
on dd96267 help sign in
typeable/pandoc/Setup.hs
123 lines 5.1 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.GHC (ghcPackageDbOptions)
10import Distribution.Simple.InstallDirs (mandir, bindir, CopyDest (NoCopyDest))
11import Distribution.Simple.Utils (copyFiles)
12import Control.Exception ( bracket_ )
13import Control.Monad ( unless )
14import System.Process ( rawSystem, runCommand, waitForProcess )
15import System.FilePath ( (</>) )
16import System.Directory
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 args _ pkg lbi = do
42 let testDir = buildDir lbi </> "test-pandoc"
43 testDir' <- canonicalizePath testDir
44 let testArgs = "--timeout=5" : concatMap (\arg -> ["-t",arg]) args
45 if any id [buildable (buildInfo exe) | exe <- executables pkg, exeName exe == "test-pandoc"]
46 then inDirectory "tests" $ rawSystem (testDir' </> "test-pandoc") testArgs >>= exitWith
47 else do
48 putStrLn "Build pandoc with the 'tests' flag to run tests"
49 exitWith $ ExitFailure 3
51-- | Build man pages from markdown sources in man/
52makeManPages :: Args -> BuildFlags -> PackageDescription -> LocalBuildInfo -> IO ()
53makeManPages _ flags _ lbi = do
54 ds1 <- modifiedDependencies (manDir </> "man1" </> "pandoc.1")
55 ["README", manDir </> "man1" </> "pandoc.1.template"]
56 ds2 <- modifiedDependencies (manDir </> "man1" </> "markdown2pdf.1")
57 [manDir </> "man1" </> "markdown2pdf.1.md"]
58 ds3 <- modifiedDependencies (manDir </> "man5" </> "pandoc_markdown.5")
59 ["README", manDir </> "man5" </> "pandoc_markdown.5.template"]
61 let distPref = fromFlag (buildDistPref flags)
62 packageDB =
63 withPackageDB lbi
64 ++ [SpecificPackageDB $ distPref </> "package.conf.inplace"]
66 verbosity = fromFlag $ buildVerbosity flags
68 args = makeGhcArgs (ghcPackageDbOptions packageDB)
69 ++ ["MakeManPage.hs"]
70 args' = if verbosity == silent
71 then args
72 else args ++ ["--verbose"]
73 -- Don't run MakeManPage.hs unless we have to
74 unless (null ds1 && null ds2 && null ds3) $ do
75 rawSystem "runghc" args' >>= exitWith
77-- format arguments to runghc that we wish to pass to ghc
78-- normally runghc gets it right, unless the argument does
79-- not begin with a '-' charecter, so we need to give clear
80-- directions.
81makeGhcArgs :: [String] -> [String]
82makeGhcArgs = map ("--ghc-arg="++)
84manpages :: [FilePath]
85manpages = ["man1" </> "pandoc.1"
86 ,"man1" </> "markdown2pdf.1"
87 ,"man5" </> "pandoc_markdown.5"]
89manDir :: FilePath
90manDir = "man"
92installScripts :: PackageDescription -> LocalBuildInfo
93 -> Verbosity -> CopyDest -> IO ()
94installScripts pkg lbi verbosity copy =
95 copyFiles verbosity (bindir (absoluteInstallDirs pkg lbi copy))
96 (zip (repeat ".") (wrappers \\ exes))
97 where exes = map exeName $ filter isBuildable $ executables pkg
98 isBuildable = buildable . buildInfo
99 wrappers = ["markdown2pdf"]
101installManpages :: PackageDescription -> LocalBuildInfo
102 -> Verbosity -> CopyDest -> IO ()
103installManpages pkg lbi verbosity copy =
104 copyFiles verbosity (mandir (absoluteInstallDirs pkg lbi copy))
105 (zip (repeat manDir) manpages)
107-- | Returns a list of 'dependencies' that have been modified after 'file'.
108modifiedDependencies :: FilePath -> [FilePath] -> IO [FilePath]
109modifiedDependencies file dependencies = do
110 fileModTime <- catch (getModificationTime file) $
111 \e -> if isDoesNotExistError e
112 then return (TOD 0 0) -- the minimum ClockTime
113 else ioError e
114 depModTimes <- mapM getModificationTime dependencies
115 let modified = zipWith (\dep time -> if time > fileModTime then Just dep else Nothing) dependencies depModTimes
116 return $ catMaybes modified
118-- | Perform an IO action in a directory.
119inDirectory :: FilePath -> IO a -> IO a
120inDirectory dir action = do
121 oldDir <- getCurrentDirectory
122 bracket_ (setCurrentDirectory dir) (setCurrentDirectory oldDir) action