|
| 1 | +// |
| 2 | +// AutoAddon.swift |
| 3 | +// LiveViewNative |
| 4 | +// |
| 5 | +// Created by Carson Katri on 3/6/25. |
| 6 | +// |
| 7 | + |
| 8 | +import ArgumentParser |
| 9 | +import Foundation |
| 10 | +import SwiftSyntax |
| 11 | +import SwiftParser |
| 12 | +import SwiftSyntaxBuilder |
| 13 | + |
| 14 | +@main |
| 15 | +struct AutoAddon: ParsableCommand { |
| 16 | + @Argument private var name: String |
| 17 | + @Argument private var outputFile: String |
| 18 | + @Argument private var inputFiles: [String] |
| 19 | + |
| 20 | + func run() throws { |
| 21 | + let visitor = LiveElementVisitor(viewMode: .fixedUp) |
| 22 | + |
| 23 | + for inputFile in self.inputFiles { |
| 24 | + guard let inputFile = URL(string: inputFile) |
| 25 | + else { continue } |
| 26 | + let source = try String(contentsOf: inputFile, encoding: .utf8) |
| 27 | + let sourceFile = Parser.parse(source: source) |
| 28 | + visitor.walk(sourceFile) |
| 29 | + } |
| 30 | + |
| 31 | + /// The `TagName` enum required by `CustomRegistry`. |
| 32 | + let tagNameDecl = EnumDeclSyntax( |
| 33 | + modifiers: [ |
| 34 | + DeclModifierSyntax(name: .keyword(.public)) |
| 35 | + ], |
| 36 | + name: .identifier("TagName"), |
| 37 | + inheritanceClause: InheritanceClauseSyntax { |
| 38 | + InheritedTypeSyntax(type: IdentifierTypeSyntax(name: .identifier("String"))) |
| 39 | + } |
| 40 | + ) { |
| 41 | + EnumCaseDeclSyntax { |
| 42 | + for liveElement in visitor.liveElements { |
| 43 | + EnumCaseElementSyntax(name: .identifier(liveElement)) |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /// static `lookup` function required by `CustomRegistry`. |
| 49 | + /// ``` |
| 50 | + /// static func lookup(_ name: TagName, element: ElementNode) -> some View |
| 51 | + /// ``` |
| 52 | + let lookupDecl = FunctionDeclSyntax( |
| 53 | + attributes: [ |
| 54 | + .attribute(AttributeSyntax(attributeName: IdentifierTypeSyntax(name: .identifier("MainActor")))), |
| 55 | + .attribute(AttributeSyntax(attributeName: IdentifierTypeSyntax(name: .identifier("ViewBuilder")))), |
| 56 | + ], |
| 57 | + modifiers: [ |
| 58 | + DeclModifierSyntax(name: .keyword(.public)), |
| 59 | + DeclModifierSyntax(name: .keyword(.static)) |
| 60 | + ], |
| 61 | + name: .identifier("lookup"), |
| 62 | + signature: FunctionSignatureSyntax( |
| 63 | + parameterClause: FunctionParameterClauseSyntax { |
| 64 | + FunctionParameterSyntax( |
| 65 | + firstName: .wildcardToken(), |
| 66 | + secondName: .identifier("name"), |
| 67 | + type: IdentifierTypeSyntax(name: .identifier("TagName")) |
| 68 | + ) |
| 69 | + FunctionParameterSyntax( |
| 70 | + firstName: .identifier("element"), |
| 71 | + type: IdentifierTypeSyntax(name: .identifier("ElementNode")) |
| 72 | + ) |
| 73 | + }, |
| 74 | + returnClause: ReturnClauseSyntax( |
| 75 | + type: SomeOrAnyTypeSyntax( |
| 76 | + someOrAnySpecifier: .keyword(.some), |
| 77 | + constraint: IdentifierTypeSyntax(name: .identifier("View")) |
| 78 | + ) |
| 79 | + ) |
| 80 | + ) |
| 81 | + ) { |
| 82 | + SwitchExprSyntax(subject: DeclReferenceExprSyntax(baseName: .identifier("name"))) { |
| 83 | + for liveElement in visitor.liveElements { |
| 84 | + SwitchCaseSyntax(label: .case(SwitchCaseLabelSyntax { |
| 85 | + SwitchCaseItemSyntax(pattern: ExpressionPatternSyntax( |
| 86 | + expression: MemberAccessExprSyntax(name: .identifier(liveElement)) |
| 87 | + )) |
| 88 | + })) { |
| 89 | + // create the matching View for the TagName |
| 90 | + FunctionCallExprSyntax( |
| 91 | + callee: TypeExprSyntax( |
| 92 | + type: IdentifierTypeSyntax( |
| 93 | + name: .identifier(liveElement), |
| 94 | + genericArgumentClause: GenericArgumentClauseSyntax { |
| 95 | + GenericArgumentSyntax(argument: IdentifierTypeSyntax(name: .identifier("Root"))) |
| 96 | + } |
| 97 | + ) |
| 98 | + ) |
| 99 | + ) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /// Addon struct declaration using the `@Addon` macro in an extension of `Addons`. |
| 106 | + let addonDecl = ExtensionDeclSyntax( |
| 107 | + modifiers: [DeclModifierSyntax(name: .keyword(.public))], |
| 108 | + extendedType: IdentifierTypeSyntax(name: .identifier("Addons")) |
| 109 | + ) { |
| 110 | + StructDeclSyntax( |
| 111 | + attributes: [.attribute(AttributeSyntax(attributeName: IdentifierTypeSyntax(name: .identifier("Addon"))))], |
| 112 | + name: .identifier(name), |
| 113 | + genericParameterClause: GenericParameterClauseSyntax { |
| 114 | + GenericParameterSyntax( |
| 115 | + name: .identifier("Root"), |
| 116 | + colon: .colonToken(), |
| 117 | + inheritedType: IdentifierTypeSyntax(name: .identifier("RootRegistry")) |
| 118 | + ) |
| 119 | + } |
| 120 | + ) { |
| 121 | + tagNameDecl |
| 122 | + lookupDecl |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + try SourceFileSyntax { |
| 127 | + ImportDeclSyntax(path: [ImportPathComponentSyntax(name: .identifier("SwiftUI"))]) |
| 128 | + ImportDeclSyntax(path: [ImportPathComponentSyntax(name: .identifier("LiveViewNative"))]) |
| 129 | + |
| 130 | + addonDecl |
| 131 | + } |
| 132 | + .formatted() |
| 133 | + .description |
| 134 | + .write(to: URL(string: outputFile)!, atomically: true, encoding: .utf8) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +final class LiveElementVisitor: SyntaxVisitor { |
| 139 | + var liveElements = Set<String>() |
| 140 | + |
| 141 | + override func visit(_ decl: StructDeclSyntax) -> SyntaxVisitorContinueKind { |
| 142 | + guard decl.attributes.contains(where: { |
| 143 | + guard case let .attribute(attribute) = $0 |
| 144 | + else { return false } |
| 145 | + return attribute.attributeName.isLiveElementMacro |
| 146 | + }) |
| 147 | + else { return .visitChildren } |
| 148 | + |
| 149 | + liveElements.insert(decl.name.text) |
| 150 | + |
| 151 | + return .visitChildren |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +extension TypeSyntax { |
| 156 | + var isLiveElementMacro: Bool { |
| 157 | + if let identifierType = self.as(IdentifierTypeSyntax.self) { |
| 158 | + return identifierType.name.text == "LiveElement" |
| 159 | + } else if let memberType = self.as(MemberTypeSyntax.self) { |
| 160 | + return memberType.baseType.as(IdentifierTypeSyntax.self)?.name.text == "" |
| 161 | + && memberType.name.text == "LiveElement" |
| 162 | + } else { |
| 163 | + return false |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments