Skip to content

Commit eb990b6

Browse files
authored
Merge pull request #562 from mac-cain13/generators
Add --generators flag to executable
2 parents 66358ef + d639a1c commit eb990b6

File tree

3 files changed

+92
-15
lines changed

3 files changed

+92
-15
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

Sources/RswiftCore/RswiftCore.swift

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@
1010
import Foundation
1111
import XcodeEdit
1212

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

@@ -38,21 +54,46 @@ public struct RswiftCore {
3854
let resources = Resources(resourceURLs: resourceURLs, fileManager: FileManager.default)
3955
let infoPlistWhitelist = ["UIApplicationShortcutItems", "UISceneConfigurations", "NSUserActivityTypes", "NSExtension"]
4056

57+
var structGenerators: [StructGenerator] = []
58+
if callInformation.generators.contains(.image) {
59+
structGenerators.append(ImageStructGenerator(assetFolders: resources.assetFolders, images: resources.images))
60+
}
61+
if callInformation.generators.contains(.color) {
62+
structGenerators.append(ColorStructGenerator(assetFolders: resources.assetFolders))
63+
}
64+
if callInformation.generators.contains(.font) {
65+
structGenerators.append(FontStructGenerator(fonts: resources.fonts))
66+
}
67+
if callInformation.generators.contains(.segue) {
68+
structGenerators.append(SegueStructGenerator(storyboards: resources.storyboards))
69+
}
70+
if callInformation.generators.contains(.storyboard) {
71+
structGenerators.append(StoryboardStructGenerator(storyboards: resources.storyboards))
72+
}
73+
if callInformation.generators.contains(.nib) {
74+
structGenerators.append(NibStructGenerator(nibs: resources.nibs))
75+
}
76+
if callInformation.generators.contains(.reuseIdentifier) {
77+
structGenerators.append(ReuseIdentifierStructGenerator(reusables: resources.reusables))
78+
}
79+
if callInformation.generators.contains(.file) {
80+
structGenerators.append(ResourceFileStructGenerator(resourceFiles: resources.resourceFiles))
81+
}
82+
if callInformation.generators.contains(.string) {
83+
structGenerators.append(StringsStructGenerator(localizableStrings: resources.localizableStrings, developmentLanguage: xcodeproj.developmentLanguage))
84+
}
85+
if callInformation.generators.contains(.id) {
86+
structGenerators.append(AccessibilityIdentifierStructGenerator(nibs: resources.nibs, storyboards: resources.storyboards))
87+
}
88+
if callInformation.generators.contains(.info) {
89+
structGenerators.append(PropertyListGenerator(name: "info", plists: infoPlists, toplevelKeysWhitelist: infoPlistWhitelist))
90+
}
91+
if callInformation.generators.contains(.entitlements) {
92+
structGenerators.append(PropertyListGenerator(name: "entitlements", plists: entitlements, toplevelKeysWhitelist: nil))
93+
}
94+
4195
// Generate regular R file
42-
let fileContents = generateRegularFileContents(resources: resources, generators: [
43-
PropertyListGenerator(name: "info", plists: infoPlists, toplevelKeysWhitelist: infoPlistWhitelist),
44-
PropertyListGenerator(name: "entitlements", plists: entitlements, toplevelKeysWhitelist: nil),
45-
ImageStructGenerator(assetFolders: resources.assetFolders, images: resources.images),
46-
ColorStructGenerator(assetFolders: resources.assetFolders),
47-
FontStructGenerator(fonts: resources.fonts),
48-
SegueStructGenerator(storyboards: resources.storyboards),
49-
StoryboardStructGenerator(storyboards: resources.storyboards),
50-
NibStructGenerator(nibs: resources.nibs),
51-
ReuseIdentifierStructGenerator(reusables: resources.reusables),
52-
ResourceFileStructGenerator(resourceFiles: resources.resourceFiles),
53-
StringsStructGenerator(localizableStrings: resources.localizableStrings, developmentLanguage: xcodeproj.developmentLanguage),
54-
AccessibilityIdentifierStructGenerator(nibs: resources.nibs, storyboards: resources.storyboards),
55-
])
96+
let fileContents = generateRegularFileContents(resources: resources, generators: structGenerators)
5697
writeIfChanged(contents: fileContents, toURL: callInformation.outputURL)
5798

5899
// 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)