module Debian.Package.Build.Cabal
( findDescriptionFile
, fillSetupHs
, setupCmd, clean, sdist
, configure, build, install, register
) where
import Control.Applicative ((<$>))
import Control.Monad (filterM, when)
import Control.Monad.Trans.Class (lift)
import Data.Maybe (listToMaybe, isJust)
import Data.List (isSuffixOf)
import System.FilePath ((</>))
import System.Directory (getDirectoryContents, doesFileExist)
import Debian.Package.Build.Monad (Trace)
import Debian.Package.Build.Command (rawSystem')
findDescriptionFile :: FilePath -> IO (Maybe FilePath)
findDescriptionFile dir = do
fs <- getDirectoryContents dir
let find f
| length f > length suf &&
suf `isSuffixOf` f = doesFileExist $ dir </> f
| otherwise = return False
where suf = ".cabal"
fmap (dir </>) . listToMaybe <$> filterM find fs
findSetupHs :: FilePath -> IO (Maybe FilePath)
findSetupHs dir =
listToMaybe <$>
filterM (doesFileExist . (dir </>)) ["Setup.hs", "Setup.lhs"]
fillSetupHs :: FilePath -> IO ()
fillSetupHs dir = do
found <- isJust <$> findSetupHs dir
when (not found) . writeFile (dir </> "Setup.hs") $
unlines ["import Distribution.Simple", "main = defaultMain"]
setup :: [String] -> Trace ()
setup args = do
setupHs <- maybe (fail "Setup.hs or Setup.lhs is not found.") id <$> lift (findSetupHs ".")
rawSystem' "runghc" $ setupHs : args
setupCmd :: String -> [String] -> Trace ()
setupCmd cmd = setup . (cmd : )
clean :: [String] -> Trace ()
clean = setupCmd "clean"
configure :: [String] -> Trace ()
configure = setupCmd "configure"
sdist :: [String] -> Trace ()
sdist = setupCmd "sdist"
build :: [String] -> Trace ()
build = setupCmd "build"
install :: [String] -> Trace ()
install = setupCmd "install"
register :: [String] -> Trace ()
register = setupCmd "register"