git.iko.soy/typeable/pandoc
on 1.8 help sign in
typeable/pandoc/Setup.hs
98 lines 4.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.InstallDirs (mandir, bindir, CopyDest (NoCopyDest))
10import Distribution.Simple.Utils (copyFiles)
11import Control.Exception ( bracket_ )
12import System.Process ( rawSystem, runCommand, waitForProcess )
13import System.FilePath ( (</>) )
14import System.Directory
15import System.Exit
16import System.Time
17import System.IO.Error ( isDoesNotExistError )
18import Data.Maybe ( catMaybes )
19import Data.List ( (\\) )
21main :: IO ()
22main = do
23 defaultMainWithHooks $ simpleUserHooks {
24 runTests = runTestSuite
25 , postBuild = makeManPages
26 , postCopy = \ _ flags pkg lbi -> do
27 installManpages pkg lbi (fromFlag $ copyVerbosity flags)
28 (fromFlag $ copyDest flags)
29 installScripts pkg lbi (fromFlag $ copyVerbosity flags)
30 (fromFlag $ copyDest flags)
31 , postInst = \ _ flags pkg lbi -> do
32 installManpages pkg lbi (fromFlag $ installVerbosity flags) NoCopyDest
33 installScripts pkg lbi (fromFlag $ installVerbosity flags) NoCopyDest
34 }
35 exitWith ExitSuccess
37-- | Run test suite.
38runTestSuite :: Args -> Bool -> PackageDescription -> LocalBuildInfo -> IO a
39runTestSuite args _ pkg lbi = do
40 let testDir = buildDir lbi </> "test-pandoc"
41 testDir' <- canonicalizePath testDir
42 let testArgs = concatMap (\arg -> ["-t",arg]) args
43 if any id [buildable (buildInfo exe) | exe <- executables pkg, exeName exe == "test-pandoc"]
44 then inDirectory "tests" $ rawSystem (testDir' </> "test-pandoc") testArgs >>= exitWith
45 else do
46 putStrLn "Build pandoc with the 'tests' flag to run tests"
47 exitWith $ ExitFailure 3
49-- | Build man pages from markdown sources in man/
50makeManPages :: Args -> BuildFlags -> PackageDescription -> LocalBuildInfo -> IO ()
51makeManPages _ flags _ _ = do
52 let verbosity = fromFlag $ buildVerbosity flags
53 let cmd = "runghc -package-conf=dist/package.conf.inplace MakeManPage.hs"
54 let cmd' = if verbosity == silent
55 then cmd
56 else cmd ++ " --verbose"
57 runCommand cmd' >>= waitForProcess >>= exitWith
59manpages :: [FilePath]
60manpages = ["man1" </> "pandoc.1"
61 ,"man1" </> "markdown2pdf.1"
62 ,"man5" </> "pandoc_markdown.5"]
64manDir :: FilePath
65manDir = "man"
67installScripts :: PackageDescription -> LocalBuildInfo
68 -> Verbosity -> CopyDest -> IO ()
69installScripts pkg lbi verbosity copy =
70 copyFiles verbosity (bindir (absoluteInstallDirs pkg lbi copy))
71 (zip (repeat ".") (wrappers \\ exes))
72 where exes = map exeName $ filter isBuildable $ executables pkg
73 isBuildable = buildable . buildInfo
74 wrappers = ["markdown2pdf"]
76installManpages :: PackageDescription -> LocalBuildInfo
77 -> Verbosity -> CopyDest -> IO ()
78installManpages pkg lbi verbosity copy =
79 copyFiles verbosity (mandir (absoluteInstallDirs pkg lbi copy))
80 (zip (repeat manDir) manpages)
82-- | Returns a list of 'dependencies' that have been modified after 'file'.
83modifiedDependencies :: FilePath -> [FilePath] -> IO [FilePath]
84modifiedDependencies file dependencies = do
85 fileModTime <- catch (getModificationTime file) $
86 \e -> if isDoesNotExistError e
87 then return (TOD 0 0) -- the minimum ClockTime
88 else ioError e
89 depModTimes <- mapM getModificationTime dependencies
90 let modified = zipWith (\dep time -> if time > fileModTime then Just dep else Nothing) dependencies depModTimes
91 return $ catMaybes modified
93-- | Perform an IO action in a directory.
94inDirectory :: FilePath -> IO a -> IO a
95inDirectory dir action = do
96 oldDir <- getCurrentDirectory
97 bracket_ (setCurrentDirectory dir) (setCurrentDirectory oldDir) action