11
11
module Distribution.Client.PackageEnvironment (
12
12
PackageEnvironment (.. )
13
13
, IncludeComments (.. )
14
+ , PackageEnvironmentType (.. )
15
+ , classifyPackageEnvironment
14
16
, createPackageEnvironment
15
17
, tryLoadPackageEnvironment
16
18
, readPackageEnvironmentFile
17
19
, showPackageEnvironment
18
20
, showPackageEnvironmentWithComments
19
21
, setPackageDB
22
+ , loadUserConfig
20
23
21
24
, basePackageEnvironment
22
25
, initialPackageEnvironment
@@ -49,7 +52,7 @@ import Control.Monad ( foldM, when )
49
52
import Data.List ( partition )
50
53
import Data.Monoid ( Monoid (.. ) )
51
54
import Distribution.Compat.Exception ( catchIO )
52
- import System.Directory ( renameFile )
55
+ import System.Directory ( doesFileExist , renameFile )
53
56
import System.FilePath ( (<.>) , (</>) )
54
57
import System.IO.Error ( isDoesNotExistError )
55
58
import Text.PrettyPrint ( ($+$) )
@@ -67,6 +70,8 @@ import qualified Distribution.Text as Text
67
70
-- TODO: would be nice to remove duplication between D.C.PackageEnvironment and
68
71
-- D.C.Config.
69
72
data PackageEnvironment = PackageEnvironment {
73
+ -- The 'inherit' feature is not used ATM, but could be useful in the future
74
+ -- for constructing nested sandboxes (see discussion in #1196).
70
75
pkgEnvInherit :: Flag FilePath ,
71
76
pkgEnvSavedConfig :: SavedConfig
72
77
}
@@ -94,6 +99,25 @@ sandboxPackageEnvironmentFile = "cabal.sandbox.config"
94
99
userPackageEnvironmentFile :: FilePath
95
100
userPackageEnvironmentFile = " cabal.config"
96
101
102
+ -- | Type of the current package environment.
103
+ data PackageEnvironmentType =
104
+ SandboxPackageEnvironment -- ^ './cabal.sandbox.config'
105
+ | UserPackageEnvironment -- ^ './cabal.config'
106
+ | AmbientPackageEnvironment -- ^ '~/.cabal/config'
107
+
108
+ -- | Is there a 'cabal.sandbox.config' or 'cabal.config' in this
109
+ -- directory?
110
+ classifyPackageEnvironment :: FilePath -> IO PackageEnvironmentType
111
+ classifyPackageEnvironment pkgEnvDir = do
112
+ isSandbox <- configExists sandboxPackageEnvironmentFile
113
+ isUser <- configExists userPackageEnvironmentFile
114
+ case (isSandbox, isUser) of
115
+ (True , _) -> return SandboxPackageEnvironment
116
+ (False , True ) -> return UserPackageEnvironment
117
+ (False , False ) -> return AmbientPackageEnvironment
118
+ where
119
+ configExists fname = doesFileExist (pkgEnvDir </> fname)
120
+
97
121
-- | Defaults common to 'initialPackageEnvironment' and
98
122
-- 'commentPackageEnvironment'.
99
123
commonPackageEnvironmentConfig :: FilePath -> SavedConfig
@@ -233,6 +257,11 @@ userPackageEnvironment verbosity pkgEnvDir = do
233
257
++ maybe " " (\ n -> " :" ++ show n) line ++ " :\n " ++ msg
234
258
return mempty
235
259
260
+ -- | Same as @userPackageEnvironmentFile@, but returns a SavedConfig.
261
+ loadUserConfig :: Verbosity -> FilePath -> IO SavedConfig
262
+ loadUserConfig verbosity pkgEnvDir = fmap pkgEnvSavedConfig
263
+ $ userPackageEnvironment verbosity pkgEnvDir
264
+
236
265
-- | Try to load the package environment file ("cabal.sandbox.config"), exiting
237
266
-- with error if it doesn't exist. Also returns the path to the sandbox
238
267
-- directory. Note that the path parameter should be a name of an existing
@@ -265,6 +294,7 @@ tryLoadPackageEnvironment verbosity pkgEnvDir configFileFlag = do
265
294
user <- userPackageEnvironment verbosity pkgEnvDir
266
295
inherited <- inheritedPackageEnvironment verbosity user
267
296
297
+ -- Layer the package environment settings over settings from ~/.cabal/config.
268
298
cabalConfig <- loadConfig verbosity configFileFlag NoFlag
269
299
return (sandboxDir,
270
300
base `mappend` (cabalConfig `overrideSandboxSettings`
0 commit comments