summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorMikhail Glushenkov <the.dead.shall.rise@gmail.com>2012-11-05 14:48:32 +0100
committerMikhail Glushenkov <the.dead.shall.rise@gmail.com>2012-11-05 14:53:17 +0100
commit559a8062bc98315a00e492899d2167955bcf3ca9 (patch)
tree36a7d1084f5744bc17b21e6582edb58991a078e6 /src
parent28ba0499f8f5be9d9308df2408707efa6d6b59a4 (diff)
downloadstylish-haskell-559a8062bc98315a00e492899d2167955bcf3ca9.tar.gz
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.
Diffstat (limited to 'src')
-rw-r--r--src/Language/Haskell/Stylish/Config.hs27
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'