Skip to content

Commit

Permalink
Add a verbose option, off by default. (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
samdeane authored Jan 31, 2025
1 parent eab8345 commit 41a656d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
30 changes: 22 additions & 8 deletions Sources/EntryPointGenerator/EntryPointGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,23 @@ struct EntryPointGenerator: ParsableCommand {
@Argument(help: "Source files containing @Godot classes to generate entry points for.")
var sourceFiles: [String]

@Option(name: .shortAndLong, help: "Produce more output.")
var verbose: Bool = false

mutating func run() throws {
let visitor = GodotMacroSearchingVisitor(viewMode: .all)
let visitor = GodotMacroSearchingVisitor(viewMode: .all, logger: verbose ? logVerbose : nil)

print("Scanning source files...")
logVerbose("Scanning source files...")
for file in sourceFiles {
print("Scanning '\(file)'...")
logVerbose("Scanning '\(file)'...")
let source = try String(contentsOf: URL(fileURLWithPath: file))
let fileSyntax = Parser.parse(source: source)

visitor.walk(fileSyntax)
}

let names = visitor.classes
.map { name in
" \(name).self"
}
.map { name in " \(name).self" }
.joined(separator: ",\n")

let source = """
Expand All @@ -45,9 +46,22 @@ struct EntryPointGenerator: ParsableCommand {
"""

print("Writing \(visitor.classes.count) to '\(outputFile)'...")
let count = visitor.classes.count
logVerbose("Writing \(count) to '\(outputFile)'...")
let outputURL = URL(fileURLWithPath: outputFile)
try source.write(to: outputURL, atomically: true, encoding: .utf8)
print("Success! Entry point is `swift_entry_point`.")
log("Generated swift_entry_point, registering \(count) classes, in \(outputURL.lastPathComponent).")
}

/// Log a message to the console.
func log(_ message: String) {
print(message)
}

/// Log a message to the console if verbose mode is enabled.
func logVerbose(_ message: String) {
if verbose {
print(message)
}
}
}
14 changes: 12 additions & 2 deletions Sources/EntryPointGenerator/GodotMacroSearchingVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,27 @@
import Foundation
import SwiftSyntax

/// Visitor that searches for classes with the @Godot macro
public class GodotMacroSearchingVisitor: SyntaxVisitor {
/// Initialize the visitor, optionally with a logger.
internal init(viewMode: SyntaxTreeViewMode, logger: ((String) -> Void)? = nil) {
self.logger = logger
super.init(viewMode: viewMode)
}

/// List of classes with the @Godot macro
public var classes: [String] = []

/// Logger function for verbose output
public let logger: ((String) -> Void)?

public override func visit(_ classDecl: ClassDeclSyntax) -> SyntaxVisitorContinueKind {
// Check for attached macros (attributes)
for attribute in classDecl.attributes {
if let attributeSyntax = attribute.as(AttributeSyntax.self) {
let attributeName = attributeSyntax.attributeName.description.trimmingCharacters(in: .whitespacesAndNewlines)
if attributeName == "Godot" {
let className = classDecl.name.text
print("Found '\(className)' with @Godot macro.")
logger?("Found '\(className)' with @Godot macro.")
classes.append(className)
break // Found the @Godot macro, no need to check further
}
Expand Down

0 comments on commit 41a656d

Please sign in to comment.