Programmatically access information from Package.swift

Hi, I would like to be able to somehow parse the Package.swift file from either a new target or (even better) a CLI tool. The idea is that I would like to be able to automatically generate a graph of all the module dependencies (e.g. using graphviz). What is the best way to approach this? I could write a very ad-hoc parser in any programming language, but maybe there's already a better way?

I thought about maybe using SwiftSyntax, but that seems too low-level: I don't really need to know the AST representation, it would be easier if I could just access the Package object directly and inspect its targets.

Use the package manager itself:

https://github.com/apple/swift-package-manager/blob/master/Examples/package-info/Sources/package-info/main.swift

(There is no tag that corresponds to Swift 5.1, because it has not been release yet. You can use 0.4.0 with Swift 5.0, but some of the convenient helpers were not yet available then. If you need to use an older version of Swift (or you don’t want to worry about hunting down the compiler executable), you can use the equivalent helpers I provide in my SDGSwift wrapper.)

3 Likes