From e3ab394665c2c308cab6fffb41b3acc66d0ca989 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Thu, 18 Feb 2021 12:11:48 +0100 Subject: Second --- Coolbal/FindSpec.hs | 61 ----------------------------------------------------- 1 file changed, 61 deletions(-) delete mode 100644 Coolbal/FindSpec.hs (limited to 'Coolbal/FindSpec.hs') diff --git a/Coolbal/FindSpec.hs b/Coolbal/FindSpec.hs deleted file mode 100644 index ce9ab45..0000000 --- a/Coolbal/FindSpec.hs +++ /dev/null @@ -1,61 +0,0 @@ -module Coolbal.FindSpec (findCabalSpec, FoundCabalSpec(..)) where - -import Data.List (unfoldr) -import System.Directory -import System.Exit (exitFailure) -import System.FilePath (()) -import System.IO (hPutStrLn, stderr) - - -data FoundCabalSpec = FoundCabalSpec - { foundcsRootDir :: FilePath - , foundcsCabal :: FilePath } - deriving (Show) - -findCabalSpec :: IO FoundCabalSpec -findCabalSpec = do - cwd <- getCurrentDirectory >>= makeAbsolute - mfound <- findCabalSpecInDirs (ancestors cwd) - case mfound of - Just found -> do - putStrLn ("Found .cabal file: " ++ foundcsCabal found) - return found - Nothing -> do - hPutStrLn stderr (".cabal file not found in ancestors of PWD: " ++ cwd) - exitFailure - -findCabalSpecInDirs :: [FilePath] -> IO (Maybe FoundCabalSpec) -findCabalSpecInDirs [] = return Nothing -findCabalSpecInDirs (p:ps) = - findCabalSpecInDir p >>= maybe (findCabalSpecInDirs ps) (return . Just) - -findCabalSpecInDir :: FilePath -> IO (Maybe FoundCabalSpec) -findCabalSpecInDir dir = do - files <- filter (`endsWith` ".cabal") <$> listDirectory dir - case files of - [fname] -> do - exists <- doesFileExist (dir fname) - return (if exists then Just (FoundCabalSpec { foundcsRootDir = dir - , foundcsCabal = dir fname }) - else Nothing) - [] -> return Nothing - _ -> do - hPutStrLn stderr "Ambiguous cabal file! Found files:" - mapM_ (\p -> hPutStrLn stderr ("- " ++ (dir p))) files - exitFailure - -parentDir :: FilePath -> Maybe FilePath -parentDir s = case reverse s of - "/" -> Nothing - '/' : s' -> Just (reverse (dropWhile (/= '/') s')) - s' -> case dropWhile (/= '/') s' of - "/" -> Just "/" - _ : s'' -> Just (reverse s'') - "" -> Nothing - --- Includes the path itself as first element -ancestors :: FilePath -> [FilePath] -ancestors p = p : unfoldr (fmap (\x -> (x,x)) . parentDir) p - -endsWith :: String -> String -> Bool -s `endsWith` s' = reverse (take (length s') (reverse s)) == s' -- cgit v1.2.3-70-g09d2