Skip to content

Commit b1e0890

Browse files
Merge pull request #9 from lemo-nade-room/feature/change-di-swift-dependencies
swift-dependencies移行
2 parents 216db56 + 7e18e17 commit b1e0890

12 files changed

+268
-179
lines changed

Package.resolved

Lines changed: 46 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ let package = Package(
2626
.package(url: "https://github.com/vapor/fluent-sqlite-driver.git", from: "4.0.0"),
2727
.package(url: "https://github.com/Zollerboy1/SwiftCommand.git", from: "1.4.0"),
2828
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.0.0"),
29+
.package(url: "https://github.com/pointfreeco/swift-dependencies.git", from: "1.0.0"),
2930
],
3031
targets: [
3132
.target(
@@ -36,6 +37,7 @@ let package = Package(
3637
.product(name: "FluentSQLiteDriver", package: "fluent-sqlite-driver"),
3738
.product(name: "SwiftCommand", package: "SwiftCommand"),
3839
.product(name: "AsyncHTTPClient", package: "async-http-client"),
40+
.product(name: "Dependencies", package: "swift-dependencies"),
3941
],
4042
swiftSettings: swiftSettings,
4143
plugins: swiftLintPlugins

Sources/CLIKit/CLI.swift

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import ConsoleKit
2+
import Dependencies
23
import Fluent
34
import Foundation
45
import Logging
6+
import NIOCore
7+
import NIOPosix
58

69
/// `ConsoleKit`で作成した`AsyncCommand`を実行するサービス
710
///
@@ -55,24 +58,41 @@ public struct CLI: Sendable {
5558
}
5659

5760
/// CLIを実行する
58-
public func run() async {
61+
public func run() async throws {
5962
let context = CommandContext(console: console, input: input)
63+
let eventLoopGroup = MultiThreadedEventLoopGroup.singleton
64+
let threadPool = try await prepareThreadPool()
65+
let logger = Logger(label: "CLIKit")
66+
let databases = Databases(threadPool: threadPool, on: eventLoopGroup)
67+
68+
if let sqliteURL {
69+
databases.use(.sqlite(.file(sqliteURL.absoluteString)), as: .sqlite)
70+
try await autoMigrate(
71+
databases: databases,
72+
on: eventLoopGroup.any(),
73+
logger: logger,
74+
migrations: migrations,
75+
migrationLogLevel: migrationLogLevel
76+
)
77+
}
78+
6079
do {
61-
if let sqliteURL {
62-
try await context.initDatabase(
63-
sqliteURL: sqliteURL,
64-
migrations: migrations,
65-
migrationLogLevel: migrationLogLevel
66-
)
80+
try await withDependencies {
81+
$0.databases = databases
82+
$0.eventLoopGroup = eventLoopGroup
83+
$0.threadPool = threadPool
84+
$0.logger = logger
85+
$0.httpClient = .shared
86+
} operation: {
87+
try await console.run(asyncCommandGroup, with: context)
6788
}
68-
try await console.run(asyncCommandGroup, with: context)
6989
} catch let error {
7090
console.error("\(error)")
7191
}
92+
7293
do {
73-
try await context.shutdown()
74-
} catch let error {
75-
console.error("\(error)")
94+
await databases.shutdownAsync()
95+
try await threadPool.shutdownGracefully()
7696
}
7797
}
7898

Sources/CLIKit/CommandContext+Storage.swift

Lines changed: 0 additions & 48 deletions
This file was deleted.

Sources/CLIKit/CommandContext+client.swift

Lines changed: 0 additions & 13 deletions
This file was deleted.

Sources/CLIKit/CommandContext+initDatabase.swift

Lines changed: 0 additions & 102 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import AsyncHTTPClient
2+
import Dependencies
3+
import Fluent
4+
import NIOPosix
5+
6+
extension DependencyValues {
7+
/// Fluent Database
8+
///
9+
/// データベースが設定されている場合のみ使用可能
10+
public var db: any Fluent.Database {
11+
databases.database(logger: logger, on: eventLoopGroup.any())!
12+
}
13+
14+
/// CLIKitのlogger
15+
public var logger: Logger {
16+
get { self[LoggerKey.self] }
17+
set { self[LoggerKey.self] = newValue }
18+
}
19+
private enum LoggerKey: DependencyKey {
20+
static var liveValue: Logger {
21+
fatalError("Value of type \(Value.self) is not registered in this context")
22+
}
23+
}
24+
25+
public var databases: Databases {
26+
get { self[DatabasesKey.self] }
27+
set { self[DatabasesKey.self] = newValue }
28+
}
29+
private enum DatabasesKey: DependencyKey {
30+
static var liveValue: Databases {
31+
fatalError("Value of type \(Value.self) is not registered in this context")
32+
}
33+
}
34+
35+
public var eventLoopGroup: MultiThreadedEventLoopGroup {
36+
get { self[EventLoopGroupKey.self] }
37+
set { self[EventLoopGroupKey.self] = newValue }
38+
}
39+
private enum EventLoopGroupKey: DependencyKey {
40+
static var liveValue: MultiThreadedEventLoopGroup {
41+
fatalError("Value of type \(Value.self) is not registered in this context")
42+
}
43+
}
44+
45+
public var threadPool: NIOThreadPool {
46+
get { self[ThreadPoolKey.self] }
47+
set { self[ThreadPoolKey.self] = newValue }
48+
}
49+
private enum ThreadPoolKey: DependencyKey {
50+
static var liveValue: NIOThreadPool {
51+
fatalError("Value of type \(Value.self) is not registered in this context")
52+
}
53+
}
54+
55+
public var httpClient: HTTPClient {
56+
get { self[HTTPClientKey.self] }
57+
set { self[HTTPClientKey.self] = newValue }
58+
}
59+
private enum HTTPClientKey: DependencyKey {
60+
static var liveValue: HTTPClient {
61+
fatalError("Value of type \(Value.self) is not registered in this context")
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)