Skip to content

Commit

Permalink
Merge pull request #9 from lemo-nade-room/feature/change-di-swift-dep…
Browse files Browse the repository at this point in the history
…endencies

swift-dependencies移行
  • Loading branch information
lemo-nade-room authored Sep 28, 2024
2 parents 216db56 + 7e18e17 commit b1e0890
Show file tree
Hide file tree
Showing 12 changed files with 268 additions and 179 deletions.
47 changes: 46 additions & 1 deletion Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "0e0c6ff85ba6eb953e3782bc08a8cfff3215a5047597ba294e6002c9aed58d54",
"originHash" : "c3dbd5e2af279911a3933b62aa8a390fa8076719ad56b55855d216e4b238ff87",
"pins" : [
{
"identity" : "async-http-client",
Expand Down Expand Up @@ -28,6 +28,15 @@
"version" : "0.2.0"
}
},
{
"identity" : "combine-schedulers",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/combine-schedulers",
"state" : {
"revision" : "9fa31f4403da54855f1e2aeaeff478f4f0e40b13",
"version" : "1.0.2"
}
},
{
"identity" : "console-kit",
"kind" : "remoteSourceControl",
Expand Down Expand Up @@ -163,6 +172,15 @@
"version" : "1.2.0"
}
},
{
"identity" : "swift-clocks",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-clocks",
"state" : {
"revision" : "b9b24b69e2adda099a1fa381cda1eeec272d5b53",
"version" : "1.0.5"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
Expand All @@ -172,6 +190,15 @@
"version" : "1.1.3"
}
},
{
"identity" : "swift-concurrency-extras",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-concurrency-extras",
"state" : {
"revision" : "6054df64b55186f08b6d0fd87152081b8ad8d613",
"version" : "1.2.0"
}
},
{
"identity" : "swift-crypto",
"kind" : "remoteSourceControl",
Expand All @@ -181,6 +208,15 @@
"version" : "3.7.0"
}
},
{
"identity" : "swift-dependencies",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-dependencies.git",
"state" : {
"revision" : "0fc0255e780bf742abeef29dec80924f5f0ae7b9",
"version" : "1.4.1"
}
},
{
"identity" : "swift-docc-plugin",
"kind" : "remoteSourceControl",
Expand Down Expand Up @@ -361,6 +397,15 @@
"version" : "2.15.0"
}
},
{
"identity" : "xctest-dynamic-overlay",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/xctest-dynamic-overlay",
"state" : {
"revision" : "27d767d643fa2cf083d0a73d74fa84cacb53e85c",
"version" : "1.4.1"
}
},
{
"identity" : "yams",
"kind" : "remoteSourceControl",
Expand Down
2 changes: 2 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ let package = Package(
.package(url: "https://github.com/vapor/fluent-sqlite-driver.git", from: "4.0.0"),
.package(url: "https://github.com/Zollerboy1/SwiftCommand.git", from: "1.4.0"),
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/swift-dependencies.git", from: "1.0.0"),
],
targets: [
.target(
Expand All @@ -36,6 +37,7 @@ let package = Package(
.product(name: "FluentSQLiteDriver", package: "fluent-sqlite-driver"),
.product(name: "SwiftCommand", package: "SwiftCommand"),
.product(name: "AsyncHTTPClient", package: "async-http-client"),
.product(name: "Dependencies", package: "swift-dependencies"),
],
swiftSettings: swiftSettings,
plugins: swiftLintPlugins
Expand Down
42 changes: 31 additions & 11 deletions Sources/CLIKit/CLI.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import ConsoleKit
import Dependencies
import Fluent
import Foundation
import Logging
import NIOCore
import NIOPosix

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

/// CLIを実行する
public func run() async {
public func run() async throws {
let context = CommandContext(console: console, input: input)
let eventLoopGroup = MultiThreadedEventLoopGroup.singleton
let threadPool = try await prepareThreadPool()
let logger = Logger(label: "CLIKit")
let databases = Databases(threadPool: threadPool, on: eventLoopGroup)

if let sqliteURL {
databases.use(.sqlite(.file(sqliteURL.absoluteString)), as: .sqlite)
try await autoMigrate(
databases: databases,
on: eventLoopGroup.any(),
logger: logger,
migrations: migrations,
migrationLogLevel: migrationLogLevel
)
}

do {
if let sqliteURL {
try await context.initDatabase(
sqliteURL: sqliteURL,
migrations: migrations,
migrationLogLevel: migrationLogLevel
)
try await withDependencies {
$0.databases = databases
$0.eventLoopGroup = eventLoopGroup
$0.threadPool = threadPool
$0.logger = logger
$0.httpClient = .shared
} operation: {
try await console.run(asyncCommandGroup, with: context)
}
try await console.run(asyncCommandGroup, with: context)
} catch let error {
console.error("\(error)")
}

do {
try await context.shutdown()
} catch let error {
console.error("\(error)")
await databases.shutdownAsync()
try await threadPool.shutdownGracefully()
}
}

Expand Down
48 changes: 0 additions & 48 deletions Sources/CLIKit/CommandContext+Storage.swift

This file was deleted.

13 changes: 0 additions & 13 deletions Sources/CLIKit/CommandContext+client.swift

This file was deleted.

102 changes: 0 additions & 102 deletions Sources/CLIKit/CommandContext+initDatabase.swift

This file was deleted.

64 changes: 64 additions & 0 deletions Sources/CLIKit/DependencyValues+dependencies.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import AsyncHTTPClient
import Dependencies
import Fluent
import NIOPosix

extension DependencyValues {
/// Fluent Database
///
/// データベースが設定されている場合のみ使用可能
public var db: any Fluent.Database {
databases.database(logger: logger, on: eventLoopGroup.any())!
}

/// CLIKitのlogger
public var logger: Logger {
get { self[LoggerKey.self] }
set { self[LoggerKey.self] = newValue }
}
private enum LoggerKey: DependencyKey {
static var liveValue: Logger {
fatalError("Value of type \(Value.self) is not registered in this context")
}
}

public var databases: Databases {
get { self[DatabasesKey.self] }
set { self[DatabasesKey.self] = newValue }
}
private enum DatabasesKey: DependencyKey {
static var liveValue: Databases {
fatalError("Value of type \(Value.self) is not registered in this context")
}
}

public var eventLoopGroup: MultiThreadedEventLoopGroup {
get { self[EventLoopGroupKey.self] }
set { self[EventLoopGroupKey.self] = newValue }
}
private enum EventLoopGroupKey: DependencyKey {
static var liveValue: MultiThreadedEventLoopGroup {
fatalError("Value of type \(Value.self) is not registered in this context")
}
}

public var threadPool: NIOThreadPool {
get { self[ThreadPoolKey.self] }
set { self[ThreadPoolKey.self] = newValue }
}
private enum ThreadPoolKey: DependencyKey {
static var liveValue: NIOThreadPool {
fatalError("Value of type \(Value.self) is not registered in this context")
}
}

public var httpClient: HTTPClient {
get { self[HTTPClientKey.self] }
set { self[HTTPClientKey.self] = newValue }
}
private enum HTTPClientKey: DependencyKey {
static var liveValue: HTTPClient {
fatalError("Value of type \(Value.self) is not registered in this context")
}
}
}
Loading

0 comments on commit b1e0890

Please sign in to comment.