Browse files

If the environment variable PACKAGE_MANAGER exists, default to that.

As requested by bug 317551.
  • Loading branch information...
1 parent 2ae67a0 commit e75fe10c676fefdc94854f5345477afe0cdba239 @ivan-m ivan-m committed May 7, 2010
Showing with 31 additions and 18 deletions.
  1. +14 −4 Distribution/Gentoo/PkgManager.hs
  2. +17 −14 Main.hs
View
18 Distribution/Gentoo/PkgManager.hs
@@ -21,9 +21,11 @@ module Distribution.Gentoo.PkgManager
import Distribution.Gentoo.Packages
import Data.Char(toLower)
-import Data.Maybe(mapMaybe)
+import Data.Maybe(mapMaybe, fromMaybe)
import qualified Data.Map as M
import Data.Map(Map)
+import System.IO.Error(try)
+import System.Environment(getEnv)
-- -----------------------------------------------------------------------------
@@ -38,9 +40,17 @@ data PkgManager = Portage
dummy :: PkgManager
dummy = CustomPM "echo"
--- | The default package manager.
-defaultPM :: PkgManager
-defaultPM = pmNameMap M.! defaultPMName
+-- | The default package manager. If the environment variable
+-- @PACKAGE_MANAGER@ exists, use that; otherwise default to
+-- "portage". Note that even if that environment variable is
+-- defined, if it is unknown then it won't be used.
+defaultPM :: IO PkgManager
+defaultPM = do eDPM <- try $ getEnv "PACKAGE_MANAGER"
+ let dPM = either (const defaultPMName) id eDPM
+ mPM = dPM `M.lookup` pmNameMap
+ return $ fromMaybe knownDef mPM
+ where
+ knownDef = pmNameMap M.! defaultPMName
defaultPMName :: String
defaultPMName = "portage"
View
31 Main.hs
@@ -129,23 +129,24 @@ data Flag = HelpFlag
parseArgs :: IO (RunModifier, Action)
parseArgs = do args <- getArgs
- argParser $ getOpt Permute options args
-
-argParser :: ([Flag], [String], [String])
- -> IO (RunModifier, Action)
-argParser (fls, oth, []) = do unless (null oth)
- $ putErrLn
- $ unwords $ "Unknown options:" : oth
- unless (null bPms)
- $ putErrLn
- $ unwords $ "Unknown package managers:" : bPms
- return (rm, a)
+ defPM <- defaultPM
+ argParser defPM $ getOpt Permute options args
+
+argParser :: PkgManager -> ([Flag], [String], [String])
+ -> IO (RunModifier, Action)
+argParser dPM (fls, oth, []) = do unless (null oth)
+ $ putErrLn
+ $ unwords $ "Unknown options:" : oth
+ unless (null bPms)
+ $ putErrLn
+ $ unwords $ "Unknown package managers:" : bPms
+ return (rm, a)
where
(fls', as) = partitionBy flagToAction fls
a = combineAllActions as
(opts, pms) = partitionBy flagToPM fls'
(bPms, pms') = partitionBy choosePM pms
- pm = emptyElse defaultPM last pms'
+ pm = emptyElse dPM last pms'
opts' = Set.fromList opts
hasFlag = flip Set.member opts'
pmFlags = bool id (PretendBuild:) (hasFlag Pretend)
@@ -156,7 +157,7 @@ argParser (fls, oth, []) = do unless (null oth)
, withCmd = PrintAndRun
}
-argParser (_, _, errs) = die $ unwords $ "Errors in arguments:" : errs
+argParser _ (_, _, errs) = die $ unwords $ "Errors in arguments:" : errs
flagToAction :: Flag -> Either Flag Action
flagToAction HelpFlag = Right Help
@@ -190,7 +191,9 @@ options =
where
pmList = unlines . map ((++) " * ") $ definedPMs
defPM = "The last valid value of PM specified is chosen.\n\
- \The default package manager is: " ++ defaultPMName
+ \The default package manager is: " ++ defaultPMName ++ ",\n\
+ \which can be overriden with the \"PACKAGE_MANAGER\"\n\
+ \environment variable."
-- -----------------------------------------------------------------------------
-- Printing information.

0 comments on commit e75fe10

Please sign in to comment.