Skip to content

Commit fc3f02d

Browse files
authored
Merge pull request #809 from jimmya/feature/allow-omiting-of-main-let
Allow main let to not be generated by providing omit-main-let flag
2 parents 62a6b01 + a5b3662 commit fc3f02d

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

Sources/rswift/App.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ struct GlobalOptions: ParsableArguments {
4949
@Option(help: "Source of default bundle to use")
5050
var bundleSource: BundleSource = .finder
5151

52+
@Flag(help: "Don't generate main let")
53+
var omitMainLet = false
54+
5255
// MARK: Project specific - Environment variable overrides
5356

5457
@Option(help: "Override environment variable \(EnvironmentKeys.targetName)")
@@ -103,6 +106,7 @@ extension App {
103106
productModuleName: productModuleName,
104107
infoPlistFile: infoPlistFile.map(URL.init(fileURLWithPath:)),
105108
codeSignEntitlements: codeSignEntitlements.map(URL.init(fileURLWithPath:)),
109+
omitMainLet: globals.omitMainLet,
106110
rswiftIgnoreURL: rswiftIgnoreURL,
107111
sourceTreeURLs: sourceTreeURLs
108112
)

Sources/rswift/RswiftCore.swift

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public struct RswiftCore {
3535
let productModuleName: String?
3636
let infoPlistFile: URL?
3737
let codeSignEntitlements: URL?
38+
let omitMainLet: Bool
3839

3940
let sourceTreeURLs: SourceTreeURLs
4041

@@ -49,6 +50,7 @@ public struct RswiftCore {
4950
productModuleName: String?,
5051
infoPlistFile: URL?,
5152
codeSignEntitlements: URL?,
53+
omitMainLet: Bool,
5254
rswiftIgnoreURL: URL,
5355
sourceTreeURLs: SourceTreeURLs
5456
) {
@@ -60,6 +62,7 @@ public struct RswiftCore {
6062
self.productModuleName = productModuleName
6163
self.infoPlistFile = infoPlistFile
6264
self.codeSignEntitlements = codeSignEntitlements
65+
self.omitMainLet = omitMainLet
6366

6467
self.rswiftIgnoreURL = rswiftIgnoreURL
6568

@@ -295,29 +298,28 @@ public struct RswiftCore {
295298
.map { "import \($0)" }
296299
.joined(separator: "\n")
297300

298-
let mainLet: String
299-
switch bundleSource {
300-
case .module:
301+
let mainLet: String?
302+
switch (bundleSource, omitMainLet) {
303+
case (.module, false):
301304
mainLet = "\(accessLevel == .publicLevel ? "public " : "")let R = _R(bundle: Bundle.module)"
302-
case .finder:
305+
case (.finder, false):
303306
mainLet = """
304-
private class BundleFinder {}
305-
\(accessLevel == .publicLevel ? "public " : "")let R = _R(bundle: Bundle(for: BundleFinder.self))
306-
"""
307+
private class BundleFinder {}
308+
\(accessLevel == .publicLevel ? "public " : "")let R = _R(bundle: Bundle(for: BundleFinder.self))
309+
"""
310+
default:
311+
mainLet = nil
307312
}
308313

309314
let str = s.prettyPrint()
315+
let body = [imports, mainLet, str].compactMap { $0 }.joined(separator: "\n\n")
310316
let code = """
311317
//
312318
// This is a generated file, do not edit!
313319
// Generated by R.swift, see https://github.com/mac-cain13/R.swift
314320
//
315321
316-
\(imports)
317-
318-
\(mainLet)
319-
320-
\(str)
322+
\(body)
321323
"""
322324

323325
try writeIfChanged(contents: code, toURL: outputURL)

0 commit comments

Comments
 (0)