-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexport-dependencies.sh
More file actions
286 lines (231 loc) · 6.98 KB
/
export-dependencies.sh
File metadata and controls
286 lines (231 loc) · 6.98 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
#!/usr/bin/env bash
#
# export - Interactive TUI to generate an install script from currently installed software
#
# This script uses gum (https://github.com/charmbracelet/gum) to provide a nice
# terminal UI for selecting which Homebrew formulae, casks, and Mac App Store
# apps should be included in the install script.
#
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
INSTALL_FILE="$SCRIPT_DIR/install"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
info() { echo -e "${BLUE}▶${NC} $1"; }
success() { echo -e "${GREEN}✓${NC} $1"; }
warn() { echo -e "${YELLOW}⚠${NC} $1"; }
error() { echo -e "${RED}✗${NC} $1"; }
# Check for required dependencies
check_dependencies() {
if ! command -v brew &> /dev/null; then
error "Homebrew is not installed. Please install it first."
exit 1
fi
if ! command -v gum &> /dev/null; then
warn "gum is not installed. Installing via Homebrew..."
brew install gum
fi
if ! command -v mas &> /dev/null; then
warn "mas is not installed. Installing via Homebrew..."
brew install mas
fi
}
# Extract currently selected items from install script (for re-runs)
get_current_selections() {
local start_marker="$1"
local end_marker="$2"
local pattern="$3"
if [ ! -f "$INSTALL_FILE" ] || [ ! -s "$INSTALL_FILE" ]; then
return
fi
sed -n "/$start_marker/,/$end_marker/p" "$INSTALL_FILE" | \
grep -E "$pattern" | \
sed -E 's/.*install (--cask )?([^ ]+).*/\2/' | \
sed 's/ 2>.*//'
}
# Get top-level formulae (not dependencies)
get_installed_formulae() {
brew leaves 2>/dev/null | sort
}
# Get installed casks
get_installed_casks() {
brew list --cask 2>/dev/null | sort
}
# Get installed Mac App Store apps
get_installed_mas() {
mas list 2>/dev/null | while read -r line; do
id=$(echo "$line" | awk '{print $1}')
name=$(echo "$line" | sed 's/^[0-9]* *//' | sed 's/ *([^)]*) *$//')
echo "$id|$name"
done
}
# Interactive selection using gum
select_items() {
local title="$1"
local items="$2"
local preselected="$3"
if [ -z "$items" ]; then
return
fi
# Build the gum choose arguments
local args=()
while IFS= read -r item; do
[ -z "$item" ] && continue
args+=("$item")
done <<< "$items"
if [ ${#args[@]} -eq 0 ]; then
return
fi
# Build preselected arguments
local selected_args=()
while IFS= read -r sel; do
[ -z "$sel" ] && continue
for arg in "${args[@]}"; do
if [[ "$arg" == "$sel" ]] || [[ "$arg" == *"|$sel"* ]] || [[ "$arg" == "$sel|"* ]]; then
selected_args+=("--selected=$arg")
break
fi
done
done <<< "$preselected"
echo "" >&2
gum style --foreground 212 --bold "$title" >&2
echo "" >&2
# Run gum choose with multi-select
gum choose --no-limit --height=20 "${selected_args[@]}" "${args[@]}"
}
# Generate the complete install script
generate_install_script() {
local formulae_content="$1"
local casks_content="$2"
local mas_content="$3"
cat <<'HEADER'
#!/usr/bin/env bash
#
# install - Set up a new Mac with Homebrew packages, casks, and Mac App Store apps
#
# Usage: ./install/install
#
set -e
echo "Installing software..."
# Install Homebrew if not present
if ! command -v brew &> /dev/null; then
echo "Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
brew update
HEADER
echo "### BEGIN FORMULAE ###"
printf "%s" "$formulae_content"
echo "### END FORMULAE ###"
echo ""
echo "### BEGIN CASKS ###"
printf "%s" "$casks_content"
echo "### END CASKS ###"
echo ""
echo "### BEGIN MAS ###"
printf "%s" "$mas_content"
echo "### END MAS ###"
echo ""
cat <<'FOOTER'
brew cleanup
echo "Done!"
FOOTER
}
# Main function
main() {
info "Dotfiles Export Tool"
echo "This tool generates install/install from your currently installed software."
echo ""
check_dependencies
# Get current selections from install file (supports re-running)
info "Reading current install configuration..."
current_formulae=$(get_current_selections "BEGIN FORMULAE" "END FORMULAE" "^brew install [^-]")
current_casks=$(get_current_selections "BEGIN CASKS" "END CASKS" "brew install --cask")
current_mas=$(get_current_selections "BEGIN MAS" "END MAS" "^mas install")
# Get installed software
info "Discovering installed software..."
installed_formulae=$(get_installed_formulae)
installed_casks=$(get_installed_casks)
installed_mas=$(get_installed_mas)
formulae_count=$(echo "$installed_formulae" | grep -c . || echo 0)
casks_count=$(echo "$installed_casks" | grep -c . || echo 0)
mas_count=$(echo "$installed_mas" | grep -c . || echo 0)
success "Found $formulae_count top-level formulae, $casks_count casks, $mas_count MAS apps"
echo ""
# Confirm to proceed
if ! gum confirm "Continue with selection?"; then
info "Cancelled."
exit 0
fi
# Select formulae
selected_formulae=$(select_items "Select Homebrew Formulae (top-level only, no dependencies)" "$installed_formulae" "$current_formulae")
# Select casks
selected_casks=$(select_items "Select Homebrew Casks" "$installed_casks" "$current_casks")
# Select MAS apps
selected_mas=$(select_items "Select Mac App Store Apps" "$installed_mas" "$current_mas")
echo ""
info "Generating install script content..."
# Generate formulae content
formulae_content=""
while IFS= read -r formula; do
[ -z "$formula" ] && continue
formulae_content+="brew install $formula"$'\n'
done <<< "$selected_formulae"
# Generate casks content
casks_content=""
while IFS= read -r cask; do
[ -z "$cask" ] && continue
casks_content+="brew install --cask $cask 2> /dev/null"$'\n'
done <<< "$selected_casks"
# Generate MAS content
mas_content=""
while IFS= read -r mas_item; do
[ -z "$mas_item" ] && continue
id=$(echo "$mas_item" | cut -d'|' -f1)
name=$(echo "$mas_item" | cut -d'|' -f2)
mas_content+="mas install $id # $name"$'\n'
done <<< "$selected_mas"
# Show summary
echo ""
gum style --foreground 212 --bold "Summary:"
echo ""
echo "Formulae to install:"
if [ -n "$selected_formulae" ]; then
echo "$selected_formulae" | sed 's/^/ /'
else
echo " (none)"
fi
echo ""
echo "Casks to install:"
if [ -n "$selected_casks" ]; then
echo "$selected_casks" | sed 's/^/ /'
else
echo " (none)"
fi
echo ""
echo "Mac App Store apps to install:"
if [ -n "$selected_mas" ]; then
echo "$selected_mas" | sed 's/|/ - /' | sed 's/^/ /'
else
echo " (none)"
fi
echo ""
# Confirm to write
if ! gum confirm "Write install script to $INSTALL_FILE?"; then
info "Cancelled. No changes made."
exit 0
fi
# Generate and write the install script
info "Writing $INSTALL_FILE..."
generate_install_script "$formulae_content" "$casks_content" "$mas_content" > "$INSTALL_FILE"
chmod +x "$INSTALL_FILE"
success "Install script created successfully!"
echo ""
info "Review with: cat install"
info "Run with: ./install"
}
main "$@"