diff options
author | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2012-11-05 07:13:40 -0800 |
---|---|---|
committer | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2012-11-05 07:13:40 -0800 |
commit | e0a4dca876ccc3ceb5dac98f1951a773377f7b82 (patch) | |
tree | 36a7d1084f5744bc17b21e6582edb58991a078e6 /src | |
parent | 28ba0499f8f5be9d9308df2408707efa6d6b59a4 (diff) | |
parent | 559a8062bc98315a00e492899d2167955bcf3ca9 (diff) | |
download | stylish-haskell-e0a4dca876ccc3ceb5dac98f1951a773377f7b82.tar.gz |
Merge pull request #21 from 23Skidoo/search-parents
Search for the config file in the project root.
Diffstat (limited to 'src')
-rw-r--r-- | src/Language/Haskell/Stylish/Config.hs | 27 |
1 files changed, 22 insertions, 5 deletions
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' |