diff options
author | Jasper Van der Jeugt <m@jaspervdj.be> | 2012-06-06 11:47:29 +0200 |
---|---|---|
committer | Jasper Van der Jeugt <m@jaspervdj.be> | 2012-06-06 11:47:29 +0200 |
commit | e662d09c53fabb7b55cfdbb098eb276f139c0d5b (patch) | |
tree | a7e1f89a914c8722a4cc516d06768d0a1fd856b6 | |
parent | 41a5af08fffad69010d7163769207d8a1857cf97 (diff) | |
download | stylish-haskell-e662d09c53fabb7b55cfdbb098eb276f139c0d5b.tar.gz |
More notes on configuration
-rw-r--r-- | README.markdown | 14 | ||||
-rw-r--r-- | src/Main.hs | 4 | ||||
-rw-r--r-- | src/StylishHaskell/Config.hs | 57 |
3 files changed, 46 insertions, 29 deletions
diff --git a/README.markdown b/README.markdown index 9831935..9520cd1 100644 --- a/README.markdown +++ b/README.markdown @@ -65,7 +65,19 @@ into: Configuration ------------- -The tool is customizable to some extent. +The tool is customizable to some extent. It tries to find a config file in the +following order: + +1. A file passed to the tool using the `-c/--config` argument +2. `.stylish-haskell.yaml` in the current directory (useful for per-project + settings) +3. `.stylish-haskell.yaml` in your home directory (useful for user-wide + settings) +4. The default settings. + +Use `stylish-haskell --defaults > .stylish-haskell.yaml` to dump a +well-documented default configuration to a file, this way you can get started +quickly. VIM integration --------------- diff --git a/src/Main.hs b/src/Main.hs index 5268ba9..eb331cc 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -14,7 +14,7 @@ import System.Console.CmdArgs -------------------------------------------------------------------------------- -import Paths_stylish_haskell (getDataFileName, version) +import Paths_stylish_haskell (version) import StylishHaskell import StylishHaskell.Config import StylishHaskell.Step @@ -51,7 +51,7 @@ main = cmdArgs stylishArgs >>= stylishHaskell stylishHaskell :: StylishArgs -> IO () stylishHaskell sa | defaults sa = do - fileName <- getDataFileName ".stylish-haskell.yaml" + fileName <- defaultConfigFilePath verbose' $ "Dumping config from " ++ fileName readFile fileName >>= putStr | otherwise = do diff --git a/src/StylishHaskell/Config.hs b/src/StylishHaskell/Config.hs index 9b533c6..a2daaab 100644 --- a/src/StylishHaskell/Config.hs +++ b/src/StylishHaskell/Config.hs @@ -2,27 +2,29 @@ {-# LANGUAGE OverloadedStrings #-} module StylishHaskell.Config ( Config (..) + , defaultConfigFilePath , configFilePath , loadConfig ) where -------------------------------------------------------------------------------- -import Control.Applicative ((<$>), (<*>)) -import Control.Monad (forM, msum, mzero) -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.Map (Map) -import qualified Data.Map as M -import Data.Yaml (decodeEither) +import Control.Applicative ((<$>), (<*>)) +import Control.Monad (forM, msum, mzero) +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.Map (Map) +import qualified Data.Map as M +import Data.Yaml (decodeEither) import System.Directory -import System.FilePath ((</>)) +import System.FilePath ((</>)) -------------------------------------------------------------------------------- +import Paths_stylish_haskell (getDataFileName) import StylishHaskell.Step import qualified StylishHaskell.Step.Imports as Imports import qualified StylishHaskell.Step.LanguagePragmas as LanguagePragmas @@ -44,12 +46,8 @@ instance FromJSON Config where -------------------------------------------------------------------------------- -defaultConfig :: Config -defaultConfig = Config $ - [ Imports.step Imports.Global - , LanguagePragmas.step LanguagePragmas.Vertical True - , TrailingWhitespace.step - ] +emptyConfig :: Config +emptyConfig = Config [] -------------------------------------------------------------------------------- @@ -58,21 +56,28 @@ configFileName = ".stylish-haskell.yaml" -------------------------------------------------------------------------------- +defaultConfigFilePath :: IO FilePath +defaultConfigFilePath = getDataFileName ".stylish-haskell.yaml" + + +-------------------------------------------------------------------------------- configFilePath :: Verbose -> Maybe FilePath -> IO (Maybe FilePath) configFilePath verbose userSpecified = do - current <- (</> configFileName) <$> getCurrentDirectory - currentE <- doesFileExist current - report current currentE - home <- (</> configFileName) <$> getHomeDirectory - homeE <- doesFileExist home - report home homeE + (current, currentE) <- check $ (</> configFileName) <$> getCurrentDirectory + (home, homeE) <- check $ (</> configFileName) <$> getHomeDirectory + (def, defE) <- check defaultConfigFilePath return $ msum [ userSpecified , if currentE then Just current else Nothing , if homeE then Just home else Nothing + , if defE then Just def else Nothing ] where - report fp e = verbose $ fp ++ if e then " exists" else " does not exist" + check fp = do + fp' <- fp + ex <- doesFileExist fp' + verbose $ fp' ++ if ex then " exists" else " does not exist" + return (fp', ex) -------------------------------------------------------------------------------- @@ -81,8 +86,8 @@ loadConfig verbose mfp = do mfp' <- configFilePath verbose mfp case mfp' of Nothing -> do - verbose $ "Using default configuration" - return defaultConfig + verbose $ "Using empty configuration" + return emptyConfig Just fp -> do verbose $ "Loading configuration at " ++ fp bs <- B.readFile fp |