split out setEnv to avoid adding dep

Windows needs the setenv package in custom-setup, but I don't want to
pull it in on unix, which would probably break some builds and need more
work. Instead, split out setEnv to a separate module.

Quite likely, unix-compat will get a portable environment layer, and
then both modules can be removed from here.

This commit was sponsored by Øyvind Andersen Holm.
This commit is contained in:
Joey Hess 2017-11-14 14:25:46 -04:00
parent ea12fb38f4
commit 8d68112be5
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
10 changed files with 53 additions and 30 deletions

View file

@ -14,6 +14,7 @@ import Utility.UserInfo
import qualified Git.Config import qualified Git.Config
import Config import Config
import Utility.Env import Utility.Env
import Utility.Env.Set
{- Checks that the system's environment allows git to function. {- Checks that the system's environment allows git to function.
- Git requires a GECOS username, or suitable git configuration, or - Git requires a GECOS username, or suitable git configuration, or

View file

@ -15,6 +15,7 @@ import qualified Annex
import Assistant.Alert import Assistant.Alert
import Assistant.DaemonStatus import Assistant.DaemonStatus
import Utility.Env import Utility.Env
import Utility.Env.Set
import Types.Distribution import Types.Distribution
import Types.Transfer import Types.Transfer
import Logs.Web import Logs.Web

View file

@ -12,6 +12,7 @@ import Git.Types
import Git.Construct import Git.Construct
import qualified Git.Config import qualified Git.Config
import Utility.Env import Utility.Env
import Utility.Env.Set
{- Gets the current git repository. {- Gets the current git repository.
- -

View file

@ -10,6 +10,7 @@ module Git.Index where
import Common import Common
import Git import Git
import Utility.Env import Utility.Env
import Utility.Env.Set
indexEnv :: String indexEnv :: String
indexEnv = "GIT_INDEX_FILE" indexEnv = "GIT_INDEX_FILE"

11
Test.hs
View file

@ -95,6 +95,7 @@ import qualified Utility.Process
import qualified Utility.Misc import qualified Utility.Misc
import qualified Utility.InodeCache import qualified Utility.InodeCache
import qualified Utility.Env import qualified Utility.Env
import qualified Utility.Env.Set
import qualified Utility.Matcher import qualified Utility.Matcher
import qualified Utility.Exception import qualified Utility.Exception
import qualified Utility.Hash import qualified Utility.Hash
@ -142,7 +143,7 @@ runner = Just go
subenv = "GIT_ANNEX_TEST_SUBPROCESS" subenv = "GIT_ANNEX_TEST_SUBPROCESS"
runsubprocesstests opts Nothing = do runsubprocesstests opts Nothing = do
pp <- Annex.Path.programPath pp <- Annex.Path.programPath
Utility.Env.setEnv subenv "1" True Utility.Env.Set.setEnv subenv "1" True
ps <- getArgs ps <- getArgs
(Nothing, Nothing, Nothing, pid) <-createProcess (proc pp ps) (Nothing, Nothing, Nothing, pid) <-createProcess (proc pp ps)
exitcode <- waitForProcess pid exitcode <- waitForProcess pid
@ -1931,9 +1932,9 @@ ensuretmpdir = do
isolateGitConfig :: IO a -> IO a isolateGitConfig :: IO a -> IO a
isolateGitConfig a = Utility.Tmp.withTmpDir "testhome" $ \tmphome -> do isolateGitConfig a = Utility.Tmp.withTmpDir "testhome" $ \tmphome -> do
tmphomeabs <- absPath tmphome tmphomeabs <- absPath tmphome
Utility.Env.setEnv "HOME" tmphomeabs True Utility.Env.Set.setEnv "HOME" tmphomeabs True
Utility.Env.setEnv "XDG_CONFIG_HOME" tmphomeabs True Utility.Env.Set.setEnv "XDG_CONFIG_HOME" tmphomeabs True
Utility.Env.setEnv "GIT_CONFIG_NOSYSTEM" "1" True Utility.Env.Set.setEnv "GIT_CONFIG_NOSYSTEM" "1" True
a a
cleanup :: FilePath -> IO () cleanup :: FilePath -> IO ()
@ -2119,7 +2120,7 @@ setTestMode testmode = do
currdir <- getCurrentDirectory currdir <- getCurrentDirectory
p <- Utility.Env.getEnvDefault "PATH" "" p <- Utility.Env.getEnvDefault "PATH" ""
mapM_ (\(var, val) -> Utility.Env.setEnv var val True) mapM_ (\(var, val) -> Utility.Env.Set.setEnv var val True)
-- Ensure that the just-built git annex is used. -- Ensure that the just-built git annex is used.
[ ("PATH", currdir ++ [searchPathSeparator] ++ p) [ ("PATH", currdir ++ [searchPathSeparator] ++ p)
, ("TOPDIR", currdir) , ("TOPDIR", currdir)

View file

@ -16,7 +16,6 @@ import Control.Applicative
import Data.Maybe import Data.Maybe
import Prelude import Prelude
import qualified System.Environment as E import qualified System.Environment as E
import qualified System.SetEnv
#else #else
import qualified System.Posix.Env as PE import qualified System.Posix.Env as PE
#endif #endif
@ -42,29 +41,6 @@ getEnvironment = PE.getEnvironment
getEnvironment = E.getEnvironment getEnvironment = E.getEnvironment
#endif #endif
{- Sets an environment variable. To overwrite an existing variable,
- overwrite must be True.
-
- On Windows, setting a variable to "" unsets it. -}
setEnv :: String -> String -> Bool -> IO ()
#ifndef mingw32_HOST_OS
setEnv var val overwrite = PE.setEnv var val overwrite
#else
setEnv var val True = System.SetEnv.setEnv var val
setEnv var val False = do
r <- getEnv var
case r of
Nothing -> setEnv var val True
Just _ -> return ()
#endif
unsetEnv :: String -> IO ()
#ifndef mingw32_HOST_OS
unsetEnv = PE.unsetEnv
#else
unsetEnv = System.SetEnv.unsetEnv
#endif
{- Adds the environment variable to the input environment. If already {- Adds the environment variable to the input environment. If already
- present in the list, removes the old value. - present in the list, removes the old value.
- -

40
Utility/Env/Set.hs Normal file
View file

@ -0,0 +1,40 @@
{- portable environment variables
-
- Copyright 2013 Joey Hess <id@joeyh.name>
-
- License: BSD-2-clause
-}
{-# LANGUAGE CPP #-}
module Utility.Env.Set where
#ifdef mingw32_HOST_OS
import qualified System.Environment as E
import qualified System.SetEnv
#else
import qualified System.Posix.Env as PE
#endif
{- Sets an environment variable. To overwrite an existing variable,
- overwrite must be True.
-
- On Windows, setting a variable to "" unsets it. -}
setEnv :: String -> String -> Bool -> IO ()
#ifndef mingw32_HOST_OS
setEnv var val overwrite = PE.setEnv var val overwrite
#else
setEnv var val True = System.SetEnv.setEnv var val
setEnv var val False = do
r <- getEnv var
case r of
Nothing -> setEnv var val True
Just _ -> return ()
#endif
unsetEnv :: String -> IO ()
#ifndef mingw32_HOST_OS
unsetEnv = PE.unsetEnv
#else
unsetEnv = System.SetEnv.unsetEnv
#endif

View file

@ -15,6 +15,7 @@ import qualified Build.SysConfig as SysConfig
import System.Posix.Types import System.Posix.Types
import qualified System.Posix.IO import qualified System.Posix.IO
import Utility.Env import Utility.Env
import Utility.Env.Set
#endif #endif
import Utility.Tmp import Utility.Tmp
import Utility.Format (decode_c) import Utility.Format (decode_c)

View file

@ -11,7 +11,7 @@ module Utility.Lsof where
import Common import Common
import Build.SysConfig as SysConfig import Build.SysConfig as SysConfig
import Utility.Env import Utility.Env.Set
import System.Posix.Types import System.Posix.Types

View file

@ -995,6 +995,7 @@ Executable git-annex
Utility.Dot Utility.Dot
Utility.DottedVersion Utility.DottedVersion
Utility.Env Utility.Env
Utility.Env.Set
Utility.Exception Utility.Exception
Utility.ExternalSHA Utility.ExternalSHA
Utility.FileMode Utility.FileMode