git-annex/configure.hs

98 lines
2.2 KiB
Haskell
Raw Normal View History

{- Checks system configuration and generates SysConfig.hs. -}
import System.IO
import System.Cmd
import System.Exit
import System.Directory
type Test = IO Bool
2010-11-27 21:31:20 +00:00
data TestCase = TestCase String String Test
data Config = Config String Bool
2010-11-18 18:07:22 +00:00
instance Show Config where
show (Config key value) = unlines [
2010-11-18 18:07:22 +00:00
key ++ " :: Bool"
, key ++ " = " ++ show value
]
2010-11-27 21:31:20 +00:00
tests :: [TestCase]
tests = [
2010-11-27 21:31:20 +00:00
TestCase "cp -a" "cp_a" $ testCp "-a"
, TestCase "cp -p" "cp_p" $ testCp "-p"
, TestCase "cp --reflink=auto" "cp_reflink_auto" $ testCp "--reflink=auto"
, TestCase "uuid" "uuid" $ requireCmd "uuid" "uuid"
, TestCase "xargs -0" "xargs_0" $ requireCmd "xargs -0" "xargs -0 </dev/null"
, TestCase "rsync" "rsync" $ testCmd "rsync --version >/dev/null"
]
tmpDir :: String
tmpDir = "tmp"
testFile :: String
testFile = tmpDir ++ "/testfile"
quiet :: String -> String
2010-11-18 19:15:40 +00:00
quiet s = s ++ " >/dev/null 2>&1"
requireCmd :: String -> String -> Test
requireCmd c cmdline = do
2010-11-18 19:15:40 +00:00
ret <- testCmd $ quiet cmdline
if ret
2010-11-18 19:15:40 +00:00
then return True
else do
testEnd False
error $ "** the " ++ c ++ " command is required to use git-annex"
testCp :: String -> Test
testCp option = testCmd $ quiet $ "cp " ++ option ++ " " ++ testFile ++
" " ++ testFile ++ ".new"
testCmd :: String -> Test
testCmd c = do
ret <- system c
return $ ret == ExitSuccess
testStart :: String -> IO ()
testStart s = do
putStr $ " checking " ++ s ++ "..."
hFlush stdout
testEnd :: Bool -> IO ()
testEnd r = putStrLn $ " " ++ show r
writeSysConfig :: [Config] -> IO ()
2010-11-18 18:11:18 +00:00
writeSysConfig config = writeFile "SysConfig.hs" body
where
2010-11-18 18:11:18 +00:00
body = unlines $ header ++ map show config ++ footer
header = [
"{- Automatically generated by configure. -}"
, "module SysConfig where"
, ""
]
footer = []
2010-11-27 21:31:20 +00:00
runTests :: [TestCase] -> IO [Config]
runTests [] = return []
2010-11-27 21:31:20 +00:00
runTests ((TestCase tname key t):ts) = do
testStart tname
val <- t
testEnd val
rest <- runTests ts
return $ (Config key val):rest
2010-11-18 18:11:18 +00:00
setup :: IO ()
setup = do
createDirectoryIfMissing True tmpDir
writeFile testFile "test file contents"
2010-11-18 18:11:18 +00:00
cleanup :: IO ()
cleanup = do
removeDirectoryRecursive tmpDir
2010-11-18 18:11:18 +00:00
main :: IO ()
main = do
setup
config <- runTests tests
writeSysConfig config
2010-11-18 18:11:18 +00:00
cleanup