@@ -1,13 +1,48 @@
11import Distribution.Simple
2+import Distribution.PackageDescription ( emptyHookedBuildInfo )
3+import Control.Exception ( bracket_ )
24import System.Process ( runCommand, waitForProcess )
3−import System.Directory ( setCurrentDirectory )
5+import System.FilePath ( (</>) )
6+import System.Directory ( getCurrentDirectory, setCurrentDirectory, findExecutable, doesFileExist )
7+import System.IO ( stderr )
8+import System.Exit
9+import Data.Maybe ( fromJust )
410
5−main = defaultMainWithHooks (simpleUserHooks {runTests = runTestSuite})
6−
7−testDir = "tests"
11+main = defaultMainWithHooks (simpleUserHooks {runTests = runTestSuite, preConf = checkReferenceODT})
812
13+-- | Run test suite.
914runTestSuite _ _ _ _ = do
10− setCurrentDirectory testDir
11− runCommand "runhaskell RunTests.hs" >>= waitForProcess
15+ inDirectory "tests" $ runCommand "runhaskell RunTests.hs" >>= waitForProcess
1216 return ()
1317
18+-- | If reference.odt does not exist, build it.
19+checkReferenceODT _ _ = do
20+ refODTexists <- doesFileExist ("odt-styles" </> "reference.odt")
21+ if refODTexists
22+ then return ()
23+ else makeReferenceODT
24+ return emptyHookedBuildInfo
25+
26+-- | Create reference.odt by zipping up sources in odt-styles directory.
27+makeReferenceODT :: IO ()
28+makeReferenceODT = do
29+ zipPathMaybe <- findExecutable "zip"
30+ if zipPathMaybe == Nothing
31+ then error $ "The 'zip' command, which is needed to build reference.odt\n" ++
32+ "from sources in the odt-styles directory, was not found.\n" ++
33+ "Try again after installing zip (http://www.info-zip.org/Zip.html).\n" ++
34+ "Or use the pandoc source tarball, which contains a prebuilt reference.odt."
35+ else do
36+ putStrLn "Creating reference.odt:"
37+ inDirectory "odt-styles" $ do
38+ ec <- runCommand "zip -9 -r reference.odt *" >>= waitForProcess
39+ case ec of
40+ ExitSuccess -> putStrLn "Done."
41+ _ -> error "Error creating ODT."
42+
43+-- | Perform an IO action in a directory.
44+inDirectory :: FilePath -> IO a -> IO a
45+inDirectory dir action = do
46+ oldDir <- getCurrentDirectory
47+ bracket_ (setCurrentDirectory dir) (setCurrentDirectory oldDir) action
48+