Skip to content

Commit

Permalink
Fix Sourcery templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Pietro Caselani committed Jan 9, 2020
1 parent 504e76c commit 5d94a42
Show file tree
Hide file tree
Showing 21 changed files with 111 additions and 67 deletions.
5 changes: 0 additions & 5 deletions .sourcery.yml

This file was deleted.

2 changes: 1 addition & 1 deletion CommonSources/DataStruct.swift
@@ -1,4 +1,4 @@
protocol DataStruct {}
protocol DataStruct: Hashable {}

extension DataStruct {
func setValueOptional<T>(_ value: OptionalCopyValue<T>, _ defaultValue: T?) -> T? {
Expand Down
11 changes: 3 additions & 8 deletions CouchTrackerApp/MovieDetails/MovieDetailsViewController.swift
Expand Up @@ -66,14 +66,9 @@ public final class MovieDetailsViewController: UIViewController {
}

private func handleViewState(_ viewState: MovieDetailsViewState) {
switch viewState {
case .loading:
showLoadingView()
case let .error(error):
showErrorView(error)
case let .showing(movie):
show(details: movie)
}
viewState.onLoading { showLoadingView() }
viewState.onError { showError($0) }
viewState.onShowing { show(details: $0) }
}

private func handleImagesState(_ imagesState: MovieDetailsImagesState) {
Expand Down
8 changes: 8 additions & 0 deletions CouchTrackerCore-sourcery.yml
@@ -0,0 +1,8 @@
project:
file: CouchTracker.xcodeproj
target:
- name: CouchTrackerCore
- name: TraktSwift
templates:
- SourceryTemplates/
output: CouchTrackerCore/CodeGenerated/
8 changes: 8 additions & 0 deletions CouchTrackerSync-sourcery.yml
@@ -0,0 +1,8 @@
project:
file: CouchTracker.xcodeproj
target:
- name: CouchTrackerSync
- name: TraktSwift
templates:
- SourceryTemplates/
output: CouchTrackerSync/CodeGenerated/
1 change: 0 additions & 1 deletion CouchTrackerSync/APIFunctions.swift
@@ -1,4 +1,3 @@
import TraktSwift
import RxSwift

func syncWatchedShows(extended: [Extended]) -> Single<[BaseShow]> {
Expand Down
3 changes: 0 additions & 3 deletions CouchTrackerSync/CouchTrackerSync-Main.swift

This file was deleted.

30 changes: 1 addition & 29 deletions CouchTrackerSync/CouchTrackerSync.swift
@@ -1,37 +1,9 @@
import TraktSwift
import RxSwift
import Moya

typealias TraktShow = TraktSwift.Show
typealias DomainShow = CouchTrackerSync.Show

public enum SyncError: Error {
case showIsNil
case missingEpisodes(showIds: ShowIds, baseSeason: BaseSeason, season: Season)
}

public struct WatchedProgressOptions: Hashable {
public let hidden: Bool
public let specials: Bool
public let countSpecials: Bool

public init(hidden: Bool = false, specials: Bool = false, countSpecials: Bool = false) {
self.hidden = hidden
self.specials = specials
self.countSpecials = countSpecials
}
}

public struct SyncOptions: Hashable {
public static let defaultOptions = SyncOptions()

public let watchedProgress: WatchedProgressOptions

public init(watchedProgress: WatchedProgressOptions = WatchedProgressOptions()) {
self.watchedProgress = watchedProgress
}
}

func startSync(_ options: SyncOptions = SyncOptions()) -> Observable<WatchedShow> {
let genresObservable = Current.genres().asObservable()

Expand All @@ -52,7 +24,7 @@ private func watchedProgress(options: WatchedProgressOptions, baseShow: BaseShow

private func seasonsForShow(showData: ShowDataForSyncing) -> Single<ShowDataForSyncing> {
return Current.seasonsForShow(showData.showIds, [.full, .episodes])
.map { ShowDataForSyncing(progressShow: showData.progressShow, show: showData.show, seasons: $0) }
.map { showData.copy(seasons: .new($0)) }
}

private func genresFromSlugs(allGenres: Set<Genre>, slugs: [String]) -> [Genre] {
Expand Down
2 changes: 1 addition & 1 deletion CouchTrackerSync/Headers/Public/CouchTrackerSync.h
Expand Up @@ -5,7 +5,7 @@
// Created by Pietro Caselani on 30.12.19.
//

#import <Foundation/Foundation.h>
#import <TraktSwift/TraktSwift.h>

//! Project version number for CouchTrackerSync.
FOUNDATION_EXPORT double CouchTrackerSyncVersionNumber;
Expand Down
2 changes: 0 additions & 2 deletions CouchTrackerSync/Models/Episode.swift
@@ -1,5 +1,3 @@
import TraktSwift

public struct Episode: Hashable, Codable {
public let ids: EpisodeIds
public let showIds: ShowIds
Expand Down
2 changes: 0 additions & 2 deletions CouchTrackerSync/Models/Show.swift
@@ -1,5 +1,3 @@
import TraktSwift

public struct Show: Hashable, Codable {
public let ids: ShowIds
public let title: String?
Expand Down
2 changes: 0 additions & 2 deletions CouchTrackerSync/Models/WatchedSeason.swift
@@ -1,5 +1,3 @@
import TraktSwift

public struct WatchedSeason: Hashable, Codable {
public let showIds: ShowIds
public let seasonIds: SeasonIds
Expand Down
16 changes: 14 additions & 2 deletions CouchTrackerSync/ShowDataForSyncing.swift
@@ -1,5 +1,3 @@
import TraktSwift

struct ShowDataForSyncing: DataStruct {
let progressShow: BaseShow
let show: TraktSwift.Show
Expand All @@ -8,4 +6,18 @@ struct ShowDataForSyncing: DataStruct {
var showIds: ShowIds {
return show.ids
}

// sourcery:inline:ShowDataForSyncing.TemplateName
internal func copy(
progressShow: CopyValue<BaseShow> = .same,
show: CopyValue<TraktSwift.Show> = .same,
seasons: CopyValue<[Season]> = .same
) -> ShowDataForSyncing {
let newProgressShow: BaseShow = setValue(progressShow, self.progressShow)
let newShow: TraktSwift.Show = setValue(show, self.show)
let newSeasons: [Season] = setValue(seasons, self.seasons)

return ShowDataForSyncing(progressShow: newProgressShow, show: newShow, seasons: newSeasons)
}
// sourcery:end
}
1 change: 0 additions & 1 deletion CouchTrackerSync/SyncEnvironment.swift
@@ -1,4 +1,3 @@
import TraktSwift
import RxSwift

public struct SyncEnvironment {
Expand Down
4 changes: 4 additions & 0 deletions CouchTrackerSync/SyncError.swift
@@ -0,0 +1,4 @@
public enum SyncError: Error, EnumPoetry {
case showIsNil
case missingEpisodes(showIds: ShowIds, baseSeason: BaseSeason, season: Season)
}
21 changes: 21 additions & 0 deletions CouchTrackerSync/SyncOptions.swift
@@ -0,0 +1,21 @@
public struct SyncOptions: Hashable {
public static let defaultOptions = SyncOptions()

public let watchedProgress: WatchedProgressOptions

public init(watchedProgress: WatchedProgressOptions = WatchedProgressOptions()) {
self.watchedProgress = watchedProgress
}
}

public struct WatchedProgressOptions: Hashable {
public let hidden: Bool
public let specials: Bool
public let countSpecials: Bool

public init(hidden: Bool = false, specials: Bool = false, countSpecials: Bool = false) {
self.hidden = hidden
self.specials = specials
self.countSpecials = countSpecials
}
}
26 changes: 22 additions & 4 deletions Project.swift
Expand Up @@ -202,7 +202,7 @@ enum CouchTrackerCore {
product: .framework,
bundleId: "\(baseBundleId).CouchTrackerCore",
infoPlist: "CouchTrackerCore/Info.plist",
sources: ["CouchTrackerCore/**"],
sources: ["CouchTrackerCore/**", "CommonSources/**"],
resources: ["CouchTrackerCore/Resources/**/*.{xcassets,png,strings,json}"],
headers: Headers(public: "CouchTrackerCore/Headers/Public/CouchTrackerCore.h"),
dependencies: [
Expand Down Expand Up @@ -262,7 +262,7 @@ enum CouchTrackerSync {
product: .framework,
bundleId: "\(baseBundleId).CouchTrackerSync",
infoPlist: "CouchTrackerSync/Info.plist",
sources: ["CouchTrackerSync/**"],
sources: ["CouchTrackerSync/**", "CommonSources/**"],
headers: Headers(public: "CouchTrackerSync/Headers/Public/CouchTrackerSync.h"),
dependencies: [
.target(name: TraktSwift.name)
Expand Down Expand Up @@ -681,10 +681,28 @@ func allSchemes() -> [Scheme] {

func additionalFiles() -> [FileElement] {
return [
"CouchTrackerCore-sourcery.yml",
"CouchTrackerSync-sourcery.yml",
".circleci",
".codecov.yml",
".editorconfig",
".gitignore",
".swift-version",
".swiftlint.yml",
".tuist-version",
"Brewfile",
"Gemfile",
"Podfile",
"Readme.md",
"SourceryTemplates",
"build_phases",
"changelog.md",
"fastlane",
"scripts",
"setup.sh",
"sonar-project.properties",
"CouchTrackerPlayground.playground",
"Readme.md",
".swiftlint.yml"
"CommonSources"
]
}

Expand Down
21 changes: 21 additions & 0 deletions SourceryTemplates/DataStruct.stencil
@@ -0,0 +1,21 @@
// MARK: - DataStruct

{% for type in types.structs|based:"DataStruct" %}
// sourcery:inline:{{ type.name }}.TemplateName
{{ type.accessLevel }} func copy(
{% for variable in type.storedVariables %}
{% if variable.typeName.isOptional %}{{ variable.name }}: OptionalCopyValue<{{ variable.unwrappedTypeName }}> = .same{% else %}{{ variable.name }}: CopyValue<{{ variable.typeName }}> = .same{% endif %}{% if not forloop.last %},{% endif %}
{% endfor %}
) -> {{ type.name }} {
{% for variable in type.storedVariables %}
{% if variable.typeName.isOptional %}
let new{{ variable.name|upperFirstLetter }}: {{ variable.typeName }} = setValueOptional({{ variable.name }}, self.{{ variable.name }}
{% else %}
let new{{ variable.name|upperFirstLetter }}: {{ variable.typeName }} = setValue({{ variable.name }}, self.{{ variable.name }})
{% endif %}
{% endfor %}

return {{ type.name }}({% for variable in type.storedVariables %}{{ variable.name }}: new{{ variable.name|upperFirstLetter }}{% if not forloop.last %}, {% else %}){% endif %}{% endfor %}
}
// sourcery:end
{% endfor %}
6 changes: 3 additions & 3 deletions SourceryTemplates/EnumClosures.stencil
Expand Up @@ -3,16 +3,16 @@
{% 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 %}
{% for enum in types.enums|based:"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) {
{{ enum.accessLevel }} func on{{ case.name|upperFirstLetter }}(_ 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) {
{{ enum.accessLevel }} func on{{ case.name|upperFirstLetter }}(_ fn: () -> Void) {
guard case .{{ case.name }} = self else { return }
fn()
}
Expand Down
4 changes: 2 additions & 2 deletions SourceryTemplates/EnumProperties.stencil
Expand Up @@ -4,10 +4,10 @@
{% 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 %}
{% for enum in types.enums|based:"EnumProperties" %}
extension {{ enum.name }} {
{% for case in enum.cases %}
{{ enum.accessLevel }} var is{{ case.name|capitalize }}: Bool {
{{ enum.accessLevel }} var is{{ case.name|upperFirstLetter }}: Bool {
guard case .{{ case.name }} = self else { return false }
return true
}
Expand Down
3 changes: 2 additions & 1 deletion scripts/codegeneration
@@ -1,3 +1,4 @@
#!/usr/bin/env bash

sourcery
sourcery --config CouchTrackerCore-sourcery.yml
sourcery --config CouchTrackerSync-sourcery.yml

0 comments on commit 5d94a42

Please sign in to comment.