Skip to content

Commit 6d35eca

Browse files
committed
Merge branch 'master' into xcode_plugin_support
* master: Follow-up changes to #699 Added schemes_arguments to configuration (#699) Add `--retain-encodable-properties` option. Closes #736 # Conflicts: # Sources/XcodeSupport/XcodeProjectDriver.swift
2 parents 1e1db48 + 115df72 commit 6d35eca

File tree

14 files changed

+128
-34
lines changed

14 files changed

+128
-34
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
##### Enhancements
88

99
- Unused import detection is now enabled by default.
10+
- Added the `--retain-encodable-properties` option to retain all properties on `Encodable` types only.
11+
- Added the `--xcode-list-arguments` option to pass additional arguments to `xcodebuild -list`.
1012

1113
##### Bug Fixes
1214

Sources/Frontend/Commands/ScanCommand.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,12 @@ struct ScanCommand: FrontendCommand {
8787
@Flag(help: "Retain SwiftUI previews")
8888
var retainSwiftUIPreviews: Bool = defaultConfiguration.$retainSwiftUIPreviews.defaultValue
8989

90-
@Flag(help: "Retain properties on Codable types")
90+
@Flag(help: "Retain properties on Codable types (including Encodable and Decodable)")
9191
var retainCodableProperties: Bool = defaultConfiguration.$retainCodableProperties.defaultValue
9292

93+
@Flag(help: "Retain properties on Encodable types only")
94+
var retainEncodableProperties: Bool = defaultConfiguration.$retainEncodableProperties.defaultValue
95+
9396
@Flag(help: "Automatically remove code that can be done so safely without introducing build errors (experimental)")
9497
var autoRemove: Bool = defaultConfiguration.$autoRemove.defaultValue
9598

@@ -169,6 +172,7 @@ struct ScanCommand: FrontendCommand {
169172
configuration.apply(\.$buildArguments, buildArguments)
170173
configuration.apply(\.$relativeResults, relativeResults)
171174
configuration.apply(\.$retainCodableProperties, retainCodableProperties)
175+
configuration.apply(\.$retainEncodableProperties, retainEncodableProperties)
172176
configuration.apply(\.$jsonPackageManifestPath, jsonPackageManifestPath)
173177

174178
try scanBehavior.main { project in

Sources/PeripheryKit/SourceGraph/Mutators/CodablePropertyRetainer.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,23 @@ final class CodablePropertyRetainer: SourceGraphMutator {
1111
}
1212

1313
func mutate() {
14-
guard configuration.retainCodableProperties else { return }
14+
if configuration.retainCodableProperties {
15+
for decl in graph.declarations(ofKinds: Declaration.Kind.discreteConformableKinds) {
16+
guard graph.isCodable(decl) else { continue }
1517

16-
for decl in graph.declarations(ofKinds: Declaration.Kind.discreteConformableKinds) {
17-
guard graph.isCodable(decl) else { continue }
18+
for decl in decl.declarations {
19+
guard decl.kind == .varInstance else { continue }
20+
graph.markRetained(decl)
21+
}
22+
}
23+
} else if configuration.retainEncodableProperties {
24+
for decl in graph.declarations(ofKinds: Declaration.Kind.discreteConformableKinds) {
25+
guard graph.isEncodable(decl) else { continue }
1826

19-
for decl in decl.declarations {
20-
guard decl.kind == .varInstance else { continue }
21-
graph.markRetained(decl)
27+
for decl in decl.declarations {
28+
guard decl.kind == .varInstance else { continue }
29+
graph.markRetained(decl)
30+
}
2231
}
2332
}
2433
}

Sources/PeripheryKit/SourceGraph/SourceGraph.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,4 +362,13 @@ public final class SourceGraph {
362362
return [.protocol, .typealias].contains($0.kind) && codableTypes.contains(name)
363363
}
364364
}
365+
366+
func isEncodable(_ decl: Declaration) -> Bool {
367+
let encodableTypes = ["Encodable"] + configuration.externalEncodableProtocols + configuration.externalCodableProtocols
368+
369+
return inheritedTypeReferences(of: decl).contains {
370+
guard let name = $0.name else { return false }
371+
return [.protocol, .typealias].contains($0.kind) && encodableTypes.contains(name)
372+
}
373+
}
365374
}

Sources/Shared/Configuration.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public final class Configuration {
4141
@Setting(key: "build_arguments", defaultValue: [])
4242
public var buildArguments: [String]
4343

44+
@Setting(key: "xcode_list_arguments", defaultValue: [])
45+
public var xcodeListArguments: [String]
46+
4447
@Setting(key: "retain_assign_only_property_types", defaultValue: [], valueSanitizer: PropertyTypeSanitizer.sanitize)
4548
public var retainAssignOnlyPropertyTypes: [String]
4649

@@ -83,6 +86,9 @@ public final class Configuration {
8386
@Setting(key: "retain_codable_properties", defaultValue: false)
8487
public var retainCodableProperties: Bool
8588

89+
@Setting(key: "retain_encodable_properties", defaultValue: false)
90+
public var retainEncodableProperties: Bool
91+
8692
@Setting(key: "auto_remove", defaultValue: false)
8793
public var autoRemove: Bool
8894

@@ -254,13 +260,21 @@ public final class Configuration {
254260
config[$buildArguments.key] = buildArguments
255261
}
256262

263+
if $xcodeListArguments.hasNonDefaultValue {
264+
config[$xcodeListArguments.key] = xcodeListArguments
265+
}
266+
257267
if $relativeResults.hasNonDefaultValue {
258268
config[$relativeResults.key] = relativeResults
259269
}
260270

261271
if $retainCodableProperties.hasNonDefaultValue {
262272
config[$retainCodableProperties.key] = retainCodableProperties
263273
}
274+
275+
if $retainEncodableProperties.hasNonDefaultValue {
276+
config[$retainEncodableProperties.key] = retainEncodableProperties
277+
}
264278

265279
if $jsonPackageManifestPath.hasNonDefaultValue {
266280
config[$jsonPackageManifestPath.key] = jsonPackageManifestPath
@@ -349,10 +363,14 @@ public final class Configuration {
349363
$cleanBuild.assign(value)
350364
case $buildArguments.key:
351365
$buildArguments.assign(value)
366+
case $xcodeListArguments.key:
367+
$xcodeListArguments.assign(value)
352368
case $relativeResults.key:
353369
$relativeResults.assign(value)
354370
case $retainCodableProperties.key:
355371
$retainCodableProperties.assign(value)
372+
case $retainEncodableProperties.key:
373+
$retainEncodableProperties.assign(value)
356374
case $jsonPackageManifestPath.key:
357375
$jsonPackageManifestPath.assign(value)
358376
default:
@@ -394,8 +412,10 @@ public final class Configuration {
394412
$skipSchemesValidation.reset()
395413
$cleanBuild.reset()
396414
$buildArguments.reset()
415+
$xcodeListArguments.reset()
397416
$relativeResults.reset()
398417
$retainCodableProperties.reset()
418+
$retainEncodableProperties.reset()
399419
$jsonPackageManifestPath.reset()
400420
}
401421

Sources/XcodeSupport/XcodeProject.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ final class XcodeProject: XcodeProjectlike {
105105
}
106106
}
107107

108-
func schemes() throws -> Set<String> {
109-
try xcodebuild.schemes(project: self)
108+
func schemes(additionalArguments: [String]) throws -> Set<String> {
109+
try xcodebuild.schemes(project: self, additionalArguments: additionalArguments)
110110
}
111111
}
112112

Sources/XcodeSupport/XcodeProjectDriver.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ public final class XcodeProjectDriver {
5252
schemes = Set(configuration.schemes)
5353
} else {
5454
// Ensure schemes exist within the project
55-
schemes = try project.schemes().filter { configuration.schemes.contains($0) }
55+
schemes = try project.schemes(
56+
additionalArguments: configuration.xcodeListArguments
57+
).filter { configuration.schemes.contains($0) }
5658
let validSchemeNames = schemes.mapSet { $0 }
5759

5860
if let scheme = Set(configuration.schemes).subtracting(validSchemeNames).first {

Sources/XcodeSupport/XcodeProjectSetupGuide.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public final class XcodeProjectSetupGuide: SetupGuideHelpers, ProjectSetupGuide
4343
print(colorize("Select build targets to analyze:", .bold))
4444
configuration.targets = select(multiple: targets, allowAll: true).selectedValues
4545

46-
let schemes = try filter(project.schemes(), project).map { $0 }.sorted()
46+
let schemes = try filter(
47+
project.schemes(additionalArguments: configuration.xcodeListArguments),
48+
project
49+
).map { $0 }.sorted()
4750

4851
print(colorize("\nSelect the schemes necessary to build your chosen targets:", .bold))
4952
configuration.schemes = select(multiple: schemes, allowAll: false).selectedValues
@@ -82,7 +85,11 @@ public final class XcodeProjectSetupGuide: SetupGuideHelpers, ProjectSetupGuide
8285
private func getPodSchemes(in project: XcodeProjectlike) throws -> Set<String> {
8386
let path = project.sourceRoot.appending("Pods/Pods.xcodeproj")
8487
guard path.exists else { return [] }
85-
return try xcodebuild.schemes(type: "project", path: path.lexicallyNormalized().string)
88+
return try xcodebuild.schemes(
89+
type: "project",
90+
path: path.lexicallyNormalized().string,
91+
additionalArguments: configuration.xcodeListArguments
92+
)
8693
}
8794

8895
private func filter(_ schemes: Set<String>, _ project: XcodeProjectlike) throws -> [String] {

Sources/XcodeSupport/XcodeProjectlike.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ protocol XcodeProjectlike: AnyObject {
1010
var name: String { get }
1111
var sourceRoot: FilePath { get }
1212

13-
func schemes() throws -> Set<String>
13+
func schemes(additionalArguments: [String]) throws -> Set<String>
1414
}
1515

1616
extension XcodeProjectlike {

Sources/XcodeSupport/XcodeWorkspace.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ final class XcodeWorkspace: XcodeProjectlike {
4242
}
4343
}
4444

45-
func schemes() throws -> Set<String> {
46-
try xcodebuild.schemes(project: self)
45+
func schemes(additionalArguments: [String]) throws -> Set<String> {
46+
try xcodebuild.schemes(project: self, additionalArguments: additionalArguments)
4747
}
4848

4949
// MARK: - Private

0 commit comments

Comments
 (0)