Skip to content

Latest commit

History

20 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

SourceryBuildPlugin

SPM and Xcode Build plugins to run Sourcery CLI easier. Learn more about it from the Sourcery here.

How to install

SPM

Add this configuration to your Package.swift:

dependencies:[.package(url:"https://github.com/fenli/SourceryBuildPlugin", from:"2.3.0"),],

This package version will follow Sourcery binary versioning

Then add the plugins to your target:

.target(
name:"MyPackage",
plugins:[.plugin(name:"SourceryBuildPlugin",package:"SourceryBuildPlugin")])

XCode

Integration into Xcode project:

  • In Xcode root project, navigate to your targets list in side bar.
  • Select target to integrate (usually app or library target).
  • Go to Build Phase -> Run Build Tool Plug-ins -> Add the plugin

Build on CI

If you are building on CI server, you have to add this as the xcodebuild command arguments to bypass xcode validation:

-skipPackagePluginValidation
-skipMacroValidation

Or if you are building on Xcode Cloud, you can disable it by adding this to ci_scripts/ci_post_clone.sh:

defaults write com.apple.dt.Xcode IDESkipPackagePluginFingerprintValidatation -bool YES
defaults write com.apple.dt.Xcode IDESkipMacroFingerprintValidation -bool YES

Advantages

Offering sourcery plugins in a separate package has some advantages you should consider:

  • No need to clone the whole Sourcery repository.
  • Sourcery itself is included as a binary dependency, so consumer doesn't need to build it first.
  • No other dependencies that need to be downloaded, resolved and compiled.
  • Plugin runs in sanboxed environment so generated file is stored in intermediary build directory (it doesn't change source directory).
  • Easily integrated into projects / packages since sourcery cli arguments is provided automatically:

Configuration

Auto configured

This plugin will run with minimum setup with auto configuration.

ArgumentsProvided Value
--sourcesUsing target source root directory
--templatesUsing any stencils/swifttemplate files found in source root directory or sub-directories
--outputAuto generated directory inside plugin work directory (sandboxed)
--cacheBasePathAuto generated directory inside plugin work directory (sandboxed)

Optionally, if you need to pass extra arguments (other than those 4) to sourcery cli, create .sourcery.argfile inside your target source directory. Sample of valid .sourcery.argfile:

--args arg1=value1
--args arg2=value with spaces
--args arg3
--verbose

Note

All auto provided arguments will be ignored if specified in the argfile. See table below for the provided ENV variable that can be used on .sourcery.argfile file.

With configuration file

Create .sourcery.yml in the target source directory and it will be used instead of auto provided arguments. Read more regarding this format here. Sample of valid .sourcery.yml:

sources:
- "${TARGET_SOURCE_DIR}"templates:
- ../../Templatesoutput:
"${TARGET_OUTPUT_DIR}"args:
autoImports: ["SwiftUI"]

Note

It's required to use ENV variable for output since you cannot write to source directory in sandbox mode. See table below for the provided ENV variable that can be used on .sourcery.yml file.

Environment variables

These are environment variables that are visible in .sourcery.yml and .sourcery.argfile

ENVValue
PROJECT_ROOT_DIRProject root directory (Xcode target only)
PACKAGE_ROOT_DIRPackage root directory (Swift package target only)
TARGET_SOURCE_DIRTarget source directory (PackageName -> Sources -> TargetName)
TARGET_OUTPUT_DIRAuto generated directory inside plugin work directory (sandboxed)
TARGET_CACHE_DIRAuto generated directory inside plugin work directory (sandboxed)
HOMEHome directory
USERLogged username

About

SPM and Xcode Build plugins to run Sourcery CLI easier

Topics

Resources

Stars

12 stars

Watchers

0 watching

Forks

Releases

Sponsor this project

Contributors

Languages