Files

86 lines
2.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# 给 Gitea 28.x 安装 GitHub 风格主题(lutinglt/gitea-github-theme 的 28.4.0 版 + 手机端微调)
# 用法:sudo bash install.sh [--dir /opt/gitea] [--no-restart] [--no-templates] [--no-locale] [--revert]
set -euo pipefail
GITEA_DIR=/opt/gitea
RESTART=1
DO_TEMPLATES=1
DO_LOCALE=1
REVERT=0
while [ $# -gt 0 ]; do
case "$1" in
--dir) GITEA_DIR="$2"; shift 2 ;;
--no-restart) RESTART=0; shift ;;
--no-templates) DO_TEMPLATES=0; shift ;;
--no-locale) DO_LOCALE=0; shift ;;
--revert) REVERT=1; shift ;;
-h|--help) sed -n '2,4p' "$0"; exit 0 ;;
*) echo "未知参数:$1" >&2; exit 1 ;;
esac
done
SRC="$(cd "$(dirname "$0")" && pwd)"
CSS_DIR="$GITEA_DIR/custom/public/assets/css"
TPL_DIR="$GITEA_DIR/custom/templates"
LOC_DIR="$GITEA_DIR/custom/options/locale"
CONF="$GITEA_DIR/custom/conf/app.ini"
[ -f "$CONF" ] || { echo "找不到 $CONF,先用 --dir 指定 Gitea 根目录" >&2; exit 1; }
if [ "$REVERT" = 1 ]; then
rm -f "$CSS_DIR"/theme-github-*.css
if [ "$DO_TEMPLATES" = 1 ]; then rm -rf "$TPL_DIR"/base "$TPL_DIR"/repo; fi
python3 - "$CONF" <<'PY'
import re, sys
p = sys.argv[1]
s = open(p).read()
s = re.sub(r'\n?\[ui\]\n(?:THEMES = [^\n]*\n)?(?:DEFAULT_THEME = [^\n]*\n)?', '\n', s, count=1)
open(p, 'w').write(s)
PY
echo "已移除主题文件与 [ui] 主题配置(templates 目录里的 base/ repo/ 也一并删了)"
[ "$RESTART" = 1 ] && systemctl restart gitea && echo "gitea 已重启"
exit 0
fi
mkdir -p "$CSS_DIR"
cp "$SRC"/theme/theme-github-*.css "$CSS_DIR"/
echo "✓ CSS → $CSS_DIR"
if [ "$DO_TEMPLATES" = 1 ]; then
mkdir -p "$TPL_DIR"
cp -r "$SRC"/templates/. "$TPL_DIR"/
echo "✓ 模板 → $TPL_DIR(会改变仓库页布局,和 Gitea 版本强绑定)"
fi
if [ "$DO_LOCALE" = 1 ]; then
mkdir -p "$LOC_DIR"
cp "$SRC"/locale/locale_zh-CN.json "$LOC_DIR"/
echo "✓ 中文翻译 → $LOC_DIR"
fi
cp "$CONF" "$CONF.bak.$(date +%s)"
python3 - "$CONF" <<'PY'
import re, sys
p = sys.argv[1]
s = open(p).read()
block = ("[ui]\n"
"THEMES = gitea-auto, gitea-light, gitea-dark, github-auto, github-light, github-dark, github-soft-dark\n"
"DEFAULT_THEME = github-auto\n")
if '[ui]' in s:
s = re.sub(r'\[ui\][^\[]*', block, s, count=1)
else:
s = s.rstrip() + "\n\n" + block
open(p, 'w').write(s)
PY
echo "✓ app.ini 已写入 THEMES / DEFAULT_THEME(原文件留了 .bak 备份)"
if [ "$RESTART" = 1 ]; then
systemctl restart gitea
sleep 2
systemctl is-active gitea >/dev/null && echo "✓ gitea 已重启,去 设置 → 外观 里选 GitHub 主题"
else
echo "提示:改完 app.ini 要重启 gitea 才生效"
fi