Skip to content

Commit

Permalink
Fixing diagnostics using very simple visitor pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdsupremacist committed Jan 24, 2021
1 parent d0638da commit b094a6b
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 586 deletions.
71 changes: 38 additions & 33 deletions Sources/Graphaello/Commands/Codegen/CodegenCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,46 +76,51 @@ class CodegenCommand : Command {
cache[.lastRunHash] = hashValue
}

Console.print(title: "🔎 Validating Paths against API definitions:")
let validated = try pipeline.validate(parsed: parsed)
Console.print(result: "Checked \(validated.graphQLPaths.count) fields")
do {
Console.print(title: "🔎 Validating Paths against API definitions:")
let validated = try pipeline.validate(parsed: parsed)
Console.print(result: "Checked \(validated.graphQLPaths.count) fields")

Console.print(title: "🧰 Resolving Fragments and Queries:")
let resolved = try pipeline.resolve(validated: validated)
Console.print(title: "🧰 Resolving Fragments and Queries:")
let resolved = try pipeline.resolve(validated: validated)

Console.print(result: "Resolved \(resolved.allQueries.count) Queries:")
resolved.allQueries.forEach { query in
Console.print(result: "\(inverse: query.name)", indentation: 2)
}
Console.print(result: "Resolved \(resolved.allQueries.count) Queries:")
resolved.allQueries.forEach { query in
Console.print(result: "\(inverse: query.name)", indentation: 2)
}

Console.print(result: "Resolved \(resolved.allFragments.count) Fragments:")
resolved.allFragments.forEach { fragment in
Console.print(result: "\(inverse: fragment.name)", indentation: 2)
}
Console.print(result: "Resolved \(resolved.allFragments.count) Fragments:")
resolved.allFragments.forEach { fragment in
Console.print(result: "\(inverse: fragment.name)", indentation: 2)
}

Console.print(title: "🧹 Cleaning Queries and Fragments:")
let cleaned = try pipeline.clean(resolved: resolved)
Console.print(title: "🧹 Cleaning Queries and Fragments:")
let cleaned = try pipeline.clean(resolved: resolved)

Console.print(title: "✏️ Generating Swift Code:")

Console.print(title: "🎨 Writing GraphQL Code", indentation: 1)
let assembled = try pipeline.assemble(cleaned: cleaned)

Console.print(title: "🚀 Delegating some stuff to Apollo codegen", indentation: 1)
let prepared = try pipeline.prepare(assembled: assembled, using: apollo)

Console.print(title: "🎁 Bundling it all together", indentation: 1)

let autoGeneratedFile = try pipeline.generate(prepared: prepared, useFormatting: !skipFormatting)

Console.print(result: "Generated \(autoGeneratedFile.components(separatedBy: "\n").count) lines of code")
Console.print(result: "You're welcome 🙃", indentation: 2)
Console.print(title: "✏️ Generating Swift Code:")

Console.print(title: "🎨 Writing GraphQL Code", indentation: 1)
let assembled = try pipeline.assemble(cleaned: cleaned)

Console.print(title: "🚀 Delegating some stuff to Apollo codegen", indentation: 1)
let prepared = try pipeline.prepare(assembled: assembled, using: apollo)

Console.print(title: "🎁 Bundling it all together", indentation: 1)

Console.print(title: "💾 Saving Autogenerated Code")
try project.writeFile(name: "Graphaello.swift", content: autoGeneratedFile)
let autoGeneratedFile = try pipeline.generate(prepared: prepared, useFormatting: !skipFormatting)

Console.print("")
Console.print(title: "✅ Done")
Console.print(result: "Generated \(autoGeneratedFile.components(separatedBy: "\n").count) lines of code")
Console.print(result: "You're welcome 🙃", indentation: 2)

Console.print(title: "💾 Saving Autogenerated Code")
try project.writeFile(name: "Graphaello.swift", content: autoGeneratedFile)

Console.print("")
Console.print(title: "✅ Done")
} catch {
cache?[.lastRunHash] = nil
throw error
}
}
}

Expand Down
10 changes: 4 additions & 6 deletions Sources/Graphaello/Processing/Model/Struct/Property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,36 @@ struct Property<CurrentStage: StageProtocol> {
let code: SourceCode
let name: String
let type: PropertyType
let usr: String
let context: Context

init(code: SourceCode, name: String, type: PropertyType, usr: String, @ContextBuilder context: () throws -> ContextProtocol) rethrows {
init(code: SourceCode, name: String, type: PropertyType, @ContextBuilder context: () throws -> ContextProtocol) rethrows {
self.code = code
self.name = name
self.type = type
self.usr = usr
self.context = try Context(context: context)
}
}

extension Property where CurrentStage: GraphQLStage {

func map<Stage: GraphQLStage>(_ transform: (CurrentStage.Path) throws -> Stage.Path) rethrows -> Property<Stage> {
return Property<Stage>(code: code, name: name, type: type, usr: usr, graphqlPath: try graphqlPath.map(transform))
return Property<Stage>(code: code, name: name, type: type, graphqlPath: try graphqlPath.map(transform))
}

}

extension Property {

func with<Stage: StageProtocol>(@ContextBuilder context: () throws -> ContextProtocol) rethrows -> Property<Stage> {
return try Property<Stage>(code: code, name: name, type: type, usr: usr, context: context)
return try Property<Stage>(code: code, name: name, type: type, context: context)
}

}

extension Property where CurrentStage: GraphQLStage {

func convert<Stage: GraphQLStage>() -> Property<Stage> where Stage.Path == CurrentStage.Path {
return Property<Stage>(code: code, name: name, type: type, usr: usr, graphqlPath: graphqlPath)
return Property<Stage>(code: code, name: name, type: type, graphqlPath: graphqlPath)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ extension SourceCode {
extension SourceCode {

init(file: WithTargets<File>, index: LineColumnIndex, location: Location) throws {
let arguments = ["-sdk", sdkPath(), "-j4", location.file.absoluteURL.path]
let docs = SwiftDocs(file: file.value, arguments: arguments)
self.init(file: file.value,
index: index,
location: location,
targets: file.targets,
dictionary: docs?.docsDictionary ?? [:])
dictionary: try Structure(file: file.value).dictionary)
}

init(content: String, parent: SourceCode, location: Location) throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ struct BasicPropertyExtractor: PropertyExtractor {

return Property(code: code,
name: try code.name(),
type: finalType.map { .concrete($0) } ?? .inferred,
usr: try code.usr()) {
type: finalType.map { .concrete($0) } ?? .inferred) {

.attributes ~> attributes
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ struct PropertyResolver<Resolver: ValueResolver>: ValueResolver where Resolver.P
using context: StructResolution.Context) throws -> StructResolution.Result<Property<Stage.Resolved>> {

guard let path = value.graphqlPath else {
return .resolved(Property(code: value.code, name: value.name, type: value.type, usr: value.usr, graphqlPath: nil))
return .resolved(Property(code: value.code, name: value.name, type: value.type, graphqlPath: nil))
}

return try locateErrors(with: path.parsed.extracted.code.location) {
return try resolver
.resolve(value: path, in: value, using: context)
.map { path in
Property(code: value.code, name: value.name, type: value.type, usr: value.usr, graphqlPath: path)
Property(code: value.code, name: value.name, type: value.type, graphqlPath: path)
}
}
}
Expand Down
Loading

0 comments on commit b094a6b

Please sign in to comment.