Expose readConfigs
parent
4dd28e5052
commit
83b39de3d4
|
@ -108,6 +108,7 @@ library {
|
||||||
, cmdargs >=0.10.14 && <0.11
|
, cmdargs >=0.10.14 && <0.11
|
||||||
, czipwith >=1.0.0.0 && <1.1
|
, czipwith >=1.0.0.0 && <1.1
|
||||||
, ghc-boot-th >=8.0.1 && <8.3
|
, ghc-boot-th >=8.0.1 && <8.3
|
||||||
|
, filepath >=1.4.1.0 && <1.5
|
||||||
}
|
}
|
||||||
default-extensions: {
|
default-extensions: {
|
||||||
CPP
|
CPP
|
||||||
|
|
|
@ -158,7 +158,7 @@ mainCmdParser helpDesc = do
|
||||||
when printVersion $ do
|
when printVersion $ do
|
||||||
do
|
do
|
||||||
putStrLn $ "brittany version " ++ showVersion version
|
putStrLn $ "brittany version " ++ showVersion version
|
||||||
putStrLn $ "Copyright (C) 2016-2017 Lennart Spitzner"
|
putStrLn $ "Copyright (C) 2016-2018 Lennart Spitzner"
|
||||||
putStrLn $ "There is NO WARRANTY, to the extent permitted by law."
|
putStrLn $ "There is NO WARRANTY, to the extent permitted by law."
|
||||||
System.Exit.exitSuccess
|
System.Exit.exitSuccess
|
||||||
when printHelp $ do
|
when printHelp $ do
|
||||||
|
@ -170,10 +170,14 @@ mainCmdParser helpDesc = do
|
||||||
Display -> repeat Nothing
|
Display -> repeat Nothing
|
||||||
Inplace -> inputPaths
|
Inplace -> inputPaths
|
||||||
|
|
||||||
config <- runMaybeT (readConfigs cmdlineConfig configPaths) >>= \case
|
configsToLoad <- liftIO $ if null configPaths
|
||||||
|
then maybeToList <$> (Directory.getCurrentDirectory >>= findLocalConfigPath)
|
||||||
|
else pure configPaths
|
||||||
|
|
||||||
|
config <- runMaybeT (readConfigsWithUserConfig cmdlineConfig configsToLoad) >>= \case
|
||||||
Nothing -> System.Exit.exitWith (System.Exit.ExitFailure 53)
|
Nothing -> System.Exit.exitWith (System.Exit.ExitFailure 53)
|
||||||
Just x -> return x
|
Just x -> return x
|
||||||
when (confUnpack $ _dconf_dump_config $ _conf_debug $ config) $ do
|
when (confUnpack $ _dconf_dump_config $ _conf_debug $ config) $
|
||||||
trace (showConfigYaml config) $ return ()
|
trace (showConfigYaml config) $ return ()
|
||||||
|
|
||||||
results <- zipWithM (coreIO putStrErrLn config suppressOutput) inputPaths outputPaths
|
results <- zipWithM (coreIO putStrErrLn config suppressOutput) inputPaths outputPaths
|
||||||
|
@ -317,42 +321,3 @@ coreIO putErrorLnIO config suppressOutput inputPathM outputPathM = ExceptT.runEx
|
||||||
]
|
]
|
||||||
then trace "----"
|
then trace "----"
|
||||||
else id
|
else id
|
||||||
|
|
||||||
|
|
||||||
readConfigs :: CConfig Option -> [System.IO.FilePath] -> MaybeT IO Config
|
|
||||||
readConfigs cmdlineConfig configPaths = do
|
|
||||||
userBritPathSimple <- liftIO $ Directory.getAppUserDataDirectory "brittany"
|
|
||||||
userBritPathXdg <- liftIO
|
|
||||||
$ Directory.getXdgDirectory Directory.XdgConfig "brittany"
|
|
||||||
let userConfigPathSimple = userBritPathSimple FilePath.</> "config.yaml"
|
|
||||||
let userConfigPathXdg = userBritPathXdg FilePath.</> "config.yaml"
|
|
||||||
let
|
|
||||||
findLocalConfig :: MaybeT IO (Maybe (CConfig Option))
|
|
||||||
findLocalConfig = do
|
|
||||||
cwd <- liftIO $ Directory.getCurrentDirectory
|
|
||||||
let dirParts = FilePath.splitDirectories cwd
|
|
||||||
let searchDirs =
|
|
||||||
[ FilePath.joinPath x | x <- reverse $ List.inits dirParts ]
|
|
||||||
-- when cwd is "a/b/c", searchDirs is ["a/b/c", "a/b", "a", "/"]
|
|
||||||
mFilePath <- liftIO $ Directory.findFileWith Directory.doesFileExist
|
|
||||||
searchDirs
|
|
||||||
"brittany.yaml"
|
|
||||||
case mFilePath of
|
|
||||||
Nothing -> pure Nothing
|
|
||||||
Just fp -> readConfig fp
|
|
||||||
configsRead <- case configPaths of
|
|
||||||
[] -> do
|
|
||||||
localConfig <- findLocalConfig
|
|
||||||
userConfigSimple <- readConfig userConfigPathSimple
|
|
||||||
userConfigXdg <- readConfig userConfigPathXdg
|
|
||||||
let userConfig = userConfigSimple <|> userConfigXdg
|
|
||||||
when (Data.Maybe.isNothing userConfig) $ do
|
|
||||||
liftIO $ Directory.createDirectoryIfMissing True userBritPathXdg
|
|
||||||
writeDefaultConfig userConfigPathXdg
|
|
||||||
-- rightmost has highest priority
|
|
||||||
pure $ [userConfig, localConfig]
|
|
||||||
paths -> readConfig `mapM` reverse paths
|
|
||||||
-- reverse to give highest priority to the first
|
|
||||||
merged <-
|
|
||||||
pure $ Semigroup.mconcat $ catMaybes $ configsRead ++ [Just cmdlineConfig]
|
|
||||||
return $ cZipWith fromOptionIdentity staticDefaultConfig merged
|
|
||||||
|
|
|
@ -4,6 +4,10 @@ module Language.Haskell.Brittany
|
||||||
( parsePrintModule
|
( parsePrintModule
|
||||||
, staticDefaultConfig
|
, staticDefaultConfig
|
||||||
, forwardOptionsSyntaxExtsEnabled
|
, forwardOptionsSyntaxExtsEnabled
|
||||||
|
, userConfigPath
|
||||||
|
, findLocalConfigPath
|
||||||
|
, readConfigs
|
||||||
|
, readConfigsWithUserConfig
|
||||||
, Config
|
, Config
|
||||||
, CConfig(..)
|
, CConfig(..)
|
||||||
, CDebugConfig(..)
|
, CDebugConfig(..)
|
||||||
|
|
|
@ -9,6 +9,10 @@ module Language.Haskell.Brittany.Internal.Config
|
||||||
, staticDefaultConfig
|
, staticDefaultConfig
|
||||||
, forwardOptionsSyntaxExtsEnabled
|
, forwardOptionsSyntaxExtsEnabled
|
||||||
, readConfig
|
, readConfig
|
||||||
|
, userConfigPath
|
||||||
|
, findLocalConfigPath
|
||||||
|
, readConfigs
|
||||||
|
, readConfigsWithUserConfig
|
||||||
, writeDefaultConfig
|
, writeDefaultConfig
|
||||||
, showConfigYaml
|
, showConfigYaml
|
||||||
)
|
)
|
||||||
|
@ -22,8 +26,10 @@ import Language.Haskell.Brittany.Internal.Types
|
||||||
import Language.Haskell.Brittany.Internal.LayouterBasics
|
import Language.Haskell.Brittany.Internal.LayouterBasics
|
||||||
|
|
||||||
import qualified Data.Yaml
|
import qualified Data.Yaml
|
||||||
|
import Data.CZipWith
|
||||||
|
|
||||||
import UI.Butcher.Monadic
|
import UI.Butcher.Monadic
|
||||||
|
import Data.Monoid ((<>))
|
||||||
|
|
||||||
import qualified System.Console.CmdArgs.Explicit as CmdArgs
|
import qualified System.Console.CmdArgs.Explicit as CmdArgs
|
||||||
|
|
||||||
|
@ -33,7 +39,8 @@ import Language.Haskell.Brittany.Internal.Utils
|
||||||
|
|
||||||
import Data.Coerce ( Coercible, coerce )
|
import Data.Coerce ( Coercible, coerce )
|
||||||
|
|
||||||
|
import qualified System.Directory as Directory
|
||||||
|
import qualified System.FilePath.Posix as FilePath
|
||||||
|
|
||||||
staticDefaultConfig :: Config
|
staticDefaultConfig :: Config
|
||||||
staticDefaultConfig = Config
|
staticDefaultConfig = Config
|
||||||
|
@ -227,6 +234,50 @@ readConfig path = do
|
||||||
return $ Just fileConf
|
return $ Just fileConf
|
||||||
else return $ Nothing
|
else return $ Nothing
|
||||||
|
|
||||||
|
-- | Returns a global brittany config file
|
||||||
|
-- If there is no global config in a system, one will be created
|
||||||
|
userConfigPath :: IO System.IO.FilePath
|
||||||
|
userConfigPath = do
|
||||||
|
userBritPathSimple <- Directory.getAppUserDataDirectory "brittany"
|
||||||
|
userBritPathXdg <- Directory.getXdgDirectory Directory.XdgConfig "brittany"
|
||||||
|
let searchDirs = [userBritPathSimple, userBritPathXdg]
|
||||||
|
globalConfig <- Directory.findFileWith Directory.doesFileExist searchDirs "config.yaml"
|
||||||
|
maybe (writeUserConfig userBritPathXdg) pure globalConfig
|
||||||
|
where
|
||||||
|
writeUserConfig dir = do
|
||||||
|
let createConfPath = dir FilePath.</> "config.yaml"
|
||||||
|
liftIO $ Directory.createDirectoryIfMissing True dir
|
||||||
|
writeDefaultConfig $ createConfPath
|
||||||
|
pure createConfPath
|
||||||
|
|
||||||
|
-- | Searhes for a local brittany config path starting from a given directory
|
||||||
|
findLocalConfigPath :: System.IO.FilePath -> IO (Maybe System.IO.FilePath)
|
||||||
|
findLocalConfigPath dir = do
|
||||||
|
let dirParts = FilePath.splitDirectories dir
|
||||||
|
-- when provided dir is "a/b/c", searchDirs is ["a/b/c", "a/b", "a", "/"]
|
||||||
|
let searchDirs = FilePath.joinPath <$> reverse (List.inits dirParts)
|
||||||
|
Directory.findFileWith Directory.doesFileExist searchDirs "brittany.yaml"
|
||||||
|
|
||||||
|
-- | Reads specified configs.
|
||||||
|
readConfigs
|
||||||
|
:: CConfig Option -- ^ Explicit options, take highest priority
|
||||||
|
-> [System.IO.FilePath] -- ^ List of config files to load and merge, highest priority first
|
||||||
|
-> MaybeT IO Config
|
||||||
|
readConfigs cmdlineConfig configPaths = do
|
||||||
|
configs <- readConfig `mapM` configPaths
|
||||||
|
let merged = Semigroup.mconcat $ reverse (cmdlineConfig:catMaybes configs)
|
||||||
|
return $ cZipWith fromOptionIdentity staticDefaultConfig merged
|
||||||
|
|
||||||
|
-- | Reads provided configs
|
||||||
|
-- but also applies the user default configuration (with a lowest priority)
|
||||||
|
readConfigsWithUserConfig
|
||||||
|
:: CConfig Option -- ^ Explicit options, take highest priority
|
||||||
|
-> [System.IO.FilePath] -- ^ List of config files to load and merge, highest priority first
|
||||||
|
-> MaybeT IO Config
|
||||||
|
readConfigsWithUserConfig cmdlineConfig configPaths = do
|
||||||
|
defaultPath <- liftIO $ userConfigPath
|
||||||
|
readConfigs cmdlineConfig (configPaths ++ [defaultPath])
|
||||||
|
|
||||||
writeDefaultConfig :: MonadIO m => System.IO.FilePath -> m ()
|
writeDefaultConfig :: MonadIO m => System.IO.FilePath -> m ()
|
||||||
writeDefaultConfig path =
|
writeDefaultConfig path =
|
||||||
liftIO $ ByteString.writeFile path $ Data.Yaml.encode $ cMap
|
liftIO $ ByteString.writeFile path $ Data.Yaml.encode $ cMap
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
resolver: lts-10.0
|
resolver: lts-10.5
|
||||||
|
|
||||||
packages:
|
packages:
|
||||||
- .
|
- .
|
||||||
|
|
||||||
|
extra-deps:
|
||||||
|
- butcher-1.3.0.0
|
||||||
|
|
Loading…
Reference in New Issue