Skip to content

Commit baa8e6c

Browse files
authored
feat: Core/をubuntuでビルドできるように変更 (#254)
* feat: add ubuntu build ci * ci: use swift 6.2 on ubuntu * fix: remove unnecessary import * fix: refactor to remove NSEvent from InputState.swift * fix: make ubuntu-buildable * fix: import FoundationNetworking on linux * fix: make Core config helpers linux-friendly * fix: disable Zenzai for testing * fix: test case for linux * fix: make Error Equatable * fix: add await
1 parent 34b1fd2 commit baa8e6c

File tree

12 files changed

+111
-46
lines changed

12 files changed

+111
-46
lines changed

.github/workflows/swift.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ jobs:
2222
- name: Test
2323
run: swift test --package-path Core
2424

25+
ubuntu-build:
26+
name: Build and Test on Ubuntu
27+
runs-on: ubuntu-22.04
28+
steps:
29+
- uses: actions/checkout@v4
30+
with:
31+
submodules: true
32+
- name: Setup Swift 6.2
33+
uses: swift-actions/setup-swift@v2
34+
with:
35+
swift-version: "6.2"
36+
- name: Build
37+
run: swift build --package-path Core
38+
- name: Test
39+
run: swift test --package-path Core
40+
2541
xcodebuild-test:
2642
name: Xcodebuild test on macOS
2743
runs-on: macos-15

Core/Package.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33

44
import PackageDescription
55

6+
#if os(macOS)
7+
let kanaKanjiConverterTraits: Set<Package.Dependency.Trait> = ["Zenzai"]
8+
#else
9+
// for testing in Ubuntu environment.
10+
let kanaKanjiConverterTraits: Set<Package.Dependency.Trait> = []
11+
#endif
12+
613
let package = Package(
714
name: "Core",
815
platforms: [.macOS(.v13)],
@@ -14,7 +21,7 @@ let package = Package(
1421
)
1522
],
1623
dependencies: [
17-
.package(url: "https://github.com/azooKey/AzooKeyKanaKanjiConverter", revision: "07486b32d4e22ff76f4c2167c38d08e1eb36de9a", traits: ["Zenzai"])
24+
.package(url: "https://github.com/azooKey/AzooKeyKanaKanjiConverter", revision: "07486b32d4e22ff76f4c2167c38d08e1eb36de9a", traits: kanaKanjiConverterTraits)
1825
],
1926
targets: [
2027
.executableTarget(

Core/Sources/Core/Configs/BoolConfigItem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ protocol BoolConfigItem: ConfigItem<Bool> {
77
extension BoolConfigItem {
88
public var value: Bool {
99
get {
10-
if let value = UserDefaults.standard.value(forKey: Self.key) {
10+
if let value = UserDefaults.standard.object(forKey: Self.key) {
1111
value as? Bool ?? Self.default
1212
} else {
1313
Self.default

Core/Sources/Core/Configs/CustomCodableConfigItem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ extension Config {
209209
public static var `default`: Value {
210210
// Migration: If user had OpenAI API enabled, preserve that setting
211211
let legacyKey = Config.Deprecated.EnableOpenAiApiKey.key
212-
if let legacyValue = UserDefaults.standard.value(forKey: legacyKey) as? Bool,
212+
if let legacyValue = UserDefaults.standard.object(forKey: legacyKey) as? Bool,
213213
legacyValue {
214214
return .openAI
215215
}

Core/Sources/Core/Configs/CustomInputTableStore.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import Foundation
2+
#if canImport(UniformTypeIdentifiers)
3+
import UniformTypeIdentifiers
4+
#endif
25
import KanaKanjiConverterModule
36

47
public enum CustomInputTableStore {
@@ -15,7 +18,11 @@ public enum CustomInputTableStore {
1518
}
1619

1720
static var fileURL: URL {
18-
directoryURL.appendingPathComponent(fileName, conformingTo: .text)
21+
#if canImport(UniformTypeIdentifiers) && !os(Linux)
22+
return directoryURL.appendingPathComponent(fileName, conformingTo: .text)
23+
#else
24+
return directoryURL.appendingPathComponent(fileName)
25+
#endif
1926
}
2027

2128
@discardableResult

Core/Sources/Core/Configs/IntConfigItem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ protocol IntConfigItem: ConfigItem<Int> {
77
extension IntConfigItem {
88
public var value: Int {
99
get {
10-
if let value = UserDefaults.standard.value(forKey: Self.key) {
10+
if let value = UserDefaults.standard.object(forKey: Self.key) {
1111
value as? Int ?? Self.default
1212
} else {
1313
Self.default

Core/Sources/Core/InputUtils/Actions/ClientAction.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import InputMethodKit
21
import KanaKanjiConverterModule
32

43
public enum ClientAction {

Core/Sources/Core/InputUtils/InputState.swift

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import InputMethodKit
1+
import Foundation
22
import KanaKanjiConverterModule
33

44
public enum InputState: Sendable, Hashable {
@@ -10,29 +10,18 @@ public enum InputState: Sendable, Hashable {
1010
case replaceSuggestion
1111
case unicodeInput(String)
1212

13+
public enum ModifierFlag: Sendable, Equatable, Hashable {
14+
case option
15+
case control
16+
case command
17+
case shift
18+
}
19+
1320
public struct EventCore: Sendable, Equatable {
14-
public init(modifierFlags: NSEvent.ModifierFlags) {
21+
public init(modifierFlags: [ModifierFlag]) {
1522
self.modifierFlags = modifierFlags
1623
}
17-
var modifierFlags: NSEvent.ModifierFlags
18-
}
19-
20-
public func event( // swiftlint:disable:this function_parameter_count
21-
_ event: NSEvent!,
22-
userAction: UserAction,
23-
inputLanguage: InputLanguage,
24-
liveConversionEnabled: Bool,
25-
enableDebugWindow: Bool,
26-
enableSuggestion: Bool
27-
) -> (ClientAction, ClientActionCallback) {
28-
self.event(
29-
eventCore: EventCore(modifierFlags: event.modifierFlags),
30-
userAction: userAction,
31-
inputLanguage: inputLanguage,
32-
liveConversionEnabled: liveConversionEnabled,
33-
enableDebugWindow: enableDebugWindow,
34-
enableSuggestion: enableSuggestion
35-
)
24+
var modifierFlags: [ModifierFlag]
3625
}
3726

3827
// この種のコードは複雑にしかならないので、lintを無効にする

Core/Sources/Core/MagicConversion/OpenAIClient.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import Foundation
2+
#if canImport(FoundationNetworking)
3+
import FoundationNetworking
4+
#endif
25

36
struct Prompt {
47
static let dictionary: [String: String] = [

Core/Sources/Core/UserDictionary/SystemUserDictionaryHelper.swift

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Foundation
2-
import SQLite3
3-
#if canImport(AppKit)
2+
#if os(macOS)
43
import AppKit
4+
import SQLite3
55
#endif
66

77
/// macOSのユーザ辞書データを取り出すためのヘルパー
@@ -12,7 +12,20 @@ import AppKit
1212
/// sqlite3 -header -csv ~/Library/KeyboardServices/TextReplacements.db "SELECT ZSHORTCUT, ZPHRASE FROM ZTEXTREPLACEMENTENTRY"
1313
/// ```
1414
public enum SystemUserDictionaryHelper: Sendable {
15-
#if canImport(AppKit)
15+
public struct Entry: Sendable {
16+
public let shortcut: String
17+
public let phrase: String
18+
}
19+
20+
public enum FetchError: Sendable, Error, Equatable {
21+
case unsupportedOperatingSystem
22+
case fileNotExist(String)
23+
case fileNotReadable(String)
24+
case failedToOpenDatabase(status: Int32)
25+
case failedToPrepareStatement(status: Int32)
26+
}
27+
28+
#if os(macOS)
1629
/// Delegate that allows the user to choose **only** a directory named "KeyboardServices".
1730
private final class KeyboardServicesDirectoryDelegate: NSObject, NSOpenSavePanelDelegate {
1831
private let allowedFolderName = "KeyboardServices"
@@ -37,37 +50,26 @@ public enum SystemUserDictionaryHelper: Sendable {
3750

3851
/// A shared delegate instance that remains alive for the lifetime of the open panel.
3952
@MainActor private static let keyboardServicesDelegate = KeyboardServicesDirectoryDelegate()
40-
#endif
4153

42-
public struct Entry: Sendable {
43-
public let shortcut: String
44-
public let phrase: String
45-
}
46-
47-
public enum FetchError: Sendable, Error {
48-
case fileNotExist(String)
49-
case fileNotReadable(String)
50-
case failedToOpenDatabase(status: Int32)
51-
case failedToPrepareStatement(status: Int32)
52-
}
53-
54-
@MainActor static func promptUserForTextReplacementDirectory() -> URL? {
54+
@MainActor private static func promptUserForTextReplacementDirectory() -> URL? {
5555
let panel = NSOpenPanel()
5656
panel.title = "システムのユーザ辞書ディレクトリ(KeyboardServices)を選択してください"
5757
panel.message = "システムのユーザ辞書ディレクトリ(KeyboardServices)を選択してください"
5858
panel.canChooseDirectories = true
5959
panel.canChooseFiles = false
6060
panel.allowsMultipleSelection = false
6161
panel.directoryURL = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("Library/KeyboardServices")
62-
#if canImport(AppKit)
6362
panel.delegate = keyboardServicesDelegate
64-
#endif
6563

6664
let response = panel.runModal()
6765
return response == .OK ? panel.url : nil
6866
}
67+
#endif
6968

7069
@MainActor public static func fetchEntries() throws(FetchError) -> [Entry] {
70+
#if !os(macOS)
71+
throw .unsupportedOperatingSystem
72+
#else
7173
let userName = NSUserName()
7274
var dbPath = "/Users/\(userName)/Library/KeyboardServices/TextReplacements.db"
7375
guard FileManager.default.fileExists(atPath: dbPath) else {
@@ -134,5 +136,6 @@ public enum SystemUserDictionaryHelper: Sendable {
134136
}
135137

136138
return entries
139+
#endif
137140
}
138141
}

0 commit comments

Comments
 (0)