-
Notifications
You must be signed in to change notification settings - Fork 43
Add AutoAddonPlugin
to detect @LiveElement
views in a project
#1553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
carson-katri
wants to merge
2
commits into
main
Choose a base branch
from
auto-addon-registration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// AppAddonPlugin.swift | ||
// LiveViewNative | ||
// | ||
// Created by Carson Katri on 3/6/25. | ||
// | ||
|
||
import PackagePlugin | ||
import Foundation | ||
|
||
@main | ||
struct AutoAddonPlugin: BuildToolPlugin { | ||
func createBuildCommands(context: PluginContext, target: any Target) async throws -> [Command] { | ||
let tool = try context.tool(named: "AutoAddon") | ||
let inputFiles = target.sourceModule?.sourceFiles | ||
.filter({ | ||
(try? String(contentsOf: $0.url, encoding: .utf8))? | ||
.contains("@LiveElement") | ||
?? false | ||
}) | ||
.map(\.url) | ||
?? [] | ||
let outputFile = context.pluginWorkDirectoryURL.appending(path: "AutoAddon.swift") | ||
|
||
return [ | ||
.buildCommand( | ||
displayName: "Auto Addon", | ||
executable: tool.url, | ||
arguments: [ | ||
target.name, | ||
outputFile.absoluteString | ||
] + inputFiles.map(\.absoluteString), | ||
environment: [:], | ||
inputFiles: inputFiles, | ||
outputFiles: [outputFile] | ||
) | ||
] | ||
} | ||
} | ||
|
||
#if canImport(XcodeProjectPlugin) | ||
import XcodeProjectPlugin | ||
|
||
extension AutoAddonPlugin: XcodeBuildToolPlugin { | ||
func createBuildCommands(context: XcodePluginContext, target: XcodeTarget) throws -> [Command] { | ||
let tool = try context.tool(named: "AutoAddon") | ||
let inputFiles = target.inputFiles | ||
.filter({ | ||
(try? String(contentsOf: $0.url, encoding: .utf8))? | ||
.contains("@LiveElement") | ||
?? false | ||
}) | ||
.map(\.url) | ||
let outputFile = context.pluginWorkDirectoryURL.appending(path: "AutoAddon.swift") | ||
|
||
return [ | ||
.buildCommand( | ||
displayName: "Auto Addon", | ||
executable: tool.url, | ||
arguments: [ | ||
target.displayName, | ||
outputFile.absoluteString | ||
] + inputFiles.map(\.absoluteString), | ||
environment: [:], | ||
inputFiles: inputFiles, | ||
outputFiles: [outputFile] | ||
) | ||
] | ||
} | ||
} | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
// | ||
// AutoAddon.swift | ||
// LiveViewNative | ||
// | ||
// Created by Carson Katri on 3/6/25. | ||
// | ||
|
||
import ArgumentParser | ||
import Foundation | ||
import SwiftSyntax | ||
import SwiftParser | ||
import SwiftSyntaxBuilder | ||
|
||
@main | ||
struct AutoAddon: ParsableCommand { | ||
@Argument private var name: String | ||
@Argument private var outputFile: String | ||
@Argument private var inputFiles: [String] | ||
|
||
func run() throws { | ||
let visitor = LiveElementVisitor(viewMode: .fixedUp) | ||
|
||
for inputFile in self.inputFiles { | ||
guard let inputFile = URL(string: inputFile) | ||
else { continue } | ||
let source = try String(contentsOf: inputFile, encoding: .utf8) | ||
let sourceFile = Parser.parse(source: source) | ||
visitor.walk(sourceFile) | ||
} | ||
|
||
/// The `TagName` enum required by `CustomRegistry`. | ||
let tagNameDecl = EnumDeclSyntax( | ||
modifiers: [ | ||
DeclModifierSyntax(name: .keyword(.public)) | ||
], | ||
name: .identifier("TagName"), | ||
inheritanceClause: InheritanceClauseSyntax { | ||
InheritedTypeSyntax(type: IdentifierTypeSyntax(name: .identifier("String"))) | ||
} | ||
) { | ||
EnumCaseDeclSyntax { | ||
for liveElement in visitor.liveElements { | ||
EnumCaseElementSyntax(name: .identifier(liveElement)) | ||
} | ||
} | ||
} | ||
|
||
/// static `lookup` function required by `CustomRegistry`. | ||
/// ``` | ||
/// static func lookup(_ name: TagName, element: ElementNode) -> some View | ||
/// ``` | ||
let lookupDecl = FunctionDeclSyntax( | ||
attributes: [ | ||
.attribute(AttributeSyntax(attributeName: IdentifierTypeSyntax(name: .identifier("MainActor")))), | ||
.attribute(AttributeSyntax(attributeName: IdentifierTypeSyntax(name: .identifier("ViewBuilder")))), | ||
], | ||
modifiers: [ | ||
DeclModifierSyntax(name: .keyword(.public)), | ||
DeclModifierSyntax(name: .keyword(.static)) | ||
], | ||
name: .identifier("lookup"), | ||
signature: FunctionSignatureSyntax( | ||
parameterClause: FunctionParameterClauseSyntax { | ||
FunctionParameterSyntax( | ||
firstName: .wildcardToken(), | ||
secondName: .identifier("name"), | ||
type: IdentifierTypeSyntax(name: .identifier("TagName")) | ||
) | ||
FunctionParameterSyntax( | ||
firstName: .identifier("element"), | ||
type: IdentifierTypeSyntax(name: .identifier("ElementNode")) | ||
) | ||
}, | ||
returnClause: ReturnClauseSyntax( | ||
type: SomeOrAnyTypeSyntax( | ||
someOrAnySpecifier: .keyword(.some), | ||
constraint: IdentifierTypeSyntax(name: .identifier("View")) | ||
) | ||
) | ||
) | ||
) { | ||
SwitchExprSyntax(subject: DeclReferenceExprSyntax(baseName: .identifier("name"))) { | ||
for liveElement in visitor.liveElements { | ||
SwitchCaseSyntax(label: .case(SwitchCaseLabelSyntax { | ||
SwitchCaseItemSyntax(pattern: ExpressionPatternSyntax( | ||
expression: MemberAccessExprSyntax(name: .identifier(liveElement)) | ||
)) | ||
})) { | ||
// create the matching View for the TagName | ||
FunctionCallExprSyntax( | ||
callee: TypeExprSyntax( | ||
type: IdentifierTypeSyntax( | ||
name: .identifier(liveElement), | ||
genericArgumentClause: GenericArgumentClauseSyntax { | ||
GenericArgumentSyntax(argument: IdentifierTypeSyntax(name: .identifier("Root"))) | ||
} | ||
) | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Addon struct declaration using the `@Addon` macro in an extension of `Addons`. | ||
let addonDecl = ExtensionDeclSyntax( | ||
modifiers: [DeclModifierSyntax(name: .keyword(.public))], | ||
extendedType: IdentifierTypeSyntax(name: .identifier("Addons")) | ||
) { | ||
StructDeclSyntax( | ||
attributes: [.attribute(AttributeSyntax(attributeName: IdentifierTypeSyntax(name: .identifier("Addon"))))], | ||
name: .identifier(name), | ||
genericParameterClause: GenericParameterClauseSyntax { | ||
GenericParameterSyntax( | ||
name: .identifier("Root"), | ||
colon: .colonToken(), | ||
inheritedType: IdentifierTypeSyntax(name: .identifier("RootRegistry")) | ||
) | ||
} | ||
) { | ||
tagNameDecl | ||
lookupDecl | ||
} | ||
} | ||
|
||
try SourceFileSyntax { | ||
ImportDeclSyntax(path: [ImportPathComponentSyntax(name: .identifier("SwiftUI"))]) | ||
ImportDeclSyntax(path: [ImportPathComponentSyntax(name: .identifier("LiveViewNative"))]) | ||
|
||
addonDecl | ||
} | ||
.formatted() | ||
.description | ||
.write(to: URL(string: outputFile)!, atomically: true, encoding: .utf8) | ||
} | ||
} | ||
|
||
final class LiveElementVisitor: SyntaxVisitor { | ||
var liveElements = Set<String>() | ||
|
||
override func visit(_ decl: StructDeclSyntax) -> SyntaxVisitorContinueKind { | ||
guard decl.attributes.contains(where: { | ||
guard case let .attribute(attribute) = $0 | ||
else { return false } | ||
return attribute.attributeName.isLiveElementMacro | ||
}) | ||
else { return .visitChildren } | ||
|
||
liveElements.insert(decl.name.text) | ||
|
||
return .visitChildren | ||
} | ||
} | ||
|
||
extension TypeSyntax { | ||
var isLiveElementMacro: Bool { | ||
if let identifierType = self.as(IdentifierTypeSyntax.self) { | ||
return identifierType.name.text == "LiveElement" | ||
} else if let memberType = self.as(MemberTypeSyntax.self) { | ||
return memberType.baseType.as(IdentifierTypeSyntax.self)?.name.text == "" | ||
&& memberType.name.text == "LiveElement" | ||
} else { | ||
return false | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be reverted if/when
SDKROOT
is added back to the environment