@@ -252,24 +252,44 @@ public class Workspace {
252
252
pinsFile: AbsolutePath ,
253
253
manifestLoader: ManifestLoaderProtocol ,
254
254
repositoryManager: RepositoryManager ? = nil ,
255
- currentToolsVersion: ToolsVersion = ToolsVersion . currentToolsVersion ,
256
- toolsVersionLoader: ToolsVersionLoaderProtocol = ToolsVersionLoader ( ) ,
255
+ currentToolsVersion: ToolsVersion ? = nil ,
256
+ toolsVersionLoader: ToolsVersionLoaderProtocol ? = nil ,
257
257
delegate: WorkspaceDelegate ? = nil ,
258
- config: Workspace . Configuration = Workspace . Configuration ( ) ,
259
- fileSystem: FileSystem = localFileSystem ,
260
- repositoryProvider: RepositoryProvider = GitRepositoryProvider ( ) ,
258
+ config: Workspace . Configuration ? = nil ,
259
+ fileSystem: FileSystem ? = nil ,
260
+ repositoryProvider: RepositoryProvider ? = nil ,
261
261
identityResolver: IdentityResolver ? = nil ,
262
- httpClient: HTTPClient = HTTPClient ( ) ,
262
+ httpClient: HTTPClient ? = nil ,
263
263
netrcFilePath: AbsolutePath ? = nil ,
264
- archiver: Archiver = ZipArchiver ( ) ,
265
- checksumAlgorithm: HashAlgorithm = SHA256 ( ) ,
266
- additionalFileRules: [ FileRuleDescription ] = [ ] ,
267
- isResolverPrefetchingEnabled: Bool = false ,
268
- enablePubgrubResolver: Bool = false ,
269
- skipUpdate: Bool = false ,
270
- enableResolverTrace: Bool = false ,
264
+ archiver: Archiver ? = nil ,
265
+ checksumAlgorithm: HashAlgorithm ? = nil ,
266
+ additionalFileRules: [ FileRuleDescription ] ? = nil ,
267
+ isResolverPrefetchingEnabled: Bool ? = nil ,
268
+ enablePubgrubResolver: Bool ? = nil ,
269
+ skipUpdate: Bool ? = nil ,
270
+ enableResolverTrace: Bool ? = nil ,
271
271
cachePath: AbsolutePath ? = nil
272
272
) {
273
+ // defaults
274
+ let currentToolsVersion = currentToolsVersion ?? ToolsVersion . currentToolsVersion
275
+ let toolsVersionLoader = toolsVersionLoader ?? ToolsVersionLoader ( )
276
+ let config = config ?? Workspace . Configuration ( )
277
+ let fileSystem = fileSystem ?? localFileSystem
278
+ let repositoryProvider = repositoryProvider ?? GitRepositoryProvider ( )
279
+ let httpClient = httpClient ?? HTTPClient ( )
280
+ let archiver = archiver ?? ZipArchiver ( )
281
+ var checksumAlgorithm = checksumAlgorithm ?? SHA256 ( )
282
+ #if canImport(CryptoKit)
283
+ if checksumAlgorithm is SHA256 , #available( macOS 10 . 15 , * ) {
284
+ checksumAlgorithm = CryptoKitSHA256 ( )
285
+ }
286
+ #endif
287
+ let additionalFileRules = additionalFileRules ?? [ ]
288
+ let isResolverPrefetchingEnabled = isResolverPrefetchingEnabled ?? false
289
+ let skipUpdate = skipUpdate ?? false
290
+ let enableResolverTrace = enableResolverTrace ?? false
291
+
292
+ // initialize
273
293
self . delegate = delegate
274
294
self . dataPath = dataPath
275
295
self . config = config
@@ -281,12 +301,6 @@ public class Workspace {
281
301
self . netrcFilePath = netrcFilePath
282
302
self . archiver = archiver
283
303
284
- var checksumAlgorithm = checksumAlgorithm
285
- #if canImport(CryptoKit)
286
- if checksumAlgorithm is SHA256 , #available( macOS 10 . 15 , * ) {
287
- checksumAlgorithm = CryptoKitSHA256 ( )
288
- }
289
- #endif
290
304
self . checksumAlgorithm = checksumAlgorithm
291
305
self . isResolverPrefetchingEnabled = isResolverPrefetchingEnabled
292
306
self . skipUpdate = skipUpdate
@@ -329,21 +343,66 @@ public class Workspace {
329
343
///
330
344
/// The root package path is used to compute the build directory and other
331
345
/// default paths.
332
- public static func create(
346
+ public convenience init (
347
+ forRootPackage packagePath: AbsolutePath ,
348
+ toolchain: UserToolchain ? = nil ,
349
+ repositoryManager: RepositoryManager ? = nil ,
350
+ delegate: WorkspaceDelegate ? = nil
351
+ ) throws {
352
+ // 👀 is this correct (default toolchain)
353
+ let toolchain = try toolchain ?? UserToolchain ( destination: . hostDestination( ) )
354
+ let manifestLoader = ManifestLoader ( toolchain: toolchain. configuration)
355
+
356
+ try self . init (
357
+ forRootPackage: packagePath,
358
+ manifestLoader: manifestLoader,
359
+ repositoryManager: repositoryManager,
360
+ delegate: delegate
361
+ )
362
+ }
363
+
364
+ /// A convenience method for creating a workspace for the given root
365
+ /// package path.
366
+ ///
367
+ /// The root package path is used to compute the build directory and other
368
+ /// default paths.
369
+ public convenience init (
333
370
forRootPackage packagePath: AbsolutePath ,
334
371
manifestLoader: ManifestLoaderProtocol ,
335
372
repositoryManager: RepositoryManager ? = nil ,
336
- delegate: WorkspaceDelegate ? = nil ,
337
- identityResolver : IdentityResolver ? = nil
338
- ) -> Workspace {
339
- return Workspace (
373
+ delegate: WorkspaceDelegate ? = nil
374
+ ) throws {
375
+
376
+ self . init (
340
377
dataPath: packagePath. appending ( component: " .build " ) ,
341
378
editablesPath: packagePath. appending ( component: " Packages " ) ,
342
379
pinsFile: packagePath. appending ( component: " Package.resolved " ) ,
343
380
manifestLoader: manifestLoader,
344
381
repositoryManager: repositoryManager,
345
- delegate: delegate,
346
- identityResolver: identityResolver
382
+ delegate: delegate
383
+ )
384
+ }
385
+
386
+ /// A convenience method for creating a workspace for the given root
387
+ /// package path.
388
+ ///
389
+ /// The root package path is used to compute the build directory and other
390
+ /// default paths.
391
+ // FIXME: this one is kind of messy to backwards support, hopefully we can remove quickly
392
+ // deprecated 8/2021
393
+ @available ( * , deprecated, message: " use constructor instead " )
394
+ public static func create(
395
+ forRootPackage packagePath: AbsolutePath ,
396
+ manifestLoader: ManifestLoaderProtocol ,
397
+ repositoryManager: RepositoryManager ? = nil ,
398
+ delegate: WorkspaceDelegate ? = nil ,
399
+ identityResolver: IdentityResolver ? = nil
400
+ ) -> Workspace {
401
+ return try ! . init( forRootPackage: packagePath,
402
+ manifestLoader: manifestLoader,
403
+ repositoryManager: repositoryManager,
404
+ delegate: delegate//,
405
+ //identityResolver: identityResolver
347
406
)
348
407
}
349
408
}
@@ -628,14 +687,16 @@ extension Workspace {
628
687
/// - diagnostics: Optional. The diagnostics engine.
629
688
/// - on: The dispatch queue to perform asynchronous operations on.
630
689
/// - completion: The completion handler .
690
+ // deprecated 8/2021
691
+ @available ( * , deprecated, message: " use workspace instance API instead " )
631
692
public static func loadRootGraph(
632
693
at packagePath: AbsolutePath ,
633
694
swiftCompiler: AbsolutePath ,
634
695
swiftCompilerFlags: [ String ] ,
635
696
identityResolver: IdentityResolver ? = nil ,
636
697
diagnostics: DiagnosticsEngine
637
698
) throws -> PackageGraph {
638
- let toolchain = try ToolchainConfiguration ( swiftCompiler: swiftCompiler, swiftCompilerFlags: swiftCompilerFlags)
699
+ let toolchain = ToolchainConfiguration ( swiftCompiler: swiftCompiler, swiftCompilerFlags: swiftCompilerFlags)
639
700
let loader = ManifestLoader ( toolchain: toolchain)
640
701
let workspace = Workspace . create ( forRootPackage: packagePath, manifestLoader: loader, identityResolver: identityResolver)
641
702
return try workspace. loadPackageGraph ( rootPath: packagePath, diagnostics: diagnostics)
@@ -752,6 +813,49 @@ extension Workspace {
752
813
}
753
814
}
754
815
816
+ /// Loads and returns manifest at the given path.
817
+ public func loadRootManifest(
818
+ at path: AbsolutePath ,
819
+ diagnostics: DiagnosticsEngine ,
820
+ completion: @escaping ( Result < Manifest , Error > ) -> Void
821
+ ) {
822
+ self . loadRootManifests ( packages: [ path] , diagnostics: diagnostics) { result in
823
+ completion ( result. tryMap {
824
+ // normally, we call loadRootManifests which attempts to load any manifest it can and report errors via diagnostics
825
+ // in this case, we want to load a specific manifest, so if the diagnostics contains an error we want to throw
826
+ guard !diagnostics. hasErrors else {
827
+ // not sure about this one
828
+ throw StringError ( " \( diagnostics) " )
829
+ }
830
+ guard let manifest = $0 [ path] else {
831
+ throw InternalError ( " Unknown manifest for ' \( path) ' " )
832
+ }
833
+ return manifest
834
+ } )
835
+ }
836
+ }
837
+
838
+ public func loadRootPackage(
839
+ at path: AbsolutePath ,
840
+ diagnostics: DiagnosticsEngine ,
841
+ completion: @escaping ( Result < Package , Error > ) -> Void
842
+ ) {
843
+ self . loadRootManifest ( at: path, diagnostics: diagnostics) { result in
844
+ let result = result. tryMap { manifest -> Package in
845
+ let identity = self . identityResolver. resolveIdentity ( for: manifest. packageLocation)
846
+ let builder = PackageBuilder (
847
+ identity: identity,
848
+ manifest: manifest,
849
+ productFilter: . everything,
850
+ path: path,
851
+ xcTestMinimumDeploymentTargets: MinimumDeploymentTarget . default. xcTestMinimumDeploymentTargets,
852
+ diagnostics: diagnostics)
853
+ return try builder. construct ( )
854
+ }
855
+ completion ( result)
856
+ }
857
+ }
858
+
755
859
/// Generates the checksum
756
860
public func checksum(
757
861
forBinaryArtifactAt path: AbsolutePath ,
0 commit comments