Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Обновление Stencil и небольшие доработки #62

Merged
merged 4 commits into from
Oct 7, 2024
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
16 changes: 8 additions & 8 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@
"repositoryURL": "https://github.com/shibapm/Komondor.git",
"state": {
"branch": null,
"revision": "855c74f395a4dc9e02828f58d931be6920bcbf6f",
"version": "1.0.6"
"revision": "90b087b1e39069684b1ff4bf915c2aae594f2d60",
"version": "1.1.3"
}
},
{
"package": "PackageConfig",
"repositoryURL": "https://github.com/shibapm/PackageConfig.git",
"state": {
"branch": null,
"revision": "bf90dc69fa0792894b08a0b74cf34029694ae486",
"version": "0.13.0"
"revision": "58523193c26fb821ed1720dcd8a21009055c7cdb",
"version": "1.1.3"
}
},
{
Expand Down Expand Up @@ -96,17 +96,17 @@
"repositoryURL": "https://github.com/kylef/Stencil.git",
"state": {
"branch": null,
"revision": "94197b3adbbf926348ad8765476a158aa4e54f8a",
"version": "0.14.0"
"revision": "4f222ac85d673f35df29962fc4c36ccfdaf9da5b",
"version": "0.15.1"
}
},
{
"package": "StencilSwiftKit",
"repositoryURL": "https://github.com/SwiftGen/StencilSwiftKit.git",
"state": {
"branch": null,
"revision": "54cbedcdbb4334e03930adcff7343ffaf317bf0f",
"version": "2.8.0"
"revision": "20e2de5322c83df005939d9d9300fab130b49f97",
"version": "2.10.1"
}
},
{
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ let package = Package(
.package(url: "https://github.com/kylef/PathKit.git", from: "1.0.1"),
.package(url: "https://github.com/onevcat/Rainbow", from: "3.0.0"),
.package(url: "https://github.com/mxcl/PromiseKit", from: "6.8.0"),
.package(url: "https://github.com/kylef/Stencil.git", from: "0.13.0"),
.package(url: "https://github.com/SwiftGen/StencilSwiftKit.git", from: "2.7.2"),
.package(url: "https://github.com/kylef/Stencil.git", from: "0.15.1"),
.package(url: "https://github.com/SwiftGen/StencilSwiftKit.git", from: "2.10.1"),
.package(url: "https://github.com/almazrafi/DictionaryCoder.git", from: "1.0.4"),
.package(url: "https://github.com/jpsim/Yams.git", from: "4.0.4"),
.package(url: "https://github.com/kylef/JSONSchema.swift.git", from: "0.5.0"),
Expand Down
87 changes: 56 additions & 31 deletions Sources/AnalyticsGen/Generators/Event/DefaultEventGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,8 @@ final class DefaultEventGenerator: EventGenerator {
let fileManager = FileManager.default

try? fileManager.contentsOfDirectory(atPath: path).forEach { filename in
if filename.hasSuffix(.swiftExtension) {
try fileManager.removeItem(atPath: path + "/" + filename)
}
}
}

private func resolveInternalEventProtocol(event: InternalEvent) -> String {
if event.knownEventName == .screenShown {
return "ScreenShownEvent"
try fileManager.removeItem(atPath: path + "/" + filename)
}

return "InternalEvent"
}

private func resolveExternalEventCategory(event: ExternalEvent) -> ExternalEventContext.Category {
Expand Down Expand Up @@ -93,27 +83,40 @@ final class DefaultEventGenerator: EventGenerator {
private func generate(
parameters: GenerationParameters,
event: Event,
schemaURL: URL,
targetPath: String,
schemePath: [String],
platform: EventPlatform
) throws {
let filename = schemaURL.deletingPathExtension().lastPathComponent.deletingSuffix("event").camelized
let filePath = schemePath
.dropLast()
.map { $0.camelized }
.joined(separator: "/")

let schemeName = schemePath
.last?
.components(separatedBy: ".")
.first?
.deletingSuffix("event") ?? ""

if let internalEvent = event.internal, (internalEvent.platform ?? .androidIOS) == platform {
let schemePath = schemePath.prepending(targetPath).filter { !$0.isEmpty }.joined(separator: "/")
let renderDestination = parameters.render.destination.appending(path: filePath)

if let internalEvent = event.internal, (internalEvent.platform ?? .iOSAndroid) == platform {
try templateRenderer.renderTemplate(
parameters.render.internalTemplate,
to: parameters.render.destination.appending(path: "\(filename)Event.swift"),
to: renderDestination.appending(path: "\(schemeName.camelized)Event.swift"),
context: InternalEventContext(
edition: event.edition,
deprecated: event.deprecated ?? false,
name: event.name,
description: event.description,
category: event.category,
eventName: internalEvent.event,
experiment: event.experiment.map {
InternalEventContext.Experiment(description: $0.description, url: $0.url.absoluteString)
},
structName: filename.appending("Event"),
protocol: resolveInternalEventProtocol(event: internalEvent),
eventName: internalEvent.event,
schemeName: schemeName,
schemePath: schemePath,
parameters: internalEvent.parameters.nonEmpty?.map { parameter in
InternalEventContext.Parameter(
name: parameter.name,
Expand All @@ -131,17 +134,18 @@ final class DefaultEventGenerator: EventGenerator {
)
}

if let externalEvent = event.external, (externalEvent.platform ?? .androidIOS) == platform {
if let externalEvent = event.external, (externalEvent.platform ?? .iOSAndroid) == platform {
try templateRenderer.renderTemplate(
parameters.render.externalTemplate,
to: parameters.render.destination.appending(path: "\(filename)ExternalEvent.swift"),
to: renderDestination.appending(path: "\(schemeName.camelized)ExternalEvent.swift"),
context: ExternalEventContext(
edition: event.edition,
deprecated: event.deprecated ?? false,
name: event.name,
description: event.description,
category: resolveExternalEventCategory(event: externalEvent),
structName: filename.appending("ExternalEvent"),
schemeName: schemeName,
schemePath: schemePath,
action: ExternalEventContext.Action(
description: externalEvent.action.description,
value: externalEvent.action.value,
Expand All @@ -167,30 +171,44 @@ final class DefaultEventGenerator: EventGenerator {
}
}

private func generate(configuration: GeneratedConfiguration, schemasPath: URL) throws {
private func generate(configuration: GeneratedConfiguration, targetPath: String? = nil, schemasPath: URL) throws {
guard let enumerator = FileManager.default.enumerator(at: schemasPath, includingPropertiesForKeys: nil) else {
throw MessageError("Failed to create enumerator at \(schemasPath).")
}

Log.info("(\(configuration.name)) Starting code generation... 🚀")

let generarionParameters = try resolveGenerationParameters(from: configuration)
let platform = configuration.platform ?? .androidIOS
let platform = configuration.platform ?? .iOSAndroid

let events: [(Event, URL)] = try enumerator
let events: [(Event, [String])] = try enumerator
.lazy
.compactMap { $0 as? URL }
.filter { $0.pathExtension == .yamlExtension }
.map { url in
Log.debug("(\(configuration.name)) Reading schema: \(url.lastPathComponent)")
let event: Event = try fileProvider.readFile(at: url.path)
return (event, url)
let basePathComponents = schemasPath.pathComponents

let filePathComponents = url
.pathComponents
.drop(while: basePathComponents.contains(_:))

let filePath = filePathComponents.joined(separator: "/")

Log.debug("(\(configuration.name)) Reading schema: \(filePath)")

return (try fileProvider.readFile(at: url.path), Array(filePathComponents))
}

try clearDestinationFolder(at: configuration.destination ?? .rootPath)

try events.forEach { event, url in
try generate(parameters: generarionParameters, event: event, schemaURL: url, platform: platform)
try events.forEach { event, schemePath in
try generate(
parameters: generarionParameters,
event: event,
targetPath: targetPath ?? "",
schemePath: schemePath,
platform: platform
)
}
}

Expand Down Expand Up @@ -254,7 +272,10 @@ final class DefaultEventGenerator: EventGenerator {
}.map { repoPathURL in
try self.generate(
configuration: configuration,
schemasPath: gitHubConfiguration.path.map { repoPathURL.appendingPathComponent($0) } ?? repoPathURL
targetPath: gitHubConfiguration.path,
schemasPath: gitHubConfiguration.path.map { targetPath in
repoPathURL.appendingPathComponent(targetPath)
} ?? repoPathURL
)

try self.saveLockfile(configurationName: configuration.name, remoteReferenceSHA: remoteReferenceSHA)
Expand Down Expand Up @@ -363,7 +384,11 @@ final class DefaultEventGenerator: EventGenerator {
) throws -> Promise<EventGenerationResult> {
switch configuration.source {
case .local(let path):
try generate(configuration: configuration, schemasPath: URL(fileURLWithPath: path))
try generate(
configuration: configuration,
schemasPath: URL(fileURLWithPath: path)
)

return .value(.success)

case .gitHub(let gitHubConfiguration):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ struct ExternalEventContext: Encodable {
let name: String
let description: String?
let category: Category
let structName: String
let schemeName: String
let schemePath: String
let action: Action
let label: Label?
let initialisationParameters: [Parameter]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ struct InternalEventContext: Encodable {
let name: String
let description: String?
let category: String
let eventName: String
let experiment: Experiment?
let structName: String
let `protocol`: String
let eventName: String
let schemeName: String
let schemePath: String
let parameters: [Parameter]?
let hasParametersToInit: Bool
}
12 changes: 6 additions & 6 deletions Sources/AnalyticsGen/Models/Event/EventPlatform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ enum EventPlatform: String, Decodable {
// MARK: - Enumeration Cases

case android = "Android"
case androidIOS = "Android/iOS"
case iOSAndroid = "Android/iOS"
case iOS
}

Expand All @@ -20,25 +20,25 @@ extension EventPlatform: Equatable {
case (.android, .android):
return true

case (.android, .androidIOS):
case (.android, .iOSAndroid):
return true

case (.android, .iOS):
return false

case (.androidIOS, .android):
case (.iOSAndroid, .android):
return true

case (.androidIOS, .androidIOS):
case (.iOSAndroid, .iOSAndroid):
return true

case (.androidIOS, .iOS):
case (.iOSAndroid, .iOS):
return true

case (.iOS, .android):
return false

case (.iOS, .androidIOS):
case (.iOS, .iOSAndroid):
return true

case (.iOS, .iOS):
Expand Down
4 changes: 2 additions & 2 deletions Templates/ExternalEvent.stencil
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{% include "FileHeader.stencil" %}
{% set accessModifier %}{% if options.publicAccess %}public{% else %}internal{% endif %}{% endset %}
{% set structName %}{{ schemeName|swiftIdentifier:"pretty" }}ExternalEvent{% endset %}
{% macro propertyName name %}{{ name|swiftIdentifier:"pretty"|lowerFirstLetter|escapeReservedKeywords }}{% endmacro %}
{% macro optional types %}{% if nullable %}?{% endif %}{% endmacro %}

{% set accessModifier %}{% if options.publicAccess %}public{% else %}internal{% endif %}{% endset %}

import Analytics

/**
Expand Down
6 changes: 3 additions & 3 deletions Templates/InternalEvent.stencil
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{% include "FileHeader.stencil" %}
{% set accessModifier %}{% if options.publicAccess %}public{% else %}internal{% endif %}{% endset %}
{% set structName %}{{ schemeName|swiftIdentifier:"pretty" }}Event{% endset %}
{% macro propertyName name %}{{ name|swiftIdentifier:"pretty"|lowerFirstLetter|escapeReservedKeywords|replace:"Id","ID"|uppercaseSuffix:"json","Json","Hhid" }}{% endmacro %}
{% macro enumName name %}{{ name|swiftIdentifier:"pretty"|escapeReservedKeywords|replace:"`Type`","EventType"|uppercasePrefix:"Hhtm" }}{% endmacro %}

{% set accessModifier %}{% if options.publicAccess %}public{% else %}internal{% endif %}{% endset %}

import Analytics

/**
Expand All @@ -21,7 +21,7 @@ import Analytics
- **Эксперимент**: [{{ experiment.description }}]({{ experiment.url }})
{% endif %}
*/
{{ accessModifier }} struct {{ structName }}: {{ protocol }} {
{{ accessModifier }} struct {{ structName }}: InternalEvent {

{{ accessModifier }} var edition: [AnalyticsEventEdition] {
{% if edition %}
Expand Down
Loading