-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitpack
More file actions
245 lines (206 loc) · 5.92 KB
/
Copy pathgitpack
File metadata and controls
245 lines (206 loc) · 5.92 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
#!/bin/sh
gitpack_url="https://gitpack.htlc.io"
bin_dir="$HOME/.local/bin"
cmd_help() {
echo "Usage: gitpack <command>"
echo ""
echo "Commands:"
echo " gitpack install [app] - Install an app"
echo " gitpack remove [app] - Remove an app"
echo " gitpack run [app] % - Update and run an app. Additional arguments are passed through to the app"
echo " gitpack update - Update gitpack itself"
echo ""
}
ensure_gitpack_installed() {
# Ensure ~/.local/bin exists
mkdir -p "$bin_dir"
# Ensure ~/.local/bin is in PATH
case ":$PATH:" in
*":$bin_dir:"*) ;;
*)
echo "Warning: $bin_dir is not in your PATH."
# Detect the appropriate dotfile for the user's shell
shell_name="$(basename "$SHELL")"
case "$shell_name" in
zsh) dotfile="$HOME/.zshrc" ;;
bash) dotfile="$HOME/.bashrc" ;;
fish) dotfile="$HOME/.config/fish/config.fish" ;;
ksh) dotfile="$HOME/.kshrc" ;;
*) dotfile="" ;;
esac
if [ -n "$dotfile" ] && [ -e /dev/tty ]; then
printf "Add %s to PATH in %s? [Y/n] " "$bin_dir" "$dotfile"
read -r answer </dev/tty
case "$answer" in
[Nn]*) ;;
*)
if [ "$shell_name" = "fish" ]; then
echo "fish_add_path $bin_dir" >>"$dotfile"
else
echo "export PATH=\"$bin_dir:\$PATH\"" >>"$dotfile"
fi
echo "Added. Restart your shell or run: source $dotfile"
;;
esac
else
echo "Could not detect your shell config file. Add $bin_dir to your PATH manually."
fi
;;
esac
# Copy gitpack itself if not already installed
if [ ! -f "$bin_dir/gitpack" ]; then
curl -fsSL "$gitpack_url/gitpack" -o "$bin_dir/gitpack"
chmod +x "$bin_dir/gitpack"
fi
}
parse_yml_field() {
field="$1" file="$2"
grep "^${field}:" "$file" |
sed "s/^${field}:[[:space:]]*//" |
sed 's/[[:space:]]*#.*//' |
tr -d "\"'"
}
cmd_install() {
ensure_gitpack_installed
git_url="$1"
if [ -z "$git_url" ]; then
echo "Usage: gitpack install <git-url>"
exit 1
fi
tmp_dir="$(mktemp -d)"
echo "Cloning $git_url..."
if ! git clone "$git_url" "$tmp_dir"; then
rm -rf "$tmp_dir"
echo "Error: Failed to clone repository."
exit 1
fi
config_file="$tmp_dir/.gitpack.yml"
if [ ! -f "$config_file" ]; then
rm -rf "$tmp_dir"
echo "Error: Not a valid gitpack package (missing .gitpack.yml)."
exit 1
fi
app_name="$(parse_yml_field name "$config_file")"
entrypoint="$(parse_yml_field entrypoint "$config_file")"
setup="$(parse_yml_field setup "$config_file")"
if [ -z "$app_name" ]; then
rm -rf "$tmp_dir"
echo "Error: .gitpack.yml is missing required field: name"
exit 1
fi
if [ -z "$entrypoint" ]; then
rm -rf "$tmp_dir"
echo "Error: .gitpack.yml is missing required field: entrypoint"
exit 1
fi
install_dir="$HOME/.local/share/gitpack/$app_name"
if [ -d "$install_dir" ]; then
rm -rf "$tmp_dir"
echo "Error: '$app_name' is already installed. Use 'gitpack remove $app_name' first."
exit 1
fi
mkdir -p "$(dirname "$install_dir")"
mv "$tmp_dir" "$install_dir"
# Write a launcher script that delegates to `gitpack run`
launcher="$bin_dir/$app_name"
printf '#!/bin/sh\nexec gitpack run %s "$@"\n' "$app_name" >"$launcher"
chmod +x "$launcher"
if [ -n "$setup" ]; then
setup_path="$install_dir/$setup"
if [ -f "$setup_path" ]; then
echo "Running setup..."
chmod +x "$setup_path"
"$setup_path"
else
echo "Warning: setup script '$setup' not found in repository."
fi
fi
echo ""
echo "📦 $app_name installed successfully!"
}
cmd_remove() {
app_name="$1"
if [ -z "$app_name" ]; then
echo "Usage: gitpack remove <app>"
exit 1
fi
install_dir="$HOME/.local/share/gitpack/$app_name"
if [ ! -d "$install_dir" ]; then
echo "Error: '$app_name' is not installed."
exit 1
fi
rm -rf "$install_dir"
rm -f "$bin_dir/$app_name"
echo "$app_name removed."
}
cmd_run() {
app_name="$1"
shift
if [ -z "$app_name" ]; then
echo "Usage: gitpack run <app>"
exit 1
fi
install_dir="$HOME/.local/share/gitpack/$app_name"
if [ ! -d "$install_dir" ]; then
echo "Error: '$app_name' is not installed."
exit 1
fi
config_file="$install_dir/.gitpack.yml"
# Fetch remote and check if we're behind before attempting a merge
git -C "$install_dir" fetch origin --quiet
local_rev="$(git -C "$install_dir" rev-parse HEAD)"
remote_rev="$(git -C "$install_dir" rev-parse @{u} 2>/dev/null)"
if [ -n "$remote_rev" ] && [ "$local_rev" != "$remote_rev" ]; then
echo "[📦 gitpack] Update for $app_name is available. Fetching..."
echo ""
if ! git -C "$install_dir" merge --ff-only --quiet; then
echo "Warning: Could not fast-forward $app_name. Skipping update."
fi
setup="$(parse_yml_field setup "$config_file")"
if [ -n "$setup" ]; then
setup_path="$install_dir/$setup"
if [ -f "$setup_path" ]; then
chmod +x "$setup_path"
"$setup_path"
fi
fi
fi
entrypoint="$(parse_yml_field entrypoint "$config_file")"
if [ -z "$entrypoint" ]; then
echo "Error: .gitpack.yml is missing required field: entrypoint"
exit 1
fi
entrypoint_path="$install_dir/$entrypoint"
if [ ! -f "$entrypoint_path" ]; then
echo "Error: entrypoint '$entrypoint' not found in repository."
exit 1
fi
chmod +x "$entrypoint_path"
exec "$entrypoint_path" "$@"
}
cmd_update() {
target="$bin_dir/gitpack"
tmp="$(mktemp)"
echo "Updating gitpack..."
if curl -fsSL "$gitpack_url/gitpack" -o "$tmp"; then
chmod +x "$tmp"
mv "$tmp" "$target"
echo "gitpack updated."
else
rm -f "$tmp"
echo "Error: Failed to download update."
exit 1
fi
}
case "$1" in
help) cmd_help ;;
install) cmd_install "$2" ;;
remove) cmd_remove "$2" ;;
run) shift; cmd_run "$@" ;;
update) cmd_update ;;
*)
echo "Unknown command: $1"
echo "Run gitpack help for usage instructions"
exit 1
;;
esac