Skip to content

Commit

Permalink
* エクスポートファイパスを渡す所を引数にした
Browse files Browse the repository at this point in the history
* 変数、関数名修正
* いくつかの構造をクラス化
  • Loading branch information
nmori committed Feb 1, 2024
1 parent 7cff6f0 commit c93a819
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 27 deletions.
8 changes: 3 additions & 5 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import EngineManager from "./background/engineManager";
import VvppManager, { isVvppFile } from "./background/vvppManager";
import configMigration014 from "./background/configMigration014";
import { failure, success } from "./type/result";
import { RuntimeInfoManager } from "./shared/RuntimeInfoManager";
import { ipcMainHandle, ipcMainSend } from "@/electron/ipc";
import { getConfigManager } from "@/background/electronConfig";

Expand Down Expand Up @@ -145,10 +146,7 @@ if (!fs.existsSync(vvppEngineDir)) {
fs.mkdirSync(vvppEngineDir);
}

const exportEngineInfoFilename = path.join(
app.getPath("userData"),
"runtime-info.json"
);
const runtimeInfoPath = new RuntimeInfoManager(app.getPath("userData"));

const onEngineProcessError = (engineInfo: EngineInfo, error: Error) => {
const engineId = engineInfo.uuid;
Expand All @@ -172,7 +170,7 @@ const engineManager = new EngineManager({
defaultEngineDir: appDirPath,
vvppEngineDir,
onEngineProcessError,
exportEngineInfoFilename,
runtimeInfoPath,
});
const vvppManager = new VvppManager({ vvppEngineDir });

Expand Down
38 changes: 16 additions & 22 deletions src/background/engineManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import {
} from "@/type/preload";
import { AltPortInfos } from "@/store/type";
import { BaseConfigManager } from "@/shared/ConfigManager";
import {
OutputInfoDataFor3rdParty,
RuntimeInfoManager,
} from "@/shared/RuntimeInfoManager";

type EngineProcessContainer = {
willQuitEngine: boolean;
Expand Down Expand Up @@ -62,7 +66,7 @@ export class EngineManager {
defaultEngineDir: string;
vvppEngineDir: string;
onEngineProcessError: (engineInfo: EngineInfo, error: Error) => void;
exportEngineInfoFilename: string;
runtimeInfoPath: RuntimeInfoManager;

defaultEngineInfos: EngineInfo[] = [];
additionalEngineInfos: EngineInfo[] = [];
Expand All @@ -75,19 +79,19 @@ export class EngineManager {
defaultEngineDir,
vvppEngineDir,
onEngineProcessError,
exportEngineInfoFilename,
runtimeInfoPath,
}: {
configManager: BaseConfigManager;
defaultEngineDir: string;
vvppEngineDir: string;
onEngineProcessError: (engineInfo: EngineInfo, error: Error) => void;
exportEngineInfoFilename: string;
runtimeInfoPath: RuntimeInfoManager;
}) {
this.configManager = configManager;
this.defaultEngineDir = defaultEngineDir;
this.vvppEngineDir = vvppEngineDir;
this.onEngineProcessError = onEngineProcessError;
this.exportEngineInfoFilename = exportEngineInfoFilename;
this.runtimeInfoPath = runtimeInfoPath;
this.engineProcessContainers = {};
}

Expand Down Expand Up @@ -216,7 +220,7 @@ export class EngineManager {
await this.runEngine(engineInfo.uuid);
}

await this.writeEngineInfoFor3rdParty();
await this.writeEngineInfosFor3rdParty();
}

/**
Expand Down Expand Up @@ -476,7 +480,7 @@ export class EngineManager {
);

this.runEngine(engineId);
this.writeEngineInfoFor3rdParty();
this.writeEngineInfosFor3rdParty();
resolve();
return;
}
Expand All @@ -490,7 +494,7 @@ export class EngineManager {
log.info(`ENGINE ${engineId}: Process killed. Restarting process...`);

this.runEngine(engineId);
this.writeEngineInfoFor3rdParty();
this.writeEngineInfosFor3rdParty();
resolve();
};

Expand Down Expand Up @@ -562,28 +566,18 @@ export class EngineManager {
/**
* サードパーティ向けの設定ファイルを書き出す
*/
async writeEngineInfoFor3rdParty() {
async writeEngineInfosFor3rdParty() {
await this.lock.acquire(this.lockKey, async () => {
log.info(`Engine information file (runtime-info.json) has been updated.`);

const engineInfos = this.fetchEngineInfos();
const engineInfoList = engineInfos.map((engineInfo) => {
return {
uuid: engineInfo.uuid,
host: engineInfo.host,
name: engineInfo.name,
path: engineInfo.path,
executionEnabled: engineInfo.executionEnabled,
executionFilePath: engineInfo.executionFilePath,
executionArgs: engineInfo.executionArgs,
type: engineInfo.type,
};
});
const outputInfoData = new OutputInfoDataFor3rdParty(app.getVersion());

// ファイルに書き出すデータをつくる
try {
await fs.promises.writeFile(
this.exportEngineInfoFilename,
JSON.stringify(engineInfoList)
this.runtimeInfoPath.getRuntimeInfoPath(),
JSON.stringify(outputInfoData.getOutputInfoData(engineInfos))
);
} catch (e) {
// ディスクの空き容量がない、他ツールからのファイルロック時をトラップ。
Expand Down
96 changes: 96 additions & 0 deletions src/shared/RuntimeInfoManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import path from "path";
import { EngineId, EngineInfo } from "@/type/preload";
/**
* サードパーティ向けランタイム情報のエクスポート情報クラス
*/
export class RuntimeInfoManager {
private runtimeInfoPath: string;

constructor(userDataPath: string) {
this.runtimeInfoPath = path.join(userDataPath, "runtime-info.json");
}

/**
* サードパーティ向けランタイム情報のファイルパスを取得する
*/
public getRuntimeInfoPath(): string {
return this.runtimeInfoPath;
}
}

/**
* サードパーティ向けランタイム情報のレコード定義
* Note:変更時はOutputInfoDataFor3rdPartyのバージョン定義も変更すること
*/
export interface EngineInfoRecordFor3rdParty {
uuid: EngineId;
url: string;
name: string;
path: string | undefined;
executionEnabled: boolean;
executionFilePath: string;
executionArgs: Array<string>;
type: string;
}

/**
* サードパーティ向けランタイム情報のレコード定義
* Note:変更時はOutputInfoDataFor3rdPartyのバージョン定義も変更すること
*/
export interface EngineInfoFormatFor3rdParty {
versions: {
fileFormat: string;
VOICEVOX: string;
};
engineInfos: EngineInfoRecordFor3rdParty[];
}

/**
* サードパーティ向けランタイム情報のエクスポートフォーマットクラス
*/
export class OutputInfoDataFor3rdParty {
private VOICEVOXVersion: string;

constructor(VOICEVOXVersion: string) {
this.VOICEVOXVersion = VOICEVOXVersion;
}

/**
* サードパーティ向けランタイム情報のフォーマットバージョン
* Note: 破壊的変更(削除、型変更)があった場合にメジャーバージョンを上げる
* 互換性のある変更(追加)があった場合にマイナーバージョンを上げる
*/
private fileFormatVersion = "1.0";

/**
* サードパーティ向けに提供するデータを取得
*/
public getOutputInfoData(
engineInfos: EngineInfo[]
): EngineInfoFormatFor3rdParty {
const engineInfoList: EngineInfoRecordFor3rdParty[] = engineInfos.map(
(engineInfo) => {
return {
uuid: engineInfo.uuid,
url: engineInfo.host,
name: engineInfo.name,
path: engineInfo.path,
executionEnabled: engineInfo.executionEnabled,
executionFilePath: engineInfo.executionFilePath,
executionArgs: engineInfo.executionArgs,
type: engineInfo.type,
};
}
);

const engineInfoFormatFor3rdParty: EngineInfoFormatFor3rdParty = {
versions: {
fileFormat: this.fileFormatVersion,
VOICEVOX: this.VOICEVOXVersion,
},
engineInfos: engineInfoList,
};

return engineInfoFormatFor3rdParty;
}
}

0 comments on commit c93a819

Please sign in to comment.