Fix finding sandbox in sandbox only projects
This commit is contained in:
parent
11562b4fe7
commit
417cacbf81
@ -46,7 +46,7 @@ cabalCradle wdir = do
|
|||||||
|
|
||||||
sandboxCradle :: FilePath -> IO Cradle
|
sandboxCradle :: FilePath -> IO Cradle
|
||||||
sandboxCradle wdir = do
|
sandboxCradle wdir = do
|
||||||
Just sbDir <- getSandboxDb wdir
|
Just sbDir <- findCabalSandboxDir wdir
|
||||||
pkgDbStack <- getPackageDbStack sbDir
|
pkgDbStack <- getPackageDbStack sbDir
|
||||||
tmpDir <- newTempDir sbDir
|
tmpDir <- newTempDir sbDir
|
||||||
return Cradle {
|
return Cradle {
|
||||||
|
@ -29,11 +29,8 @@ getPackageDbStack :: FilePath -- ^ Project Directory (where the
|
|||||||
-- cabal.sandbox.config file would be if it
|
-- cabal.sandbox.config file would be if it
|
||||||
-- exists)
|
-- exists)
|
||||||
-> IO [GhcPkgDb]
|
-> IO [GhcPkgDb]
|
||||||
getPackageDbStack cdir = do
|
getPackageDbStack cdir =
|
||||||
mSDir <- getSandboxDb cdir
|
([GlobalDb] ++) . maybe [UserDb] return <$> getSandboxDb cdir
|
||||||
return $ [GlobalDb] ++ case mSDir of
|
|
||||||
Nothing -> [UserDb]
|
|
||||||
Just db -> [PackageDb db]
|
|
||||||
|
|
||||||
----------------------------------------------------------------
|
----------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -31,31 +31,66 @@ type FileName = String
|
|||||||
-- directory it is returned otherwise @findCabalFiles@ throws 'GMENoCabalFile'
|
-- directory it is returned otherwise @findCabalFiles@ throws 'GMENoCabalFile'
|
||||||
-- or 'GMETooManyCabalFiles'
|
-- or 'GMETooManyCabalFiles'
|
||||||
findCabalFile :: FilePath -> IO (Maybe FilePath)
|
findCabalFile :: FilePath -> IO (Maybe FilePath)
|
||||||
findCabalFile directory = do
|
findCabalFile dir = do
|
||||||
-- Look for cabal files in @dir@ and all it's parent directories
|
dcs <- findFileInParentsP isCabalFile dir
|
||||||
dcs <- getCabalFiles `zipMapM` parents directory
|
|
||||||
-- Extract first non-empty list, which represents a directory with cabal
|
-- Extract first non-empty list, which represents a directory with cabal
|
||||||
-- files.
|
-- files.
|
||||||
case find (not . null) $ uncurry appendDir `map` dcs of
|
case find (not . null) $ uncurry appendDir `map` dcs of
|
||||||
Just [] -> throw $ GMENoCabalFile
|
Just [] -> throw $ GMENoCabalFile
|
||||||
Just cfs@(_:_:_) -> throw $ GMETooManyCabalFiles cfs
|
Just cfs@(_:_:_) -> throw $ GMETooManyCabalFiles cfs
|
||||||
a -> return $ head <$> a
|
a -> return $ head <$> a
|
||||||
where
|
|
||||||
appendDir :: DirPath -> [FileName] -> [FilePath]
|
|
||||||
appendDir dir fs = (dir </>) `map` fs
|
|
||||||
|
|
||||||
-- | @getCabalFiles dir@. Find all files ending in @.cabal@ in @dir@.
|
-- |
|
||||||
getCabalFiles :: DirPath -> IO [FileName]
|
-- >>> isCabalFile "/home/user/.cabal"
|
||||||
getCabalFiles dir =
|
-- False
|
||||||
filterM isCabalFile =<< getDirectoryContents dir
|
isCabalFile :: FilePath -> Bool
|
||||||
where
|
isCabalFile f = takeExtension' f == ".cabal"
|
||||||
isCabalFile f = do
|
|
||||||
exists <- doesFileExist $ dir </> f
|
|
||||||
return (exists && takeExtension' f == ".cabal")
|
|
||||||
|
|
||||||
takeExtension' p = if takeFileName p == takeExtension p
|
-- |
|
||||||
then ""
|
-- >>> takeExtension' "/some/dir/bla.cabal"
|
||||||
else takeExtension p
|
-- ".cabal"
|
||||||
|
--
|
||||||
|
-- >>> takeExtension' "some/reldir/bla.cabal"
|
||||||
|
-- ".cabal"
|
||||||
|
--
|
||||||
|
-- >>> takeExtension' "bla.cabal"
|
||||||
|
-- ".cabal"
|
||||||
|
--
|
||||||
|
-- >>> takeExtension' ".cabal"
|
||||||
|
-- ""
|
||||||
|
takeExtension' :: FilePath -> String
|
||||||
|
takeExtension' p =
|
||||||
|
if takeFileName p == takeExtension p
|
||||||
|
then "" -- just ".cabal" is not a valid cabal file
|
||||||
|
else takeExtension p
|
||||||
|
|
||||||
|
-- | @findFileInParentsP p dir@ Look for files satisfying @p@ in @dir@ and all
|
||||||
|
-- it's parent directories.
|
||||||
|
findFileInParentsP :: (FilePath -> Bool) -> FilePath
|
||||||
|
-> IO [(DirPath, [FileName])]
|
||||||
|
findFileInParentsP p dir =
|
||||||
|
getFilesP p `zipMapM` parents dir
|
||||||
|
|
||||||
|
-- | @getFilesP p dir@. Find all __files__ satisfying @p@ in @.cabal@ in @dir@.
|
||||||
|
getFilesP :: (FilePath -> Bool) -> DirPath -> IO [FileName]
|
||||||
|
getFilesP p dir = filterM p' =<< getDirectoryContents dir
|
||||||
|
where
|
||||||
|
p' fn = do
|
||||||
|
(p fn && ) <$> doesFileExist (dir </> fn)
|
||||||
|
|
||||||
|
findCabalSandboxDir :: FilePath -> IO (Maybe FilePath)
|
||||||
|
findCabalSandboxDir dir = do
|
||||||
|
dss <- findFileInParentsP isSandboxConfig dir
|
||||||
|
return $ case find (not . null . snd) $ dss of
|
||||||
|
Just (sbDir, _:_) -> Just sbDir
|
||||||
|
_ -> Nothing
|
||||||
|
|
||||||
|
where
|
||||||
|
isSandboxConfig = (=="cabal.sandbox.config")
|
||||||
|
|
||||||
|
|
||||||
|
appendDir :: DirPath -> [FileName] -> [FilePath]
|
||||||
|
appendDir d fs = (d </>) `map` fs
|
||||||
|
|
||||||
zipMapM :: Monad m => (a -> m c) -> [a] -> m [(a,c)]
|
zipMapM :: Monad m => (a -> m c) -> [a] -> m [(a,c)]
|
||||||
zipMapM f as = mapM (\a -> liftM (a,) $ f a) as
|
zipMapM f as = mapM (\a -> liftM (a,) $ f a) as
|
||||||
@ -91,10 +126,10 @@ parents dir' =
|
|||||||
-- | Get path to sandbox config file
|
-- | Get path to sandbox config file
|
||||||
getSandboxDb :: FilePath -- ^ Path to the cabal package root directory
|
getSandboxDb :: FilePath -- ^ Path to the cabal package root directory
|
||||||
-- (containing the @cabal.sandbox.config@ file)
|
-- (containing the @cabal.sandbox.config@ file)
|
||||||
-> IO (Maybe FilePath)
|
-> IO (Maybe GhcPkgDb)
|
||||||
getSandboxDb d = do
|
getSandboxDb d = do
|
||||||
mConf <- traverse readFile =<< U.mightExist (d </> "cabal.sandbox.config")
|
mConf <- traverse readFile =<< U.mightExist (d </> "cabal.sandbox.config")
|
||||||
return $ fixPkgDbVer <$> (extractSandboxDbDir =<< mConf)
|
return $ PackageDb . fixPkgDbVer <$> (extractSandboxDbDir =<< mConf)
|
||||||
|
|
||||||
where
|
where
|
||||||
fixPkgDbVer dir =
|
fixPkgDbVer dir =
|
||||||
|
Loading…
Reference in New Issue
Block a user