Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 16 additions & 5 deletions Sources/SWBCore/ProjectModel/PlatformFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ public final class PlatformFilter: ProjectModelItem, Hashable, Codable {
/// The name of the platform, as defined in the LC_BUILD_VERSION info. e.g. macos, ios, etc...
public let platform: String

/// Invert the filter
public let exclude: Bool

/// The name of the environment, as defined in the LC_BUILD_VERSION info. e.g. simulator, maccatalyst, etc...
public let environment: String

/// Initializes a new filter instance.
public init(platform: String, environment: String = "") {
public init(platform: String, exclude: Bool = false, environment: String = "") {
self.platform = platform
self.exclude = exclude

// Rev-lock until we update the LLVM target environment to use maccatalyst
self.environment = environment == "maccatalyst" ? "macabi" : environment
Expand All @@ -38,6 +42,7 @@ public final class PlatformFilter: ProjectModelItem, Hashable, Codable {
convenience init(fromDictionary pifDict: ProjectModelItemPIF, withPIFLoader pifLoader: PIFLoader) throws {
try self.init(
platform: Self.parseValueForKeyAsString("platform", pifDict: pifDict),
exclude: Self.parseValueForKeyAsBool("exclude", pifDict: pifDict, defaultValue: false),
environment: Self.parseOptionalValueForKeyAsString("environment", pifDict: pifDict) ?? ""
)
}
Expand All @@ -52,7 +57,11 @@ public final class PlatformFilter: ProjectModelItem, Hashable, Codable {
}

public static func ==(lhs: PlatformFilter, rhs: PlatformFilter) -> Bool {
Comment thread
dschaefer2 marked this conversation as resolved.
return lhs.platform == rhs.platform && lhs.environment == rhs.environment
if lhs.exclude != rhs.exclude {
return lhs.platform != rhs.platform || lhs.environment != rhs.environment
} else {
return lhs.platform == rhs.platform && lhs.environment == rhs.environment
}
}
}

Expand All @@ -62,19 +71,21 @@ extension PlatformFilter: Comparable {
}

public var comparisonString: String {
return platform + (!environment.isEmpty ? "-\(environment)" : "")
return (exclude ? "!" : "") + platform + (!environment.isEmpty ? "-\(environment)" : "")
}
}

extension PlatformFilter {
static func fromBuildConditionParameterString(_ string: String) -> Set<PlatformFilter> {
return Set(string.components(separatedBy: ";").compactMap {
let parameters = $0.components(separatedBy: "-")
let exclude = parameters[0].first == "!"
Comment thread
jakepetroules marked this conversation as resolved.
let platform = exclude ? String(parameters[0].dropFirst(1)) : parameters[0]
switch parameters.count {
case 1:
return PlatformFilter(platform: parameters[0])
return PlatformFilter(platform: platform, exclude: exclude)
case 2:
return PlatformFilter(platform: parameters[0], environment: parameters[1])
return PlatformFilter(platform: platform, exclude: exclude, environment: parameters[1])
default:
return nil
}
Expand Down
9 changes: 8 additions & 1 deletion Sources/SwiftBuild/ProjectModel/PlatformFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import SWBProtocol
extension ProjectModel {
public struct PlatformFilter: Hashable, Sendable, Comparable {
public var platform: String
public var exclude: Bool?
public var environment: String

public init(platform: String, environment: String = "") {
public init(platform: String, exclude: Bool? = nil, environment: String = "") {
self.platform = platform
self.exclude = exclude
self.environment = environment
}

Expand All @@ -33,19 +35,24 @@ extension ProjectModel.PlatformFilter: Codable {
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.platform = try container.decode(String.self, forKey: .platform)
self.exclude = try container.decodeIfPresent(Bool.self, forKey: .exclude) ?? false
self.environment = try container.decodeIfPresent(String.self, forKey: .environment) ?? ""
}

public func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.platform, forKey: .platform)
if let exclude {
Comment thread
dschaefer2 marked this conversation as resolved.
Outdated
try container.encode(exclude ? "true" : "false", forKey: .exclude)
}
if !self.environment.isEmpty {
try container.encode(self.environment, forKey: .environment)
}
}

enum CodingKeys: String, CodingKey {
case platform
case exclude
case environment
}
}
Loading