Skip to content

Commit

Permalink
Implement withEnv in Distribution.Client.Utils.
Browse files Browse the repository at this point in the history
Signed-off-by: Edward Z. Yang <ezyang@cs.stanford.edu>
  • Loading branch information
ezyang committed Aug 16, 2016
1 parent 2450095 commit c1c01bc
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion cabal-install/Distribution/Client/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Distribution.Client.Utils ( MergeResult(..)
, mergeBy, duplicates, duplicatesBy
, readMaybe
, inDir, logDirChange
, inDir, withEnv, logDirChange
, determineNumJobs, numberOfProcessors
, removeExistingFile
, withTempFileName
Expand All @@ -18,6 +18,7 @@ module Distribution.Client.Utils ( MergeResult(..)
, relaxEncodingErrors)
where

import Distribution.Compat.Environment ( lookupEnv, setEnv, unsetEnv )
import Distribution.Compat.Exception ( catchIO )
import Distribution.Compat.Time ( getModTime )
import Distribution.Simple.Setup ( Flag(..) )
Expand Down Expand Up @@ -139,6 +140,19 @@ inDir (Just d) m = do
setCurrentDirectory d
m `Exception.finally` setCurrentDirectory old

-- | Executes the action with an environment variable set to some
-- value.
--
-- Warning: This operation is NOT thread-safe, because current
-- environment is a process-global concept.
withEnv :: String -> String -> IO a -> IO a
withEnv k v m = do
mb_old <- lookupEnv k
setEnv k v
m `Exception.finally` (case mb_old of
Nothing -> unsetEnv k
Just old -> setEnv k old)

-- | Log directory change in 'make' compatible syntax
logDirChange :: (String -> IO ()) -> Maybe FilePath -> IO a -> IO a
logDirChange _ Nothing m = m
Expand Down

0 comments on commit c1c01bc

Please sign in to comment.