From 559a8062bc98315a00e492899d2167955bcf3ca9 Mon Sep 17 00:00:00 2001 From: Mikhail Glushenkov Date: Mon, 5 Nov 2012 14:48:32 +0100 Subject: Search for the config file in the project root. Project root is defined as the nearest ancestor directory that contains a .stylish-haskell.yaml file (the same logic that Git uses for finding the .git directory). Project root is searched after the current directory, but before the home directory. --- src/Language/Haskell/Stylish/Config.hs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/Language/Haskell/Stylish/Config.hs b/src/Language/Haskell/Stylish/Config.hs index 2593004..ad2ae61 100644 --- a/src/Language/Haskell/Stylish/Config.hs +++ b/src/Language/Haskell/Stylish/Config.hs @@ -16,12 +16,13 @@ import Data.Aeson (FromJSON (..)) import qualified Data.Aeson as A import qualified Data.Aeson.Types as A import qualified Data.ByteString as B -import Data.List (intercalate) +import Data.List (inits, intercalate) import Data.Map (Map) import qualified Data.Map as M import Data.Yaml (decodeEither) import System.Directory -import System.FilePath (()) +import System.FilePath (joinPath, splitPath, + ()) -------------------------------------------------------------------------------- @@ -71,16 +72,32 @@ defaultConfigFilePath = getDataFileName "data/stylish-haskell.yaml" -------------------------------------------------------------------------------- configFilePath :: Verbose -> Maybe FilePath -> IO (Maybe FilePath) configFilePath verbose userSpecified = do - (current, currentE) <- check $ ( configFileName) <$> getCurrentDirectory - (home, homeE) <- check $ ( configFileName) <$> getHomeDirectory - (def, defE) <- check defaultConfigFilePath + (current, currentE) <- check $ ( configFileName) <$> + getCurrentDirectory + (projectRoot, projectRootE) <- checkUntilFound =<< (map ( configFileName)) + <$> getAncestorDirectories + (home, homeE) <- check $ ( configFileName) <$> + getHomeDirectory + (def, defE) <- check defaultConfigFilePath return $ msum [ userSpecified , if currentE then Just current else Nothing + , if projectRootE then Just projectRoot else Nothing , if homeE then Just home else Nothing , if defE then Just def else Nothing ] where + getAncestorDirectories :: IO [FilePath] + getAncestorDirectories = map joinPath . reverse . drop 2 + . inits . splitPath <$> getCurrentDirectory + + checkUntilFound :: [FilePath] -> IO (FilePath, Bool) + checkUntilFound [] = return ("", False) + checkUntilFound (f:fs) = do + res@(_, ex) <- check (return f) + if ex then return res else checkUntilFound fs + + check :: IO FilePath -> IO (FilePath, Bool) check fp = do fp' <- fp ex <- doesFileExist fp' -- cgit v1.2.3