Skip to content

Commit 0e7eb90

Browse files
committed
Add --generator flag to executable, optionally configure which generators to run
1 parent 9f07e32 commit 0e7eb90

File tree

4 files changed

+123
-13
lines changed

4 files changed

+123
-13
lines changed

Sources/RswiftCore/CallInformation.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public struct CallInformation {
1515
let uiTestOutputURL: URL?
1616
let rswiftIgnoreURL: URL
1717

18+
let generators: [Generator]
1819
let accessLevel: AccessLevel
1920
let imports: [Module]
2021

@@ -38,6 +39,7 @@ public struct CallInformation {
3839
uiTestOutputURL: URL?,
3940
rswiftIgnoreURL: URL,
4041

42+
generators: [Generator],
4143
accessLevel: AccessLevel,
4244
imports: [Module],
4345

@@ -62,6 +64,7 @@ public struct CallInformation {
6264

6365
self.accessLevel = accessLevel
6466
self.imports = imports
67+
self.generators = generators
6568

6669
self.xcodeprojURL = xcodeprojURL
6770
self.targetName = targetName
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Generators.swift
3+
// Commander
4+
//
5+
// Created by Tom Lokhorst on 2019-09-21.
6+
//
7+
8+
import Foundation
9+
10+
public enum Generator: String, CaseIterable {
11+
case image
12+
case color
13+
case font
14+
case segue
15+
case storyboard
16+
case nib
17+
case reuseIdentifier
18+
case file
19+
case string
20+
case id
21+
22+
static func parseGenerators(_ string: String) -> ([Generator], [String]) {
23+
var generators: [Generator] = []
24+
var unknowns: [String] = []
25+
26+
let parts = string.components(separatedBy: ",")
27+
.map { $0.trimmingCharacters(in: CharacterSet.whitespaces) }
28+
29+
for part in parts {
30+
if let generator = Generator(rawValue: part) {
31+
generators.append(generator)
32+
} else {
33+
unknowns.append(part)
34+
}
35+
}
36+
37+
return (generators, unknowns)
38+
}
39+
}

Sources/RswiftCore/RswiftCore.swift

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@
1010
import Foundation
1111
import XcodeEdit
1212

13+
public typealias RswiftGenerator = Generator
14+
public enum Generator: String, CaseIterable {
15+
case image
16+
case color
17+
case font
18+
case segue
19+
case storyboard
20+
case nib
21+
case reuseIdentifier
22+
case file
23+
case string
24+
case id
25+
}
26+
1327
public struct RswiftCore {
1428
private let callInformation: CallInformation
1529

@@ -29,19 +43,40 @@ public struct RswiftCore {
2943

3044
let resources = Resources(resourceURLs: resourceURLs, fileManager: FileManager.default)
3145

46+
var structGenerators: [StructGenerator] = []
47+
if callInformation.generators.contains(.image) {
48+
structGenerators.append(ImageStructGenerator(assetFolders: resources.assetFolders, images: resources.images))
49+
}
50+
if callInformation.generators.contains(.color) {
51+
structGenerators.append(ColorStructGenerator(assetFolders: resources.assetFolders))
52+
}
53+
if callInformation.generators.contains(.font) {
54+
structGenerators.append(FontStructGenerator(fonts: resources.fonts))
55+
}
56+
if callInformation.generators.contains(.segue) {
57+
structGenerators.append(SegueStructGenerator(storyboards: resources.storyboards))
58+
}
59+
if callInformation.generators.contains(.storyboard) {
60+
structGenerators.append(StoryboardStructGenerator(storyboards: resources.storyboards))
61+
}
62+
if callInformation.generators.contains(.nib) {
63+
structGenerators.append(NibStructGenerator(nibs: resources.nibs))
64+
}
65+
if callInformation.generators.contains(.reuseIdentifier) {
66+
structGenerators.append(ReuseIdentifierStructGenerator(reusables: resources.reusables))
67+
}
68+
if callInformation.generators.contains(.file) {
69+
structGenerators.append(ResourceFileStructGenerator(resourceFiles: resources.resourceFiles))
70+
}
71+
if callInformation.generators.contains(.string) {
72+
structGenerators.append(StringsStructGenerator(localizableStrings: resources.localizableStrings, developmentLanguage: xcodeproj.developmentLanguage))
73+
}
74+
if callInformation.generators.contains(.id) {
75+
structGenerators.append(AccessibilityIdentifierStructGenerator(nibs: resources.nibs, storyboards: resources.storyboards))
76+
}
77+
3278
// Generate regular R file
33-
let fileContents = generateRegularFileContents(resources: resources, generators: [
34-
ImageStructGenerator(assetFolders: resources.assetFolders, images: resources.images),
35-
ColorStructGenerator(assetFolders: resources.assetFolders),
36-
FontStructGenerator(fonts: resources.fonts),
37-
SegueStructGenerator(storyboards: resources.storyboards),
38-
StoryboardStructGenerator(storyboards: resources.storyboards),
39-
NibStructGenerator(nibs: resources.nibs),
40-
ReuseIdentifierStructGenerator(reusables: resources.reusables),
41-
ResourceFileStructGenerator(resourceFiles: resources.resourceFiles),
42-
StringsStructGenerator(localizableStrings: resources.localizableStrings, developmentLanguage: xcodeproj.developmentLanguage),
43-
AccessibilityIdentifierStructGenerator(nibs: resources.nibs, storyboards: resources.storyboards),
44-
])
79+
let fileContents = generateRegularFileContents(resources: resources, generators: structGenerators)
4580
writeIfChanged(contents: fileContents, toURL: callInformation.outputURL)
4681

4782
// Generate UITest R file

Sources/rswift/main.swift

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ struct EnvironmentKeys {
6565

6666
// Options grouped in struct for readability
6767
struct CommanderOptions {
68+
static let generators = Option("generators", default: "", description: "Only run specified generators, comma seperated")
6869
static let uiTest = Option("generateUITestFile", default: "", description: "Output path for an extra generated file that contains resources commonly used in UI tests such as accessibility identifiers")
6970
static let importModules = Option("import", default: "", description: "Add extra modules as import in the generated file, comma seperated")
7071
static let accessLevel = Option("accessLevel", default: AccessLevel.internalLevel, description: "The access level [public|internal] to use for the generated R-file")
@@ -77,15 +78,35 @@ struct CommanderArguments {
7778
static let outputPath = Argument<String>("outputPath", description: "Output path for the generated file")
7879
}
7980

81+
func parseGenerators(_ string: String) -> ([RswiftGenerator], [String]) {
82+
var generators: [Generator] = []
83+
var unknowns: [String] = []
84+
85+
let parts = string.components(separatedBy: ",")
86+
.map { $0.trimmingCharacters(in: CharacterSet.whitespaces) }
87+
.filter { !$0.isEmpty }
88+
89+
for part in parts {
90+
if let generator = RswiftGenerator(rawValue: part) {
91+
generators.append(generator)
92+
} else {
93+
unknowns.append(part)
94+
}
95+
}
96+
97+
return (generators, unknowns)
98+
}
99+
80100
let generate = command(
101+
CommanderOptions.generators,
81102
CommanderOptions.uiTest,
82103
CommanderOptions.importModules,
83104
CommanderOptions.accessLevel,
84105
CommanderOptions.rswiftIgnore,
85106
CommanderOptions.inputOutputFilesValidation,
86107

87108
CommanderArguments.outputPath
88-
) { uiTestOutputPath, importModules, accessLevel, rswiftIgnore, inputOutputFilesValidation, outputPath in
109+
) { generatorNames, uiTestOutputPath, importModules, accessLevel, rswiftIgnore, inputOutputFilesValidation, outputPath in
89110

90111
let processInfo = ProcessInfo()
91112

@@ -113,6 +134,16 @@ let generate = command(
113134
let outputURL = URL(fileURLWithPath: outputPath)
114135
let uiTestOutputURL = uiTestOutputPath.count > 0 ? URL(fileURLWithPath: uiTestOutputPath) : nil
115136
let rswiftIgnoreURL = URL(fileURLWithPath: sourceRootPath).appendingPathComponent(rswiftIgnore, isDirectory: false)
137+
138+
let (knownGenerators, unknownGenerators) = parseGenerators(generatorNames)
139+
if !unknownGenerators.isEmpty {
140+
warn("Unknown generator options: \(unknownGenerators.joined(separator: ", "))")
141+
if knownGenerators.isEmpty {
142+
warn("No known generators, falling back to all generators")
143+
}
144+
}
145+
let generators = knownGenerators.isEmpty ? RswiftGenerator.allCases : knownGenerators
146+
116147
let modules = importModules
117148
.components(separatedBy: ",")
118149
.map { $0.trimmingCharacters(in: CharacterSet.whitespaces) }
@@ -164,6 +195,7 @@ let generate = command(
164195
uiTestOutputURL: uiTestOutputURL,
165196
rswiftIgnoreURL: rswiftIgnoreURL,
166197

198+
generators: generators,
167199
accessLevel: accessLevel,
168200
imports: modules,
169201

@@ -186,6 +218,7 @@ let generate = command(
186218
try RswiftCore(callInformation).run()
187219
}
188220

221+
189222
// Start parsing the launch arguments
190223
let group = Group()
191224
group.addCommand("generate", "Generates R.generated.swift file", generate)

0 commit comments

Comments
 (0)