From 12dfad93bfe0ded9ff576a53a4b97edacc797425 Mon Sep 17 00:00:00 2001 From: hanyixuanten <105723997+hanyixuanten@users.noreply.github.com> Date: Wed, 29 Jul 2026 13:53:01 +0800 Subject: [PATCH] use build.sh --- .github/workflows/package-plugin.yml | 25 +--------- README.md | 2 + README.zh-CN.md | 2 + build.sh | 70 ++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 23 deletions(-) create mode 100755 build.sh diff --git a/.github/workflows/package-plugin.yml b/.github/workflows/package-plugin.yml index c396d8a..6447e2b 100644 --- a/.github/workflows/package-plugin.yml +++ b/.github/workflows/package-plugin.yml @@ -30,30 +30,9 @@ jobs: id: package run: | set -eu - plugin_slug="btranslate" version="$(grep -E '^ \* Version:' btranslate.php | sed -E 's/^ \* Version: *//')" - archive_name="${plugin_slug}-${version}.zip" - - rm -rf build - mkdir -p "build/${plugin_slug}" - cp btranslate.php uninstall.php readme.txt readme-zh_CN.txt LICENSE "build/${plugin_slug}/" - cp -R includes "build/${plugin_slug}/" - cp -R assets "build/${plugin_slug}/" - cp -R languages "build/${plugin_slug}/" - msgfmt --check --check-format \ - --output-file="build/${plugin_slug}/languages/btranslate-zh_CN.mo" \ - languages/btranslate-zh_CN.po - ( - cd build - zip -qr "../${archive_name}" "${plugin_slug}" - ) - - unzip -p "${archive_name}" "${plugin_slug}/btranslate.php" | grep -q '^ \* Plugin Name: Btranslate$' - unzip -p "${archive_name}" "${plugin_slug}/readme.txt" | grep -q "^Stable tag: ${version}$" - unzip -p "${archive_name}" "${plugin_slug}/readme-zh_CN.txt" | grep -q "^Stable tag: ${version}$" - unzip -Z1 "${archive_name}" | grep -q "^${plugin_slug}/languages/btranslate-zh_CN.mo$" - ! unzip -Z1 "${archive_name}" | grep -Eq '/README(\.zh-CN)?\.md$' - + archive_name="btranslate-${version}.zip" + ./build.sh build printf 'archive=%s\n' "${archive_name}" >> "$GITHUB_OUTPUT" - name: Upload plugin archive diff --git a/README.md b/README.md index a4dba1c..8db4d03 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,8 @@ The repository has no Composer or PHPUnit dependency. Validate PHP syntax with t find . -path './.git' -prune -o -type f -name '*.php' -print0 | xargs -0 -n1 php -l ``` +Build the installable ZIP locally with `./build.sh build`. The command compiles the Simplified Chinese translation catalog, creates a versioned `btranslate-.zip` in the repository root, and validates its contents. Run `./build.sh clean` to remove the build directory and generated plugin ZIP files. The build requires `gettext`, `zip`, and `unzip`. + Before production use, test the plugin in a WordPress staging environment. Cover language resolution, rewrite behavior, persistence reuse and invalidation, Baidu request signing and errors, content filtering, and fallback rendering. Mock `wp_remote_post` responses for any automated tests so they do not need live credentials, a database, or outbound network access. ## Retranslation diff --git a/README.zh-CN.md b/README.zh-CN.md index c63b5fb..4161716 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -76,6 +76,8 @@ WordPress 后台界面会跟随当前 WordPress 语言:默认显示英文,Wo find . -path './.git' -prune -o -type f -name '*.php' -print0 | xargs -0 -n1 php -l ``` +运行 `./build.sh build` 可在本地构建可安装的 ZIP。该命令会编译简体中文翻译目录,在仓库根目录生成带版本号的 `btranslate-<版本>.zip`,并校验压缩包内容。运行 `./build.sh clean` 可删除构建目录和生成的插件 ZIP。构建依赖 `gettext`、`zip` 和 `unzip`。 + 投入生产前,应在 WordPress 预发布环境中测试插件,覆盖语言解析、重写行为、持久化复用与失效、百度请求签名和错误处理、内容过滤和回退渲染。任何自动化测试都应模拟 `wp_remote_post` 响应,避免依赖真实凭据、数据库或外部网络访问。 ## 重新翻译 diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..a818d34 --- /dev/null +++ b/build.sh @@ -0,0 +1,70 @@ +#!/usr/bin/env sh + +set -eu + +plugin_slug="btranslate" +script_dir="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)" +build_dir="${script_dir}/build" +plugin_dir="${build_dir}/${plugin_slug}" + +clean() { + find "${script_dir}" -maxdepth 1 -type f -name "${plugin_slug}-*.zip" -delete + rm -rf "${build_dir}" +} + +build() { + for command in grep sed msgfmt zip unzip; do + if ! command -v "${command}" >/dev/null 2>&1; then + printf 'Required command not found: %s\n' "${command}" >&2 + exit 1 + fi + done + + version="$(grep -E '^ \* Version:' "${script_dir}/btranslate.php" | sed -E 's/^ \* Version: *//')" + archive_name="${plugin_slug}-${version}.zip" + archive_path="${script_dir}/${archive_name}" + + clean + mkdir -p "${plugin_dir}" + cp "${script_dir}/btranslate.php" \ + "${script_dir}/uninstall.php" \ + "${script_dir}/readme.txt" \ + "${script_dir}/readme-zh_CN.txt" \ + "${script_dir}/LICENSE" \ + "${plugin_dir}/" + cp -R "${script_dir}/includes" "${plugin_dir}/" + cp -R "${script_dir}/assets" "${plugin_dir}/" + cp -R "${script_dir}/languages" "${plugin_dir}/" + msgfmt --check --check-format \ + --output-file="${plugin_dir}/languages/btranslate-zh_CN.mo" \ + "${script_dir}/languages/btranslate-zh_CN.po" + ( + cd "${build_dir}" + zip -qr "${archive_path}" "${plugin_slug}" + ) + + unzip -p "${archive_path}" "${plugin_slug}/btranslate.php" | grep -q '^ \* Plugin Name: Btranslate$' + unzip -p "${archive_path}" "${plugin_slug}/readme.txt" | grep -q "^Stable tag: ${version}$" + unzip -p "${archive_path}" "${plugin_slug}/readme-zh_CN.txt" | grep -q "^Stable tag: ${version}$" + unzip -Z1 "${archive_path}" | grep -q "^${plugin_slug}/languages/btranslate-zh_CN.mo$" + if unzip -Z1 "${archive_path}" | grep -Eq '/README(\.zh-CN)?\.md$'; then + printf 'Unexpected development README found in archive.\n' >&2 + exit 1 + fi + + printf 'Built %s\n' "${archive_path}" +} + +case "${1:-build}" in + build) + build + ;; + clean) + clean + printf 'Removed local build artifacts.\n' + ;; + *) + printf 'Usage: %s [build|clean]\n' "$0" >&2 + exit 2 + ;; +esac \ No newline at end of file