Skip to content

Commit

Permalink
Remove Sourcery version from header (#1309)
Browse files Browse the repository at this point in the history
* Remove version from headers

* Remove from generated files

* Revert "Remove from generated files"

This reverts commit 9a8c14e.

* Revert "Remove version from headers"

This reverts commit 93ddd54.

* Add support to hide header via config

* Remove redundant self

* Make non-lazy
  • Loading branch information
dcacenabes committed Mar 26, 2024
1 parent 7a12709 commit 33be2ab
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
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
- `--hideVersionHeader` [default: false] - Stop adding the Sourcery version to the generated files headers.

### Configuration file

Expand Down
16 changes: 12 additions & 4 deletions Sourcery/Sourcery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import XcodeProj
public class Sourcery {
public static let version: String = SourceryVersion.current.value
public static let generationMarker: String = "// Generated using Sourcery"
public static let generationHeader = "\(Sourcery.generationMarker) \(Sourcery.version) — https://github.com/krzysztofzablocki/Sourcery\n"
+ "// DO NOT EDIT\n"
public let generationHeader: String

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

fileprivate var status = ""
fileprivate var templatesPaths = Paths(include: [])
Expand All @@ -47,7 +47,7 @@ public class Sourcery {
fileprivate var fileAnnotatedContent: [Path: [String]] = [:]

/// Creates Sourcery processor
public init(verbose: Bool = false, watcherEnabled: Bool = false, cacheDisabled: Bool = false, cacheBasePath: Path? = nil, buildPath: Path? = nil, prune: Bool = false, serialParse: Bool = false, arguments: [String: NSObject] = [:]) {
public init(verbose: Bool = false, watcherEnabled: Bool = false, cacheDisabled: Bool = false, cacheBasePath: Path? = nil, buildPath: Path? = nil, prune: Bool = false, serialParse: Bool = false, hideVersionHeader: Bool = false, arguments: [String: NSObject] = [:]) {
self.verbose = verbose
self.arguments = arguments
self.watcherEnabled = watcherEnabled
Expand All @@ -56,6 +56,14 @@ public class Sourcery {
self.buildPath = buildPath
self.prune = prune
self.serialParse = serialParse
self.hideVersionHeader = hideVersionHeader

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

/// Processes source files and generates corresponding code.
Expand Down Expand Up @@ -613,7 +621,7 @@ extension Sourcery {
let resultIsEmpty = result.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
var result = result
if !resultIsEmpty, outputPath.extension == "swift" {
result = Sourcery.generationHeader + result
result = generationHeader + result
}

if isDryRun {
Expand Down
12 changes: 8 additions & 4 deletions SourceryExecutable/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ func runCLI() {
"""),
Option<Path>("ejsPath", default: "", description: "Path to EJS file for JavaScript templates."),
Option<Path>("cacheBasePath", default: "", description: "Base path to Sourcery's cache directory"),
Option<Path>("buildPath", default: "", description: "Sets a custom build path")
) { watcherEnabled, disableCache, verboseLogging, logAST, logBenchmark, parseDocumentation, quiet, prune, serialParse, sources, excludeSources, templates, excludeTemplates, output, isDryRun, configPaths, forceParse, baseIndentation, args, ejsPath, cacheBasePath, buildPath in
Option<Path>("buildPath", default: "", description: "Sets a custom build path"),
Flag("hideVersionHeader", description: "Do not include Sourcery version in the generated files headers.")
) { watcherEnabled, disableCache, verboseLogging, logAST, logBenchmark, parseDocumentation, quiet, prune, serialParse, sources, excludeSources, templates, excludeTemplates, output, isDryRun, configPaths, forceParse, baseIndentation, args, ejsPath, cacheBasePath, buildPath, hideVersionHeader in
do {
Log.stackMessages = isDryRun
switch (quiet, verboseLogging) {
Expand Down Expand Up @@ -205,6 +206,7 @@ func runCLI() {
buildPath: buildPath.string.isEmpty ? nil : buildPath,
prune: prune,
serialParse: serialParse,
hideVersionHeader: hideVersionHeader,
arguments: configuration.args)

if isDryRun, watcherEnabled {
Expand Down Expand Up @@ -299,8 +301,9 @@ func runCLI() {
via `argument.<name>`. To pass in string you should use escaped quotes (\\").
"""),
Option<Path>("cacheBasePath", default: "", description: "Base path to Sourcery's cache directory"),
Option<Path>("buildPath", default: "", description: "Sets a custom build path")
) { disableCache, verboseLogging, logAST, logBenchmark, parseDocumentation, quiet, prune, serialParse, sources, excludeSources, templates, excludeTemplates, output, isDryRun, configPaths, forceParse, baseIndentation, args, cacheBasePath, buildPath in
Option<Path>("buildPath", default: "", description: "Sets a custom build path"),
Flag("hideVersionHeader", description: "Do not include Sourcery version in the generated files headers.")
) { disableCache, verboseLogging, logAST, logBenchmark, parseDocumentation, quiet, prune, serialParse, sources, excludeSources, templates, excludeTemplates, output, isDryRun, configPaths, forceParse, baseIndentation, args, cacheBasePath, buildPath, hideVersionHeader in
do {
Log.stackMessages = isDryRun
switch (quiet, verboseLogging) {
Expand Down Expand Up @@ -382,6 +385,7 @@ func runCLI() {
buildPath: buildPath.string.isEmpty ? nil : buildPath,
prune: prune,
serialParse: serialParse,
hideVersionHeader: hideVersionHeader,
arguments: configuration.args)

return try sourcery.processFiles(
Expand Down
25 changes: 22 additions & 3 deletions SourceryTests/SourcerySpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,25 @@ class SourcerySpecTests: QuickSpec {
expect(result?.withoutWhitespaces).to(equal(expectedResult.withoutWhitespaces))
}

context("with hide version from header enabled") {
beforeEach {
expect { try Sourcery(watcherEnabled: false, cacheDisabled: true, hideVersionHeader: true).processFiles(.sources(Paths(include: [sourcePath])), usingTemplates: Paths(include: [templatePath]), output: output, baseIndentation: 0) }.toNot(throwError())
}
it("removes version information from within generated template") {
let expectedResult = """
// Generated using Sourcery — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT
// 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 @@ -1132,14 +1151,14 @@ class SourcerySpecTests: QuickSpec {

it("re-generates on template change") {
updateTemplate(code: "Found {{ types.enums.count }} Enums")

expect { watcher = try Sourcery(watcherEnabled: true, cacheDisabled: true).processFiles(.sources(Paths(include: [Stubs.sourceDirectory])), usingTemplates: Paths(include: [tmpTemplate]), output: output, baseIndentation: 0) }.toNot(throwError())
let sourcery = Sourcery(watcherEnabled: true, cacheDisabled: true)
expect { watcher = try sourcery.processFiles(.sources(Paths(include: [Stubs.sourceDirectory])), usingTemplates: Paths(include: [tmpTemplate]), output: output, baseIndentation: 0) }.toNot(throwError())

// ! Change the template
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

0 comments on commit 33be2ab

Please sign in to comment.