Accessing packages without directly importing

Was wondering if there was a way I could access the list of packages (in a project) within code (don't want to explicitly import)? This is possible with Pods by looking through 'Bundle.allFrameworks'. However, it seems that packages are not put there, but instead within 'Bundle.main' as scattered files. In the package manifest, it is creating a property 'package' that holds a Package object. Surely there is a way to access these post compile using a string? For example something like:

let package = Bundle.packages.first(where: { $0.name == "MyPackage" })

Realistically, all I need is to get a list of strings containing the names of all the package names in my project. For example:

"MyPackage"
"MyPackage2"
"MyPackage3"

Then I can use a class to access the rest of the package like so:

for name in packageNames {
     let class = classNamed("\(name).MyClass")
}

I understand I may not be grasping the process in which these packages work, but if anyone has some insight on this, it would be greatly appreciated!

Packages are only a source notion. While some targets produce bundles or binaries as an implementation detail, many only produce intermediate files used by the build system in following steps. They do not exist as distinct entities once compilation is complete.

If you want to record a list during compilation for use later, you can try generating it with a plugin.


Also, FYI, classNamed(_:) is really an Objective C feature and thus only available on platforms where the two languages are able to interact.

Ah I see, that makes sense. I will look into ways to generate this list. Are there any existing plugins you are aware of that at least do something similar? Also thanks for the heads-up on classNamed(_:)! I'll have to keep that in mind.

There are some relevant examples here.

1 Like

Thanks!