Skip to content

Commit

Permalink
[#349] Create target
Browse files Browse the repository at this point in the history
  • Loading branch information
phongvhd93 committed Dec 4, 2023
1 parent a9b5099 commit ce88272
Show file tree
Hide file tree
Showing 14 changed files with 238 additions and 6 deletions.
11 changes: 11 additions & 0 deletions Modules/Data/Sources/NetworkAPI/Core/NetworkAPIError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// NetworkAPIError.swift
//

import Foundation

enum NetworkAPIError: Error {

case generic
case dataNotFound
}
33 changes: 33 additions & 0 deletions Modules/Data/Sources/NetworkAPI/Core/NetworkAPIProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// NetworkAPIProtocol.swift
//

import Alamofire

protocol NetworkAPIProtocol {

func performRequest<T: Decodable>(
_ configuration: RequestConfiguration,
for type: T.Type
) async throws -> T
}

extension NetworkAPIProtocol {

func request<T: Decodable>(
session: Session,
configuration: RequestConfiguration,
decoder: JSONDecoder
) async throws -> T {
try await session.request(
configuration.url,
method: configuration.method,
parameters: configuration.parameters,
encoding: configuration.encoding,
headers: configuration.headers,
interceptor: configuration.interceptor
)
.serializingDecodable(T.self)
.value
}
}
39 changes: 39 additions & 0 deletions Modules/Data/Sources/NetworkAPI/Core/RequestConfiguration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// RequestConfiguration.swift
//

import Alamofire
import Foundation

protocol RequestConfiguration {

var baseURL: String { get }

var endpoint: String { get }

var method: HTTPMethod { get }

var url: URLConvertible { get }

var parameters: Parameters? { get }

var encoding: ParameterEncoding { get }

var headers: HTTPHeaders? { get }

var interceptor: RequestInterceptor? { get }
}

extension RequestConfiguration {

var url: URLConvertible {
let url = URL(string: baseURL)?.appendingPathComponent(endpoint)
return url?.absoluteString ?? "\(baseURL)\(endpoint)"
}

var parameters: Parameters? { nil }

var headers: HTTPHeaders? { nil }

var interceptor: RequestInterceptor? { nil }
}
Empty file.
Empty file.
25 changes: 25 additions & 0 deletions Modules/Data/Sources/NetworkAPI/NetworkAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// NetworkAPI.swift
//

import Alamofire

final class NetworkAPI: NetworkAPIProtocol {

private let decoder: JSONDecoder

init(decoder: JSONDecoder = JSONDecoder()) {
self.decoder = decoder
}

func performRequest<T: Decodable>(
_ configuration: RequestConfiguration,
for type: T.Type
) async throws -> T {
try await request(
session: Session(),
configuration: configuration,
decoder: decoder
)
}
}
Empty file.
Empty file.
Empty file.
Empty file.
5 changes: 5 additions & 0 deletions Modules/Domain/Sources/UseCases/UseCaseFactoryProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//
// UseCaseFactoryProtocol.swift
//

protocol UseCaseFactoryProtocol: AnyObject {}
10 changes: 10 additions & 0 deletions Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ let project = Project.project(name: "{PROJECT_NAME}", bundleId: "${PRODUCT_BUNDL
extension Project {

static func project(name: String, bundleId: String) -> Project {
var targets: [Target] = [
.mainTarget(name: name, bundleId: bundleId),
.testsTarget(name: name, bundleId: bundleId),
.kifUITestsTarget(name: name, bundleId: bundleId),
]

Module.allCases.forEach {
targets.append(Target.makeFramework(module: $0, bundleId: bundleId))
}

return Project(
name: name,
organizationName: "Nimble",
Expand Down
33 changes: 33 additions & 0 deletions Tuist/ProjectDescriptionHelpers/Module.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Modules.swift
// ProjectDescriptionHelpers
//
// Created by Phong on 16/10/2023.
//

import ProjectDescription


public enum Module: CaseIterable {

case domain
case data

public var name: String {
switch self {
case .domain:
return "Domain"
case .data:
return "Data"
}
}

public var dependencies: [TargetDependency] {
switch self {
case .domain:
return []
case .data:
return [.target(name: Module.domain.name)]
}
}
}
88 changes: 82 additions & 6 deletions Tuist/ProjectDescriptionHelpers/Target+Initializing.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import ProjectDescription

extension Target {

private static let plistsPath: String = "Configurations/Plists"


enum Constant {

static let modulesRootPath: String = "Modules"
static let sourcesPath: String = "Sources"
static let resourcesPath: String = "Resources"
}

public static func mainTarget(name: String, bundleId: String) -> Target {
return Target(
name: name,
platform: .iOS,
product: .app,
bundleId: bundleId,
deploymentTarget: .iOS(
targetVersion: "{TARGET_VERSION}",
targetVersion: "{TARGET_VERSION}",
devices: [.iphone]
),
infoPlist: "\(name)/\(plistsPath)/Info.plist",
Expand All @@ -26,10 +33,14 @@ extension Target {
.swiftLintScript(),
.swiftFormatLintScript(),
.firebaseScript()
],
dependencies: [
.target(name: Module.data.name),
.target(name: Module.data.name)
]
)
}

public static func testsTarget(name: String, bundleId: String) -> Target {
let targetName = "\(name)Tests"
return Target(
Expand All @@ -47,7 +58,7 @@ extension Target {
dependencies: [.target(name: name)]
)
}

public static func kifUITestsTarget(name: String, bundleId: String) -> Target {
let targetName = "\(name)KIFUITests"
return Target(
Expand All @@ -59,8 +70,73 @@ extension Target {
sources: ["\(targetName)/**"],
resources: [
"\(targetName)/**/.gitkeep", // To include empty folders
],
],
dependencies: [.target(name: name)]
)
}

public static func makeFramework(module: Module, bundleId: String) -> Target {
let frameworkPath = "\(Constant.modulesRootPath)/\(module.name)"
let resourcesElement = ResourceFileElement.glob(pattern: "\(frameworkPath)/\(Constant.resourcesPath)/**")

return Target(
name: module.name,
platform: .iOS,
product: .framework,
bundleId: bundleId,
sources: ["\(frameworkPath)/\(Constant.sourcesPath)/**"],
resources: ResourceFileElements(resources: [resourcesElement]),
dependencies: module.dependencies
)
}
}

//// MARK: - Domain
//
//extension Target {
//
// public static func domainTarget(bundleId: String) -> Target {
// return Target(
// name: "Domain",
// platform: .iOS,
// product: .staticLibrary,
// bundleId: bundleId
// )
// }
//
// public static func domainTestsTarget(bundleId: String) -> Target {
// return Target(
// name: "DomainTests",
// platform: .iOS,
// product: .unitTests,
// bundleId: bundleId
// )
// }
//}
//
//
//// MARK: - Data
//
//extension Target {
//
// public static func domainTarget(bundleId: String) -> Target {
// let name = "Data"
// return Target(
// name: name,
// platform: .iOS,
// product: .staticLibrary,
// bundleId: bundleId,
// sources: ["\(modulesPath)/\(name)/**"]
// )
// }
//
// public static func domainTestsTarget(bundleId: String) -> Target {
// return Target(
// name: "DataTests",
// platform: .iOS,
// product: .unitTests,
// bundleId: bundleId,
// sources: [""]
// )
// }
//}

0 comments on commit ce88272

Please sign in to comment.