Skip to content

Remove unused Codable conformances on PackageModel types #7687

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

Merged
merged 2 commits into from
Jun 20, 2024
Merged
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
2 changes: 1 addition & 1 deletion Sources/PackageModel/BuildConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//===----------------------------------------------------------------------===//

/// The configuration of the build environment.
public enum BuildConfiguration: String, CaseIterable, Codable, Sendable {
public enum BuildConfiguration: String, CaseIterable, Encodable, Sendable {
case debug
case release

Expand Down
2 changes: 1 addition & 1 deletion Sources/PackageModel/BuildEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//===----------------------------------------------------------------------===//

/// A build environment with which to evaluate conditions.
public struct BuildEnvironment: Codable {
public struct BuildEnvironment {
public let platform: Platform
public let configuration: BuildConfiguration?

Expand Down
24 changes: 6 additions & 18 deletions Sources/PackageModel/BuildSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/// Namespace for build settings.
public enum BuildSettings {
/// Build settings declarations.
public struct Declaration: Hashable, Codable {
public struct Declaration: Hashable {
// Swift.
public static let SWIFT_ACTIVE_COMPILATION_CONDITIONS: Declaration =
.init("SWIFT_ACTIVE_COMPILATION_CONDITIONS")
Expand All @@ -40,43 +40,31 @@ public enum BuildSettings {
}

/// An individual build setting assignment.
public struct Assignment: Codable, Equatable, Hashable {
public struct Assignment: Equatable, Hashable {
/// The assignment value.
public var values: [String]

// FIXME: This should use `Set` but we need to investigate potential build failures on Linux caused by using it.
/// The condition associated with this assignment.
public var conditions: [PackageCondition] {
get {
self._conditions.map(\.underlying)
}
set {
self._conditions = newValue.map { PackageConditionWrapper($0) }
}
}

private var _conditions: [PackageConditionWrapper]
public var conditions: [PackageCondition]

/// Indicates whether this assignment represents a default
/// that should be used only if no other assignments match.
public let `default`: Bool

public init(default: Bool = false) {
self._conditions = []
self.conditions = []
self.values = []
self.default = `default`
}

public init(values: [String] = [], conditions: [PackageCondition] = []) {
self._conditions = []
self.values = values
self.default = false // TODO(franz): Check again
self.conditions = conditions
}
}

/// Build setting assignment table which maps a build setting to a list of assignments.
public struct AssignmentTable: Codable {
public struct AssignmentTable {
public private(set) var assignments: [Declaration: [Assignment]]

public init() {
Expand All @@ -93,7 +81,7 @@ public enum BuildSettings {
/// Provides a view onto assignment table with a given set of bound parameters.
///
/// This class can be used to get the assignments matching the bound parameters.
public final class Scope {
public struct Scope {
/// The assignment table.
public let table: AssignmentTable

Expand Down
64 changes: 3 additions & 61 deletions Sources/PackageModel/Manifest/PackageConditionDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,64 +24,6 @@ public struct PackageConditionDescription: Codable, Hashable, Sendable {
}
}

/// A manifest condition.
public protocol PackageConditionProtocol: Codable {
func satisfies(_ environment: BuildEnvironment) -> Bool
}

/// Wrapper for package condition so it can conform to Codable.
struct PackageConditionWrapper: Codable, Equatable, Hashable {
var platform: PlatformsCondition?
var config: ConfigurationCondition?
var traits: TraitCondition?

@available(*, deprecated, renamed: "underlying")
var condition: PackageConditionProtocol {
if let platform {
return platform
} else if let config {
return config
} else {
fatalError("unreachable")
}
}

var underlying: PackageCondition {
if let platform {
return .platforms(platform)
} else if let config {
return .configuration(config)
} else if let traits {
return .traits(traits)
} else {
fatalError("unreachable")
}
}

@available(*, deprecated, message: "Use .init(_ condition: PackageCondition) instead")
init(_ condition: PackageConditionProtocol) {
switch condition {
case let platform as PlatformsCondition:
self.platform = platform
case let config as ConfigurationCondition:
self.config = config
default:
fatalError("unknown condition \(condition)")
}
}

init(_ condition: PackageCondition) {
switch condition {
case let .platforms(platforms):
self.platform = platforms
case let .configuration(configuration):
self.config = configuration
case .traits(let traits):
self.traits = traits
}
}
}

/// One of possible conditions used in package manifests to restrict targets from being built for certain platforms or
/// build configurations.
public enum PackageCondition: Hashable, Sendable {
Expand Down Expand Up @@ -134,7 +76,7 @@ public enum PackageCondition: Hashable, Sendable {
}

/// Platforms condition implies that an assignment is valid on these platforms.
public struct PlatformsCondition: PackageConditionProtocol, Hashable, Sendable {
public struct PlatformsCondition: Hashable, Sendable {
public let platforms: [Platform]

public init(platforms: [Platform]) {
Expand All @@ -149,7 +91,7 @@ public struct PlatformsCondition: PackageConditionProtocol, Hashable, Sendable {

/// A configuration condition implies that an assignment is valid on
/// a particular build configuration.
public struct ConfigurationCondition: PackageConditionProtocol, Hashable, Sendable {
public struct ConfigurationCondition: Hashable, Sendable {
public let configuration: BuildConfiguration

public init(configuration: BuildConfiguration) {
Expand All @@ -168,7 +110,7 @@ public struct ConfigurationCondition: PackageConditionProtocol, Hashable, Sendab

/// A configuration condition implies that an assignment is valid on
/// a particular build configuration.
public struct TraitCondition: PackageConditionProtocol, Hashable, Sendable {
public struct TraitCondition: Hashable, Sendable {
public let traits: Set<String>

public init(traits: Set<String>) {
Expand Down
6 changes: 2 additions & 4 deletions Sources/PackageModel/PackageModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import Basics
import struct Foundation.URL

import enum TSCUtility.PackageLocation
import struct TSCUtility.PolymorphicCodableArray
import struct TSCUtility.Version

/// The basic package representation.
Expand Down Expand Up @@ -45,7 +44,7 @@ import struct TSCUtility.Version
/// 5. A loaded package, as in #4, for which the targets have also been
/// loaded. There is not currently a data structure for this, but it is the
/// result after `PackageLoading.transmute()`.
public final class Package: Encodable {
public final class Package {
/// The identity of the package.
public let identity: PackageIdentity

Expand All @@ -56,7 +55,6 @@ public final class Package: Encodable {
public let path: AbsolutePath

/// The targets contained in the package.
@PolymorphicCodableArray
public var targets: [Target]

/// The products produced by the package.
Expand Down Expand Up @@ -84,7 +82,7 @@ public final class Package: Encodable {
self.identity = identity
self.manifest = manifest
self.path = path
self._targets = .init(wrappedValue: targets)
self.targets = targets
self.products = products
self.targetSearchPath = targetSearchPath
self.testTargetSearchPath = testTargetSearchPath
Expand Down
7 changes: 2 additions & 5 deletions Sources/PackageModel/Product.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@

import Basics

import struct TSCUtility.PolymorphicCodableArray

public class Product: Codable {
public class Product {
/// The name of the product.
public let name: String

Expand All @@ -28,7 +26,6 @@ public class Product: Codable {
///
/// This is never empty, and is only the targets which are required to be in
/// the product, but not necessarily their transitive dependencies.
@PolymorphicCodableArray
public var targets: [Target]

/// The path to test entry point file.
Expand All @@ -54,7 +51,7 @@ public class Product: Codable {
self.name = name
self.type = type
self.identity = package.description.lowercased() + "_" + name
self._targets = .init(wrappedValue: targets)
self.targets = targets
self.testEntryPointPath = testEntryPointPath
}
}
Expand Down
63 changes: 2 additions & 61 deletions Sources/PackageModel/Target/BinaryTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,36 +47,7 @@ public final class BinaryTarget: Target {
)
}

private enum CodingKeys: String, CodingKey {
case kind
case origin
case artifactSource // backwards compatibility 2/2021
}

public override func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.origin, forKey: .origin)
try container.encode(self.kind, forKey: .kind)
}

required public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
// backwards compatibility 2/2021
if !container.contains(.kind) {
self.kind = .xcframework
} else {
self.kind = try container.decode(Kind.self, forKey: .kind)
}
// backwards compatibility 2/2021
if container.contains(.artifactSource) {
self.origin = try container.decode(Origin.self, forKey: .artifactSource)
} else {
self.origin = try container.decode(Origin.self, forKey: .origin)
}
try super.init(from: decoder)
}

public enum Kind: String, RawRepresentable, Codable, CaseIterable {
public enum Kind: String, RawRepresentable, CaseIterable {
case xcframework
case artifactsArchive
case unknown // for non-downloaded artifacts
Expand All @@ -98,42 +69,12 @@ public final class BinaryTarget: Target {
return self.kind == .artifactsArchive
}

public enum Origin: Equatable, Codable {
public enum Origin: Equatable {

/// Represents an artifact that was downloaded from a remote URL.
case remote(url: String)

/// Represents an artifact that was available locally.
case local

private enum CodingKeys: String, CodingKey {
case remote, local
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .remote(let a1):
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .remote)
try unkeyedContainer.encode(a1)
case .local:
try container.encodeNil(forKey: .local)
}
}

public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
guard let key = values.allKeys.first(where: values.contains) else {
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Did not find a matching key"))
}
switch key {
case .remote:
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
let a1 = try unkeyedValues.decode(String.self)
self = .remote(url: a1)
case .local:
self = .local
}
}
}
}
26 changes: 0 additions & 26 deletions Sources/PackageModel/Target/ClangTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,30 +82,4 @@ public final class ClangTarget: Target {
usesUnsafeFlags: usesUnsafeFlags
)
}

private enum CodingKeys: String, CodingKey {
case includeDir, moduleMapType, headers, isCXX, cLanguageStandard, cxxLanguageStandard
}

public override func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(includeDir, forKey: .includeDir)
try container.encode(moduleMapType, forKey: .moduleMapType)
try container.encode(headers, forKey: .headers)
try container.encode(isCXX, forKey: .isCXX)
try container.encode(cLanguageStandard, forKey: .cLanguageStandard)
try container.encode(cxxLanguageStandard, forKey: .cxxLanguageStandard)
try super.encode(to: encoder)
}

required public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.includeDir = try container.decode(AbsolutePath.self, forKey: .includeDir)
self.moduleMapType = try container.decode(ModuleMapType.self, forKey: .moduleMapType)
self.headers = try container.decode([AbsolutePath].self, forKey: .headers)
self.isCXX = try container.decode(Bool.self, forKey: .isCXX)
self.cLanguageStandard = try container.decodeIfPresent(String.self, forKey: .cLanguageStandard)
self.cxxLanguageStandard = try container.decodeIfPresent(String.self, forKey: .cxxLanguageStandard)
try super.init(from: decoder)
}
}
Loading