Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build Tool Plugin Support #733

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Sources/XcodeProj/Objects/Project/PBXProject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public final class PBXProject: PBXObject {
public func addSwiftPackage(repositoryURL: String,
productName: String,
versionRequirement: XCRemoteSwiftPackageReference.VersionRequirement,
plugin: Bool = false,
targetName: String) throws -> XCRemoteSwiftPackageReference {
let objects = try self.objects()

Expand All @@ -190,6 +191,7 @@ public final class PBXProject: PBXObject {
// Product
let productDependency = try addSwiftPackageProduct(reference: reference,
productName: productName,
plugin: plugin,
target: target)

// Build file
Expand All @@ -212,6 +214,7 @@ public final class PBXProject: PBXObject {
/// - addFileReference: Include a file reference to the package (defaults to main group)
public func addLocalSwiftPackage(path: Path,
productName: String,
plugin: Bool = false,
targetName: String,
addFileReference: Bool = true) throws -> XCSwiftPackageProductDependency {
guard path.isRelative else { throw PBXProjError.pathIsAbsolute(path) }
Expand All @@ -223,6 +226,7 @@ public final class PBXProject: PBXObject {
// Product
let productDependency = try addLocalSwiftPackageProduct(path: path,
productName: productName,
plugin: plugin,
target: target)

// Build file
Expand Down Expand Up @@ -402,6 +406,7 @@ extension PBXProject {
/// Adds package product for remote Swift package
private func addSwiftPackageProduct(reference: XCRemoteSwiftPackageReference,
productName: String,
plugin: Bool,
target: PBXTarget) throws -> XCSwiftPackageProductDependency {
let objects = try self.objects()

Expand All @@ -410,7 +415,9 @@ extension PBXProject {
if let product = objects.swiftPackageProductDependencies.first(where: { $0.value.package == reference })?.value {
productDependency = product
} else {
productDependency = XCSwiftPackageProductDependency(productName: productName, package: reference)
productDependency = XCSwiftPackageProductDependency(productName: productName,
package: reference,
plugin: plugin)
objects.add(object: productDependency)
}
target.packageProductDependencies.append(productDependency)
Expand All @@ -421,6 +428,7 @@ extension PBXProject {
/// Adds package product for local Swift package
private func addLocalSwiftPackageProduct(path: Path,
productName: String,
plugin: Bool,
target: PBXTarget) throws -> XCSwiftPackageProductDependency {
let objects = try self.objects()

Expand All @@ -432,7 +440,7 @@ extension PBXProject {
}
productDependency = product.value
} else {
productDependency = XCSwiftPackageProductDependency(productName: productName)
productDependency = XCSwiftPackageProductDependency(productName: productName, plugin: plugin)
objects.add(object: productDependency)
}
target.packageProductDependencies.append(productDependency)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ public class XCSwiftPackageProductDependency: PBXContainerItem, PlistSerializabl
packageReference = newValue?.reference
}
}

let plugin: Bool

// MARK: - Init

public init(productName: String,
package: XCRemoteSwiftPackageReference? = nil) {
self.productName = productName
package: XCRemoteSwiftPackageReference? = nil,
plugin: Bool) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to make this change non source breaking, can I trouble you to default the plugin parameter value?

Suggested change
plugin: Bool) {
plugin: Bool = false) {

self.productName = "\(plugin ? "plugin:" : "")\(productName)"
packageReference = package?.reference
self.plugin = plugin
super.init()
}

Expand All @@ -37,6 +41,11 @@ public class XCSwiftPackageProductDependency: PBXContainerItem, PlistSerializabl
} else {
packageReference = nil
}
if let plugin = try container.decodeIfPresent(Bool.self, forKey: .plugin) {
self.plugin = plugin
} else {
plugin = false
}
Comment on lines +44 to +48
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if I followed along, there isn't a plugin attribute in the pbxproj file, rather the product name is prefixed with plugin: - in that case would this not require checking for that instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kwridan Good point! I just realized that I completely misunderstood what this initializer is for. My bad πŸ€¦β€β™‚οΈ

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no worries

try super.init(from: decoder)
}

Expand All @@ -57,6 +66,7 @@ public class XCSwiftPackageProductDependency: PBXContainerItem, PlistSerializabl
fileprivate enum CodingKeys: String, CodingKey {
case productName
case package
case plugin
}

override func isEqual(to object: Any?) -> Bool {
Expand Down
14 changes: 14 additions & 0 deletions Sources/XcodeProj/Utils/ReferenceGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ final class ReferenceGenerator: ReferenceGenerating {
identifiers.append($0.productName)
fixReference(for: $0, identifiers: identifiers)
}

// Build Tool Plugins
target.dependencies.forEach {
guard
let product = $0.product,
product.plugin
else {
return
}

var identifiers = identifiers
identifiers.append(product.productName)
fixReference(for: product, identifiers: identifiers)
}

fixReference(for: target, identifiers: identifiers)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ final class XCSwiftPackageProductDependencyTests: XCTestCase {
let proj = PBXProj()
let package = XCRemoteSwiftPackageReference(repositoryURL: "repository")
let subject = XCSwiftPackageProductDependency(productName: "product",
package: package)
package: package,
plugin: false)

// When
let got = try subject.plistKeyAndValue(proj: proj, reference: "reference")
Expand All @@ -43,9 +44,11 @@ final class XCSwiftPackageProductDependencyTests: XCTestCase {
// Given
let package = XCRemoteSwiftPackageReference(repositoryURL: "repository")
let first = XCSwiftPackageProductDependency(productName: "product",
package: package)
package: package,
plugin: false)
let second = XCSwiftPackageProductDependency(productName: "product",
package: package)
package: package,
plugin: false)

// Then
XCTAssertEqual(first, second)
Expand Down