Repository navigation
Expand file tree
/
Copy pathborris-flow
More file actions
executable file
·193 lines (165 loc) · 3.72 KB
/
Copy pathborris-flow
File metadata and controls
executable file
·193 lines (165 loc) · 3.72 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
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_NAME="$(basename "$0")"
SCRIPT_SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SCRIPT_SOURCE" ]; do
SCRIPT_DIR="$(cd -P "$(dirname "$SCRIPT_SOURCE")" && pwd)"
SCRIPT_SOURCE="$SCRIPT_DIR/$(readlink "$SCRIPT_SOURCE")"
done
SCRIPT_DIR="$(cd -P "$(dirname "$SCRIPT_SOURCE")" && pwd)"
TEMPLATE_CLAUDE_DIR="${SCRIPT_DIR}/.claude"
TEMPLATE_CLAUDE_MD="${SCRIPT_DIR}/CLAUDE.md"
PACKAGE_JSON="${SCRIPT_DIR}/package.json"
get_version() {
if [ -f "$PACKAGE_JSON" ]; then
python3 - "$PACKAGE_JSON" <<'PY'
import json
import sys
with open(sys.argv[1], 'r', encoding='utf-8') as f:
data = json.load(f)
print(data.get('version', 'dev'))
PY
return
fi
echo "0.0.0"
}
usage() {
cat <<EOF
Usage:
${SCRIPT_NAME} init [TARGET_DIR] [--force]
${SCRIPT_NAME} --help
${SCRIPT_NAME} --version
Commands:
init [TARGET_DIR] Install Borris-Flow into TARGET_DIR (defaults to current directory).
Options:
--force, -f Overwrite existing files
--help, -h Show this help text
--version, -V Show version
EOF
}
version() {
echo "${SCRIPT_NAME} $(get_version)"
}
confirm() {
local prompt="$1"
while true; do
read -r -p "${prompt} [y/N]: " reply
case "$reply" in
[yY]|[yY][eE][sS])
return 0
;;
[nN]|[nN][oO]|"")
return 1
;;
*)
echo "Please enter y or n."
;;
esac
done
}
copy_template() {
local target="$1"
local force="$2"
python3 - "$target" "$TEMPLATE_CLAUDE_DIR" "$TEMPLATE_CLAUDE_MD" "$force" <<'PY'
from pathlib import Path
import shutil
import sys
target = Path(sys.argv[1])
template_dir = Path(sys.argv[2])
template_md = Path(sys.argv[3])
force = sys.argv[4] == "1"
def sync(src: Path, dst: Path):
if src.is_dir():
# avoid copying generated worktrees if template accidentally contains them
if src.name == "worktrees" and src.parent.name == ".claude":
return
dst.mkdir(parents=True, exist_ok=True)
for child in src.iterdir():
sync(child, dst / child.name)
return
if dst.exists() and not force:
return
dst.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(src, dst)
sync(template_dir, target / ".claude")
sync(template_md, target / "CLAUDE.md")
PY
}
run_init() {
local target="."
local force=false
while [[ $# -gt 0 ]]; do
case "$1" in
--force|-f)
force=true
shift
;;
--help|-h)
usage
return 0
;;
*)
if [[ "$target" == "." ]]; then
target="$1"
shift
else
echo "Error: too many positional arguments: $1" >&2
usage
return 1
fi
;;
esac
done
if [[ ! -d "$TEMPLATE_CLAUDE_DIR" || ! -f "$TEMPLATE_CLAUDE_MD" ]]; then
echo "Error: template files missing from ${SCRIPT_DIR}" >&2
return 1
fi
mkdir -p "$target"
local target_abs
target_abs="$(cd "$target" && pwd)"
if ! confirm "Install Borris-Flow into '${target_abs}'?"; then
echo "Aborted."
return 0
fi
local force_flag="0"
if [[ "$force" == true ]]; then
force_flag="1"
fi
copy_template "$target_abs" "$force_flag"
echo "Installed Borris-Flow into: ${target_abs}"
}
if [[ $# -eq 0 ]]; then
usage
exit 0
fi
FORCE_GLOBAL=false
while [[ $# -gt 0 ]]; do
case "$1" in
--help|-h)
usage
exit 0
;;
--version|-V)
version
exit 0
;;
--force|-f)
FORCE_GLOBAL=true
shift
;;
init)
shift
if [[ "$FORCE_GLOBAL" == true ]]; then
run_init --force "$@"
else
run_init "$@"
fi
exit $?
;;
*)
echo "Error: unknown command: $1" >&2
usage
exit 1
;;
esac
done