@@ -399,25 +399,25 @@ extension Workspace.Configuration {
399
399
sharedRegistriesFile: AbsolutePath ? ,
400
400
fileSystem: FileSystem
401
401
) throws {
402
- self . localRegistries = . init( path: localRegistriesFile, fileSystem: fileSystem, deleteWhenEmpty : true )
403
- self . sharedRegistries = sharedRegistriesFile. map { . init( path: $0, fileSystem: fileSystem, deleteWhenEmpty : false ) }
402
+ self . localRegistries = . init( path: localRegistriesFile, fileSystem: fileSystem)
403
+ self . sharedRegistries = sharedRegistriesFile. map { . init( path: $0, fileSystem: fileSystem) }
404
404
self . fileSystem = fileSystem
405
405
try self . computeRegistries ( )
406
406
}
407
407
408
408
@discardableResult
409
- public func applyLocal ( handler: ( inout RegistryConfiguration ) throws -> Void ) throws -> RegistryConfiguration {
410
- try self . localRegistries. apply ( handler : handler)
409
+ public func updateLocal ( with handler: ( inout RegistryConfiguration ) throws -> Void ) throws -> RegistryConfiguration {
410
+ try self . localRegistries. update ( with : handler)
411
411
try self . computeRegistries ( )
412
412
return self . configuration
413
413
}
414
414
415
415
@discardableResult
416
- public func applyShared ( handler: ( inout RegistryConfiguration ) throws -> Void ) throws -> RegistryConfiguration {
416
+ public func updateShared ( with handler: ( inout RegistryConfiguration ) throws -> Void ) throws -> RegistryConfiguration {
417
417
guard let sharedRegistries = self . sharedRegistries else {
418
418
throw InternalError ( " shared registries not configured " )
419
419
}
420
- try sharedRegistries. apply ( handler : handler)
420
+ try sharedRegistries. update ( with : handler)
421
421
try self . computeRegistries ( )
422
422
return self . configuration
423
423
}
@@ -428,11 +428,11 @@ extension Workspace.Configuration {
428
428
try self . lock. withLock {
429
429
var configuration = RegistryConfiguration ( )
430
430
431
- if let sharedConfiguration = try sharedRegistries? . get ( ) {
431
+ if let sharedConfiguration = try sharedRegistries? . load ( ) {
432
432
configuration. merge ( sharedConfiguration)
433
433
}
434
434
435
- let localConfiguration = try localRegistries. get ( )
435
+ let localConfiguration = try localRegistries. load ( )
436
436
configuration. merge ( localConfiguration)
437
437
438
438
self . _configuration = configuration
@@ -445,37 +445,13 @@ extension Workspace.Configuration {
445
445
private struct RegistriesStorage {
446
446
private let path : AbsolutePath
447
447
private let fileSystem : FileSystem
448
- private let deleteWhenEmpty : Bool
449
448
450
- public init ( path: AbsolutePath , fileSystem: FileSystem , deleteWhenEmpty : Bool ) {
449
+ public init ( path: AbsolutePath , fileSystem: FileSystem ) {
451
450
self . path = path
452
451
self . fileSystem = fileSystem
453
- self . deleteWhenEmpty = deleteWhenEmpty
454
- }
455
-
456
- public func get( ) throws -> RegistryConfiguration {
457
- return try self . fileSystem. withLock ( on: self . path. parentDirectory, type: . shared) {
458
- return try Self . load ( self . path, fileSystem: self . fileSystem)
459
- }
460
- }
461
-
462
- @discardableResult
463
- public func apply( handler: ( inout RegistryConfiguration ) throws -> Void ) throws -> RegistryConfiguration {
464
- if !self . fileSystem. exists ( self . path. parentDirectory) {
465
- try self . fileSystem. createDirectory ( self . path. parentDirectory, recursive: true )
466
- }
467
- return try self . fileSystem. withLock ( on: self . path. parentDirectory, type: . exclusive) {
468
- let configuration = try Self . load ( self . path, fileSystem: self . fileSystem)
469
- var updatedConfiguration = configuration
470
- try handler ( & updatedConfiguration)
471
- if updatedConfiguration != configuration {
472
- try Self . save ( updatedConfiguration, to: self . path, fileSystem: self . fileSystem, deleteWhenEmpty: self . deleteWhenEmpty)
473
- }
474
- return updatedConfiguration
475
- }
476
452
}
477
453
478
- private static func load( _ path : AbsolutePath , fileSystem : FileSystem ) throws -> RegistryConfiguration {
454
+ public func load( ) throws -> RegistryConfiguration {
479
455
guard fileSystem. exists ( path) else {
480
456
return RegistryConfiguration ( )
481
457
}
@@ -485,23 +461,26 @@ extension Workspace.Configuration {
485
461
return try decoder. decode ( RegistryConfiguration . self, from: data)
486
462
}
487
463
488
- private static func save( _ configuration: RegistryConfiguration , to path: AbsolutePath , fileSystem: FileSystem , deleteWhenEmpty: Bool ) throws {
489
- if configuration. isEmpty {
490
- if deleteWhenEmpty && fileSystem. exists ( path) {
491
- // deleteWhenEmpty is a backward compatibility mode
492
- return try fileSystem. removeFileTree ( path)
493
- } else if !fileSystem. exists ( path) {
494
- // nothing to do
495
- return
496
- }
497
- }
498
-
464
+ public func save( _ configuration: RegistryConfiguration ) throws {
499
465
let encoder = JSONEncoder . makeWithDefaults ( )
500
466
let data = try encoder. encode ( configuration)
467
+
501
468
if !fileSystem. exists ( path. parentDirectory) {
502
469
try fileSystem. createDirectory ( path. parentDirectory, recursive: true )
503
470
}
504
- try fileSystem. writeFileContents ( path, data: data)
471
+ try fileSystem. writeFileContents ( path, bytes: ByteString ( data) , atomically: true )
472
+ }
473
+
474
+ @discardableResult
475
+ public func update( with handler: ( inout RegistryConfiguration ) throws -> Void ) throws -> RegistryConfiguration {
476
+ let configuration = try load ( )
477
+ var updatedConfiguration = configuration
478
+ try handler ( & updatedConfiguration)
479
+ if updatedConfiguration != configuration {
480
+ try save ( updatedConfiguration)
481
+ }
482
+
483
+ return updatedConfiguration
505
484
}
506
485
}
507
486
}
0 commit comments