Add the ability to force the parsing of previousily generated files
MODIFY
Sourcery/Configuration.swift
Sourcery/Configuration.swift
@@ -151,6 +151,7 @@ struct Configuration {
let source: Source
let templates: Paths
let output: Path
+ let parsableExtensions: [String]
let args: [String: NSObject]
init(path: Path, relativePath: Path) throws {
@@ -182,6 +183,8 @@ struct Configuration {
}
self.templates = templates
+ self.parsableExtensions = dict["parsableExtensions"] as? [String] ?? []
+
guard let output = dict["output"] as? String else {
throw Configuration.Error.invalidOutput(message: "'output' key is missing or is not a string.")
}
@@ -190,10 +193,11 @@ struct Configuration {
self.args = dict["args"] as? [String: NSObject] ?? [:]
}
- init(sources: [Path], templates: [Path], output: Path, args: [String: NSObject]) {
+ init(sources: [Path], templates: [Path], output: Path, parsableExtensions: [String], args: [String: NSObject]) {
self.source = .sources(Paths(include: sources))
self.templates = Paths(include: templates)
self.output = output
+ self.parsableExtensions = parsableExtensions
self.args = args
}

MODIFY
Sourcery/Parsing/Utils/Verifier.swift
Sourcery/Parsing/Utils/Verifier.swift
@@ -16,10 +16,16 @@ enum Verifier {
case approved
}
- public static func canParse(content: String) -> Result {
+ public static func canParse(content: String,
+ path: Path,
+ parsableExtensions: [String]) -> Result {
guard !content.isEmpty else { return .approved }
- if content.hasPrefix(Sourcery.generationMarker) {
+ let hasParsableExtension = (parsableExtensions.filter({ (ext) -> Bool in
+ return path.hasExtension(as: ext)
+ }).isEmpty == false)
+
+ if content.hasPrefix(Sourcery.generationMarker) && hasParsableExtension == false {
return .isCodeGenerated
}

MODIFY
Sourcery/Sourcery.swift
Sourcery/Sourcery.swift
@@ -47,9 +47,10 @@ class Sourcery {
/// - files: Path of files to process, can be directory or specific file.
/// - templatePath: Specific Template to use for code generation.
/// - output: Path to output source code to.
+ /// - parsableExtensions: extensions of generated sourcery file that can be parsed
/// - watcherEnabled: Whether daemon watcher should be enabled.
/// - Throws: Potential errors.
- func processFiles(_ source: Source, usingTemplates templatesPaths: Paths, output: Path) throws -> [FolderWatcher.Local]? {
+ func processFiles(_ source: Source, usingTemplates templatesPaths: Paths, output: Path, parsableExtensions: [String]) throws -> [FolderWatcher.Local]? {
self.templatesPaths = templatesPaths
self.outputPath = output
@@ -66,7 +67,7 @@ class Sourcery {
var result: ParsingResult
switch source {
case let .sources(paths):
- result = try self.parse(from: paths.include, exclude: paths.exclude, modules: nil)
+ result = try self.parse(from: paths.include, exclude: paths.exclude, parsableExtensions: parsableExtensions, modules: nil)
case let .projects(projects):
var paths = [Path]()
var modules = [String]()
@@ -195,12 +196,16 @@ class Sourcery {
extension Sourcery {
typealias ParsingResult = (types: Types, inlineRanges: [(file: String, ranges: [String: NSRange])])
- fileprivate func parse(from: [Path], exclude: [Path] = [], modules: [String]?) throws -> ParsingResult {
+ fileprivate func parse(from: [Path], exclude: [Path] = [], parsableExtensions: [String] = [], modules: [String]?) throws -> ParsingResult {
if let modules = modules {
precondition(from.count == modules.count, "There should be module for each file to parse")
}
self.track("Scanning sources...", terminator: "")
+ self.track("Parsable Extensions:", terminator: "")
+ for ext in parsableExtensions {
+ self.track("ext: \(ext)", terminator: "")
+ }
var inlineRanges = [(file: String, ranges: [String: NSRange])]()
var allResults = [FileParserResult]()
@@ -216,7 +221,9 @@ extension Sourcery {
}
.map { (path: $0, contents: try $0.read(.utf8)) }
.filter {
- let result = Verifier.canParse(content: $0.contents)
+ let result = Verifier.canParse(content: $0.contents,
+ path: $0.path,
+ parsableExtensions: parsableExtensions)
if result == .containsConflictMarkers {
throw Error.containsMergeConflictMarkers
}

MODIFY
Sourcery/Utils/Path+Extensions.swift
Sourcery/Utils/Path+Extensions.swift
@@ -40,6 +40,11 @@ extension Path {
return !self.isDirectory && self.extension == "swift"
}
+ func hasExtension(as string: String) -> Bool {
+ let extensionString = ".\(string)."
+ return self.string.contains(extensionString)
+ }
+
init(_ string: String, relativeTo relativePath: Path) {
var path = Path(string)
if !path.isAbsolute {

MODIFY
Sourcery/main.swift
Sourcery/main.swift
@@ -125,8 +125,9 @@ func runCLI() {
VariadicOption<Path>("templates", description: "Path to templates. File or Directory."),
Option<Path>("output", ".", description: "Path to output. File or Directory. Default is current path."),
Option<Path>("config", ".", description: "Path to config file. File or Directory. Default is current path."),
+ VariadicOption<String>("parsableExtensions", description: "extensions that this run of Sorcery should parse"),
Argument<CustomArguments>("args", description: "Custom values to pass to templates.")
- ) { watcherEnabled, disableCache, verboseLogging, quiet, prune, sources, templates, output, configPath, args in
+ ) { watcherEnabled, disableCache, verboseLogging, quiet, prune, sources, templates, output, configPath, parsableExtensions, args in
do {
Log.level = verboseLogging ? .verbose : quiet ? .errors : .info
@@ -138,6 +139,7 @@ func runCLI() {
configuration = Configuration(sources: sources,
templates: templates,
output: output,
+ parsableExtensions: parsableExtensions,
args: args.arguments)
} else {
_ = Validators.isFileOrDirectory(path: configPath)
@@ -164,7 +166,8 @@ func runCLI() {
if let keepAlive = try sourcery.processFiles(
configuration.source,
usingTemplates: configuration.templates,
- output: configuration.output) {
+ output: configuration.output,
+ parsableExtensions: configuration.parsableExtensions) {
RunLoop.current.run()
_ = keepAlive
} else {

Add the ability to force the parsing of previousily generated files