Skip to content

Commit

Permalink
Removed 'isPlugin' calculation and changed to installation during ini…
Browse files Browse the repository at this point in the history
…tialization
  • Loading branch information
BarredEwe committed Jul 14, 2023
1 parent bd3882a commit b5a80ee
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,27 @@ public class XCSwiftPackageProductDependency: PBXContainerItem, PlistSerializabl
}

/// Is it a Plugin.
var isPlugin: Bool {
productName.hasPrefix("plugin:")
}
var isPlugin: Bool

// MARK: - Init

public init(productName: String,
package: XCRemoteSwiftPackageReference? = nil) {
package: XCRemoteSwiftPackageReference? = nil,
isPlugin: Bool = false) {
self.productName = productName
packageReference = package?.reference
self.isPlugin = isPlugin
super.init()
}

public required init(from decoder: Decoder) throws {
let objects = decoder.context.objects
let repository = decoder.context.objectReferenceRepository
let container = try decoder.container(keyedBy: CodingKeys.self)
productName = try container.decode(String.self, forKey: .productName)
let rawProductName = try container.decode(String.self, forKey: .productName)
productName = rawProductName.replacingOccurrences(of: "plugin:", with: "")
isPlugin = productName != rawProductName

if let packageString: String = try container.decodeIfPresent(.package) {
packageReference = repository.getOrCreate(reference: packageString, objects: objects)
} else {
Expand All @@ -51,7 +54,11 @@ public class XCSwiftPackageProductDependency: PBXContainerItem, PlistSerializabl
if let package = package {
dictionary["package"] = .string(.init(package.reference.value, comment: "XCRemoteSwiftPackageReference \"\(package.name ?? "")\""))
}
dictionary["productName"] = .string(.init(productName))
if isPlugin {
dictionary["productName"] = .string(.init("plugin:" + productName))
} else {
dictionary["productName"] = .string(.init(productName))
}

return (key: CommentedString(reference, comment: productName),
value: .dictionary(dictionary))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ final class XCSwiftPackageProductDependencyTests: XCTestCase {
// Then
XCTAssertEqual(got.productName, "xcodeproj")
XCTAssertEqual(got.packageReference?.value, "packageReference")
XCTAssertEqual(got.isPlugin, false)
}

func test_initAsPlugin() throws {
// Given
let decoder = XcodeprojPropertyListDecoder()
let encoder = PropertyListEncoder()
let plist = ["reference": "reference",
"productName": "plugin:xcodeproj",
"package": "packageReference"]
let data = try encoder.encode(plist)

// When
let got = try decoder.decode(XCSwiftPackageProductDependency.self, from: data)

// Then
XCTAssertEqual(got.productName, "xcodeproj")
XCTAssertEqual(got.packageReference?.value, "packageReference")
XCTAssertEqual(got.isPlugin, true)
}

func test_plistValues() throws {
Expand All @@ -39,6 +58,25 @@ final class XCSwiftPackageProductDependencyTests: XCTestCase {
]))
}

func test_plistValuesAsPlugin() throws {
// Given
let proj = PBXProj()
let package = XCRemoteSwiftPackageReference(repositoryURL: "repository")
let subject = XCSwiftPackageProductDependency(productName: "product",
package: package,
isPlugin: true)

// When
let got = try subject.plistKeyAndValue(proj: proj, reference: "reference")

// Then
XCTAssertEqual(got.value, .dictionary([
"isa": "XCSwiftPackageProductDependency",
"productName": "product",
"package": .string(.init(package.reference.value, comment: "XCRemoteSwiftPackageReference \"\(package.name ?? "")\"")),
]))
}

func test_equal() {
// Given
let package = XCRemoteSwiftPackageReference(repositoryURL: "repository")
Expand All @@ -53,7 +91,7 @@ final class XCSwiftPackageProductDependencyTests: XCTestCase {

func test_isPlugin() {
// Given
let plugin = XCSwiftPackageProductDependency(productName: "plugin:product")
let plugin = XCSwiftPackageProductDependency(productName: "product", isPlugin: true)

// Then
XCTAssertTrue(plugin.isPlugin)
Expand Down
2 changes: 1 addition & 1 deletion Tests/XcodeProjTests/Utils/ReferenceGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private extension PBXProj {

func makePluginDependency() -> PBXTargetDependency {
let packageReference = XCRemoteSwiftPackageReference(repositoryURL: "repository")
let packageDependency = XCSwiftPackageProductDependency(productName: "plugin:product", package: packageReference)
let packageDependency = XCSwiftPackageProductDependency(productName: "product", package: packageReference, isPlugin: true)
let targetDependency = PBXTargetDependency(product: packageDependency)
add(object: targetDependency.productReference!.getObject()!)

Expand Down

0 comments on commit b5a80ee

Please sign in to comment.