Fix incorrect Tera template syntax in per-language Methods templates:
- Change `elsif` to `elif` (correct Tera conditional syntax)
- Fix inline ternary-like syntax to use proper `{% if %}...{% else %}...{% endif %}`
- Fix truncated package name in Java template (codegen → codegen)
Affected templates:
- PHP: Methods.php.tera
- Python: methods.py.tera
- Ruby: methods.rb.tera
- Swift: Methods.swift.tera
- Java: Methods.java.tera
All 8 subprocess SDK templates now render correctly with the codegen
command. Verified via `pdftract sdk codegen --lang <lang> --out /tmp/sdk-<lang>`.
Co-Authored-By: Claude Code <noreply@anthropic.com>
Bead-Id: pdftract-l993m
201 lines
7.2 KiB
Text
201 lines
7.2 KiB
Text
//
|
|
// This file is auto-generated. Do not edit manually.
|
|
//
|
|
|
|
#if os(Linux)
|
|
import Foundation
|
|
#else
|
|
import Foundation
|
|
#endif
|
|
|
|
public class Pdftract {
|
|
private let binaryPath: String
|
|
public let version = "{{ version }}"
|
|
|
|
public init(binaryPath: String = "pdftract") {
|
|
self.binaryPath = binaryPath
|
|
}
|
|
|
|
private func exec(_ args: [String]) throws -> String {
|
|
let process = Process()
|
|
process.executableURL = URL(fileURLWithPath: binaryPath)
|
|
process.arguments = args
|
|
|
|
let pipe = Pipe()
|
|
process.standardOutput = pipe
|
|
process.standardError = pipe
|
|
|
|
try process.run()
|
|
process.waitUntilExit()
|
|
|
|
let data = pipe.fileHandleForReading.readDataToEndOfFile()
|
|
let output = String(data: data, encoding: .utf8) ?? ""
|
|
|
|
if process.terminationStatus != 0 {
|
|
throw mapError(output, Int(process.terminationStatus))
|
|
}
|
|
|
|
return output
|
|
}
|
|
|
|
private func mapError(_ stderr: String, _ exitCode: Int?) -> PdftractError {
|
|
guard let exitCode = exitCode else {
|
|
return PdftractError(stderr, -1)
|
|
}
|
|
|
|
switch exitCode {
|
|
{% for error in errors %}
|
|
{% if error.exit_code != 0 %}
|
|
case {{ error.exit_code }}:
|
|
return {{ error.exception_name }}(stderr, exitCode)
|
|
{% endif %}
|
|
{% endfor %}
|
|
default:
|
|
return PdftractError(stderr, exitCode)
|
|
}
|
|
}
|
|
|
|
{% for method in methods %}
|
|
{% if method.name == 'extract_stream' %}
|
|
public func {{ method.camel_name }}(_ source: Source, options: {{ method.options_type }}? = nil) -> AsyncStream<{{ method.return_type }}> {
|
|
return AsyncStream { continuation in
|
|
var args = ["{{ method.cli_flag }}"]
|
|
args.append(contentsOf: source.toArgs())
|
|
|
|
if let options = options {
|
|
args.append(contentsOf: options.toArgs())
|
|
}
|
|
|
|
let process = Process()
|
|
process.executableURL = URL(fileURLWithPath: binaryPath)
|
|
process.arguments = args
|
|
|
|
let outPipe = Pipe()
|
|
let errPipe = Pipe()
|
|
process.standardOutput = outPipe
|
|
process.standardError = errPipe
|
|
|
|
do {
|
|
try process.run()
|
|
|
|
let handler = DispatchWorkItem {
|
|
let data = outPipe.fileHandleForReading.readDataToEndOfFile()
|
|
if let output = String(data: data, encoding: .utf8) {
|
|
for line in output.components(separatedBy: .newlines) {
|
|
if !line.isEmpty {
|
|
if let jsonData = line.data(using: .utf8),
|
|
let result = try? JSONDecoder().decode({{ method.return_type }}.self, from: jsonData) {
|
|
continuation.yield(result)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
process.waitUntilExit()
|
|
|
|
if process.terminationStatus != 0 {
|
|
let errorData = errPipe.fileHandleForReading.readDataToEndOfFile()
|
|
let stderr = String(data: errorData, encoding: .utf8) ?? ""
|
|
continuation.finish(throwing: self.mapError(stderr, Int(process.terminationStatus)))
|
|
} else {
|
|
continuation.finish()
|
|
}
|
|
}
|
|
|
|
DispatchQueue.global(qos: .userInitiated).async(execute: handler)
|
|
} catch {
|
|
continuation.finish(throwing: error)
|
|
}
|
|
}
|
|
}
|
|
{% elif method.name == 'search' %}
|
|
public func {{ method.camel_name }}(_ source: Source, _ pattern: String, options: {{ method.options_type }}? = nil) -> AsyncStream<{{ method.return_type }}> {
|
|
return AsyncStream { continuation in
|
|
var args = ["grep", pattern]
|
|
args.append(contentsOf: source.toArgs())
|
|
|
|
if let options = options {
|
|
args.append(contentsOf: options.toArgs())
|
|
}
|
|
|
|
let process = Process()
|
|
process.executableURL = URL(fileURLWithPath: binaryPath)
|
|
process.arguments = args
|
|
|
|
let outPipe = Pipe()
|
|
let errPipe = Pipe()
|
|
process.standardOutput = outPipe
|
|
process.standardError = errPipe
|
|
|
|
do {
|
|
try process.run()
|
|
|
|
let handler = DispatchWorkItem {
|
|
let data = outPipe.fileHandleForReading.readDataToEndOfFile()
|
|
if let output = String(data: data, encoding: .utf8) {
|
|
for line in output.components(separatedBy: .newlines) {
|
|
if !line.isEmpty {
|
|
if let jsonData = line.data(using: .utf8),
|
|
let result = try? JSONDecoder().decode({{ method.return_type }}.self, from: jsonData) {
|
|
continuation.yield(result)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
process.waitUntilExit()
|
|
|
|
if process.terminationStatus != 0 {
|
|
let errorData = errPipe.fileHandleForReading.readDataToEndOfFile()
|
|
let stderr = String(data: errorData, encoding: .utf8) ?? ""
|
|
continuation.finish(throwing: self.mapError(stderr, Int(process.terminationStatus)))
|
|
} else {
|
|
continuation.finish()
|
|
}
|
|
}
|
|
|
|
DispatchQueue.global(qos: .userInitiated).async(execute: handler)
|
|
} catch {
|
|
continuation.finish(throwing: error)
|
|
}
|
|
}
|
|
}
|
|
{% elif method.name == 'verify_receipt' %}
|
|
public func {{ method.camel_name }}(_ path: String, _ receipt: String) throws -> Bool {
|
|
let output = try exec(["{{ method.cli_flag }}", path, receipt])
|
|
return output.trimmingCharacters(in: .whitespacesAndNewlines) == "true"
|
|
}
|
|
{% else %}
|
|
public func {{ method.camel_name }}(_ source: Source{% if method.has_options %}, options: {{ method.options_type }}? = nil{% endif %}) throws -> {% if method.return_type == 'string' %}String{% else %}{{ method.return_type }}{% endif %} {
|
|
var args = ["{{ method.cli_flag }}"]
|
|
args.append(contentsOf: source.toArgs())
|
|
|
|
{% if method.has_options %}
|
|
if let options = options {
|
|
args.append(contentsOf: options.toArgs())
|
|
}
|
|
{% endif %}
|
|
|
|
{% if method.name == 'extract_text' %}
|
|
args.append("--text")
|
|
{% elif method.name == 'extract_markdown' %}
|
|
args.append("--md")
|
|
{% elif method.name == 'get_metadata' %}
|
|
args.append("--metadata-only")
|
|
{% endif %}
|
|
|
|
let output = try exec(args)
|
|
|
|
{% if method.returns_string %}
|
|
return output
|
|
{% else %}
|
|
guard let data = output.data(using: .utf8),
|
|
let result = try? JSONDecoder().decode({{ method.return_type }}.self, from: data) else {
|
|
throw PdftractError("Failed to decode JSON output", -1)
|
|
}
|
|
return result
|
|
{% endif %}
|
|
}
|
|
{% endif %}
|
|
{% endfor %}
|
|
}
|