Skip to content

Commit

Permalink
Made protocol compositions available for EJS templates. (#1192)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianmuecke committed Aug 24, 2023
1 parent 618b65c commit 3bdaccd
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Added support for Swift Package Manager config ([#1184](https://github.com/krzysztofzablocki/Sourcery/pull/1184))
- Add support to any keyword for function parameter type to AutoMockable.stencil ([#1169](https://github.com/krzysztofzablocki/Sourcery/pull/1169))
- Add support to any keyword for function return type to AutoMockable.stencil([#1186](https://github.com/krzysztofzablocki/Sourcery/pull/1186))
- Add support for protocol compositions in EJS templates. ([#1192](https://github.com/krzysztofzablocki/Sourcery/pull/1192))

## 2.0.3
## Internal Changes
Expand Down
1 change: 0 additions & 1 deletion SourceryRuntime/Sources/AST/ProtocolComposition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import Foundation

// sourcery: skipJSExport
/// Describes a Swift [protocol composition](https://docs.swift.org/swift-book/ReferenceManual/Types.html#ID454).
@objcMembers public final class ProtocolComposition: Type {

Expand Down
1 change: 0 additions & 1 deletion SourceryRuntime/Sources/AST/Typealias.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Foundation

// sourcery: skipJSExport
/// :nodoc:
@objcMembers public final class Typealias: NSObject, Typed, SourceryModel {
// New typealias name
Expand Down
63 changes: 63 additions & 0 deletions SourceryRuntime/Sources/Generated/JSExport.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,54 @@ extension Modifier: ModifierAutoJSExport {}

extension Protocol: ProtocolAutoJSExport {}

@objc protocol ProtocolCompositionAutoJSExport: JSExport {
var kind: String { get }
var composedTypeNames: [TypeName] { get }
var composedTypes: [Type]? { get }
var module: String? { get }
var imports: [Import] { get }
var allImports: [Import] { get }
var accessLevel: String { get }
var name: String { get }
var isUnknownExtension: Bool { get }
var globalName: String { get }
var isGeneric: Bool { get }
var localName: String { get }
var variables: [Variable] { get }
var rawVariables: [Variable] { get }
var allVariables: [Variable] { get }
var methods: [Method] { get }
var rawMethods: [Method] { get }
var allMethods: [Method] { get }
var subscripts: [Subscript] { get }
var rawSubscripts: [Subscript] { get }
var allSubscripts: [Subscript] { get }
var initializers: [Method] { get }
var annotations: Annotations { get }
var documentation: Documentation { get }
var staticVariables: [Variable] { get }
var staticMethods: [Method] { get }
var classMethods: [Method] { get }
var instanceVariables: [Variable] { get }
var instanceMethods: [Method] { get }
var computedVariables: [Variable] { get }
var storedVariables: [Variable] { get }
var inheritedTypes: [String] { get }
var based: [String: String] { get }
var basedTypes: [String: Type] { get }
var inherits: [String: Type] { get }
var implements: [String: Type] { get }
var containedTypes: [Type] { get }
var containedType: [String: Type] { get }
var parentName: String? { get }
var parent: Type? { get }
var supertype: Type? { get }
var attributes: AttributeList { get }
var modifiers: [SourceryModifier] { get }
var fileName: String? { get }
}

extension ProtocolComposition: ProtocolCompositionAutoJSExport {}



Expand Down Expand Up @@ -585,6 +633,21 @@ extension Type: TypeAutoJSExport {}

extension TypeName: TypeNameAutoJSExport {}

@objc protocol TypealiasAutoJSExport: JSExport {
var aliasName: String { get }
var typeName: TypeName { get }
var type: Type? { get }
var module: String? { get }
var parent: Type? { get }
var accessLevel: String { get }
var parentName: String? { get }
var name: String { get }
var isOptional: Bool { get }
var isImplicitlyUnwrappedOptional: Bool { get }
var unwrappedTypeName: String { get }
}

extension Typealias: TypealiasAutoJSExport {}


@objc protocol TypesCollectionAutoJSExport: JSExport {
Expand Down
3 changes: 2 additions & 1 deletion SourceryRuntime/Sources/TemplateContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ import Foundation
"extensions": types.extensions,
"based": types.based,
"inheriting": types.inheriting,
"implementing": types.implementing
"implementing": types.implementing,
"protocolCompositions": types.protocolCompositions
],
"functions": functions,
"type": types.typesByName,
Expand Down
11 changes: 11 additions & 0 deletions SourceryTests/Generating/JavaScriptTemplateSpecs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ class JavaScriptTemplateTests: QuickSpec {
let result = (try? (outputDir + Sourcery().generatedPath(for: templatePath)).read(.utf8))
expect(result).to(equal(expectedResult))
}

it("provides protocol compositions") {
let templatePath = Stubs.jsTemplates + Path("ProtocolCompositions.ejs")
let expectedResult = try? (Stubs.resultDirectory + Path("FooBar.swift")).read(.utf8)

expect { try Sourcery(cacheDisabled: true).processFiles(.sources(Paths(include: [Stubs.sourceDirectory])), usingTemplates: Paths(include: [templatePath]), output: output, baseIndentation: 0) }.toNot(throwError())

let result = (try? (outputDir + Sourcery().generatedPath(for: templatePath)).read(.utf8))
print("expected:\n\(expectedResult)\n\ngot:\n\(result)")
expect(result).to(equal(expectedResult))
}

it("handles includes") {
let templatePath = Stubs.jsTemplates + Path("Includes.ejs")
Expand Down
12 changes: 12 additions & 0 deletions SourceryTests/Stub/JavaScriptTemplates/ProtocolCompositions.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%_
for (composition of types.protocolCompositions.filter((t) => t.annotations.AutoStruct != null)) { %>
struct Any<%- composition.name %>: <%- composition.name %> {
<%_ for (type of composition.composedTypes) { -%>
// MARK: <%- type.name %> properties
<%_ for (variable of type.instanceVariables) { -%>
var <%- variable.name %>: <%- variable.typeName.asSource %>
<% }
} -%>
}
<%_ } -%>
11 changes: 11 additions & 0 deletions SourceryTests/Stub/Result/FooBar.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Generated using Sourcery Major.Minor.Patch — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT

struct AnyFooBar: FooBar {

// MARK: HasFoo properties
var foo: Foo

// MARK: HasBar properties
var bar: Bar
}
11 changes: 11 additions & 0 deletions SourceryTests/Stub/Source/FooBar.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation

protocol HasFoo {
var foo: Foo { get }
}
protocol HasBar {
var bar: Bar { get }
}

// sourcery: AutoStruct
typealias FooBar = HasFoo & HasBar

0 comments on commit 3bdaccd

Please sign in to comment.