Skip to content

Commit d8d5457

Browse files
Add support for adding build tool plugins to targets (#1374)
* Add support for adding build tool plugins to targets * Added Plugin validation * Added some tests * Limited the minimum version to 5.7 Swift * Update .gitignore Co-authored-by: freddi(Yuki Aki) <freddi-kit@users.noreply.github.com> * Update CHANGELOG.md Co-authored-by: freddi(Yuki Aki) <freddi-kit@users.noreply.github.com> * Update CHANGELOG.md Co-authored-by: freddi(Yuki Aki) <freddi-kit@users.noreply.github.com> * Update Docs/ProjectSpec.md Co-authored-by: freddi(Yuki Aki) <freddi-kit@users.noreply.github.com> * Added a fixture for testing plugins * Update CHANGELOG.md * Installed the release version of XcodeProj --------- Co-authored-by: freddi(Yuki Aki) <freddi-kit@users.noreply.github.com>
1 parent 2ef94c9 commit d8d5457

14 files changed

Lines changed: 323 additions & 96 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
- Fixed source file `includes` not working when no paths were found #1337 @shnhrrsn
88

9+
### Added
10+
11+
- Added support for adding `Build Tool Plug-ins` to targets #1374 @BarredEwe
12+
913
## 2.36.1
1014

1115
### Fixed

Docs/ProjectSpec.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ You can also use environment variables in your configuration file, by using `${S
2727
- [Dependency](#dependency)
2828
- [Config Files](#config-files)
2929
- [Plist](#plist)
30+
- [Build Tool Plug-ins](#build-tool-plug-ins)
3031
- [Build Script](#build-script)
3132
- [Build Rule](#build-rule)
3233
- [Target Scheme](#target-scheme)
@@ -381,6 +382,7 @@ Settings are merged in the following order: `groups`, `base`, `configs` (simple
381382
- [ ] **directlyEmbedCarthageDependencies**: **Bool** - If this is `true` Carthage framework dependencies will be embedded using an `Embed Frameworks` build phase instead of the `copy-frameworks` script. Defaults to `true` for all targets except iOS/tvOS/watchOS Applications.
382383
- [ ] **requiresObjCLinking**: **Bool** - If this is `true` any targets that link to this target will have `-ObjC` added to their `OTHER_LDFLAGS`. This is required if a static library has any categories or extensions on Objective-C code. See [this guide](https://pewpewthespells.com/blog/objc_linker_flags.html#objc) for more details. Defaults to `true` if `type` is `library.static`. If you are 100% sure you don't have categories or extensions on Objective-C code (pure Swift with no use of Foundation/UIKit) you can set this to `false`, otherwise it's best to leave it alone.
383384
- [ ] **onlyCopyFilesOnInstall**: **Bool** – If this is `true`, the `Embed Frameworks` and `Embed App Extensions` (if available) build phases will have the "Copy only when installing" chekbox checked. Defaults to `false`.
385+
- [ ] **buildToolPlugins**: **[[Build Tool Plug-ins](#build-tool-plug-ins)]** - Commands for the build system that run automatically *during* the build.
384386
- [ ] **preBuildScripts**: **[[Build Script](#build-script)]** - Build scripts that run *before* any other build phases
385387
- [ ] **postCompileScripts**: **[[Build Script](#build-script)]** - Build scripts that run after the Compile Sources phase
386388
- [ ] **postBuildScripts**: **[[Build Script](#build-script)]** - Build scripts that run *after* any other build phases
@@ -695,6 +697,36 @@ targets:
695697
com.apple.security.application-groups: group.com.app
696698
```
697699

700+
### Build Tool Plug-ins
701+
702+
To add `Build Tool Plug-ins`, you need to add information about plugins to [Target](#target):
703+
704+
- **buildToolPlugins**: List of plugins to connect to the target
705+
706+
Each plugin includes information:
707+
708+
- [x] **plugin**: **String** - plugin name
709+
- [x] **package**: **String** - the name of the package that contains the plugin
710+
711+
Сonnect the plugin to the desired target:
712+
713+
```yaml
714+
targets:
715+
App:
716+
buildToolPlugins:
717+
- plugin: MyPlugin
718+
package: MyPackage
719+
```
720+
721+
Don't forget to add a package containing the plugin we need:
722+
723+
```yaml
724+
packages:
725+
MyPackage:
726+
url: https://github.com/MyPackage
727+
from: 1.3.0
728+
```
729+
698730
### Build Script
699731

700732
Run script build phases can be added at 3 different points in the build:
@@ -854,6 +886,7 @@ This is used to override settings or run build scripts in specific targets
854886
- [x] **targets**: **[String]** - The list of target names to include as target dependencies
855887
- [ ] **configFiles**: **[Config Files](#config-files)** - `.xcconfig` files per config
856888
- [ ] **settings**: **[Settings](#settings)** - Target specific build settings.
889+
- [ ] **buildToolPlugins**: **[[Build Tool Plug-ins](#build-tool-plug-ins)]** - Commands for the build system that run automatically *during* the build
857890
- [ ] **buildScripts**: **[[Build Script](#build-script)]** - Build scripts to run
858891
- [ ] **scheme**: **[Target Scheme](#target-scheme)** - Generated scheme
859892
- [ ] **attributes**: **[String: Any]** - This sets values in the project `TargetAttributes`. It is merged with `attributes` from the project and anything automatically added by XcodeGen, with any duplicate values being override by values specified here

Docs/Usage.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,20 @@ targets:
204204
dependencies:
205205
- framework: Vendor/MyFramework.framework
206206
```
207+
208+
# Build Tool Plug-ins
209+
XCodeGen supports working with [Swift Package Plug-ins](https://github.com/apple/swift-package-manager/blob/main/Documentation/Plugins.md#using-a-package-plugin).
210+
211+
To use plugins, you need to specify in your target which plugin you want to connect, and don't forget to connect the package to target.
212+
213+
```yaml
214+
packages:
215+
Prefire:
216+
url: https://github.com/BarredEwe/Prefire
217+
from: 1.3.0
218+
targets:
219+
App:
220+
buildToolPlugins:
221+
- plugin: PrefirePlaybookPlugin
222+
package: Prefire
223+
```

Package.resolved

Lines changed: 92 additions & 94 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ let package = Package(
1616
.package(url: "https://github.com/yonaskolb/JSONUtilities.git", from: "4.2.0"),
1717
.package(url: "https://github.com/kylef/Spectre.git", from: "0.9.2"),
1818
.package(url: "https://github.com/onevcat/Rainbow.git", from: "3.0.0"),
19-
.package(url: "https://github.com/tuist/XcodeProj.git", from: "8.12.0"),
19+
.package(url: "https://github.com/tuist/XcodeProj.git", from: "8.13.0"),
2020
.package(url: "https://github.com/jakeheis/SwiftCLI.git", from: "6.0.3"),
2121
.package(url: "https://github.com/mxcl/Version", from: "2.0.0"),
2222
.package(url: "https://github.com/SwiftDocOrg/GraphViz.git", exact: "0.2.0"),
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import Foundation
2+
import JSONUtilities
3+
4+
/// Specifies the use of a plug-in product in a target.
5+
public struct BuildToolPlugin: Equatable {
6+
7+
/// The name of the plug-in target.
8+
public var plugin: String
9+
/// The name of the package that defines the plug-in target.
10+
public var package: String
11+
12+
public init(
13+
plugin: String,
14+
package: String
15+
) {
16+
self.plugin = plugin
17+
self.package = package
18+
}
19+
}
20+
21+
extension BuildToolPlugin: JSONObjectConvertible {
22+
23+
public init(jsonDictionary: JSONDictionary) throws {
24+
if let plugin: String = jsonDictionary.json(atKeyPath: "plugin") {
25+
self.plugin = plugin
26+
} else {
27+
throw SpecParsingError.invalidDependency(jsonDictionary)
28+
}
29+
30+
if let package: String = jsonDictionary.json(atKeyPath: "package") {
31+
self.package = package
32+
} else {
33+
throw SpecParsingError.invalidDependency(jsonDictionary)
34+
}
35+
}
36+
}
37+
38+
extension BuildToolPlugin {
39+
public var uniqueID: String {
40+
return "\(plugin)/\(package)"
41+
}
42+
}
43+
44+
extension BuildToolPlugin: Hashable {
45+
public func hash(into hasher: inout Hasher) {
46+
hasher.combine(plugin)
47+
hasher.combine(package)
48+
}
49+
}
50+
51+
extension BuildToolPlugin: JSONEncodable {
52+
public func toJSONValue() -> Any {
53+
[
54+
"plugin": plugin,
55+
"package": package
56+
]
57+
}
58+
}

Sources/ProjectSpec/SpecValidation.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ extension Project {
174174
errors.append(.invalidTargetSource(target: target.name, source: sourcePath.string))
175175
}
176176
}
177+
178+
for buildToolPlugin in target.buildToolPlugins {
179+
if packages[buildToolPlugin.package] == nil {
180+
errors.append(.invalidPluginPackageReference(plugin: buildToolPlugin.plugin, package: buildToolPlugin.package))
181+
}
182+
}
177183
}
178184

179185
for projectReference in projectReferences {

Sources/ProjectSpec/SpecValidationError.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public struct SpecValidationError: Error, CustomStringConvertible {
3636
case invalidTestPlan(TestPlan)
3737
case multipleDefaultTestPlans
3838
case duplicateDependencies(target: String, dependencyReference: String)
39+
case invalidPluginPackageReference(plugin: String, package: String)
3940

4041
public var description: String {
4142
switch self {
@@ -91,6 +92,8 @@ public struct SpecValidationError: Error, CustomStringConvertible {
9192
return "Your test plans contain more than one default test plan"
9293
case let .duplicateDependencies(target, dependencyReference):
9394
return "Target \(target.quoted) has the dependency \(dependencyReference.quoted) multiple times"
95+
case let .invalidPluginPackageReference(plugin, package):
96+
return "Plugin \(plugin) has invalide package reference \(package)"
9497
}
9598
}
9699
}

0 commit comments

Comments
 (0)