Skip to content

Commit

Permalink
Ability to use custom header prefix (#1389)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilia3546 authored Dec 18, 2024
1 parent ad01b78 commit 3d3a2be
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ Inside a project/package that uses this command plugin, right-click the project
- `--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.
- `--headerPrefix` - Additional prefix for headers.

### Configuration file

Expand Down
9 changes: 7 additions & 2 deletions Sourcery/Sourcery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public class Sourcery {
serialParse: Bool = false,
hideVersionHeader: Bool = false,
arguments: [String: NSObject] = [:],
logConfiguration: Log.Configuration? = nil
logConfiguration: Log.Configuration? = nil,
headerPrefix: String? = nil
) {
self.verbose = verbose
self.arguments = arguments
Expand All @@ -76,7 +77,11 @@ public class Sourcery {
Log.setup(using: logConfiguration)
}

var prefix = Sourcery.generationMarker
var prefix = ""
if let headerPrefix {
prefix += headerPrefix + "\n"
}
prefix += Sourcery.generationMarker
if !self.hideVersionHeader {
prefix += " \(Sourcery.version)"
}
Expand Down
16 changes: 10 additions & 6 deletions SourceryExecutable/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,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"),
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
Flag("hideVersionHeader", description: "Do not include Sourcery version in the generated files headers."),
Option<String?>("headerPrefix", default: nil, description: "Additional prefix for headers.")
) { watcherEnabled, disableCache, verboseLogging, logAST, logBenchmark, parseDocumentation, quiet, prune, serialParse, sources, excludeSources, templates, excludeTemplates, output, isDryRun, configPaths, forceParse, baseIndentation, args, ejsPath, cacheBasePath, buildPath, hideVersionHeader, headerPrefix in
do {
let logConfiguration = Log.Configuration(
isDryRun: isDryRun,
Expand Down Expand Up @@ -206,7 +207,8 @@ func runCLI() {
prune: prune,
serialParse: serialParse,
hideVersionHeader: hideVersionHeader,
arguments: configuration.args)
arguments: configuration.args,
headerPrefix: headerPrefix)

if isDryRun, watcherEnabled {
throw "--dry not compatible with --watch"
Expand Down Expand Up @@ -301,8 +303,9 @@ func runCLI() {
"""),
Option<Path>("cacheBasePath", default: "", description: "Base path to Sourcery's cache directory"),
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
Flag("hideVersionHeader", description: "Do not include Sourcery version in the generated files headers."),
Option<String?>("headerPrefix", default: nil, description: "Additional prefix for headers.")
) { disableCache, verboseLogging, logAST, logBenchmark, parseDocumentation, quiet, prune, serialParse, sources, excludeSources, templates, excludeTemplates, output, isDryRun, configPaths, forceParse, baseIndentation, args, cacheBasePath, buildPath, hideVersionHeader, headerPrefix in
do {
let logConfiguration = Log.Configuration(
isDryRun: isDryRun,
Expand Down Expand Up @@ -384,7 +387,8 @@ func runCLI() {
prune: prune,
serialParse: serialParse,
hideVersionHeader: hideVersionHeader,
arguments: configuration.args)
arguments: configuration.args,
headerPrefix: headerPrefix)

return try sourcery.processFiles(
configuration.source,
Expand Down
20 changes: 20 additions & 0 deletions SourceryTests/SourcerySpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,26 @@ class SourcerySpecTests: QuickSpec {
}
}

context("with custom header prefix") {
beforeEach {
expect { try Sourcery(watcherEnabled: false, cacheDisabled: true, hideVersionHeader: true, headerPrefix: "// swiftlint:disable all").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 = """
// swiftlint:disable all
// 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

0 comments on commit 3d3a2be

Please sign in to comment.