SPM build tool plugins - bringing resources to the target?

While a resource in the general sense, this is not what SwiftPM calls a resource in the technical sense.

context exists to provide information about the execution context of the plugin (not the context of its own build). Hence the resulting path inside B is working as intended. If the plugin wants to know something about B in order to make a decision, context is where it should look.

On the other hand a resource in SwiftPM’s technical sense is something associated with a target that is to be bundled alongside it for runtime access. You register them with the target’s resources parameter and look them up at runtime with Bundle.module. In the manifest, plugin(...) does not even have a resources parameter, therefore resources are clearly not supported for plugin definitions.

What you likely want to do is create an executable that produces the template (executables do support resources). Your plugin would then depend on both the executable and Sourcery, first generating the template, then running Sourcery pointing at it.

Think of plugins as being composed of two pieces, the declaration of intent (.plugin) and one or more executables (.executableTarget) that actually do the work.

The declaration will be evaluated while SwiftPM is figuring out what it will need to do, before it actually does anything. Like the manifest, nothing is guaranteed to have been resolved yet, the source is not guaranteed to be available, and arbitrary imports do not work. The declaration should be as simple as possible.

On the other hand, the workhorse executable can be as elaborate as you want, and supports all features of SwiftPM. Since it will only be invoked if the build actually needs it, and participates in the incremental build cache, it is much less of an issue if it bloats into something cumbersome.

They can only depend on executable targets. And the executable is not actually available when the plugin is evaluated, but rather declared as needed when it comes time to execute the reported commands. Any other kind of dependency is not supported and will trigger an error (or fail silently in early versions).

1 Like