Skip to content
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

engine-infos alias poc #2254

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.development
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あ、.developmentの方はcommitしないようにしてたりします! 🙇
(個々の環境で変更するものをgit管理すると大変なので)

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
VITE_APP_NAME=voicevox
VITE_OFFICIAL_WEBSITE_URL=https://voicevox.hiroshiba.jp/
VITE_LATEST_UPDATE_INFOS_URL=https://raw.githubusercontent.com/VOICEVOX/voicevox_blog/master/src/data/updateInfos.json
VITE_GTM_CONTAINER_ID=GTM-DUMMY
10 changes: 0 additions & 10 deletions .env.production
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ちなみにenv全部まるまる移動することとかってできたりしますか・・・?
特に環境変数じゃないといけないような実装はしてないはずなのと、全部型付けられて幸せそうというのと・・・。

Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
VITE_APP_NAME=voicevox
VITE_DEFAULT_ENGINE_INFOS=`[
{
"uuid": "074fc39e-678b-4c13-8916-ffca8d505d1d",
"name": "VOICEVOX Engine",
"executionEnabled": true,
"executionFilePath": "vv-engine/run.exe",
"executionArgs": [],
"host": "http://127.0.0.1:50021"
}
]`
VITE_OFFICIAL_WEBSITE_URL=https://voicevox.hiroshiba.jp/
VITE_LATEST_UPDATE_INFOS_URL=https://raw.githubusercontent.com/VOICEVOX/voicevox_blog/master/src/data/updateInfos.json
VITE_GTM_CONTAINER_ID=GTM-DUMMY
10 changes: 0 additions & 10 deletions .env.test
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
VITE_APP_NAME=voicevox
VITE_DEFAULT_ENGINE_INFOS=`[
{
"uuid": "074fc39e-678b-4c13-8916-ffca8d505d1d",
"name": "VOICEVOX Engine",
"executionEnabled": true,
"executionFilePath": "../voicevox_engine/run.exe",
"executionArgs": [],
"host": "http://127.0.0.1:50021"
}
]`
VITE_OFFICIAL_WEBSITE_URL=https://voicevox.hiroshiba.jp/
VITE_LATEST_UPDATE_INFOS_URL=https://raw.githubusercontent.com/VOICEVOX/voicevox_blog/master/src/data/updateInfos.json
VITE_GTM_CONTAINER_ID=GTM-DUMMY
33 changes: 18 additions & 15 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,24 @@ git clone [email protected]:(個人のGitHubアカウント名)/voicevox.git

### 6. エンジンを指定する

- `.env.production`というファイルがありますので、コピーして、名前を`.env`にします。
- ファイルをエディタでひらいて、`VITE_DEFAULT_ENGINE_INFOS`内の`executionFilePath`に手順1のフォルダ名をいれます。たとえば製品版をインストーラで導入し、インストール先を変更していない場合は、下記のように書き換えて保存します。

```ini
VITE_APP_NAME=voicevox
VITE_DEFAULT_ENGINE_INFOS=`[
{
"uuid": "074fc39e-678b-4c13-8916-ffca8d505d1d",
"name": "VOICEVOX Engine",
"executionEnabled": true,
"executionFilePath": "C:/Users/(ユーザー名)/AppData/Local/Programs/VOICEVOX/vv-engine/run.exe",
"executionArgs": [],
"host": "http://127.0.0.1:50021"
}
]`
- `engine-infos/production.ts`というファイルがありますので、コピーして、名前を`engine-infos/development.ts`にします。
- ファイルをエディタでひらいて、`executionFilePath`に手順1のフォルダ名をいれます。たとえば製品版をインストーラで導入し、インストール先を変更していない場合は、下記のように書き換えて保存します。

```ts
import type { DefaultEngineInfos } from "./types";

export default [
{
uuid: "074fc39e-678b-4c13-8916-ffca8d505d1d",
name: "VOICEVOX Engine",
executionEnabled: true,
executionFilePath:
"/Applications/VOICEVOX.app/Contents/Resources/vv-engine/run",
executionArgs: [],
host: "http://127.0.0.1:50021",
},
] satisfies DefaultEngineInfos;

```

- あなたがVOICEVOX製品版のインストール先を変更している場合は個別で指定します。たとえば、`D:\VOICEVOX0.14.1`に製品版をインストールしている場合は、下記のように書き換えて保存します。
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ npm ci

### エンジンの準備

`.env.production`をコピーして`.env`を作成し、`VITE_DEFAULT_ENGINE_INFOS`内の`executionFilePath`に
`engine-infos/production.ts`をコピーして`engine-infos/development.ts`を作成し、`executionFilePath`に
[製品版 VOICEVOX](https://voicevox.hiroshiba.jp/) 内の`vv-engine/run.exe`を指定すれば動きます。

Windows でインストール先を変更していない場合は`C:/Users/(ユーザー名)/AppData/Local/Programs/VOICEVOX/vv-engine/run.exe`を指定してください。
Expand All @@ -66,7 +66,7 @@ VOICEVOX エディタの実行とは別にエンジン API のサーバを立て
代わりに`executionEnabled`を`false`にしてください。
これは製品版 VOICEVOX を起動している場合もあてはまります。

エンジン API の宛先エンドポイントを変更する場合は`VITE_DEFAULT_ENGINE_INFOS`内の`host`を変更してください。
エンジン API の宛先エンドポイントを変更する場合は`host`を変更してください。

### Electron の実行

Expand Down
13 changes: 13 additions & 0 deletions engine-infos/development.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { DefaultEngineInfos, EngineId } from "./types";

export default [
{
uuid: "074fc39e-678b-4c13-8916-ffca8d505d1d" as EngineId,
name: "VOICEVOX Engine",
executionEnabled: true,
executionFilePath:
"/Applications/VOICEVOX.app/Contents/Resources/vv-engine/run",
executionArgs: [],
host: "http://127.0.0.1:50021",
},
] satisfies DefaultEngineInfos;
12 changes: 12 additions & 0 deletions engine-infos/production.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

もし可能ならrootディレクトリに直接.tsファイルを置けると嬉しい・・・!!
env.production.tsみたいな感じとかできれば、管理しないといけないファイル数減らせるよな〜〜〜と。

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { DefaultEngineInfos, EngineId } from "./types";

export default [
{
uuid: "074fc39e-678b-4c13-8916-ffca8d505d1d" as EngineId,
name: "VOICEVOX Engine",
executionEnabled: true,
executionFilePath: "vv-engine/run",
executionArgs: [],
host: "http://127.0.0.1:50021",
},
] satisfies DefaultEngineInfos;
12 changes: 12 additions & 0 deletions engine-infos/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { DefaultEngineInfos, EngineId } from "./types";

export default [
{
uuid: "074fc39e-678b-4c13-8916-ffca8d505d1d" as EngineId,
name: "VOICEVOX Engine",
executionEnabled: true,
executionFilePath: "../voicevox_engine/run.exe",
executionArgs: [],
host: "http://127.0.0.1:50021",
},
] satisfies DefaultEngineInfos;
18 changes: 18 additions & 0 deletions engine-infos/types.d.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/vite-env.d.tsでも良いかも?

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { BRAND } from "zod";

export type EngineId = string & BRAND<"EngineId">;

export interface DefaultEngineInfo {
uuid: EngineId;
name: string;
path?: string;
executionEnabled: boolean;
executionFilePath: string;
executionArgs: string[];
host: string;
}

export type DefaultEngineInfos = DefaultEngineInfo[];

declare const engineInfos: DefaultEngineInfos;
export default engineInfos;
19 changes: 3 additions & 16 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PlaywrightTestConfig, Project } from "@playwright/test";
import { z } from "zod";

import dotenv from "dotenv";
import defaultEngineInfos from "./engine-infos/test";

dotenv.config({ override: true });

let project: Project;
Expand All @@ -14,20 +14,7 @@ if (isElectron) {
} else if (isBrowser) {
project = { name: "browser", testDir: "./tests/e2e/browser" };

// エンジンの起動が必要
const defaultEngineInfosEnv = process.env.VITE_DEFAULT_ENGINE_INFOS ?? "[]";
const envSchema = z // FIXME: electron起動時のものと共通化したい
.object({
host: z.string(),
executionFilePath: z.string(),
executionArgs: z.array(z.string()),
executionEnabled: z.boolean(),
})
.passthrough()
.array();
const engineInfos = envSchema.parse(JSON.parse(defaultEngineInfosEnv));

for (const info of engineInfos) {
for (const info of defaultEngineInfos) {
if (!info.executionEnabled) {
continue;
}
Expand Down
9 changes: 3 additions & 6 deletions src/backend/browser/contract.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { EngineInfo, envEngineInfoSchema } from "@/type/preload";

const baseEngineInfo = envEngineInfoSchema
.array()
.parse(JSON.parse(import.meta.env.VITE_DEFAULT_ENGINE_INFOS))[0];
import engineInfos from "@engine-infos";
import { EngineInfo } from "@/type/preload";

export const defaultEngine: EngineInfo = {
...baseEngineInfo,
...engineInfos[0],
type: "path", // FIXME: ダミーで"path"にしているので、エンジンAPIのURLを設定できるようにし、type: "URL"にする
isDefault: true,
};
Expand Down
14 changes: 2 additions & 12 deletions src/backend/common/ConfigManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import semver from "semver";
import AsyncLock from "async-lock";
import engineInfos from "@engine-infos";
import {
AcceptTermsStatus,
ConfigType,
Expand All @@ -12,7 +13,6 @@ import {
HotkeyCombination,
VoiceId,
PresetKey,
envEngineInfoSchema,
} from "@/type/preload";
import { ensureNotNullish } from "@/helpers/errorHelper";

Expand All @@ -36,17 +36,7 @@ const migrations: [string, (store: Record<string, unknown>) => unknown][] = [
[
">=0.14",
(config) => {
// FIXME: できるならEngineManagerからEngineIDを取得したい
if (import.meta.env.VITE_DEFAULT_ENGINE_INFOS == undefined) {
throw new Error("VITE_DEFAULT_ENGINE_INFOS == undefined");
}
const engineId = EngineId(
envEngineInfoSchema
.array()
.parse(JSON.parse(import.meta.env.VITE_DEFAULT_ENGINE_INFOS))[0].uuid,
);
if (engineId == undefined)
throw new Error("VITE_DEFAULT_ENGINE_INFOS[0].uuid == undefined");
const engineId = EngineId(engineInfos[0].uuid);
const prevDefaultStyleIds = config.defaultStyleIds as DefaultStyleId[];
config.defaultStyleIds = prevDefaultStyleIds.map((defaultStyle) => ({
engineId,
Expand Down
11 changes: 2 additions & 9 deletions src/backend/electron/manager/engineManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import shlex from "shlex";
import { app, dialog } from "electron"; // FIXME: ここでelectronをimportするのは良くない

import log from "electron-log/main";
import engineInfos from "@engine-infos";
import {
findAltPort,
getPidFromPort,
Expand All @@ -21,7 +22,6 @@ import {
MinimumEngineManifestType,
EngineId,
minimumEngineManifestSchema,
envEngineInfoSchema,
} from "@/type/preload";
import { AltPortInfos } from "@/store/type";
import { BaseConfigManager } from "@/backend/common/ConfigManager";
Expand All @@ -35,14 +35,7 @@ type EngineProcessContainer = {
* デフォルトエンジンの情報を作成する
*/
function createDefaultEngineInfos(defaultEngineDir: string): EngineInfo[] {
// TODO: envから直接ではなく、envに書いたengine_manifest.jsonから情報を得るようにする
const defaultEngineInfosEnv =
import.meta.env.VITE_DEFAULT_ENGINE_INFOS ?? "[]";

const envSchema = envEngineInfoSchema.array();
const engines = envSchema.parse(JSON.parse(defaultEngineInfosEnv));

return engines.map((engineInfo) => {
return engineInfos.map((engineInfo) => {
return {
...engineInfo,
isDefault: true,
Expand Down
11 changes: 0 additions & 11 deletions src/type/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,17 +698,6 @@ export const configSchema = z
.merge(rootMiscSettingSchema);
export type ConfigType = z.infer<typeof configSchema>;

export const envEngineInfoSchema = z.object({
uuid: engineIdSchema,
host: z.string(),
name: z.string(),
executionEnabled: z.boolean(),
executionFilePath: z.string(),
executionArgs: z.array(z.string()),
path: z.string().optional(),
});
export type EnvEngineInfoType = z.infer<typeof envEngineInfoSchema>;

// workaround. SystemError(https://nodejs.org/api/errors.html#class-systemerror)が2022/05/19時点ではNodeJSの型定義に記述されていないためこれを追加しています。
export class SystemError extends Error {
code?: string | undefined;
Expand Down
1 change: 0 additions & 1 deletion src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ interface ImportMetaEnv {
readonly VITE_7Z_BIN_NAME: string;
readonly VITE_APP_NAME: string;
readonly VITE_APP_VERSION: string;
readonly VITE_DEFAULT_ENGINE_INFOS: string;
readonly VITE_OFFICIAL_WEBSITE_URL: string;
readonly VITE_LATEST_UPDATE_INFOS_URL: string;
readonly VITE_GTM_CONTAINER_ID: string;
Expand Down
6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"baseUrl": ".",
"types": ["vitest/globals"],
"paths": {
"@/*": ["src/*"]
"@/*": ["src/*"],
"@engine-infos": ["engine-infos/types.d.ts"]
},
"lib": ["esnext", "dom", "dom.iterable"]
},
Expand All @@ -26,7 +27,8 @@
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx",
".storybook/**/*.ts",
"engine-infos/*.ts",
".storybook/**/*.ts"
],
"exclude": ["node_modules"]
}
21 changes: 18 additions & 3 deletions vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { rm } from "fs/promises";
import { fileURLToPath } from "url";

import electron from "vite-plugin-electron";
import tsconfigPaths from "vite-tsconfig-paths";
import vue from "@vitejs/plugin-vue";
import checker from "vite-plugin-checker";
import { nodePolyfills } from "vite-plugin-node-polyfills";
Expand All @@ -25,6 +24,11 @@ export default defineConfig((options) => {
`"package.json"の"name":"${packageName}"は"VITE_APP_NAME":"${env.VITE_APP_NAME}"から始まっている必要があります`,
);
}
const engineInfosPath = path.resolve(
__dirname,
"engine-infos",
`${options.mode}.ts`,
);
const shouldEmitSourcemap = ["development", "test"].includes(options.mode);
process.env.VITE_7Z_BIN_NAME =
(options.mode === "development"
Expand Down Expand Up @@ -58,6 +62,7 @@ export default defineConfig((options) => {
resolve: {
alias: {
"@": path.resolve(__dirname, "src/"),
"@engine-infos": engineInfosPath,
},
},
test: {
Expand Down Expand Up @@ -92,7 +97,12 @@ export default defineConfig((options) => {
}
},
vite: {
plugins: [tsconfigPaths({ root: __dirname })],
resolve: {
alias: {
"@": path.resolve(__dirname, "src/"),
"@engine-infos": engineInfosPath,
},
},
build: {
outDir: path.resolve(__dirname, "dist"),
sourcemap,
Expand All @@ -106,7 +116,12 @@ export default defineConfig((options) => {
reload();
},
vite: {
plugins: [tsconfigPaths({ root: __dirname })],
resolve: {
alias: {
"@": path.resolve(__dirname, "src/"),
"@engine-infos": engineInfosPath,
},
},
build: {
outDir: path.resolve(__dirname, "dist"),
sourcemap,
Expand Down
Loading