2014-11-01 21:02:47 +00:00
|
|
|
{-# LANGUAGE BangPatterns, TupleSections #-}
|
|
|
|
module Language.Haskell.GhcMod.PathsAndFiles where
|
|
|
|
|
2015-02-07 14:23:00 +00:00
|
|
|
import Config (cProjectVersion, cTargetPlatformString)
|
2014-11-01 21:02:47 +00:00
|
|
|
import Control.Applicative
|
|
|
|
import Control.Monad
|
|
|
|
import Data.List
|
2015-02-07 14:23:00 +00:00
|
|
|
import Data.List.Split (splitOn)
|
2014-11-01 21:02:47 +00:00
|
|
|
import Data.Char
|
|
|
|
import Data.Maybe
|
|
|
|
import Data.Traversable (traverse)
|
|
|
|
import Language.Haskell.GhcMod.Types
|
|
|
|
import System.Directory
|
|
|
|
import System.FilePath
|
|
|
|
|
|
|
|
import Language.Haskell.GhcMod.Error
|
|
|
|
import qualified Language.Haskell.GhcMod.Utils as U
|
|
|
|
|
|
|
|
import Distribution.Simple.BuildPaths (defaultDistPref)
|
|
|
|
import Distribution.Simple.Configure (localBuildInfoFile)
|
|
|
|
|
|
|
|
-- | Guaranteed to be a path to a directory with no trailing slash.
|
|
|
|
type DirPath = FilePath
|
|
|
|
|
|
|
|
-- | Guaranteed to be the name of a file only (no slashes).
|
|
|
|
type FileName = String
|
|
|
|
|
|
|
|
-- | @findCabalFiles dir@. Searches for a @.cabal@ files in @dir@'s parent
|
|
|
|
-- directories. The first parent directory containing more than one cabal file
|
|
|
|
-- is assumed to be the project directory. If only one cabal file exists in this
|
|
|
|
-- directory it is returned otherwise @findCabalFiles@ throws 'GMENoCabalFile'
|
|
|
|
-- or 'GMETooManyCabalFiles'
|
2014-11-02 23:45:27 +00:00
|
|
|
findCabalFile :: FilePath -> IO (Maybe FilePath)
|
2015-02-07 15:40:22 +00:00
|
|
|
findCabalFile dir = do
|
|
|
|
dcs <- findFileInParentsP isCabalFile dir
|
2014-11-01 21:02:47 +00:00
|
|
|
-- Extract first non-empty list, which represents a directory with cabal
|
|
|
|
-- files.
|
2014-11-02 23:45:27 +00:00
|
|
|
case find (not . null) $ uncurry appendDir `map` dcs of
|
2014-11-01 21:02:47 +00:00
|
|
|
Just [] -> throw $ GMENoCabalFile
|
|
|
|
Just cfs@(_:_:_) -> throw $ GMETooManyCabalFiles cfs
|
|
|
|
a -> return $ head <$> a
|
2015-02-07 15:40:22 +00:00
|
|
|
|
|
|
|
-- |
|
|
|
|
-- >>> isCabalFile "/home/user/.cabal"
|
|
|
|
-- False
|
|
|
|
isCabalFile :: FilePath -> Bool
|
|
|
|
isCabalFile f = takeExtension' f == ".cabal"
|
|
|
|
|
|
|
|
-- |
|
|
|
|
-- >>> takeExtension' "/some/dir/bla.cabal"
|
|
|
|
-- ".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
|
2014-11-02 23:45:27 +00:00
|
|
|
where
|
2015-02-07 15:40:22 +00:00
|
|
|
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
|
2014-11-01 21:02:47 +00:00
|
|
|
|
2014-11-02 23:04:15 +00:00
|
|
|
where
|
2015-02-07 15:40:22 +00:00
|
|
|
isSandboxConfig = (=="cabal.sandbox.config")
|
|
|
|
|
2014-11-02 23:04:15 +00:00
|
|
|
|
2015-02-07 15:40:22 +00:00
|
|
|
appendDir :: DirPath -> [FileName] -> [FilePath]
|
|
|
|
appendDir d fs = (d </>) `map` fs
|
2014-11-02 23:04:15 +00:00
|
|
|
|
2014-11-01 21:02:47 +00:00
|
|
|
zipMapM :: Monad m => (a -> m c) -> [a] -> m [(a,c)]
|
|
|
|
zipMapM f as = mapM (\a -> liftM (a,) $ f a) as
|
|
|
|
|
|
|
|
-- | @parents dir@. Returns all parent directories of @dir@ including @dir@.
|
|
|
|
--
|
|
|
|
-- Examples
|
|
|
|
--
|
|
|
|
-- >>> parents "foo"
|
|
|
|
-- ["foo"]
|
|
|
|
--
|
|
|
|
-- >>> parents "/foo"
|
|
|
|
-- ["/foo","/"]
|
|
|
|
--
|
|
|
|
-- >>> parents "/foo/bar"
|
|
|
|
-- ["/foo/bar","/foo","/"]
|
|
|
|
--
|
|
|
|
-- >>> parents "foo/bar"
|
|
|
|
-- ["foo/bar","foo"]
|
|
|
|
parents :: FilePath -> [FilePath]
|
|
|
|
parents "" = []
|
|
|
|
parents dir' =
|
|
|
|
let (drive, dir) = splitDrive $ normalise $ dropTrailingPathSeparator dir'
|
|
|
|
in map (joinDrive drive) $ parents' $ filter (/=".") $ splitDirectories dir
|
|
|
|
where
|
|
|
|
parents' :: [String] -> [FilePath]
|
|
|
|
parents' [] | isAbsolute dir' = "":[]
|
|
|
|
parents' [] = []
|
|
|
|
parents' dir = [joinPath dir] ++ parents' (init dir)
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | Get path to sandbox config file
|
|
|
|
getSandboxDb :: FilePath -- ^ Path to the cabal package root directory
|
|
|
|
-- (containing the @cabal.sandbox.config@ file)
|
2015-02-07 15:40:22 +00:00
|
|
|
-> IO (Maybe GhcPkgDb)
|
2014-11-01 21:02:47 +00:00
|
|
|
getSandboxDb d = do
|
|
|
|
mConf <- traverse readFile =<< U.mightExist (d </> "cabal.sandbox.config")
|
2015-02-07 15:40:22 +00:00
|
|
|
return $ PackageDb . fixPkgDbVer <$> (extractSandboxDbDir =<< mConf)
|
2015-02-07 14:23:00 +00:00
|
|
|
|
|
|
|
where
|
|
|
|
fixPkgDbVer dir =
|
|
|
|
case takeFileName dir == ghcSandboxPkgDbDir of
|
|
|
|
True -> dir
|
|
|
|
False -> takeDirectory dir </> ghcSandboxPkgDbDir
|
2014-11-01 21:02:47 +00:00
|
|
|
|
|
|
|
-- | Extract the sandbox package db directory from the cabal.sandbox.config file.
|
|
|
|
-- Exception is thrown if the sandbox config file is broken.
|
|
|
|
extractSandboxDbDir :: String -> Maybe FilePath
|
|
|
|
extractSandboxDbDir conf = extractValue <$> parse conf
|
|
|
|
where
|
|
|
|
key = "package-db:"
|
|
|
|
keyLen = length key
|
|
|
|
|
|
|
|
parse = listToMaybe . filter (key `isPrefixOf`) . lines
|
|
|
|
extractValue = U.dropWhileEnd isSpace . dropWhile isSpace . drop keyLen
|
|
|
|
|
|
|
|
setupConfigFile :: Cradle -> FilePath
|
|
|
|
setupConfigFile crdl = cradleRootDir crdl </> setupConfigPath
|
|
|
|
|
|
|
|
-- | Path to 'LocalBuildInfo' file, usually @dist/setup-config@
|
|
|
|
setupConfigPath :: FilePath
|
|
|
|
setupConfigPath = localBuildInfoFile defaultDistPref
|
|
|
|
|
2015-02-07 14:23:00 +00:00
|
|
|
ghcSandboxPkgDbDir :: String
|
|
|
|
ghcSandboxPkgDbDir =
|
|
|
|
targetPlatform ++ "-ghc-" ++ cProjectVersion ++ "-packages.conf.d"
|
|
|
|
where
|
|
|
|
targetPlatform = display buildPlatform
|
|
|
|
|
2014-11-01 21:02:47 +00:00
|
|
|
packageCache :: String
|
|
|
|
packageCache = "package.cache"
|
2015-02-07 14:23:00 +00:00
|
|
|
|