Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·353 lines (300 loc) · 8.77 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·353 lines (300 loc) · 8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env bash
set -euo pipefail
REPO_URL="${TINYPOWERS_REPO_URL:-https://github.com/Walternater/tinypowers.git}"
INSTALL_DIR="${TINYPOWERS_HOME:-$HOME/.tinypowers}"
CLAUDE_SKILLS_DIR="${CLAUDE_SKILLS_DIR:-$HOME/.claude/skills}"
TARGET_VERSION="${TINYPOWERS_VERSION:-latest}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
FORCE=false
SKIP_LINKS=false
INSTALLED_VERSION_DISPLAY=""
INTERACTIVE_TTY=false
usage() {
cat <<EOF
用法:
./install.sh
curl -fsSL https://raw.githubusercontent.com/Walternater/tinypowers/main/install.sh | bash
参数:
--dir DIR 指定安装目录,默认: $HOME/.tinypowers
--skills-dir DIR 指定 Claude Code skills 目录,默认: $HOME/.claude/skills
--repo URL 指定仓库地址,默认: $REPO_URL
--version REF 安装指定版本,默认: latest(最新稳定 tag)
--force 覆盖已存在的非 git 安装目录,或替换冲突的本地 skill 目录
--skip-links 仅准备安装目录,不创建 skill symlink
--help 显示帮助
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--dir)
INSTALL_DIR="$2"
shift 2
;;
--skills-dir)
CLAUDE_SKILLS_DIR="$2"
shift 2
;;
--repo)
REPO_URL="$2"
shift 2
;;
--version)
TARGET_VERSION="$2"
shift 2
;;
--force)
FORCE=true
shift
;;
--skip-links)
SKIP_LINKS=true
shift
;;
--help|-h)
usage
exit 0
;;
*)
echo "未知参数: $1" >&2
usage >&2
exit 1
;;
esac
done
need_cmd() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "缺少依赖命令: $1" >&2
exit 1
fi
}
resolve_version() {
local repo_url="$1"
local requested="$2"
local latest_tag
if [[ "$requested" != "latest" ]]; then
printf '%s\n' "$requested"
return 0
fi
latest_tag="$(
git ls-remote --tags --refs "$repo_url" 'v*' \
| sed 's#^.*refs/tags/##' \
| sort -V \
| tail -n 1
)"
if [[ -z "$latest_tag" ]]; then
echo "未找到可用的版本 tag,请改用 --version main 或检查仓库 tags" >&2
exit 1
fi
printf '%s\n' "$latest_tag"
}
checkout_ref() {
local repo_dir="$1"
local ref="$2"
if git -C "$repo_dir" show-ref --verify --quiet "refs/remotes/origin/$ref"; then
git -C "$repo_dir" checkout --force "$ref"
git -C "$repo_dir" pull --ff-only origin "$ref"
return 0
fi
if git -C "$repo_dir" rev-parse --verify --quiet "refs/tags/$ref" >/dev/null; then
git -C "$repo_dir" -c advice.detachedHead=false checkout --force "tags/$ref"
return 0
fi
if git -C "$repo_dir" rev-parse --verify --quiet "$ref^{commit}" >/dev/null; then
git -C "$repo_dir" checkout --force "$ref"
return 0
fi
echo "无法解析版本: $ref" >&2
exit 1
}
installed_version() {
local repo_dir="$1"
local exact_tag
local branch_name
local commit_short
exact_tag="$(git -C "$repo_dir" describe --tags --exact-match 2>/dev/null || true)"
if [[ -n "$exact_tag" ]]; then
printf '%s\n' "$exact_tag"
return 0
fi
branch_name="$(git -C "$repo_dir" symbolic-ref --quiet --short HEAD 2>/dev/null || true)"
commit_short="$(git -C "$repo_dir" rev-parse --short HEAD 2>/dev/null || true)"
if [[ -n "$branch_name" && -n "$commit_short" ]]; then
printf '%s (%s)\n' "$branch_name" "$commit_short"
return 0
fi
if [[ -n "$commit_short" ]]; then
printf '%s\n' "$commit_short"
return 0
fi
echo "unknown"
}
copy_local_checkout() {
local src="$1"
local dest="$2"
mkdir -p "$dest"
if command -v rsync >/dev/null 2>&1; then
rsync -a --delete \
--exclude '.git' \
--exclude '.claude' \
--exclude '.codex' \
--exclude '.idea' \
--exclude '.tinypowers' \
--exclude '.DS_Store' \
--exclude 'tests/reports' \
--exclude 'tests/fixtures' \
"$src/" "$dest/"
else
find "$dest" -mindepth 1 -maxdepth 1 -exec rm -rf {} +
tar -C "$src" \
--exclude='.git' \
--exclude='.claude' \
--exclude='.codex' \
--exclude='.idea' \
--exclude='.tinypowers' \
--exclude='.DS_Store' \
--exclude='tests/reports' \
--exclude='tests/fixtures' \
-cf - . | tar -C "$dest" -xf -
fi
}
safe_symlink() {
local target="$1"
local link_path="$2"
if [[ -e "$link_path" && ! -L "$link_path" ]]; then
if [[ "$FORCE" == true ]]; then
rm -rf "$link_path"
else
echo "检测到已存在的本地目录,已停止以避免覆盖: $link_path" >&2
echo "如需替换为 tinypowers 提供的 symlink,请重新执行并附加 --force" >&2
exit 1
fi
fi
ln -sfn "$target" "$link_path"
}
ensure_link_path_safe() {
local link_path="$1"
if [[ -e "$link_path" && ! -L "$link_path" ]]; then
echo "检测到已存在的本地目录,已停止以避免覆盖: $link_path" >&2
echo "如需替换为 tinypowers 提供的 symlink,请重新执行并附加 --force" >&2
exit 1
fi
}
prompt_for_link_conflicts() {
local conflicts=("$@")
local choice
echo "检测到以下同名本地目录,直接覆盖可能会丢失原内容:" >&2
for path in "${conflicts[@]}"; do
echo " - $path" >&2
done
echo "" >&2
while true; do
printf "请选择: [k] 保留原内容并退出 / [r] 替换为 tinypowers 链接: " >&2
IFS= read -r choice </dev/tty || true
case "${choice:-}" in
k|K|"")
echo "已保留原内容,安装停止。" >&2
exit 1
;;
r|R)
FORCE=true
return 0
;;
*)
echo "请输入 k 或 r。" >&2
;;
esac
done
}
need_cmd git
LOCAL_CHECKOUT=false
if [[ -e "$SCRIPT_DIR/.git" && -d "$SCRIPT_DIR/skills" && -f "$SCRIPT_DIR/README.md" ]]; then
LOCAL_CHECKOUT=true
fi
if [[ -t 1 && -r /dev/tty ]]; then
INTERACTIVE_TTY=true
fi
echo "tinypowers 一键安装"
echo "===================="
echo "安装目录: $INSTALL_DIR"
echo "Claude Skills: $CLAUDE_SKILLS_DIR"
echo "目标版本: $TARGET_VERSION"
echo ""
if [[ "$LOCAL_CHECKOUT" == true ]]; then
SCRIPT_DIR_REAL="$(cd "$SCRIPT_DIR" && pwd -P)"
INSTALL_DIR_REAL="$INSTALL_DIR"
if [[ -e "$INSTALL_DIR" ]]; then
INSTALL_DIR_REAL="$(cd "$INSTALL_DIR" && pwd -P)"
fi
echo "来源: 当前本地仓库"
echo "版本策略: 本地仓库模式使用当前工作区内容"
INSTALLED_VERSION_DISPLAY="$(installed_version "$SCRIPT_DIR")"
if [[ "$INSTALL_DIR_REAL" != "$SCRIPT_DIR_REAL" ]]; then
copy_local_checkout "$SCRIPT_DIR" "$INSTALL_DIR"
fi
else
RESOLVED_VERSION="$(resolve_version "$REPO_URL" "$TARGET_VERSION")"
echo "来源: $REPO_URL"
echo "解析版本: $RESOLVED_VERSION"
if [[ -d "$INSTALL_DIR/.git" ]]; then
git -C "$INSTALL_DIR" fetch --prune --tags origin
checkout_ref "$INSTALL_DIR" "$RESOLVED_VERSION"
else
if [[ -e "$INSTALL_DIR" ]]; then
if [[ "$FORCE" == true ]]; then
rm -rf "$INSTALL_DIR"
else
echo "安装目录已存在且不是 git 仓库,请使用 --force 覆盖: $INSTALL_DIR" >&2
exit 1
fi
fi
git clone "$REPO_URL" "$INSTALL_DIR"
git -C "$INSTALL_DIR" fetch --prune --tags origin
checkout_ref "$INSTALL_DIR" "$RESOLVED_VERSION"
fi
INSTALLED_VERSION_DISPLAY="$(installed_version "$INSTALL_DIR")"
fi
if [[ "$SKIP_LINKS" == false ]]; then
conflict_paths=()
mkdir -p "$CLAUDE_SKILLS_DIR"
if [[ "$FORCE" == false ]]; then
if [[ -e "$CLAUDE_SKILLS_DIR/tinypowers" && ! -L "$CLAUDE_SKILLS_DIR/tinypowers" ]]; then
conflict_paths+=("$CLAUDE_SKILLS_DIR/tinypowers")
fi
for skill_dir in "$INSTALL_DIR"/skills/*; do
[[ -d "$skill_dir" ]] || continue
skill_name="$(basename "$skill_dir")"
if [[ -e "$CLAUDE_SKILLS_DIR/$skill_name" && ! -L "$CLAUDE_SKILLS_DIR/$skill_name" ]]; then
conflict_paths+=("$CLAUDE_SKILLS_DIR/$skill_name")
fi
done
if [[ ${#conflict_paths[@]} -gt 0 ]]; then
if [[ "$INTERACTIVE_TTY" == true ]]; then
prompt_for_link_conflicts "${conflict_paths[@]}"
else
for conflict_path in "${conflict_paths[@]}"; do
ensure_link_path_safe "$conflict_path"
done
fi
fi
fi
safe_symlink "$INSTALL_DIR" "$CLAUDE_SKILLS_DIR/tinypowers"
for skill_dir in "$INSTALL_DIR"/skills/*; do
[[ -d "$skill_dir" ]] || continue
skill_name="$(basename "$skill_dir")"
safe_symlink "$skill_dir" "$CLAUDE_SKILLS_DIR/$skill_name"
echo "已链接 skill: $skill_name"
done
fi
echo ""
echo "安装完成"
echo "--------"
echo "tinypowers: $INSTALL_DIR"
echo "当前版本: ${INSTALLED_VERSION_DISPLAY:-unknown}"
if [[ "$SKIP_LINKS" == false ]]; then
echo "skills 目录: $CLAUDE_SKILLS_DIR"
fi
echo ""
echo "下一步:"
echo "1. 打开 Claude Code 进入你的项目"
echo "2. 运行 /tech:init"
echo "3. 后续按 /tech:feature -> /tech:code -> /tech:commit 使用"