@@ -53,9 +53,9 @@ import Path ( Abs, Dir, Path, toFilePath )
53
53
import Prelude
54
54
import System.Directory
55
55
( createDirectoryIfMissing , doesDirectoryExist
56
- , getCurrentDirectory , getDirectoryContents , removeFile
56
+ , getDirectoryContents , removeFile
57
57
)
58
- import System.Exit ( exitWith , ExitCode (.. ) )
58
+ import System.Exit ( exitWith , ExitCode (.. ) )
59
59
import System.Environment ( getProgName , getEnv )
60
60
import System.FilePath as FilePath
61
61
import System.IO ( hFlush , hPutStrLn , stderr , stdout )
@@ -83,28 +83,31 @@ ghcPkgUnregisterForce globalDb pkgDb hasIpid pkgarg_strs = do
83
83
-- -----------------------------------------------------------------------------
84
84
-- Command-line syntax
85
85
86
- data Verbosity = Silent | Normal | Verbose
87
- deriving (Show , Eq , Ord )
86
+ data Verbosity
87
+ = Silent
88
+ | Normal
89
+ | Verbose
90
+ deriving (Show , Eq , Ord )
88
91
89
92
-- -----------------------------------------------------------------------------
90
93
-- Do the business
91
94
92
95
-- | Enum flag representing argument type
93
96
data AsPackageArg
94
- = AsUnitId
95
- | AsDefault
97
+ = AsUnitId
98
+ | AsDefault
96
99
97
100
-- | Represents how a package may be specified by a user on the command line.
98
101
data PackageArg
99
102
-- | A package identifier foo-0.1, or a glob foo-*
100
- = Id GlobPackageIdentifier
103
+ = Id GlobPackageIdentifier
101
104
-- | An installed package ID foo-0.1-HASH. This is guaranteed to uniquely
102
105
-- match a single entry in the package database.
103
- | IUId UnitId
106
+ | IUId UnitId
104
107
-- | A glob against the package name. The first string is the literal
105
108
-- glob, the second is a function which returns @True@ if the argument
106
109
-- matches.
107
- | Substring String (String -> Bool )
110
+ | Substring String (String -> Bool )
108
111
109
112
parseCheck :: Cabal. Parsec a => String -> String -> IO a
110
113
parseCheck str what =
@@ -115,8 +118,8 @@ parseCheck str what =
115
118
-- | Either an exact 'PackageIdentifier', or a glob for all packages
116
119
-- matching 'PackageName'.
117
120
data GlobPackageIdentifier
118
- = ExactPackageIdentifier MungedPackageId
119
- | GlobPackageIdentifier MungedPackageName
121
+ = ExactPackageIdentifier MungedPackageId
122
+ | GlobPackageIdentifier MungedPackageName
120
123
121
124
displayGlobPkgId :: GlobPackageIdentifier -> String
122
125
displayGlobPkgId (ExactPackageIdentifier pid) = display pid
@@ -135,23 +138,12 @@ readPackageArg AsDefault str = Id <$> readGlobPkgId str
135
138
-- -----------------------------------------------------------------------------
136
139
-- Package databases
137
140
138
- -- Some commands operate on a single database:
139
- -- register, unregister, expose, hide, trust, distrust
140
- -- however these commands also check the union of the available databases
141
- -- in order to check consistency. For example, register will check that
142
- -- dependencies exist before registering a package.
143
- --
144
- -- Some commands operate on multiple databases, with overlapping semantics:
145
- -- list, describe, field
146
-
147
141
data PackageDB (mode :: GhcPkg. DbMode )
148
142
= PackageDB {
149
- location , locationAbsolute :: ! FilePath ,
150
- -- We need both possibly-relative and definitely-absolute package
151
- -- db locations. This is because the relative location is used as
152
- -- an identifier for the db, so it is important we do not modify it.
153
- -- On the other hand we need the absolute path in a few places
154
- -- particularly in relation to the ${pkgroot} stuff.
143
+ location :: ! FilePath ,
144
+ -- We only need possibly-relative package db location. The relative
145
+ -- location is used as an identifier for the db, so it is important we do
146
+ -- not modify it.
155
147
156
148
packageDbLock :: ! (GhcPkg. DbOpenMode mode GhcPkg. PackageDbLock ),
157
149
-- If package db is open in read write mode, we keep its lock around for
@@ -164,9 +156,8 @@ type PackageDBStack = [PackageDB 'GhcPkg.DbReadOnly]
164
156
-- A stack of package databases. Convention: head is the topmost
165
157
-- in the stack.
166
158
167
- -- | Selector for picking the right package DB to modify as 'register' and
168
- -- 'recache' operate on the database on top of the stack, whereas 'modify'
169
- -- changes the first database that contains a specific package.
159
+ -- | Selector for picking the right package DB to modify as 'modify' changes the
160
+ -- first database that contains a specific package.
170
161
newtype DbModifySelector = ContainsPkg PackageArg
171
162
172
163
getPkgDatabases :: Path Abs Dir
@@ -217,8 +208,8 @@ getPkgDatabases globalDb verbosity pkgarg pkgDb = do
217
208
infoLn (" modifying: " ++ location db)
218
209
infoLn (" flag db stack: " ++ show (map location flag_db_stack))
219
210
220
- return (db_stack, db_to_operate_on, flag_db_stack)
221
- where
211
+ pure (db_stack, db_to_operate_on, flag_db_stack)
212
+ where
222
213
getDatabases flag_db_names final_stack = do
223
214
-- The package db we open in read write mode is the first one included in
224
215
-- flag_db_names that contains specified package. Therefore we need to
@@ -237,7 +228,7 @@ getPkgDatabases globalDb verbosity pkgarg pkgDb = do
237
228
db <- readDatabase db_path
238
229
if hasPkg db
239
230
then couldntOpenDbForModification db_path e
240
- else return (db, Nothing )
231
+ else pure (db, Nothing )
241
232
242
233
-- If we fail to open the database in read/write mode, we need
243
234
-- to check if it's for modification first before throwing an
@@ -246,22 +237,22 @@ getPkgDatabases globalDb verbosity pkgarg pkgDb = do
246
237
db <- readParseDatabase verbosity (GhcPkg. DbOpenReadWrite $ ContainsPkg pkgarg) db_path
247
238
let ro_db = db { packageDbLock = GhcPkg. DbOpenReadOnly }
248
239
if hasPkg db
249
- then return (ro_db, Just db)
240
+ then pure (ro_db, Just db)
250
241
else do
251
242
-- If the database is not for modification after all,
252
243
-- drop the write lock as we are already finished with
253
244
-- the database.
254
245
case packageDbLock db of
255
246
GhcPkg. DbOpenReadWrite lock ->
256
247
GhcPkg. unlockPackageDb lock
257
- return (ro_db, Nothing )
248
+ pure (ro_db, Nothing )
258
249
| db_path <- final_stack ]
259
250
260
251
to_modify <- case mto_modify of
261
- Just db -> return db
252
+ Just db -> pure db
262
253
Nothing -> cannotFindPackage pkgarg Nothing
263
254
264
- return (db_stack, GhcPkg. DbOpenReadWrite to_modify)
255
+ pure (db_stack, GhcPkg. DbOpenReadWrite to_modify)
265
256
where
266
257
couldntOpenDbForModification :: FilePath -> IOError -> IO a
267
258
couldntOpenDbForModification db_path e = die $ " Couldn't open database "
@@ -291,7 +282,7 @@ readParseDatabase verbosity mode path = do
291
282
-- old single-file style db:
292
283
mdb <- tryReadParseOldFileStyleDatabase verbosity mode path
293
284
case mdb of
294
- Just db -> return db
285
+ Just db -> pure db
295
286
Nothing ->
296
287
die $ " ghc no longer supports single-file style package "
297
288
++ " databases (" ++ path ++ " ) use 'ghc-pkg init'"
@@ -300,33 +291,31 @@ readParseDatabase verbosity mode path = do
300
291
| otherwise -> ioError err
301
292
Right fs -> ignore_cache (const $ return () )
302
293
where
303
- confs = map (path </> ) $ filter (" .conf" `isSuffixOf` ) fs
304
-
305
- ignore_cache :: (FilePath -> IO () ) -> IO (PackageDB mode )
306
- ignore_cache checkTime = do
307
- -- If we're opening for modification, we need to acquire a
308
- -- lock even if we don't open the cache now, because we are
309
- -- going to modify it later.
310
- lock <- F. mapM (const $ GhcPkg. lockPackageDb cache) mode
311
- let doFile f = do checkTime f
312
- parseSingletonPackageConf verbosity f
313
- pkgs <- mapM doFile confs
314
- mkPackageDB pkgs lock
294
+ confs = map (path </> ) $ filter (" .conf" `isSuffixOf` ) fs
295
+
296
+ ignore_cache :: (FilePath -> IO () ) -> IO (PackageDB mode )
297
+ ignore_cache checkTime = do
298
+ -- If we're opening for modification, we need to acquire a
299
+ -- lock even if we don't open the cache now, because we are
300
+ -- going to modify it later.
301
+ lock <- F. mapM (const $ GhcPkg. lockPackageDb cache) mode
302
+ let doFile f = do checkTime f
303
+ parseSingletonPackageConf verbosity f
304
+ pkgs <- mapM doFile confs
305
+ mkPackageDB pkgs lock
315
306
316
- where
317
- cache = path </> cachefilename
318
-
319
- mkPackageDB :: [InstalledPackageInfo ]
320
- -> GhcPkg. DbOpenMode mode GhcPkg. PackageDbLock
321
- -> IO (PackageDB mode )
322
- mkPackageDB pkgs lock = do
323
- path_abs <- absolutePath path
324
- return $ PackageDB {
325
- location = path,
326
- locationAbsolute = path_abs,
327
- packageDbLock = lock,
328
- packages = pkgs
329
- }
307
+ where
308
+ cache = path </> cachefilename
309
+
310
+ mkPackageDB :: [InstalledPackageInfo ]
311
+ -> GhcPkg. DbOpenMode mode GhcPkg. PackageDbLock
312
+ -> IO (PackageDB mode )
313
+ mkPackageDB pkgs lock = do
314
+ pure $ PackageDB
315
+ { location = path
316
+ , packageDbLock = lock
317
+ , packages = pkgs
318
+ }
330
319
331
320
parseSingletonPackageConf :: Verbosity -> FilePath -> IO InstalledPackageInfo
332
321
parseSingletonPackageConf verbosity file = do
@@ -360,27 +349,22 @@ tryReadParseOldFileStyleDatabase verbosity mode path = do
360
349
content <- readFile path `catchIO` \ _ -> return " "
361
350
if take 2 content == " []"
362
351
then do
363
- path_abs <- absolutePath path
364
352
let path_dir = adjustOldDatabasePath path
365
353
warn $ " Warning: ignoring old file-style db and trying " ++ path_dir
366
354
direxists <- doesDirectoryExist path_dir
367
355
if direxists
368
356
then do
369
357
db <- readParseDatabase verbosity mode path_dir
370
358
-- but pretend it was at the original location
371
- return $ Just db {
372
- location = path,
373
- locationAbsolute = path_abs
374
- }
359
+ pure $ Just db { location = path }
375
360
else do
376
361
lock <- F. forM mode $ \ _ -> do
377
362
createDirectoryIfMissing True path_dir
378
363
GhcPkg. lockPackageDb $ path_dir </> cachefilename
379
- return $ Just PackageDB {
380
- location = path,
381
- locationAbsolute = path_abs,
382
- packageDbLock = lock,
383
- packages = []
364
+ pure $ Just PackageDB
365
+ { location = path
366
+ , packageDbLock = lock
367
+ , packages = []
384
368
}
385
369
386
370
-- if the path is not a file, or is not an empty db then we fail
@@ -392,23 +376,20 @@ adjustOldFileStylePackageDB db = do
392
376
mcontent <- fmap Just (readFile (location db)) `catchIO` \ _ -> return Nothing
393
377
case fmap (take 2 ) mcontent of
394
378
-- it is an old style and empty db, so look for a dir kind in location.d/
395
- Just " []" -> return db {
396
- location = adjustOldDatabasePath $ location db,
397
- locationAbsolute = adjustOldDatabasePath $ locationAbsolute db
398
- }
379
+ Just " []" -> pure db
380
+ { location = adjustOldDatabasePath $ location db }
399
381
-- it is old style but not empty, we have to bail
400
382
Just _ -> die $ " ghc no longer supports single-file style package "
401
383
++ " databases (" ++ location db ++ " ) use 'ghc-pkg init'"
402
384
++ " to create the database with the correct format."
403
385
-- probably not old style, carry on as normal
404
- Nothing -> return db
386
+ Nothing -> pure db
405
387
406
388
adjustOldDatabasePath :: FilePath -> FilePath
407
389
adjustOldDatabasePath = (<.> " d" )
408
390
409
- parsePackageInfo
410
- :: BS. ByteString
411
- -> IO (InstalledPackageInfo , [ValidateWarning ])
391
+ parsePackageInfo :: BS. ByteString
392
+ -> IO (InstalledPackageInfo , [ValidateWarning ])
412
393
parsePackageInfo str =
413
394
case parseInstalledPackageInfo str of
414
395
Right (warnings, ok) -> pure (mungePackageInfo ok, ws)
@@ -606,8 +587,3 @@ removeFileSafe :: FilePath -> IO ()
606
587
removeFileSafe fn =
607
588
removeFile fn `catchIO` \ e ->
608
589
unless (isDoesNotExistError e) $ ioError e
609
-
610
- -- | Turn a path relative to the current directory into a (normalised)
611
- -- absolute path.
612
- absolutePath :: FilePath -> IO FilePath
613
- absolutePath path = normalise . (</> path) <$> getCurrentDirectory
0 commit comments