Skip to content

Commit

Permalink
Add defaultExtensionTarget command parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
skorulis-ap committed Nov 9, 2023
1 parent 3df4f5f commit 560c11e
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Example/KnitExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "/usr/bin/xcrun --sdk macosx swift run knit gen \\\n--assembly-input-path KnitExample/KnitExampleAssembly.swift \\\n--assembly-input-path KnitExample/KnitExampleUserAssembly.swift \\\n--type-safety-extensions-output-path KnitExample/KnitDITypeSafety.swift \\\n--unit-test-output-path KnitExampleTests/KnitDIRegistrationTests.swift\n";
shellScript = "/usr/bin/xcrun --sdk macosx swift run knit-cli gen \\\n--assembly-input-path KnitExample/KnitExampleAssembly.swift \\\n--assembly-input-path KnitExample/KnitExampleUserAssembly.swift \\\n--type-safety-extensions-output-path KnitExample/KnitDITypeSafety.swift \\\n--unit-test-output-path KnitExampleTests/KnitDIRegistrationTests.swift\n";
};
/* End PBXShellScriptBuildPhase section */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "2c49d66d34dfd6f8130afdba889de77504b58ec0",
"version" : "508.0.1"
"revision" : "6ad4ea24b01559dde0773e3d091f1b9e36175036",
"version" : "509.0.2"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Example/KnitExample/KnitExampleApp.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright © Square, Inc. All rights reserved.

import SwiftUI
import Knit
import KnitLib

@main
struct KnitExampleApp: App {
Expand Down
2 changes: 1 addition & 1 deletion Example/KnitExample/KnitExampleUserAssembly.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright © Square, Inc. All rights reserved.

import Foundation
import Knit
import KnitLib

// @knit internal getter-named
/// An assembly expected to be registered at the user level rather than at the app level
Expand Down
17 changes: 11 additions & 6 deletions Sources/KnitCodeGen/AssemblyParsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import SwiftSyntax
import SwiftParser

public func parseAssemblies(at paths: [String]) throws -> ConfigurationSet {
public func parseAssemblies(at paths: [String], defaultTargetResolver: String) throws -> ConfigurationSet {
var configs = [Configuration]()
for path in paths {
let url = URL(fileURLWithPath: path, isDirectory: false)
Expand All @@ -15,7 +15,11 @@ public func parseAssemblies(at paths: [String]) throws -> ConfigurationSet {
throw AssemblyParsingError.fileReadError(error, path: path)
}
let syntaxTree = Parser.parse(source: source)
let configuration = try parseSyntaxTree(syntaxTree, errorsToPrint: &errorsToPrint)
let configuration = try parseSyntaxTree(
syntaxTree,
errorsToPrint: &errorsToPrint,
defaultTargetResolver: defaultTargetResolver
)
configs.append(configuration)
printErrors(errorsToPrint, filePath: path, syntaxTree: syntaxTree)
}
Expand All @@ -24,7 +28,8 @@ public func parseAssemblies(at paths: [String]) throws -> ConfigurationSet {

func parseSyntaxTree(
_ syntaxTree: SyntaxProtocol,
errorsToPrint: inout [Error]
errorsToPrint: inout [Error],
defaultTargetResolver: String
) throws -> Configuration {
let assemblyFileVisitor = AssemblyFileVisitor()
assemblyFileVisitor.walk(syntaxTree)
Expand All @@ -41,7 +46,7 @@ func parseSyntaxTree(
registrations: assemblyFileVisitor.registrations,
registrationsIntoCollections: assemblyFileVisitor.registrationsIntoCollections,
imports: assemblyFileVisitor.imports,
targetResolver: assemblyFileVisitor.targetResolver
targetResolver: assemblyFileVisitor.targetResolver ?? defaultTargetResolver
)
}

Expand All @@ -68,8 +73,8 @@ private class AssemblyFileVisitor: SyntaxVisitor {
return classDeclVisitor?.registrationErrors ?? []
}

var targetResolver: String {
return classDeclVisitor?.targetResolver ?? "Resolver"
var targetResolver: String? {
return classDeclVisitor?.targetResolver
}

init() {
Expand Down
5 changes: 4 additions & 1 deletion Sources/KnitCommand/GenCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ struct GenCommand: ParsableCommand {
""")
var jsonDataOutputPath: String?

@Option(help: "Default type to extend when generating Resolver type safety methods")
var defaultExtensionTarget = "Resolver"

public init() {}

public func run() throws {
let parsedConfig: ConfigurationSet
do {
parsedConfig = try parseAssemblies(at: assemblyInputPath)
parsedConfig = try parseAssemblies(at: assemblyInputPath, defaultTargetResolver: defaultExtensionTarget)
if let jsonDataOutputPath {
let data = try JSONEncoder().encode(parsedConfig.assemblies)
try data.write(to: URL(fileURLWithPath: jsonDataOutputPath))
Expand Down
6 changes: 5 additions & 1 deletion Tests/KnitCodeGenTests/AssemblyParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@ private func assertParsesSyntaxTree(
) throws -> Configuration {
var errorsToPrint = [Error]()

let configuration = try parseSyntaxTree(sourceFile, errorsToPrint: &errorsToPrint)
let configuration = try parseSyntaxTree(
sourceFile,
errorsToPrint: &errorsToPrint,
defaultTargetResolver: "Resolver"
)

if let assertErrorsCallback {
assertErrorsCallback(errorsToPrint)
Expand Down

0 comments on commit 560c11e

Please sign in to comment.