Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
helje5 committed Aug 24, 2022
2 parents c2a80a7 + db127eb commit bdc3b91
Show file tree
Hide file tree
Showing 18 changed files with 422 additions and 70 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
6 changes: 6 additions & 0 deletions Plugins/Libraries/LighterGeneration/GenModel/Property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ public extension EntityInfo {

/// Whether this property has a type that can be generated by the SQLite
/// database.
/// https://www.sqlite.org/lang_createtable.html#rowid
/// https://www.sqlite.org/autoinc.html
/// Note: This is only really true for `INTEGER`, just `INT` doesn't trigger
/// the ID generation!!
public var canBeDatabaseGenerated : Bool {
// If the key is synthesized, i.e. not marked as a pkey in the DB, the DB
// won't generate a value for it!
columnType == .integer && isPrimaryKey && !isPrimaryKeySynthesized
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public extension Fancifier.Options {

// swiftMapping
// SwiftMapping dictionary
init(section: ConfigFile.Section?) {
self.init()
guard let section = section else { return }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import LighterCodeGenAST
*
* Example:
* ```json
* "codeStyle": {
* "CodeStyle": {
* "functionCommentStyle" : "**",
* "indent" : " ",
* "lineLength" : 80
* },
*
* "embeddedLighter": {
* "EmbeddedLighter": {
* "selects": {
* "syncYield" : { "columns": 8, "sorts": 2 },
* "syncArray" : { "columns": 8, "sorts": 2 },
* "asyncArray" : { "columns": 8, "sorts": 2 }
* }
* },
*
* "swiftMapping": {
* "SwiftMapping": {
* "databaseTypeName": { ... }
* }
* ```
Expand Down Expand Up @@ -112,11 +112,13 @@ public extension LighterConfiguration {
embeddedLighter = .disabled
}
else {
embeddedLighter = .init(section: section?[section: "EmbeddedLighter"])
embeddedLighter = LighterConfiguration
.EmbeddedLighter(section: section?[section: "EmbeddedLighter"])
}

swiftMapping = .init(section: section?[section: "SwiftMapping"])
swiftMapping = Fancifier.Options(section: section?[section: "SwiftMapping"])

codeGeneration = .init(section: section?[section: "CodeGeneration"])
codeGeneration = EnlighterASTGenerator
.Options(section: section?[section: "CodeGeneration"])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Copyright © 2022 ZeeZide GmbH.
//

import Foundation
import LighterCodeGenAST

extension EnlighterASTGenerator {
Expand Down Expand Up @@ -44,9 +45,7 @@ extension EnlighterASTGenerator {
comment: commentForPropertyVariable(property))
},
computedProperties : idProperty.flatMap({ [ $0 ] }) ?? [],
functions : [
buildRegularInitForEntity(entity)
],
functions : [ buildRegularInitForEntity(entity) ],
comment : generateCommentForRecordStruct(entity)
)
}
Expand All @@ -57,10 +56,23 @@ extension EnlighterASTGenerator {
func buildInitParameter(_ property: EntityInfo.Property)
-> FunctionDeclaration.Parameter
{
.init(
FunctionDeclaration.Parameter(
keyword: property.name, name: property.name,
type: type(for: property),
defaultValue: defaultValue(for: property)
defaultValue: defaultValue(for: property) ?? {
if property.isPrimaryKey {
if property.propertyType == .uuid { // null or not
// If the primary key is a UUID, generate a default value for that.
return .call(name: "UUID")
}
if property.canBeDatabaseGenerated && property.isNotNull &&
property.propertyType == .integer
{
return .variableReference(instance: "Int", name: "min")
}
}
return nil
}()
)
}

Expand Down Expand Up @@ -89,13 +101,17 @@ extension EnlighterASTGenerator {
let sqlType = property.columnType ?? .any

var ms = "\(prefix) `\(property.externalName)` (`\(sqlType.rawValue)`), "
ms += property.isNotNull ? "required" : "optional"
ms += property.isNotNull ? "required" : "optional"
switch defaultValue {
case .none : break // no defaults
case .literal(.nil) : ms += " (default: `nil`)"
case .literal(.integer(let value)) : ms += " (default: `\(value)`)"
case .literal(.double (let value)) : ms += " (default: `\(value)`)"
case .literal(.string) : ms += " (has default string)"
default : ms += " (has default)"
case .literal(.string (let value)) :
if value.isEmpty { ms += " (empty string as default)" }
else { ms += " (has default string #\(value.count))" }
default: // this is hit w/ complex expressions!
ms += " (has default)"
}
ms += "."
return ms
Expand Down
Loading

0 comments on commit bdc3b91

Please sign in to comment.