Skip to content

Commit

Permalink
Adjusted file structure to accommodate two generated files (#1299)
Browse files Browse the repository at this point in the history
* Adjusted file structure to accommodate two generated files

* Adjusted scripting

* Removed Description, Diffable, Equality for now

* Removed Stencil import

* Updated templates

* Updated scripting

* Updated generated coding

* Disable build deletion (temp)

* Updated template

* Updated generated code

* updated generated code

* Updated generated code

* Adjusted scripting

* Updated scripting

* Updated generated code

* Reverted template deletion

* Removed Stencil imports

* Updated generated code

* trigger CI

* Removed description, diffable and equality stencil templates

* Reverted temporary changes

* Commented failing tests

* Skipping JSExport for description & hash

* Updated generated code

* Enabled failing tests

* Adding stencil templates back to test tests

* Reset generated file for linux for test

* Reverted Extensions for testing

* Reverted ParserResultsComposed

* Attempt to fix unit tests

* Attempt to resolve unit test

* Reverted TypeName asSource

* Reverted TypeName revertion

* Reverted revert of ParserResultComposed

* Reverted revert of Extensions

* Reverted revert of Linux.content.generated

* Reverted attempts to fix unit tests

* Fix for the failing codegen tests

* Added clarifying comment

* Removed description, diffable and equality stencil templates

* Updated generated code

* Tinkering with optimization level for speed boost

* Excluded stencil templates for codegen

* Fixed wrong compiler flag

* Removed speed optimization to a separate PR

* Reverted test code
  • Loading branch information
art-divin committed Mar 17, 2024
1 parent e4b50fa commit 7f41d12
Show file tree
Hide file tree
Showing 80 changed files with 13,276 additions and 11,279 deletions.
10 changes: 10 additions & 0 deletions .sourcery-macOS.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
sources:
- SourceryRuntime/Sources/Common
- SourceryRuntime/Sources/macOS
templates:
- Sourcery/Templates/Coding.stencil
- Sourcery/Templates/JSExport.ejs
- Sourcery/Templates/Typed.stencil
- Sourcery/Templates/TypedSpec.stencil
output:
SourceryRuntime/Sources/Generated
10 changes: 10 additions & 0 deletions .sourcery-ubuntu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
sources:
- SourceryRuntime/Sources/Common
- SourceryRuntime/Sources/Linux
templates:
- Sourcery/Templates/Coding.stencil
- Sourcery/Templates/JSExport.ejs
- Sourcery/Templates/Typed.stencil
- Sourcery/Templates/TypedSpec.stencil
output:
SourceryRuntime/Sources/Generated
6 changes: 0 additions & 6 deletions .sourcery.yml

This file was deleted.

8 changes: 5 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@ end

task :run_sourcery do
print_info "Generating internal boilerplate code"
sh "#{CLI_DIR}bin/sourcery"
sh "#{CLI_DIR}bin/sourcery --config .sourcery-macOS.yml"
sh "#{CLI_DIR}bin/sourcery --config .sourcery-ubuntu.yml"
end

desc "Update internal boilerplate code"
task :generate_internal_boilerplate_code => [:fat_build, :run_sourcery, :clean] do
sh "Scripts/package_content \"SourceryRuntime/Sources\" > \"SourcerySwift/Sources/SourceryRuntime.content.generated.swift\""
task :generate_internal_boilerplate_code => [:build, :run_sourcery] do
sh "Scripts/package_content \"SourceryRuntime/Sources/Common,SourceryRuntime/Sources/macOS,SourceryRuntime/Sources/Generated\" \"true\" > \"SourcerySwift/Sources/SourceryRuntime.content.generated.swift\""
sh "Scripts/package_content \"SourceryRuntime/Sources/Common,SourceryRuntime/Sources/Linux,SourceryRuntime/Sources/Generated\" \"false\" > \"SourcerySwift/Sources/SourceryRuntime_Linux.content.generated.swift\""
generated_files = `git status --porcelain`
.split("\n")
.select { |item| item.include?('.generated.') }
Expand Down
71 changes: 43 additions & 28 deletions Scripts/package_content
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
/// Merge all Swift files contained in FOLDER into swift code that can be used by the FolderSynchronizer.
/// Example: $0 Sources/SourceryRuntime > file.swift
/// Options:
/// FOLDER: the path where the Swift files to merge are
/// FOLDERS: the paths where the Swift files to merge are, separated with comma ","
/// isForDarwinPlatform: if true, the generated code will be compilable on Darwin platforms
/// -h: Display this help message
import Foundation

Expand Down Expand Up @@ -40,37 +41,45 @@ extension String {
}
}

func package(folder folderPath: String) throws {
func package(folders folderPaths: [String], isForDarwinPlatform: Bool) throws {
if !isForDarwinPlatform {
print("#if !canImport(ObjectiveC)")
} else {
print("#if canImport(ObjectiveC)")
}
print("let sourceryRuntimeFiles: [FolderSynchronizer.File] = [")
let folderURL = URL(fileURLWithPath: folderPath)
for folderPath in folderPaths {
let folderURL = URL(fileURLWithPath: folderPath)

guard let enumerator = FileManager.default.enumerator(at: folderURL, includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles, .skipsPackageDescendants]) else {
print("Unable to retrieve file enumerator")
exit(1)
}
var files = [URL]()
for case let fileURL as URL in enumerator {
do {
let fileAttributes = try fileURL.resourceValues(forKeys:[.isRegularFileKey])
if fileAttributes.isRegularFile! {
files.append(fileURL)
guard let enumerator = FileManager.default.enumerator(at: folderURL, includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles, .skipsPackageDescendants]) else {
print("Unable to retrieve file enumerator")
exit(1)
}
var files = [URL]()
for case let fileURL as URL in enumerator {
do {
let fileAttributes = try fileURL.resourceValues(forKeys:[.isRegularFileKey])
if fileAttributes.isRegularFile! {
files.append(fileURL)
}
} catch {
print(error, fileURL)
}
} catch {
print(error, fileURL)
}

try files
.sorted(by: { $0.lastPathComponent < $1.lastPathComponent })
.forEach { sourceFileURL in
print(" .init(name: \"\(sourceFileURL.lastPathComponent)\", content:")
print("\"\"\"")
let content = try String(contentsOf: sourceFileURL, encoding: .utf8)
.escapedSwiftTokens()
print(content)
print("\"\"\"),")
}
}

try files
.sorted(by: { $0.lastPathComponent < $1.lastPathComponent })
.forEach { sourceFileURL in
print(" .init(name: \"\(sourceFileURL.lastPathComponent)\", content:")
print("\"\"\"")
let content = try String(contentsOf: sourceFileURL, encoding: .utf8)
.escapedSwiftTokens()
print(content)
print("\"\"\"),")
}
print("]")
print("#endif")
}

func main() {
Expand All @@ -82,10 +91,16 @@ func main() {
print("Missing folderPath argument")
exit(1)
}
let folder = CommandLine.arguments[1]
guard CommandLine.arguments.count > 2 else {
print("Missing isForDarwinPlatform argument")
exit(1)
}
let foldersPaths = CommandLine.arguments[1]
let isForDarwinPlatform = Bool(CommandLine.arguments[2]) ?? false
let folders = foldersPaths.split(separator: ",").map(String.init)

do {
try package(folder: folder)
try package(folders: folders, isForDarwinPlatform: isForDarwinPlatform)
} catch {
print("Failed with error: \(error)")
exit(1)
Expand Down
2 changes: 2 additions & 0 deletions Sourcery/Templates/JSExport.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// swiftlint:disable vertical_whitespace trailing_newline

#if canImport(JavaScriptCore)
import JavaScriptCore

<%_ for (type of types.implementing.AutoJSExport) { -%>
Expand All @@ -15,3 +16,4 @@ import JavaScriptCore
extension <%= type.name %>: <%= type.name %>AutoJSExport {}
<%_ } %>
<%_ } %>
#endif
2 changes: 2 additions & 0 deletions Sourcery/Templates/Typed.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ extension {{ type.name }} {
{{ type.accessLevel }} var isClosure: Bool { return typeName.isClosure }
/// Whether type is an array. Shorthand for `typeName.isArray`
{{ type.accessLevel }} var isArray: Bool { return typeName.isArray }
/// Whether type is a set. Shorthand for `typeName.isSet`
{{ type.accessLevel }} var isSet: Bool { return typeName.isSet }
/// Whether type is a dictionary. Shorthand for `typeName.isDictionary`
{{ type.accessLevel }} var isDictionary: Bool { return typeName.isDictionary }
}{% endfor %}
2 changes: 2 additions & 0 deletions Sourcery/Templates/TypedSpec.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class TypedSpec: QuickSpec {
return variable?.typeName ?? TypeName(name: "")
}

#if canImport(ObjectiveC)
it("can report optional via KVC") {
expect({{ type.name }}(typeName: typeName("Int?")).value(forKeyPath: "isOptional") as? Bool).to(equal(true))
expect({{ type.name }}(typeName: typeName("Int!")).value(forKeyPath: "isOptional") as? Bool).to(equal(true))
Expand Down Expand Up @@ -68,6 +69,7 @@ class TypedSpec: QuickSpec {
sut.typeName.actualTypeName = typeName("Int")
expect(sut.value(forKeyPath: "actualTypeName") as? TypeName).to(equal(typeName("Int")))
}
#endif
}
{% endfor %}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public final class Actor: Type {

/// Whether type is final
public var isFinal: Bool {
return modifiers.contains { $0.name == "final" }
modifiers.contains { $0.name == "final" }
}

/// :nodoc:
Expand Down Expand Up @@ -54,11 +54,12 @@ public final class Actor: Type {
}

/// :nodoc:
// sourcery: skipJSExport
override public var description: String {
var string = super.description
string += ", "
string += "kind = \(String(describing: self.kind)), "
string += "isFinal = \(String(describing: self.isFinal))"
string.append(", ")
string.append("kind = \(String(describing: self.kind)), ")
string.append("isFinal = \(String(describing: self.isFinal))")
return string
}

Expand All @@ -72,6 +73,8 @@ public final class Actor: Type {
return results
}

/// :nodoc:
// sourcery: skipJSExport
public override var hash: Int {
var hasher = Hasher()
hasher.combine(super.hash)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ public class Attribute: NSObject, AutoCoding, AutoEquatable, AutoDiffable, AutoJ
return results
}

/// :nodoc:
// sourcery: skipJSExport
public override var hash: Int {
var hasher = Hasher()
hasher.combine(self.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public final class Class: Type {

/// Whether type is final
public var isFinal: Bool {
return modifiers.contains { $0.name == "final" }
modifiers.contains { $0.name == "final" }
}

/// :nodoc:
Expand Down Expand Up @@ -53,11 +53,12 @@ public final class Class: Type {
}

/// :nodoc:
// sourcery: skipJSExport
override public var description: String {
var string = super.description
string += ", "
string += "kind = \(String(describing: self.kind)), "
string += "isFinal = \(String(describing: self.isFinal))"
string.append(", ")
string.append("kind = \(String(describing: self.kind)), ")
string.append("isFinal = \(String(describing: self.isFinal))")
return string
}

Expand All @@ -71,6 +72,8 @@ public final class Class: Type {
return results
}

/// :nodoc:
// sourcery: skipJSExport
public override var hash: Int {
var hasher = Hasher()
hasher.combine(super.hash)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class Import: NSObject, SourceryModelWithoutDescription, Diffable {
return results
}

/// :nodoc:
// sourcery: skipJSExport
public override var hash: Int {
var hasher = Hasher()
hasher.combine(self.kind)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class Modifier: NSObject, AutoCoding, AutoEquatable, AutoDiffable, AutoJS
return results
}

/// :nodoc:
// sourcery: skipJSExport
public override var hash: Int {
var hasher = Hasher()
hasher.combine(self.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public final class Protocol: Type {
}
}

// sourcery: skipCoding
/// list of generic requirements
public override var genericRequirements: [GenericRequirement] {
didSet {
Expand Down Expand Up @@ -75,11 +76,12 @@ public final class Protocol: Type {
}

/// :nodoc:
// sourcery: skipJSExport
override public var description: String {
var string = super.description
string += ", "
string += "kind = \(String(describing: self.kind)), "
string += "associatedTypes = \(String(describing: self.associatedTypes)), "
string.append(", ")
string.append("kind = \(String(describing: self.kind)), ")
string.append("associatedTypes = \(String(describing: self.associatedTypes)), ")
return string
}

Expand All @@ -94,6 +96,8 @@ public final class Protocol: Type {
return results
}

/// :nodoc:
// sourcery: skipJSExport
public override var hash: Int {
var hasher = Hasher()
hasher.combine(self.associatedTypes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public final class ProtocolComposition: Type {
}

/// :nodoc:
// sourcery: skipJSExport
override public var description: String {
var string = super.description
string += ", "
Expand All @@ -75,6 +76,8 @@ public final class ProtocolComposition: Type {
return results
}

/// :nodoc:
// sourcery: skipJSExport
public override var hash: Int {
var hasher = Hasher()
hasher.combine(self.composedTypeNames)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public final class Struct: Type {
}

/// :nodoc:
// sourcery: skipJSExport
override public var description: String {
var string = super.description
string += ", "
Expand All @@ -75,6 +76,8 @@ public final class Struct: Type {
return results
}

/// :nodoc:
// sourcery: skipJSExport
public override var hash: Int {
var hasher = Hasher()
hasher.combine(super.hash)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ public final class ArrayType: NSObject, SourceryModel, Diffable {
}

/// :nodoc:
// sourcery: skipJSExport
override public var description: String {
var string = "\(Swift.type(of: self)): "
string += "name = \(String(describing: self.name)), "
string += "elementTypeName = \(String(describing: self.elementTypeName)), "
string += "asGeneric = \(String(describing: self.asGeneric)), "
string += "asSource = \(String(describing: self.asSource))"
string.append("name = \(String(describing: self.name)), ")
string.append("elementTypeName = \(String(describing: self.elementTypeName)), ")
string.append("asGeneric = \(String(describing: self.asGeneric)), ")
string.append("asSource = \(String(describing: self.asSource))")
return string
}

Expand All @@ -54,6 +55,8 @@ public final class ArrayType: NSObject, SourceryModel, Diffable {
return results
}

/// :nodoc:
// sourcery: skipJSExport
public override var hash: Int {
var hasher = Hasher()
hasher.combine(self.name)
Expand Down
Loading

0 comments on commit 7f41d12

Please sign in to comment.