Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to hide generation header #1385

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ Inside a project/package that uses this command plugin, right-click the project
- `--help` - Display help information
- `--cacheBasePath` - Base path to the cache directory. Can be overriden by the config file.
- `--buildPath` - Path to directory used when building from .swifttemplate files. This defaults to system temp directory
- `--hideHeader` [default: false] - Stop adding the Sourcery header to the generated files.
- `--hideVersionHeader` [default: false] - Stop adding the Sourcery version to the generated files headers.

### Configuration file
Expand Down
25 changes: 18 additions & 7 deletions Sourcery/Sourcery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import XcodeProj
public class Sourcery {
public static let version: String = SourceryVersion.current.value
public static let generationMarker: String = "// Generated using Sourcery"
public let generationHeader: String
public let generationHeader: String?

enum Error: Swift.Error {
case containsMergeConflictMarkers
Expand All @@ -33,6 +33,7 @@ public class Sourcery {
fileprivate let buildPath: Path?
fileprivate let prune: Bool
fileprivate let serialParse: Bool
fileprivate let hideHeader: Bool
fileprivate let hideVersionHeader: Bool

fileprivate var status = ""
Expand All @@ -55,6 +56,7 @@ public class Sourcery {
buildPath: Path? = nil,
prune: Bool = false,
serialParse: Bool = false,
hideHeader: Bool = false,
hideVersionHeader: Bool = false,
arguments: [String: NSObject] = [:],
logConfiguration: Log.Configuration? = nil
Expand All @@ -67,6 +69,11 @@ public class Sourcery {
self.buildPath = buildPath
self.prune = prune
self.serialParse = serialParse
if let hideHeader = arguments["hideHeader"] {
self.hideHeader = (hideHeader as? NSNumber)?.boolValue == true
} else {
self.hideHeader = hideHeader
}
if let hideVersionHeader = arguments["hideVersionHeader"] {
self.hideVersionHeader = (hideVersionHeader as? NSNumber)?.boolValue == true
} else {
Expand All @@ -76,12 +83,16 @@ public class Sourcery {
Log.setup(using: logConfiguration)
}

var prefix = Sourcery.generationMarker
if !self.hideVersionHeader {
prefix += " \(Sourcery.version)"
if hideHeader {
self.generationHeader = nil
} else {
var prefix = Sourcery.generationMarker
if !self.hideVersionHeader {
prefix += " \(Sourcery.version)"
}
self.generationHeader = "\(prefix) — https://github.com/krzysztofzablocki/Sourcery\n"
+ "// DO NOT EDIT\n"
}
self.generationHeader = "\(prefix) — https://github.com/krzysztofzablocki/Sourcery\n"
+ "// DO NOT EDIT\n"
}

/// Processes source files and generates corresponding code.
Expand Down Expand Up @@ -638,7 +649,7 @@ extension Sourcery {
private func output(type: DryOutputType, result: String, to outputPath: Path) throws {
let resultIsEmpty = result.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
var result = result
if !resultIsEmpty, outputPath.extension == "swift" {
if let generationHeader, !resultIsEmpty, outputPath.extension == "swift" {
result = generationHeader + result
}

Expand Down
16 changes: 15 additions & 1 deletion SourceryTests/SourcerySpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ class SourcerySpecTests: QuickSpec {
}
}

context("with hide header enabled") {
beforeEach {
expect { try Sourcery(watcherEnabled: false, cacheDisabled: true, hideHeader: true).processFiles(.sources(Paths(include: [sourcePath])), usingTemplates: Paths(include: [templatePath]), output: output, baseIndentation: 0) }.toNot(throwError())
}
it("removes header from within generated template") {
let expectedResult = "// Line One"

let generatedPath = outputDir + Sourcery().generatedPath(for: templatePath)

let result = try? generatedPath.read(.utf8)
expect(result?.withoutWhitespaces).to(equal(expectedResult.withoutWhitespaces))
}
}

it("does not remove code from within generated template when missing origin") {
update(code: """
class Foo {
Expand Down Expand Up @@ -1254,7 +1268,7 @@ class SourcerySpecTests: QuickSpec {
updateTemplate(code: "Found {{ types.all.count }} Types")

let result: () -> String? = { (try? (outputDir + Sourcery().generatedPath(for: tmpTemplate)).read(.utf8)) }
expect(result()).toEventually(contain("\(sourcery.generationHeader)Found 3 Types"))
expect(result()).toEventually(contain("\(sourcery.generationHeader ?? "")Found 3 Types"))

_ = watcher
}
Expand Down
Loading