-
Notifications
You must be signed in to change notification settings - Fork 305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: EngineInfoManagerのリファクタリング #2347
Merged
Hiroshiba
merged 12 commits into
VOICEVOX:main
from
Hiroshiba:EngineInfoManagerのリファクタリング
Nov 10, 2024
The head ref may contain hidden characters: "EngineInfoManager\u306E\u30EA\u30D5\u30A1\u30AF\u30BF\u30EA\u30F3\u30B0"
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
166ea3d
refactor: エンジン情報管理のロジックを改善し、エンジン情報の取得メソッドを整理
Hiroshiba 13e9a14
まだ要らなかった
Hiroshiba 7c8c4f8
戻したり移動したり
Hiroshiba f7b17d6
移動はしない方が良さそう
Hiroshiba 32a2f2e
FIXME要らなくなってた
Hiroshiba bacf688
ちょっとわかりやすく
Hiroshiba c0219f2
エンジン情報の取得に関するコメントを修正
Hiroshiba d8013ae
エンジン情報の取得に関するコメントを修正
Hiroshiba e903075
Merge branch 'main' into EngineInfoManagerのリファクタリング
Hiroshiba 0498e3b
Merge branch 'main' into EngineInfoManagerのリファクタリング
Hiroshiba ce1f213
コメントの調整
Hiroshiba c9286c3
docs: エンジン情報読み込みメソッドのコメントを修正
Hiroshiba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,32 +16,7 @@ import { | |
} from "@/type/preload"; | ||
import { AltPortInfos } from "@/store/type"; | ||
import { loadEnvEngineInfos } from "@/domain/defaultEngine/envEngineInfo"; | ||
|
||
/** | ||
* デフォルトエンジンの情報を取得する | ||
*/ | ||
function fetchDefaultEngineInfos(defaultEngineDir: string): EngineInfo[] { | ||
// TODO: envから直接ではなく、envに書いたengine_manifest.jsonから情報を得るようにする | ||
const engines = loadEnvEngineInfos(); | ||
|
||
return engines.map((engineInfo) => { | ||
const { protocol, hostname, port, pathname } = new URL(engineInfo.host); | ||
return { | ||
...engineInfo, | ||
protocol, | ||
hostname, | ||
defaultPort: port, | ||
pathname: pathname === "/" ? "" : pathname, | ||
isDefault: true, | ||
type: "path", | ||
executionFilePath: path.resolve(engineInfo.executionFilePath), | ||
path: | ||
engineInfo.path == undefined | ||
? undefined | ||
: path.resolve(defaultEngineDir, engineInfo.path), | ||
} satisfies EngineInfo; | ||
}); | ||
} | ||
import { failure, Result, success } from "@/type/result"; | ||
|
||
/** エンジンの情報を管理するクラス */ | ||
export class EngineInfoManager { | ||
|
@@ -57,43 +32,77 @@ export class EngineInfoManager { | |
} | ||
|
||
/** | ||
* 追加エンジンの一覧を取得する。 | ||
* FIXME: store.get("registeredEngineDirs")への副作用をEngineManager外に移動する | ||
* エンジンディレクトリのエンジンマニフェストからエンジンの情報を読み込む。 | ||
*/ | ||
private fetchAdditionalEngineInfos(): EngineInfo[] { | ||
const engines: EngineInfo[] = []; | ||
const addEngine = (engineDir: string, type: "vvpp" | "path") => { | ||
const manifestPath = path.join(engineDir, "engine_manifest.json"); | ||
if (!fs.existsSync(manifestPath)) { | ||
return "manifestNotFound"; | ||
} | ||
let manifest: MinimumEngineManifestType; | ||
try { | ||
manifest = minimumEngineManifestSchema.parse( | ||
JSON.parse(fs.readFileSync(manifestPath, { encoding: "utf8" })), | ||
); | ||
} catch (e) { | ||
return "manifestParseError"; | ||
} | ||
private loadEngineInfo( | ||
engineDir: string, | ||
type: "vvpp" | "path", | ||
): Result<EngineInfo, "manifestNotFound" | "manifestParseError"> { | ||
const manifestPath = path.join(engineDir, "engine_manifest.json"); | ||
if (!fs.existsSync(manifestPath)) { | ||
return failure("manifestNotFound", new Error("manifest not found")); | ||
} | ||
let manifest: MinimumEngineManifestType; | ||
try { | ||
manifest = minimumEngineManifestSchema.parse( | ||
JSON.parse(fs.readFileSync(manifestPath, { encoding: "utf8" })), | ||
); | ||
} catch (e) { | ||
return failure( | ||
"manifestParseError", | ||
e instanceof Error ? e : new Error("manifest parse error"), | ||
); | ||
} | ||
|
||
const [command, ...args] = shlex.split(manifest.command); | ||
|
||
return success({ | ||
uuid: manifest.uuid, | ||
protocol: "http:", | ||
hostname: "127.0.0.1", | ||
defaultPort: manifest.port.toString(), | ||
pathname: "", | ||
name: manifest.name, | ||
path: engineDir, | ||
executionEnabled: true, | ||
executionFilePath: path.join(engineDir, command), | ||
executionArgs: args, | ||
type, | ||
isDefault: false, | ||
} satisfies EngineInfo); | ||
} | ||
|
||
/** | ||
* .envにあるエンジンの情報を取得する。 | ||
*/ | ||
private fetchEnvEngineInfos(): EngineInfo[] { | ||
// TODO: envから直接ではなく、envに書いたengine_manifest.jsonから情報を得るようにする | ||
const engines = loadEnvEngineInfos(); | ||
|
||
return engines.map((engineInfo) => { | ||
const { protocol, hostname, port, pathname } = new URL(engineInfo.host); | ||
return { | ||
...engineInfo, | ||
protocol, | ||
hostname, | ||
defaultPort: port, | ||
pathname: pathname === "/" ? "" : pathname, | ||
isDefault: true, | ||
type: "path", | ||
executionFilePath: path.resolve(engineInfo.executionFilePath), | ||
path: | ||
engineInfo.path == undefined | ||
? undefined | ||
: path.resolve(this.defaultEngineDir, engineInfo.path), | ||
} satisfies EngineInfo; | ||
}); | ||
} | ||
|
||
const [command, ...args] = shlex.split(manifest.command); | ||
|
||
engines.push({ | ||
uuid: manifest.uuid, | ||
protocol: "http:", | ||
hostname: "127.0.0.1", | ||
defaultPort: manifest.port.toString(), | ||
pathname: "", | ||
name: manifest.name, | ||
path: engineDir, | ||
executionEnabled: true, | ||
executionFilePath: path.join(engineDir, command), | ||
executionArgs: args, | ||
type, | ||
isDefault: false, | ||
} satisfies EngineInfo); | ||
return "ok"; | ||
}; | ||
/** | ||
* VVPPエンジンの情報を取得する。 | ||
*/ | ||
private fetchVvppEngineInfos(): EngineInfo[] { | ||
const engineInfos: EngineInfo[] = []; | ||
for (const dirName of fs.readdirSync(this.vvppEngineDir)) { | ||
const engineDir = path.join(this.vvppEngineDir, dirName); | ||
if (!fs.statSync(engineDir).isDirectory()) { | ||
|
@@ -103,17 +112,27 @@ export class EngineInfoManager { | |
if (dirName === ".tmp") { | ||
continue; | ||
} | ||
const result = addEngine(engineDir, "vvpp"); | ||
if (result !== "ok") { | ||
log.log(`Failed to load engine: ${result}, ${engineDir}`); | ||
const result = this.loadEngineInfo(engineDir, "vvpp"); | ||
if (!result.ok) { | ||
log.log(`Failed to load engine: ${result.code}, ${engineDir}`); | ||
continue; | ||
} | ||
engineInfos.push(result.value); | ||
} | ||
return engineInfos; | ||
} | ||
|
||
/** | ||
* 設定で登録したエンジンの情報を取得する。 | ||
*/ | ||
private fetchRegisteredEngineInfos(): EngineInfo[] { | ||
const configManager = getConfigManager(); | ||
// FIXME: この関数の引数でregisteredEngineDirsを受け取り、動かないエンジンをreturnして、EngineManager外でconfig.setする | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 気軽に別のマネージャーを呼んでも良いという設計にしたので、ここのFIXMEは直さなくて良さそうということでついでに消しておきました |
||
|
||
const engineInfos: EngineInfo[] = []; | ||
for (const engineDir of configManager.get("registeredEngineDirs")) { | ||
const result = addEngine(engineDir, "path"); | ||
if (result !== "ok") { | ||
log.log(`Failed to load engine: ${result}, ${engineDir}`); | ||
const result = this.loadEngineInfo(engineDir, "path"); | ||
if (!result.ok) { | ||
log.log(`Failed to load engine: ${result.code}, ${engineDir}`); | ||
// 動かないエンジンは追加できないので削除 | ||
// FIXME: エンジン管理UIで削除可能にする | ||
dialog.showErrorBox( | ||
|
@@ -126,18 +145,21 @@ export class EngineInfoManager { | |
.get("registeredEngineDirs") | ||
.filter((p) => p !== engineDir), | ||
); | ||
continue; | ||
} | ||
engineInfos.push(result.value); | ||
} | ||
return engines; | ||
return engineInfos; | ||
} | ||
|
||
/** | ||
* 全てのエンジンの一覧を取得する。デフォルトエンジン+追加エンジン。 | ||
* 全てのエンジンの情報を取得する。 | ||
*/ | ||
fetchEngineInfos(): EngineInfo[] { | ||
const engineInfos = [ | ||
...fetchDefaultEngineInfos(this.defaultEngineDir), | ||
...this.fetchAdditionalEngineInfos(), | ||
...this.fetchEnvEngineInfos(), | ||
...this.fetchVvppEngineInfos(), | ||
...this.fetchRegisteredEngineInfos(), | ||
]; | ||
return engineInfos; | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ついでに外にあった
fetchDefaultEngineInfos
を中に移動しました。