Skip to content

Commit

Permalink
Enable Single Print when Generating Based on Swifttemplate (#1308)
Browse files Browse the repository at this point in the history
* Update SwiftTemplate.swift

Enables a single point of output into STDOUT during codegen from swifttemplate

* Fixed failing tests

* Squashed commit of the following:

commit 33be2ab
Author: David Cacenabes <david.cacenabes@gmail.com>
Date:   Tue Mar 26 07:09:02 2024 +0100

    Remove Sourcery version from header (#1309)

    * 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
art-divin committed Mar 26, 2024
1 parent 33be2ab commit b2d484c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
13 changes: 10 additions & 3 deletions SourcerySwift/Sources/SwiftTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,28 @@ open class SwiftTemplate {

var includedFiles: [Path] = []
var outputFile = [String]()
var hasContents = false
for command in commands {
switch command {
case let .includeFile(path):
includedFiles.append(path)
case let .output(code):
outputFile.append("print(\"\\(" + code + ")\", terminator: \"\");")
outputFile.append("sourceryBuffer.append(\"\\(" + code + ")\");")
hasContents = true
case let .controlFlow(code):
outputFile.append("\(code)")
hasContents = true
case let .outputEncoded(code):
if !code.isEmpty {
outputFile.append(("print(\"") + code.stringEncoded + "\", terminator: \"\");")
outputFile.append(("sourceryBuffer.append(\"") + code.stringEncoded + "\");")
hasContents = true
}
}
}
if hasContents {
outputFile.insert("var sourceryBuffer = \"\";", at: 0)
}
outputFile.append("print(\"\\(sourceryBuffer)\", terminator: \"\");")

let contents = outputFile.joined(separator: "\n")
let code = """
Expand Down Expand Up @@ -259,7 +267,6 @@ open class SwiftTemplate {
let compilationResult = try Process.runCommand(path: "/usr/bin/env",
arguments: arguments,
currentDirectoryPath: buildDir)

if compilationResult.exitCode != EXIT_SUCCESS {
throw [compilationResult.output, compilationResult.error]
.filter { !$0.isEmpty }
Expand Down
4 changes: 2 additions & 2 deletions SourceryTests/Generating/SwiftTemplateSpecs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class SwiftTemplateTests: QuickSpec {
}
.to(throwError(closure: { (error) in
let path = Path.cleanTemporaryDir(name: "build").parent() + "SwiftTemplate/version/Sources/SwiftTemplate/main.swift"
expect("\(error)").to(contain("\(path):10:11: error: missing argument for parameter #1 in call\nprint(\"\\( )\", terminator: \"\");\n ^\n"))
expect("\(error)").to(contain("\(path):11:27: error: missing argument for parameter #1 in call\nsourceryBuffer.append(\"\\( )\");\n ^\n"))
}))
}

Expand All @@ -194,7 +194,7 @@ class SwiftTemplateTests: QuickSpec {
try Generator.generate(.init(path: nil, module: nil, types: [], functions: []), types: Types(types: []), functions: [], template: SwiftTemplate(path: templatePath))
}
.to(throwError(closure: { (error) in
expect("\(error)").to(contain("\(templatePath): SwiftTemplate/main.swift:10: Fatal error: Template not implemented"))
expect("\(error)").to(contain("\(templatePath): SwiftTemplate/main.swift:11: Fatal error: Template not implemented"))
}))
}

Expand Down

0 comments on commit b2d484c

Please sign in to comment.