-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall
More file actions
executable file
·139 lines (122 loc) · 3.6 KB
/
install
File metadata and controls
executable file
·139 lines (122 loc) · 3.6 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
#!/usr/bin/env bash
# Install dirtree to a directory on your PATH
set -euo pipefail
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
ZIG_BIN="$SCRIPT_DIR/zig-out/bin/dirtree"
# Default install directory
DEFAULT_INSTALL_DIR="$HOME/.local/bin"
# Parse arguments
YES=false
SYMLINK=false
UNINSTALL=false
INSTALL_DIR=""
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
echo "Usage: ./install [OPTIONS] [INSTALL_DIR]"
echo ""
echo "Install dirtree binary to a directory on your PATH."
echo ""
echo "Options:"
echo " -y, --yes Skip confirmation prompt"
echo " -s, --symlink Symlink instead of copy (auto-tracks rebuilds)"
echo " -u, --uninstall Remove installed dirtree binary"
echo " -h, --help Show this help"
echo ""
echo "Arguments:"
echo " INSTALL_DIR Directory to install to (default: ~/.local/bin)"
echo ""
echo "Examples:"
echo " ./install # Install to ~/.local/bin"
echo " ./install -y # Install without prompting"
echo " ./install -s # Symlink instead of copy"
echo " ./install --uninstall # Remove installed binary"
echo " ./install /usr/local/bin # Install to /usr/local/bin"
echo " ./install ~/bin # Install to ~/bin"
exit 0
;;
-y|--yes) YES=true; shift ;;
-s|--symlink) SYMLINK=true; shift ;;
-u|--uninstall) UNINSTALL=true; shift ;;
-*) echo "Error: Unknown option $1" >&2; exit 1 ;;
*) INSTALL_DIR="$1"; shift ;;
esac
done
INSTALL_DIR="${INSTALL_DIR:-$DEFAULT_INSTALL_DIR}"
# Expand tilde in path
INSTALL_DIR="${INSTALL_DIR/#\~/$HOME}"
# Handle uninstall
if $UNINSTALL; then
TARGET="$INSTALL_DIR/dirtree"
if [[ -f "$TARGET" || -L "$TARGET" ]]; then
echo "Removing $TARGET"
rm "$TARGET"
echo "Done."
else
echo "Nothing to remove: $TARGET does not exist." >&2
fi
exit 0
fi
# Check if binary exists
if [[ ! -f "$ZIG_BIN" ]]; then
echo "Error: dirtree binary not found at $ZIG_BIN" >&2
echo "Run './build' first to compile the Zig binary." >&2
exit 1
fi
# Show directories on PATH (skip in non-interactive mode)
if ! $YES; then
echo "Directories on your PATH:"
echo "$PATH" | tr ':' '\n' | nl
echo ""
# Prompt for confirmation
METHOD="copy"
$SYMLINK && METHOD="symlink"
echo "Install location: $INSTALL_DIR/dirtree ($METHOD)"
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
echo "Warning: $INSTALL_DIR is not on your PATH"
fi
echo ""
read -p "Install to this location? [y/N] " -r REPLY
echo ""
if [[ ! "$REPLY" =~ ^[Yy]$ ]]; then
echo "Installation cancelled."
exit 0
fi
fi
# Create install directory if it doesn't exist
if [[ ! -d "$INSTALL_DIR" ]]; then
echo "Creating directory: $INSTALL_DIR"
mkdir -p "$INSTALL_DIR"
fi
# Check if directory is writable
if [[ ! -w "$INSTALL_DIR" ]]; then
echo "Error: Cannot write to $INSTALL_DIR" >&2
echo "Try running with sudo or choose a different directory." >&2
exit 1
fi
# Install binary
TARGET="$INSTALL_DIR/dirtree"
if $SYMLINK; then
echo "Symlinking dirtree to $TARGET"
ln -sf "$ZIG_BIN" "$TARGET"
else
echo "Copying dirtree to $TARGET"
cp "$ZIG_BIN" "$TARGET"
chmod +x "$TARGET"
fi
echo "Successfully installed dirtree to $TARGET"
echo ""
# Check if directory is on PATH
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
echo "Warning: $INSTALL_DIR is not on your PATH"
echo "Add this line to your ~/.zshrc or ~/.bashrc:"
echo ""
echo " export PATH=\"$INSTALL_DIR:\$PATH\""
echo ""
fi
# Test installation
if command -v dirtree &> /dev/null; then
echo "You can now run 'dirtree' from anywhere!"
else
echo "After adding $INSTALL_DIR to your PATH, run 'dirtree' from anywhere."
fi