cabal-meta 0.2.3 → 0.2.3.1
raw patch · 2 files changed
+60/−1 lines, 2 files
Files
- OmniConfig.hs +57/−0
- cabal-meta.cabal +3/−1
+ OmniConfig.hs view
@@ -0,0 +1,57 @@+{-# LANGUAGE OverloadedStrings, ScopedTypeVariables, GADTs #-}+module OmniConfig (+ OptionsSource(..), allProgramOpts, commandLine, environment, homeOptFile+ , checkNegatedOpt+) where++import Prelude hiding (FilePath)+import System.Environment+import Control.Exception (SomeException, handle)+import qualified Data.Text as T+import Data.Text (Text)++import Filesystem.Path.CurrentOS+import Filesystem (readTextFile, getHomeDirectory)++allProgramOpts :: [OptionsSource] -> IO [Text]+allProgramOpts confs = do+ fmap Prelude.concat $ mapM loadProgramOptions confs++checkNegatedOpt :: Text -> [Text] -> (Maybe Bool, [Text])+checkNegatedOpt optText args =+ if elem negatedOpt args+ then (Just False, filteredArgs)+ else if elem opt args+ then (Just True, filteredArgs)+ else (Nothing, args)+ where+ filteredArgs = filter (\arg -> arg /= negatedOpt && arg /= opt) args+ opt = "--" `T.append` optText+ negatedOpt = "--no-" `T.append` optText++data OptionsSource = OptionsSource {+ loadProgramOptions :: IO [Text]+ }++commandLine :: OptionsSource+commandLine = OptionsSource {+ loadProgramOptions = fmap (map T.pack) getArgs+ }++environment :: Text -> OptionsSource+environment name = OptionsSource {+ loadProgramOptions = do+ env <- getEnvironment+ return $ case lookup (T.unpack $ T.replace "-" "_" $ T.toUpper name `T.append` "_OPTS") env of+ Nothing -> []+ Just s -> T.splitOn " " $ T.pack s+ }++homeOptFile :: Text -> OptionsSource+homeOptFile name = OptionsSource {+ loadProgramOptions = do+ hd <- getHomeDirectory+ contents <- handle (\(_::SomeException) -> return "") $+ readTextFile $ hd </> fromText ("." `T.append` name) </> "opts"+ return $ T.splitOn " " $ T.strip contents+ }
cabal-meta.cabal view
@@ -1,5 +1,5 @@ name: cabal-meta-version: 0.2.3+version: 0.2.3.1 license: BSD3 license-file: LICENSE author: Greg Weber <greg@gregweber.info>@@ -40,6 +40,8 @@ -- , file-location ghc-options: -Wall++ other-modules: OmniConfig extensions: OverloadedStrings