Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,4 @@ fastlane/test_output
# https://github.com/johnno1962/injectionforxcode

iOSInjectionProject/
.swiftpm/
51 changes: 29 additions & 22 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 52 additions & 53 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.5
// swift-tools-version: 5.6

import PackageDescription

Expand All @@ -14,100 +14,99 @@ let package = Package(
.library(name: "SwiftStandAlone", targets: ["SwiftStandAlone"]),
.library(name: "KotlinStandAlone", targets: ["KotlinStandAlone"]),
.library(name: "LexiconGenerators", targets: ["LexiconGenerators"]),
.executable(name: "lexicon-generate", targets: ["lexicon-generate"]),
.plugin(name: "SwiftStandAloneGeneratorPlugin", targets: ["SwiftStandAloneGeneratorPlugin"]),
.plugin(name: "SwiftLibraryGeneratorPlugin", targets: ["SwiftLibraryGeneratorPlugin"]),
],
dependencies: [
.package(url: "https://github.com/screensailor/Hope", .branch("trunk")),
.package(url: "https://github.com/screensailor/Hope", branch: "trunk"),
.package(url: "https://github.com/apple/swift-collections", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.1.2")
],
targets: [

// MARK: Lexicon

.target(
name: "Lexicon",
dependencies: [
.product(name: "Collections", package: "swift-collections")
],
swiftSettings: [.define("EDITOR")] // TODO: meke this opt in
swiftSettings: [.define("EDITOR")] // TODO: make this opt in
),
.testTarget(
name: "LexiconTests",
dependencies: [
"Hope",
"Lexicon"
],
resources: [
.copy("Resources")
resources: [.copy("Resources")]
),
.target(
name: "LexiconGenerators",
dependencies: [
"Lexicon",
"SwiftLexicon",
"SwiftStandAlone",
"KotlinStandAlone"
]
),
.target(
name: "SwiftLexicon",
dependencies: [
"Lexicon"
]
),

// MARK: LexiconGenerators

.target(
name: "LexiconGenerators",
dependencies: [
"Lexicon",
"SwiftLexicon",
"SwiftStandAlone",
"KotlinStandAlone"
]
),

// MARK: SwiftLexicon

.target(
name: "SwiftLexicon",
dependencies: [
"Lexicon",
]
),
.testTarget(
name: "SwiftLexiconTests",
dependencies: [
"Hope",
"SwiftLexicon"
],
resources: [
.copy("Resources"),
resources: [.copy("Resources")]
),
.target(
name: "SwiftStandAlone",
dependencies: [
"Lexicon",
]
),

// MARK: SwiftStandAlone

.target(
name: "SwiftStandAlone",
dependencies: [
"Lexicon",
]
),
.testTarget(
name: "SwiftStandAloneTests",
dependencies: [
"Hope",
"SwiftStandAlone"
],
resources: [
.copy("Resources"),
resources: [.copy("Resources")]
),
.target(
name: "KotlinStandAlone",
dependencies: [
"Lexicon",
]
),

// MARK: KotlinStandAlones

.target(
name: "KotlinStandAlone",
dependencies: [
"Lexicon",
]
),
.testTarget(
name: "KotlinStandAloneTests",
dependencies: [
"Hope",
"KotlinStandAlone"
],
resources: [
.copy("Resources"),
resources: [.copy("Resources")]
),
.executableTarget(
name: "lexicon-generate",
dependencies: [
.target(name: "LexiconGenerators"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "Collections", package: "swift-collections")
]
),
.plugin(
name: "SwiftStandAloneGeneratorPlugin",
capability: .buildTool(),
dependencies: ["lexicon-generate"]
),
.plugin(
name: "SwiftLibraryGeneratorPlugin",
capability: .buildTool(),
dependencies: ["lexicon-generate"]
)
]
)
28 changes: 28 additions & 0 deletions Plugins/SwiftLibraryGeneratorPlugin/plugin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import PackagePlugin
import Foundation

@main
struct SwiftLibraryGeneratorPlugin: BuildToolPlugin {
Comment thread
ollieatkinson marked this conversation as resolved.

func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
let lexicon = try context.tool(named: "lexicon-generate")
let output = context.pluginWorkDirectory.appending("GeneratedSources")
return FileManager.default.enumerator(atPath: target.directory.string)?
.compactMap { value in (value as? String).map(target.directory.appending) }
.filter { path in (path.extension ?? "").hasSuffix("lexicon") }
.map { input in
let file = output.appending(input.stem)
return .buildCommand(
displayName: "Generate \(input)",
executable: lexicon.path,
arguments: [
input.string,
"--output", file.string,
"--type", "swift"
],
inputFiles: [input],
outputFiles: [file]
)
} ?? []
}
}
28 changes: 28 additions & 0 deletions Plugins/SwiftStandAloneGeneratorPlugin/plugin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import PackagePlugin
import Foundation

@main
struct SwiftStandAloneGeneratorPlugin: BuildToolPlugin {

func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
let lexicon = try context.tool(named: "lexicon-generate")
let output = context.pluginWorkDirectory.appending("GeneratedSources")
return FileManager.default.enumerator(atPath: target.directory.string)?
.compactMap { value in (value as? String).map(target.directory.appending) }
.filter { path in (path.extension ?? "").hasSuffix("lexicon") }
.map { input in
let file = output.appending(input.stem)
return .buildCommand(
displayName: "Generate \(input)",
executable: lexicon.path,
arguments: [
input.string,
"--output", file.string,
"--type", "swift-standalone"
],
inputFiles: [input],
outputFiles: [file]
)
} ?? []
}
}
3 changes: 2 additions & 1 deletion Sources/KotlinStandAlone/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public enum Generator: CodeGenerator {

// TODO: prefixes?

public static let utType = UTType.sourceCode
public static let utType = UTType(filenameExtension: "kt", conformingTo: .sourceCode)!
public static let command = "kotlin"

public static func generate(_ json: Lexicon.Graph.JSON) throws -> Data {
return Data(json.kotlin().utf8)
Expand Down
1 change: 1 addition & 0 deletions Sources/Lexicon/CodeGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import UniformTypeIdentifiers

public protocol CodeGenerator {
static var utType: UTType { get }
static var command: String { get }
static func generate(_ json: Lexicon.Graph.JSON) throws -> Data
}
3 changes: 2 additions & 1 deletion Sources/Lexicon/JSONClasses.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import UniformTypeIdentifiers
public enum JSONClasses: CodeGenerator {

public static let utType: UTType = .json

public static let command = "json"

public static func generate(_ json: Lexicon.Graph.JSON) throws -> Data {
let encoder = Encoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
Expand Down
11 changes: 9 additions & 2 deletions Sources/Lexicon/Lemma.swift
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,15 @@ extension Lemma {

func lazy_ownType() -> [ID: Unowned<Lemma>] {
var o: [ID: Unowned<Lemma>] = [:]
for id in node.type {
o[id] = lexicon.dictionary[id].map(Unowned.init)
if isGraphNode {
for id in node.type {
o[id] = lexicon.dictionary[id].map(Unowned.init)
}
} else {
for id in (parent?.node.type).or([]) {
guard let node = lexicon.dictionary[id]?.children[name] else { continue }
o[node.id] = Unowned(node)
}
}
return o
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/LexiconGenerators/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public extension Lexicon.Graph.JSON {

static let generators: OrderedDictionary<String, CodeGenerator.Type> = [

"Swift Library": SwiftLexicon.Generator.self,
"Swift": SwiftLexicon.Generator.self,

"Swift Stand-Alone": SwiftStandAlone.Generator.self,

Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftLexicon/Event.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public struct Event: @unchecked Sendable, Hashable, Identifiable, CustomStringCo
k(\.L) is A
}

public func `is`<A: I>(_ a: K<A>) -> Bool {
public func `is`<A>(_ a: K<A>) -> Bool {
k is A && a.____.allSatisfy {
k.____[$0] == $1
}
Expand Down
5 changes: 3 additions & 2 deletions Sources/SwiftLexicon/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public enum Generator: CodeGenerator {
// TODO: prefixes?

public static let utType = UTType.swiftSource

public static let command = "swift"

public static func generate(_ json: Lexicon.Graph.JSON) throws -> Data {
guard let o = json.swift().data(using: .utf8) else {
throw "Failed to generate Swift file"
Expand All @@ -24,7 +25,7 @@ private extension Lexicon.Graph.JSON {
func swift() -> String {

return """
@_exported import SwiftLexicon // From https://github.com/screensailor/Lexicon
@_exported import SwiftLexicon // https://github.com/thousandyears/Lexicon
import Foundation

public let \(name) = L_\(name)("\(name)")
Expand Down
3 changes: 2 additions & 1 deletion Sources/SwiftStandAlone/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public enum Generator: CodeGenerator {
// TODO: prefixes?

public static let utType = UTType.swiftSource

public static let command = "swift-standalone"

public static func generate(_ json: Lexicon.Graph.JSON) throws -> Data {
guard let o = json.swift().data(using: .utf8) else {
throw "Failed to generate Swift file"
Expand Down
Loading