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

feat: Windowsのアンインストーラーでインストールしたエンジンを削除できるようにする #1518

Merged
merged 13 commits into from
Sep 17, 2023
Merged
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
151 changes: 151 additions & 0 deletions build/installer.nsh
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -799,3 +799,154 @@ FunctionEnd
${endif}
FunctionEnd
!macroend

; "%VITE_APP_NAME%"が空の状態でビルドすると他のソフトのファイルを消してしまうためビルドエラーにする。
!define DOLLAR "$"
!if "$%VITE_APP_NAME%" == "${DOLLAR}%VITE_APP_NAME%"
!error 'The environment variable "%VITE_APP_NAME%" is undefined.'
!endif
!if "$%VITE_APP_NAME%" == ""
!error 'The environment variable "%VITE_APP_NAME%" is empty.'
!endif

!macro locateVvppTmp callbacks
${Locate} "$APPDATA\$%VITE_APP_NAME%\vvpp-engines\.tmp" "/L=D /M=????????????? /G=0" ${callbacks}
!macroend

!macro locateVvppEngines callbacks
${Locate} "$APPDATA\$%VITE_APP_NAME%\vvpp-engines" "/L=D /M=*+????????-????-????-????-???????????? /G=0" ${callbacks}
!macroend

!macro customUninstallPage
; エンジンディレクトリが存在する場合は、消去するかのチェックボックスを案内する
; 存在しない場合はそのまま終了する
UninstPage custom un.removeUserDataPage un.removeUserDataPageLeave

Function un.removeUserDataPage
Push $0

Var /GLOBAL isExistEngine
StrCpy $isExistEngine "0"

${If} $installMode == "all"
SetShellVarContext current
${EndIf}

Push $R0

StrCpy $R0 "0"
!insertmacro locateVvppTmp un.isExistVvppTmp
${If} $R0 == "1"
StrCpy $isExistEngine "1"
${Else}
RMDir "$APPDATA\$%VITE_APP_NAME%\vvpp-engines\.tmp"
${EndIf}
ClearErrors

${If} $isExistEngine == "0"
StrCpy $R0 "0"
!insertmacro locateVvppEngines un.isExistVvppEngines
${If} $R0 == "1"
StrCpy $isExistEngine "1"
${Else}
RMDir "$APPDATA\$%VITE_APP_NAME%\vvpp-engines"
${EndIf}
ClearErrors
${EndIf}

Pop $R0

${If} $installMode == "all"
SetShellVarContext all
${EndIf}

${If} $isExistEngine == "0"
Pop $0
Abort
${EndIf}

nsDialogs::Create 1018
Pop $0

${If} $0 == "error"
Pop $0
Abort
${EndIf}

; 既にアンインストールは完了してしまっているためキャンセルボタンは無効化する
GetDlgItem $0 $HWNDPARENT 2
EnableWindow $0 0

${NSD_CreateCheckBox} 0 0 100% 12u "追加エンジンを削除する"
Var /GLOBAL removeAdditionalEngineCheckBox
Pop $removeAdditionalEngineCheckBox

nsDialogs::Show

Pop $0
FunctionEnd

Function un.removeUserDataPageLeave
Push $0
; 削除の処理
${NSD_GetState} $removeAdditionalEngineCheckBox $0

${If} $0 == ${BST_CHECKED}
${If} $installMode == "all"
SetShellVarContext current
${EndIf}

!insertmacro locateVvppTmp un.removeVvppTmp
RMDir "$APPDATA\$%VITE_APP_NAME%\vvpp-engines\.tmp"
!insertmacro locateVvppEngines un.removeVvppEngines
RMDir "$APPDATA\$%VITE_APP_NAME%\vvpp-engines"
; 未知のファイルが残っている場合削除されずにエラーフラグが立つのでクリアする
ClearErrors

${If} $installMode == "all"
SetShellVarContext all
${EndIf}
${EndIf}
Pop $0
FunctionEnd

Function un.isExistVvppTmp
; 実行された場合は"$R0"に"1"を代入する。
StrCpy $R0 "1"
Push "StopLocate"
FunctionEnd

Function un.removeVvppTmp
RMDir /r "$R9"
Push ""
FunctionEnd

Function un.isExistVvppEngines
; "engine_manifest.json"がある場合"$R0"に"1"を代入する。
${If} ${FileExists} "$R9\engine_manifest.json"
StrCpy $R0 "1"
Push "StopLocate"
${Else}
Push ""
${EndIf}
FunctionEnd

Function un.removeVvppEngines
; "engine_manifest.json"があるか確認してから削除する。
${If} ${FileExists} "$R9\engine_manifest.json"
RMDir /r "$R9"
ClearErrors
${EndIf}
Push ""
FunctionEnd

; MUI_UNPAGE_FINISHの戻るボタンを無効化する
!define MUI_PAGE_CUSTOMFUNCTION_SHOW un.disableBack

Function un.disableBack
Push $0
GetDlgItem $0 $HWNDPARENT 3
EnableWindow $0 0
Pop $0
FunctionEnd
!macroend
4 changes: 4 additions & 0 deletions electron-builder.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// @ts-check
const path = require("path");
const fs = require("fs");
const dotenv = require("dotenv");

const dotenvPath = path.join(process.cwd(), ".env.production");
dotenv.config({ path: dotenvPath });

const VOICEVOX_ENGINE_DIR =
process.env.VOICEVOX_ENGINE_DIR ?? "../voicevox_engine/run.dist/";
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/background/VvppManager.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { mkdtemp, rm } from "fs/promises";
import { tmpdir } from "os";
import { join } from "path";
import { beforeEach, expect, test } from "vitest";
import { EngineId, MinimumEngineManifest } from "@/type/preload";
import VvppManager from "@/background/vvppManager";

const dummyMinimumManifest: MinimumEngineManifest = {
name: "Test Engine",
uuid: EngineId("295c656b-b800-449f-aee6-b03e493816d7"),
command: "",
port: 5021,
supported_features: {},
};

interface VvppManagerTestContext {
manager: VvppManager;
}

beforeEach<VvppManagerTestContext>(async (context) => {
const vvppEngineDir = await mkdtemp(join(tmpdir(), "vvppTest"));
context.manager = new VvppManager({ vvppEngineDir });

return async () => {
await rm(vvppEngineDir, { recursive: true });
};
});

test<VvppManagerTestContext>("追加エンジンのディレクトリ名は想定通りか", ({
manager,
}) => {
const dirName = manager.toValidDirName(dummyMinimumManifest);
// NOTE: パターンを変更する場合アンインストーラーのコードを変更する必要がある
const pattern = /^.+\+.{8}-.{4}-.{4}-.{4}-.{12}$/;
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved

expect(dirName).toMatch(pattern);
});
8 changes: 8 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ export default defineConfig((options) => {
path.resolve(__dirname, "tests/unit/**/*.spec.ts").replace(/\\/g, "/"),
],
environment: "happy-dom",
environmentMatchGlobs: [
[
path
.resolve(__dirname, "tests/unit/background/**/*.spec.ts")
.replace(/\\/g, "/"),
"node",
],
],
globals: true,
},

Expand Down