@@ -6,35 +6,29 @@ import Workspace
66// PREREQUISITES
77// ============
88
9- // We will need to know where the Swift compiler is.
10- let swiftCompiler : AbsolutePath = {
11- let string : String
12- #if os(macOS)
13- string = try ! Process . checkNonZeroExit ( args: " xcrun " , " --sdk " , " macosx " , " -f " , " swiftc " ) . spm_chomp ( )
14- #else
15- string = try ! Process . checkNonZeroExit ( args: " which " , " swiftc " ) . spm_chomp ( )
16- #endif
17- return AbsolutePath ( string)
18- } ( )
19-
209// We need a package to work with.
21- // This assumes there is one in the current working directory:
22- let packagePath = localFileSystem . currentWorkingDirectory!
10+ // This computes the path of this package root based on the file location
11+ let packagePath = AbsolutePath ( #file ) . parentDirectory . parentDirectory . parentDirectory
2312
2413// LOADING
2514// =======
2615
27- // Note:
28- // This simplified API has been added since 0.4.0 was released.
29- // See older revisions for examples that work with 0.4.0.
30-
3116// There are several levels of information available.
3217// Each takes longer to load than the level above it, but provides more detail.
3318let diagnostics = DiagnosticsEngine ( )
34- let identityResolver = DefaultIdentityResolver ( )
35- let manifest = try tsc_await { ManifestLoader . loadRootManifest ( at: packagePath, swiftCompiler: swiftCompiler, swiftCompilerFlags: [ ] , identityResolver: identityResolver, on: . global( ) , completion: $0) }
36- let loadedPackage = try tsc_await { PackageBuilder . loadRootPackage ( at: packagePath, swiftCompiler: swiftCompiler, swiftCompilerFlags: [ ] , identityResolver: identityResolver, diagnostics: diagnostics, on: . global( ) , completion: $0) }
37- let graph = try Workspace . loadRootGraph ( at: packagePath, swiftCompiler: swiftCompiler, swiftCompilerFlags: [ ] , identityResolver: identityResolver, diagnostics: diagnostics)
19+ let workspace = try Workspace ( forRootPackage: packagePath)
20+ let manifest = try tsc_await { workspace. loadRootManifest ( at: packagePath, diagnostics: diagnostics, completion: $0) }
21+
22+ let package = try tsc_await { workspace. loadRootPackage ( at: packagePath, diagnostics: diagnostics, completion: $0) }
23+ guard !diagnostics. hasErrors else {
24+ fatalError ( " error package manifest: \( diagnostics) " )
25+ }
26+
27+ let graph = try workspace. loadPackageGraph ( rootPath: packagePath, diagnostics: diagnostics)
28+ guard !diagnostics. hasErrors else {
29+ fatalError ( " error loading package dependencies: \( diagnostics) " )
30+ }
31+
3832
3933// EXAMPLES
4034// ========
@@ -46,7 +40,7 @@ let targets = manifest.targets.map({ $0.name }).joined(separator: ", ")
4640print ( " Targets: " , targets)
4741
4842// Package
49- let executables = loadedPackage . targets. filter ( { $0. type == . executable } ) . map ( { $0. name } )
43+ let executables = package . targets. filter ( { $0. type == . executable } ) . map ( { $0. name } )
5044print ( " Executable targets: " , executables)
5145
5246// PackageGraph
0 commit comments