Skip to content

Commit

Permalink
Rework handling of module databases
Browse files Browse the repository at this point in the history
To fix issue #11. The module accessor was generated,
even if the module didn't actually bundle database
resources (i.e. binary SQLite databases).

This commit should fix the issue, though the desired
behaviour might still be a little different. And
maybe should be dealt with in sqlite2swift instead
(which doesn't currently know about resource files).
  • Loading branch information
helje5 committed Aug 24, 2022
1 parent 2ed6af2 commit db127eb
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 47 deletions.
42 changes: 28 additions & 14 deletions Plugins/Enlighter/Enlighter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ struct Enlighter: BuildToolPlugin {
?? (rootJSON["outputFile"] as? String)

let targetConfig = EnlighterTargetConfig(
extensions : extensions(in: rootJSON, target: target.name),
outputFile : outputFile,
verbose : verbose,
configURL : configURL
dbExtensions : dbExtensions(in: rootJSON, target: target.name),
extensions : extensions(in: rootJSON, target: target.name),
outputFile : outputFile,
verbose : verbose,
configURL : configURL
)
guard !targetConfig.extensions.isEmpty else {
print("Skipping \"\(target.name)\",",
Expand Down Expand Up @@ -97,6 +98,16 @@ struct Enlighter: BuildToolPlugin {
#endif
}

fileprivate func dbExtensions(in rootJSON: [ String : Any ], target: String)
-> Set<String>
{
let targetJSON = rootJSON[target] as? [ String : Any ]
let exts1 = (targetJSON? ["databaseExtensions"] as? [ String ])
?? (rootJSON ["databaseExtensions"] as? [ String ])
?? (defaultConfig["databaseExtensions"] as? [ String ])
?? []
return Set(exts1)
}
fileprivate func extensions(in rootJSON: [ String : Any ], target: String)
-> Set<String>
{
Expand All @@ -112,7 +123,7 @@ struct Enlighter: BuildToolPlugin {

return Set(exts1).union(exts2)
}

fileprivate func locateConfigFile(in context: PackagePlugin.PluginContext)
-> URL?
{
Expand Down Expand Up @@ -151,7 +162,7 @@ struct Enlighter: BuildToolPlugin {
}
return result
}

private func generate(context : PackagePlugin.PluginContext,
target : SwiftSourceModuleTarget,
configuration : EnlighterTargetConfig,
Expand Down Expand Up @@ -191,8 +202,9 @@ struct Enlighter: BuildToolPlugin {
if configuration.verbose {
args.append("--verbose")
}
if !group.resourceURLs.isEmpty {
args.append("--has-resources")
if let name = group.moduleFilename(using: configuration.dbExtensions) {
args.append("--module-filename")
args.append(name)
}
args.append(configuration.configURL?.path ?? "default")
args.append(target.name) // required to resolve configs
Expand Down Expand Up @@ -264,10 +276,11 @@ extension Enlighter: XcodeBuildToolPlugin {
?? (rootJSON["outputFile"] as? String)

let targetConfig = EnlighterTargetConfig(
extensions : extensions(in: rootJSON, target: target.name),
outputFile : outputFile,
verbose : verbose,
configURL : configURL
dbExtensions : dbExtensions(in: rootJSON, target: target.name),
extensions : extensions(in: rootJSON, target: target.name),
outputFile : outputFile,
verbose : verbose,
configURL : configURL
)
guard !targetConfig.extensions.isEmpty else {
print("Skipping \"\(target.name)\",",
Expand Down Expand Up @@ -353,8 +366,9 @@ extension Enlighter: XcodeBuildToolPlugin {
if configuration.verbose {
args.append("--verbose")
}
if !group.resourceURLs.isEmpty {
args.append("--has-resources")
if let name = group.moduleFilename(using: configuration.dbExtensions) {
args.append("--module-filename")
args.append(name)
}
args.append(configuration.configURL?.path ?? "default")
args.append(target.name) // required to resolve configs
Expand Down
8 changes: 8 additions & 0 deletions Plugins/Enlighter/EnlighterGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ struct EnlighterGroup: CustomStringConvertible {
matches.map(\.lastPathComponent).joined(separator: ",") + ">"
}

func moduleFilename(using extensions: Set<String>) -> String? {
guard !resourceURLs.isEmpty && !extensions.isEmpty else { return nil }
for url in resourceURLs {
if extensions.contains(url.pathExtension) { return url.lastPathComponent }
}
return nil
}

static func load(from baseURL : URL,
resourcesPathes : Set<String>,
configuration : EnlighterTargetConfig)
Expand Down
11 changes: 6 additions & 5 deletions Plugins/Enlighter/EnlighterTargetConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
// Copyright © 2022 ZeeZide GmbH.
//

import Foundation
import struct Foundation.URL

struct EnlighterTargetConfig { // that's all we need from Lighter.json

let extensions : Set<String>
let outputFile : String?
let dbExtensions : Set<String>
let extensions : Set<String>
let outputFile : String?

let verbose : Bool
let configURL : URL?
let verbose : Bool
let configURL : URL?
}
10 changes: 9 additions & 1 deletion Plugins/GenerateCodeForSQLite/EnlighterGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ struct EnlighterGroup: CustomStringConvertible {
"<Group[\(stem)]: " +
matches.map(\.lastPathComponent).joined(separator: ",") + ">"
}


func moduleFilename(using extensions: Set<String>) -> String? {
guard !resourceURLs.isEmpty && !extensions.isEmpty else { return nil }
for url in resourceURLs {
if extensions.contains(url.pathExtension) { return url.lastPathComponent }
}
return nil
}

static func load(from baseURL : URL,
resourcesPathes : Set<String>,
configuration : EnlighterTargetConfig)
Expand Down
11 changes: 6 additions & 5 deletions Plugins/GenerateCodeForSQLite/EnlighterTargetConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
// Copyright © 2022 ZeeZide GmbH.
//

import Foundation
import struct Foundation.URL

struct EnlighterTargetConfig { // that's all we need from Lighter.json

let extensions : Set<String>
let outputFile : String?
let dbExtensions : Set<String>
let extensions : Set<String>
let outputFile : String?

let verbose : Bool
let configURL : URL?
let verbose : Bool
let configURL : URL?
}
38 changes: 26 additions & 12 deletions Plugins/GenerateCodeForSQLite/GenerateCodeForSQLite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ struct GenerateCodeForSQLite: CommandPlugin {
?? (rootJSON["outputFile"] as? String)

let targetConfig = EnlighterTargetConfig(
extensions : extensions(in: rootJSON, target: target.name),
outputFile : outputFile,
verbose : args.verbose,
configURL : configURL
dbExtensions : dbExtensions(in: rootJSON, target: target.name),
extensions : extensions(in: rootJSON, target: target.name),
outputFile : outputFile,
verbose : args.verbose,
configURL : configURL
)
guard !targetConfig.extensions.isEmpty else {
if args.verbose {
Expand Down Expand Up @@ -117,6 +118,16 @@ struct GenerateCodeForSQLite: CommandPlugin {
#endif
}

fileprivate func dbExtensions(in rootJSON: [ String : Any ], target: String)
-> Set<String>
{
let targetJSON = rootJSON[target] as? [ String : Any ]
let exts1 = (targetJSON? ["databaseExtensions"] as? [ String ])
?? (rootJSON ["databaseExtensions"] as? [ String ])
?? (defaultConfig["databaseExtensions"] as? [ String ])
?? []
return Set(exts1)
}
fileprivate func extensions(in rootJSON: [ String : Any ], target: String)
-> Set<String>
{
Expand Down Expand Up @@ -208,8 +219,9 @@ struct GenerateCodeForSQLite: CommandPlugin {
if configuration.verbose {
args.append("--verbose")
}
if !group.resourceURLs.isEmpty {
args.append("--has-resources")
if let name = group.moduleFilename(using: configuration.dbExtensions) {
args.append("--module-filename")
args.append(name)
}
args.append(configuration.configURL?.path ?? "default")
args.append(target.name) // required to resolve configs
Expand Down Expand Up @@ -281,10 +293,11 @@ extension GenerateCodeForSQLite: XcodeCommandPlugin {
?? (rootJSON["outputFile"] as? String)

let targetConfig = EnlighterTargetConfig(
extensions : extensions(in: rootJSON, target: target.name),
outputFile : outputFile,
verbose : args.verbose,
configURL : configURL
dbExtensions : dbExtensions(in: rootJSON, target: target.name),
extensions : extensions(in: rootJSON, target: target.name),
outputFile : outputFile,
verbose : args.verbose,
configURL : configURL
)
guard !targetConfig.extensions.isEmpty else {
if args.verbose {
Expand Down Expand Up @@ -369,8 +382,9 @@ extension GenerateCodeForSQLite: XcodeCommandPlugin {
if configuration.verbose {
args.append("--verbose")
}
if !group.resourceURLs.isEmpty {
args.append("--has-resources")
if let name = group.moduleFilename(using: configuration.dbExtensions) {
args.append("--module-filename")
args.append(name)
}
args.append(configuration.configURL?.path ?? "default")
args.append(target.name) // required to resolve configs
Expand Down
30 changes: 25 additions & 5 deletions Plugins/Tools/sqlite2swift/Arguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct Arguments {
/// Enable verbose logging
let verbose : Bool
/// Whether one of the input files is also a package resource.
let hasResources : Bool
let moduleFilename : String?

/// The URL to the configuration file, Lighter.json.
let configURL : URL
Expand Down Expand Up @@ -55,12 +55,25 @@ struct Arguments {
else {
verbose = false
}
if let idx = args.firstIndex(of: "--has-resources") {
hasResources = true

var wantsModuleFilename = false
var moduleFilename : String?
if let idx = args.firstIndex(of: "--module-filename") {
wantsModuleFilename = true
let valueIdx = args.index(after: idx)
if valueIdx < args.endIndex {
moduleFilename = args[valueIdx]
args.remove(at: valueIdx)
}
else {
print("No module filename?!")
}
args.remove(at: idx)
}
else {
hasResources = false

if args.count < 4 {
Self.usage(toolName)
throw ExitCodes.invalidArguments
}

configURL = args[0] != "default"
Expand All @@ -78,6 +91,13 @@ struct Arguments {
stem = firstFileName.firstIndex(where: { $0 == "-" || $0 == "." })
.flatMap { String(firstFileName[..<$0]) }
?? firstFileName

if wantsModuleFilename {
self.moduleFilename = moduleFilename ?? inputURLs.first?.lastPathComponent
}
else {
self.moduleFilename = nil
}
}

/// Print the arguments.
Expand Down
6 changes: 1 addition & 5 deletions Plugins/Tools/sqlite2swift/SQLite2Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@ struct SQLite2Swift {
filename : args.outputURL.lastPathComponent,
options : config.codeGeneration
)

let moduleFileName = args.inputURLs.first?.lastPathComponent
return gen.generateCombinedFile(
moduleFileName: args.hasResources ? moduleFileName : nil
)
return gen.generateCombinedFile(moduleFileName: args.moduleFilename)
}

private func writeToOutput(_ unit: CompilationUnit) throws {
Expand Down

0 comments on commit db127eb

Please sign in to comment.