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

Enum plus plus #199

Merged
merged 5 commits into from
Jul 2, 2019
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
5 changes: 5 additions & 0 deletions .sourcery.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sources:
- CouchTrackerCore
templates:
- SourceryTemplates
output: CouchTrackerCore/CodeGenerated
3 changes: 2 additions & 1 deletion Brewfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
tap 'blender/homebrew-tap'
brew 'rome'

brew 'sourcery'
brew 'swiftlint'
brew 'sonar-scanner'
brew 'tailor'
Expand Down
31 changes: 31 additions & 0 deletions CouchTrackerCore/CodeGeneration/EnumClosures.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Given the following enum

```
enum ViewState<T>: EnumClosures {
case start(data: T)
case loading
case completed(count: Int, message: String)
}
```

will generate the following code

```
extension ViewState {
internal func onStart(_ fn: (T) -> Void) {
guard case let .start(data) = self else { return }
fn(data)
}
internal func onLoading(_ fn: () -> Void) {
guard case .loading = self else { return }
fn()
}
internal func onCompleted(_ fn: (Int, String) -> Void) {
guard case let .completed(count, message) = self else { return }
fn(count, message)
}
}
```
*/
public protocol EnumClosures {}
2 changes: 2 additions & 0 deletions CouchTrackerCore/CodeGeneration/EnumPoetry.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// See `EnumClosures` and `EnumProperties`
public protocol EnumPoetry: EnumClosures, EnumProperties {}
40 changes: 40 additions & 0 deletions CouchTrackerCore/CodeGeneration/EnumProperties.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Given the following enum

```
enum ViewState<T>: EnumProperties {
case start(T)
case loading
case completed(Int, String)
}
```

will generate the following code

```
extension ViewState {
internal var isStart: Bool {
guard case .start = self else { return false }
return true
}
internal var isLoading: Bool {
guard case .loading = self else { return false }
return true
}
internal var isCompleted: Bool {
guard case .completed = self else { return false }
return true
}

internal var start: T? {
guard case let .start(data) = self else { return nil }
return (data)
}
internal var completed: (count: Int, message: String)? {
guard case let .completed(count, message) = self else { return nil }
return (count, message)
}
}
```
*/
public protocol EnumProperties {}
2 changes: 1 addition & 1 deletion CouchTrackerCore/MovieDetails/MovieDetailsViewState.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public enum MovieDetailsViewState: Hashable {
public enum MovieDetailsViewState: Hashable, EnumPoetry {
case loading
case showing(movie: MovieEntity)
case error(error: Error)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public enum MoviesManagerViewState: Hashable {
public enum MoviesManagerViewState: Hashable, EnumPoetry {
case loading
case showing(pages: [ModulePage], selectedIndex: Int)
}
22 changes: 22 additions & 0 deletions SourceryTemplates/EnumClosures.stencil
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% macro associatedValueNamedType associatedValue %}{% if associatedValue.localName %}{{ associatedValue.localName }}{% else %}value{{ forloop.counter }}{% endif %}{% endmacro %}
{% macro associatedValueTypes associatedValues %}{% for associatedValue in associatedValues %}{% if forloop.first %}{{ associatedValue.typeName }}{% else %}, {{ associatedValue.typeName }}{% endif %}{% endfor %}{% endmacro %}
{% macro associatedValueNames associatedValues %}{% for associatedValue in case.associatedValues %}{% if forloop.first %}{% call associatedValueNamedType associatedValue %}{% else %}, {% call associatedValueNamedType associatedValue %}{% endif %}{% endfor %}{% endmacro %}
// MARK: - EnumClosures

{% for enum in types.enums where enum.implements.EnumClosures %}
extension {{ enum.name }} {
{% for case in enum.cases %}
{% if case.hasAssociatedValue %}
{{ enum.accessLevel }} func on{{ case.name|capitalize }}(_ fn: ({% call associatedValueTypes case.associatedValues %}) -> Void) {
guard case let .{{ case.name }}({% call associatedValueNames case.associatedValues %}) = self else { return }
fn({% call associatedValueNames case.associatedValues %})
}
{% else %}
{{ enum.accessLevel }} func on{{ case.name|capitalize }}(_ fn: () -> Void) {
guard case .{{ case.name }} = self else { return }
fn()
}
{% endif %}
{% endfor %}
}
{% endfor %}
24 changes: 24 additions & 0 deletions SourceryTemplates/EnumProperties.stencil
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% macro associatedValueNamedType associatedValue %}{% if associatedValue.localName %}{{ associatedValue.localName }}{% else %}value{{ forloop.counter }}{% endif %}{% endmacro %}
{% macro associatedValueTypes associatedValues %}{% for associatedValue in associatedValues %}{% if forloop.first %}{{ associatedValue.typeName }}{% else %}, {{ associatedValue.typeName }}{% endif %}{% endfor %}{% endmacro %}
{% macro associatedValueNames associatedValues %}{% for associatedValue in case.associatedValues %}{% if forloop.first %}{% call associatedValueNamedType associatedValue %}{% else %}, {% call associatedValueNamedType associatedValue %}{% endif %}{% endfor %}{% endmacro %}
{% macro associatedTypeAsTuple associatedValues %}{% if associatedValues.count == 1 %}{{ associatedValues[0].typeName }}{% else %}({% for associatedValue in associatedValues %}{% if forloop.first %}{% call associatedValueNamedType associatedValue %}: {{ associatedValue.typeName }}{% else %}, {% call associatedValueNamedType associatedValue %}: {{ associatedValue.typeName }}{% endif %}{% endfor %}){% endif %}{% endmacro %}
// MARK: - EnumProperties

{% for enum in types.enums where enum.implements.EnumProperties %}
extension {{ enum.name }} {
{% for case in enum.cases %}
{{ enum.accessLevel }} var is{{ case.name|capitalize }}: Bool {
guard case .{{ case.name }} = self else { return false }
return true
}
{% endfor %}
{% for case in enum.cases %}
{% if case.hasAssociatedValue %}
{{ enum.accessLevel }} var {{ case.name }}: {% call associatedTypeAsTuple case.associatedValues %}? {
guard case let .{{ case.name }}({% call associatedValueNames case.associatedValues %}) = self else { return nil }
return ({% call associatedValueNames case.associatedValues %})
}
{% endif %}
{% endfor %}
}
{% endfor %}
7 changes: 6 additions & 1 deletion fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ platform :ios do

sh("../scripts/generate_secrets #{secrets_param}", log: false)
sh('cd .. && tuist generate')

pods
end

Expand All @@ -19,6 +18,12 @@ platform :ios do
cocoapods(podfile: '.', repo_update: false)
end

desc 'Setup Certificates'
lane :setup_certs do
match(type: 'development')
match(type: 'appstore')
end

desc 'Run CouchTrackerCore tests'
lane :tests do
run_tests_for_scheme(scheme: 'TVDBSwiftTests')
Expand Down
5 changes: 5 additions & 0 deletions fastlane/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ or alternatively using `brew cask install fastlane`
fastlane ios pods
```
Run bundle exec pod install --repo-update
### ios setup_certs
```
fastlane ios setup_certs
```
Setup Certificates
### ios tests
```
fastlane ios tests
Expand Down
3 changes: 3 additions & 0 deletions scripts/codegeneration
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

sourcery