module Shim.CabalInfo where
import Shim.Utils
import System.FilePath
import Control.Monad.State
import Control.Exc
import Control.Applicative
import Distribution.ModuleName
import Distribution.PackageDescription
import qualified Distribution.PackageDescription as Library (Library(..))
import qualified Distribution.PackageDescription as BuildInfo (BuildInfo(..))
import System.Directory hiding (canonicalizePath)
import System.CanonicalizePath
guessCabalFile :: String -> IO (Maybe FilePath)
guessCabalFile sourcefile = do
let dir = takeDirectory $ dropFileName sourcefile
recurseDir findCabalFile dir
where findCabalFile dir = do
logS $ "looking in: " ++ dir
pdfile <- ignoringException (findPackageDesc dir)
case pdfile of
Just f -> return . Just $ dir </> f
Nothing -> return Nothing
guessCabalStanza :: FilePath -> FilePath -> PackageDescription -> IO (Maybe String, BuildInfo)
guessCabalStanza projpath sourcefile pkg_descr = do
matchingStanzas <- filterM matchingStanza allStanzas'
let ((name, _, bi):_) = matchingStanzas ++ allStanzas'
return (name, bi)
where allStanzas =
[ (Nothing, concatMap moduleFiles (Library.exposedModules lib) , libBuildInfo lib)
| Just lib <- [library pkg_descr] ]
++ [ (Just (exeName exe), [modulePath exe], buildInfo exe)
| exe <- executables pkg_descr ]
moduleFiles modl = [toFilePath modl <.> ext | ext <- ["hs", "lhs"] ]
allStanzas' = [(name, [projpath </> dir </> file | dir <- hsSourceDirs bi, file <- files ++ concatMap moduleFiles (BuildInfo.otherModules bi)], bi)
| (name, files, bi) <- allStanzas, buildable bi]
eqPath p1 p2 = equalFilePath <$> canonicalizePath p1 <*> canonicalizePath p2
matchingStanza (_,files,_) = or <$> mapM (eqPath sourcefile) files
findPackageDesc :: FilePath
-> IO (Maybe FilePath)
findPackageDesc dir
= do files <- getDirectoryContents dir
cabalFiles <- filterM doesFileExist
[ dir </> file
| file <- files
, let (name, ext) = splitExtension file
, not (null name) && ext == ".cabal" ]
case cabalFiles of
[] -> return Nothing
[cabalFile] -> return (Just cabalFile)
_ -> return Nothing