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

fix: Docker関連ファイルの削除とTyposのスクリプトの追加 #2239

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
17502c8
[add] #2206 typosのバイナリをダウンロードするスクリプトが必要なため
gigi434 Aug 13, 2024
3539b04
[fix] #2206 windowsにてTyposのバイナリデータがダウンロードと解凍・削除されない不具合を修正した
gigi434 Aug 14, 2024
a70e64e
[fix] #2206 cross-envを使用しているためプラットフォームの違いを吸収するコマンドに修正した
gigi434 Aug 17, 2024
35321fe
[fix] #2206 ダウンロードしたいバイナリがすでにある時に再度ダウンロードされるのではなく、スキップされるように修正した
gigi434 Aug 17, 2024
c1c44a2
[fix] #2206 もしバイナリが既に存在する場合、ダウンロードをスキップするように修正した
gigi434 Aug 18, 2024
0645e30
[fix] #2206 macosのCPUアーキテクチャの違いを考慮し最適なバイナリをダウンロードするように修正した
gigi434 Aug 18, 2024
4c2b448
[document] #2206 cargoを通したインストールではなくなったためそれに応じたドキュメントに修正した
gigi434 Aug 18, 2024
8adb48b
[delete] #2206 Docker関連のファイルは使用しないため
gigi434 Aug 18, 2024
6456c06
[fix] #2206 バイナリをダウンロードする際に使用する渡すURLのオブジェクトを文字列にすることで無駄な処理を削減した
gigi434 Aug 21, 2024
cc565e8
[fix] #2206 Github以外に公開している場合のバイナリにも対処し、かつ、ダウンロードする処理を共通化して冗長な部分を削除した
gigi434 Aug 24, 2024
a5c42e3
[fix] #2206 空行を入れていなかったため修正した
gigi434 Aug 24, 2024
b2b4ceb
[delete] #2206 typosは手動で行うため削除する
gigi434 Aug 24, 2024
2d620bd
[fix] #2206 linuxで.tar.gzファイルが解凍されない不具合を修正した
gigi434 Aug 26, 2024
2aee482
[fix] #2206 タイポがあったため修正した
gigi434 Aug 26, 2024
8387004
[fix] #2206 定数であるのにコンスタントケースになっていないため修正した
gigi434 Aug 26, 2024
b9a9768
[fix] #2206 条件を間違えていたため修正した
gigi434 Aug 26, 2024
6a34889
[fix] #2206 typosのCIが実行できていないため追加した
gigi434 Sep 1, 2024
ee91136
[fix] #2206 TYPOS_URLSがstringまたはobjectを返すことになるため、型ガードを追加しなければならないことを修正した
gigi434 Sep 1, 2024
e9a4b24
[fix] #2206 curlコマンドがない環境のためにnode-fetchを使用する方法に修正した
gigi434 Sep 1, 2024
6d8483a
[fix] #2206 変数であるのに定数として定義していたため修正した
gigi434 Sep 5, 2024
1472a72
[fix] #2206 markdownlintのCIでエラーが出るため、typosのmarkdownファイルが存在するdocディレクトリ…
gigi434 Sep 9, 2024
3a45518
Merge branch 'main' into feature/#2206/FixAroundDockerEnvironment
Hiroshiba Sep 13, 2024
4e7c3ae
Apply suggestions from code review
Hiroshiba Sep 13, 2024
a538cbd
Apply suggestions from code review
Hiroshiba Sep 13, 2024
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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ VITE_DEFAULT_ENGINE_INFOS=`[
- 命名に使っている英語が誤っていないことを確認します。

```bash
typos
npm run typos
```

- 個人環境でVOICEVOXを実行し、提出前に、一通り動くことを確認します。
Expand Down
14 changes: 0 additions & 14 deletions Dockerfile

This file was deleted.

3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,9 @@ npm run lint
## タイポチェック

[typos](https://github.com/crate-ci/typos) を使ってタイポのチェックを行っています。
[typos をインストール](https://github.com/crate-ci/typos#install) した後

```bash
typos
npm run typos
```

でタイポチェックを行えます。
Expand Down
278 changes: 278 additions & 0 deletions build/downloadTypos.js
Copy link
Member

Choose a reason for hiding this comment

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

(判断メモです!)

こちらのファイルは他のファイルに比べるとコメントが結構多めで、一部冗長な箇所もあるなと感じました!
が、問題なさそうと思いました!

ここはビルドでしか使われなく、おそらく滅多に変更されることはないと思われます。
よく変更される箇所でしたらより洗練する方がいいかもしれませんが、ここはあまり変更されない場所なので、丁寧にコメントされているのはOKだと思います!

Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
// @ts-check
/**
* OSに合ったtyposのバイナリをダウンロードするスクリプト。
*/
const { exec } = require("child_process");
const { promisify } = require("util");
const { platform, arch } = require("os");
const { join, resolve } = require("path");
const { mkdirSync, existsSync, unlinkSync } = require("fs");
const fetch = require("node-fetch");
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved

// 全バイナリデータのパス
const BINARY_BASE_PATH = resolve(__dirname, "vendored");
// 7zのバイナリデータのパス
const SEVEN_ZIP_BINARY_PATH = join(BINARY_BASE_PATH, "7z", "7za.exe");
// ダウンロードしたいバイナリデータの配列オブジェクト
const WANT_TO_DOWNLOAD_BINARIES = [
{
name: "typos",
repo: "crate-ci/typos",
version: "latest",
},
// {
// name: 'anotherBinary',
// repo: 'some-other-repo/some-binary',
// version: 'v1.2.3', // 指定したバージョン
// }
];
Copy link
Member

Choose a reason for hiding this comment

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

Githubリポジトリに沿う形で書くことで、他のを追加しやすくしてみた感じですよね!
VOICEVOXで長いことメンテしてて気づいたのですが、そもそもGithubでリリースしてない人とか、Githubに置いてるけどファイル名のOS・アーキテクチャの書き方がバラバラだったりとかいろいろあるので、逆に汎用性が下がっているかもです。

個人的には、数年単位でメンテすること考えると、ダウンロードURLを直書きしちゃうのが良い塩梅に感じてます。
@sevenc-nanashi さんと同じ意見になりそう)

// OS名を定義するオブジェクト
const OS = {
LINUX: "linux",
MACOS: "darwin",
WINDOWS: "win32",
};
// 動作しているPCのOS文字列
const CURRENT_OS = platform();
// 動作しているOSのCPUアーキテクチャ文字列
const CURRENT_CPU_ARCHITECTURE = arch();
// OSのコマンドを非同期関数として処理させるために必要な関数
const execAsync = promisify(exec);

/**
* コマンドを実行し、その進行状況を出力するヘルパー関数
* @param {Object} params - コマンド実行のパラメータ
* @param {string} params.command - 実行するシェルコマンド
* @param {string} params.description - コマンドの説明を表示するテキスト
*/
async function runCommand({ command, description }) {
console.log(`Running: ${description}`);
try {
await execAsync(command);
} catch (error) {
console.error(`An error occured: ${error.message}`);

Check warning on line 53 in build/downloadTypos.js

View workflow job for this annotation

GitHub Actions / typos

"occured" should be "occurred".
throw error;
}
}

/**
* バイナリをダウンロードして解凍し、実行権限を付与する関数
*
* @param {Object} params - バイナリをダウンロードするための情報を含むオブジェクト
* @param {Array<Object>} params.binaries - バイナリの情報を含む配列オブジェクト
* @param {Function} params.downloadBinaryFunction - OSに応じたバイナリのダウンロード関数
*/
async function processBinaries({ binaries, downloadBinaryFunction }) {
// 各バイナリを非同期で並行してダウンロードする
await Promise.all(
binaries.map(async (binary) => {
// もしバイナリが既に存在する場合、ダウンロードをスキップする
const binaryPath = join(BINARY_BASE_PATH, binary.name);
let binaryFilePath;

// OSに応じたバイナリを確認し、存在するならダウンロードをスキップする
switch (CURRENT_OS) {
case OS.LINUX:
binaryFilePath = `${binaryPath}/${binary.name}`;
break;
case OS.MACOS:
binaryFilePath = `${binaryPath}/${binary.name}`;
break;
case OS.WINDOWS:
binaryFilePath = `${binaryPath}\\${binary.name}.exe`;
break;
default:
throw new Error("Unsupported OS");
}

if (existsSync(binaryFilePath)) {
console.log(`${binary.name} already downloaded`);
return;
}

try {
console.log(`Fetching download URL for ${binary.name}`);
const url = await getBinaryURL({
repo: binary.repo,
version: binary.version,
});

await downloadBinaryFunction({ name: binary.name, url });
} catch (err) {
console.error(
`An error occurred during ${binary.name} installation: ${err.message}`,
);
}
}),
);
}

/**
* GitHub APIを使って、リポジトリのリリースから特定のOSのバイナリをダウンロードするURLを取得する関数
* @param {Object} params - Githubからバイナリをダウンロードするための情報を含むオブジェクト
* @param {string} params.repo - GitHubリポジトリの名前(例: 'crate-ci/typos')
* @param {string} [params.version='latest'] - インストールしたい特定のバージョン(省略した場合は最新バージョン)
* @returns {Promise<string>} 特定のOSに対応するバイナリのダウンロードするためのURL文字列
*/
async function getBinaryURL({ repo, version = "latest" }) {
const apiUrl =
version === "latest"
? `https://api.github.com/repos/${repo}/releases/latest`
: `https://api.github.com/repos/${repo}/releases/tags/${version}`;

const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(
`Release information for the specified version "${version}" could not be found.`,
);
}

const data = await response.json();

// Githubのリリース情報から各OSに最適化されたバイナリをダウンロードするURLを定義する
const asset = data.assets.find((asset) => {
if (CURRENT_OS === OS.LINUX && asset.name.includes("linux"))

Check failure on line 134 in build/downloadTypos.js

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎·····`
return true;
else if (CURRENT_OS === OS.WINDOWS && asset.name.includes("windows"))
return true;
else if (
CURRENT_OS === OS.MACOS &&
CURRENT_CPU_ARCHITECTURE === "arm64" &&
asset.name.includes("aarch64-apple-darwin")
)
return true;
else if (
CURRENT_OS === OS.MACOS &&
CURRENT_CPU_ARCHITECTURE === "x64" &&
asset.name.includes("x86_64-apple-darwin")
)
return true;
});

return asset.browser_download_url;
}

/**
* Linuxに合わせたバイナリをダウンロードするための関数
* @param {Object} params - バイナリの情報を含むオブジェクト
* @param {string} params.name - バイナリの名前
* @param {string} params.url - 各プラットフォームのダウンロードURL
*/
async function downloadBinaryForLinux({ name, url }) {
Copy link
Member

Choose a reason for hiding this comment

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

ダウンロード→解凍→ファイル権限変更→削除の流れですが、たぶんlinux/mac/winで別々にせず、
nodejs fetch→7z解凍→nodjsのfs.chmod→unlinkSyncにすればほとんど共通化できると思います!

解凍だけ引数が違うので、getBinaryURLのように解凍用関数だけは作ることになるかもですが。

const binaryPath = join(BINARY_BASE_PATH, name);
const tarballPath = `${binaryPath}/${name}.tar.gz`;

// もし各バイナリのディレクトリがないなら新しく作成する
if (!existsSync(binaryPath)) {
mkdirSync(binaryPath, { recursive: true });
}

await runCommand({
command: `curl -L ${url} -o ${tarballPath}`,
description: `Downloading ${name} binary`,
});
await runCommand({
command: `tar -xzvf ${tarballPath} -C ${binaryPath}`,
description: `Extracting ${name} binary`,
});
await runCommand({
command: `chmod +x ${binaryPath}/${name}`,
description: `Granting execute permissions to the ${name} binary`,
});

// 解凍後に圧縮ファイルを削除
unlinkSync(tarballPath);
}

/**
* Windowsに合わせたバイナリをダウンロードするための関数
* @param {Object} params - バイナリの情報を含むオブジェクト
* @param {string} params.name - バイナリの名前
* @param {string} params.url - 各プラットフォームのダウンロードURL
*/
async function downloadBinaryForWin({ name, url }) {
const binaryPath = join(BINARY_BASE_PATH, name);
const zipFilePath = `${binaryPath}\\${name}.zip`;

// もし各バイナリのディレクトリがないなら新しく作成する
if (!existsSync(binaryPath)) {
mkdirSync(binaryPath, { recursive: true });
}

await runCommand({
command: `curl -L ${url} -o ${zipFilePath}`,
description: `Downloading ${name} binary`,
});
await runCommand({
command: `"${SEVEN_ZIP_BINARY_PATH}" x ${zipFilePath} -o${binaryPath}`,
description: `Extracting ${name} binary`,
});

// 解凍後に圧縮ファイルを削除
unlinkSync(zipFilePath);
}

/**
* macOSに合わせたバイナリをダウンロードするための関数
* @param {Object} params - バイナリの情報を含むオブジェクト
* @param {string} params.name - バイナリの名前
* @param {string} params.url - 各プラットフォームのダウンロードURL
*/
async function downloadBinaryForMac({ name, url }) {
const binaryPath = join(BINARY_BASE_PATH, name);
const tarballPath = `${binaryPath}/${name}.tar.gz`;

// もし各バイナリのディレクトリがないなら新しく作成する
if (!existsSync(binaryPath)) {
mkdirSync(binaryPath, { recursive: true });
}

await runCommand({
command: `curl -L ${url} -o ${tarballPath}`,
description: `Downloading ${name} binary`,
});
await runCommand({
command: `tar -xzvf ${tarballPath} -C ${binaryPath}`,
description: `Extracting ${name} binary`,
});
await runCommand({
command: `chmod +x ${binaryPath}/${name}`,
description: `Granting execute permissions to the ${name} binary`,
});

// 解凍後に圧縮ファイルを削除
unlinkSync(tarballPath);
}

/**
* OSに応じた関数を選択し、バイナリデータを処理する関数
*
* @param {Object} params - メイン処理のパラメータ
* @param {Array<Object>} params.binaries - 複数のバイナリの情報を含む配列オブジェクト
*/
async function main({ binaries }) {
let downloadBinaryFunction;

// OSに応じたインストール関数を選択
switch (CURRENT_OS) {
case OS.LINUX:
downloadBinaryFunction = downloadBinaryForLinux;
break;
case OS.MACOS:
downloadBinaryFunction = downloadBinaryForMac;
break;
case OS.WINDOWS:
downloadBinaryFunction = downloadBinaryForWin;
break;
default:
throw new Error("Unsupported OS");
}

// バイナリデータを処理
await processBinaries({ binaries, downloadBinaryFunction });
}

// main関数実行
(async () => {
await main({ binaries: WANT_TO_DOWNLOAD_BINARIES });
})();
7 changes: 4 additions & 3 deletions build/vendored/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

このディレクトリはダウンロードしたファイルを格納するためのものです。

| ディレクトリ名 | 内容 | ダウンローダー |
| -------------- | ------------------------------ | --------------------- |
| `7z` | [7-Zip](http://www.7-zip.org/) | `build/download7z.js` |
| ディレクトリ名 | 内容 | ダウンローダー |
| -------------- | ------------------------------------------ | ------------------------ |
| `7z` | [7-Zip](http://www.7-zip.org/) | `build/download7z.js` |
| `typos` | [typos](https://github.com/crate-ci/typos) | `build/downloadTypos.js` |
Copy link
Member

Choose a reason for hiding this comment

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

空行を入れた方がいいかも。

33 changes: 0 additions & 33 deletions docker-compose.yml

This file was deleted.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
"fmt": "eslint --ext .js,.vue,.ts *.config.* src tests build .storybook --fix",
"markdownlint": "markdownlint --ignore node_modules/ --ignore dist/ --ignore dist_electron/ ./",
"typecheck": "vue-tsc --noEmit",
"typos": "cross-env ./build/vendored/typos/typos",
sevenc-nanashi marked this conversation as resolved.
Show resolved Hide resolved
"electron:build": "cross-env VITE_TARGET=electron vite build && electron-builder --config electron-builder.config.js --publish never",
"electron:serve": "cross-env VITE_TARGET=electron vite",
"browser:serve": "cross-env VITE_TARGET=browser vite",
"browser:build": "cross-env VITE_TARGET=browser vite build",
"postinstall": "electron-builder install-app-deps && node build/download7z.js",
"postinstall": "electron-builder install-app-deps && node build/download7z.js && node build/downloadTypos.js",
"postuninstall": "electron-builder install-app-deps",
"license:generate": "node build/generateLicenses.js",
"license:merge": "node build/mergeLicenses.js",
Expand Down Expand Up @@ -128,4 +129,4 @@
"vue-tsc": "2.0.24",
"yargs": "17.2.1"
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

ファイル末の空行を入れた方がいいかも。

Loading