1+ #! /usr/bin/env bash
2+
3+ # Prerequisite validation utilities for dotfiles setup
4+ # Validates system requirements before installation begins
5+
6+ set -euo pipefail
7+
8+ # Minimum supported macOS version (major.minor)
9+ readonly MIN_MACOS_VERSION=" 12.0"
10+
11+ # Validate Command Line Tools are installed
12+ validate_command_line_tools () {
13+ local xcode_path
14+
15+ # Check if xcode-select can find the developer directory
16+ if xcode_path=$( xcode-select -p 2> /dev/null) ; then
17+ # Verify the path exists and contains expected tools
18+ if [[ -d " $xcode_path " && -f " $xcode_path /usr/bin/git" ]]; then
19+ return 0
20+ else
21+ echo " Command Line Tools directory exists but appears incomplete" >&2
22+ return 1
23+ fi
24+ else
25+ echo " Command Line Tools not found. Install with: xcode-select --install" >&2
26+ return 1
27+ fi
28+ }
29+
30+ # Validate network connectivity to essential services
31+ validate_network_connectivity () {
32+ local hosts=(" github.com" " raw.githubusercontent.com" )
33+ local failed_hosts=()
34+
35+ for host in " ${hosts[@]} " ; do
36+ # Use ping with timeout to test connectivity
37+ if ! ping -c 1 -W 3000 " $host " > /dev/null 2>&1 ; then
38+ failed_hosts+=(" $host " )
39+ fi
40+ done
41+
42+ if [[ ${# failed_hosts[@]} -gt 0 ]]; then
43+ echo " Network connectivity failed for: ${failed_hosts[*]} " >&2
44+ echo " Please check your internet connection and try again" >&2
45+ return 1
46+ fi
47+
48+ return 0
49+ }
50+
51+ # Validate directory exists and has proper permissions
52+ validate_directory_permissions () {
53+ local directory=" $1 "
54+
55+ if [[ -z " $directory " ]]; then
56+ echo " Directory path is required" >&2
57+ return 1
58+ fi
59+
60+ # Check if directory exists
61+ if [[ ! -d " $directory " ]]; then
62+ echo " Directory does not exist: $directory " >&2
63+ return 1
64+ fi
65+
66+ return 0
67+ }
68+
69+ # Validate write permissions to a directory
70+ validate_write_permissions () {
71+ local directory=" $1 "
72+
73+ if [[ -z " $directory " ]]; then
74+ echo " Directory path is required" >&2
75+ return 1
76+ fi
77+
78+ # Check if directory exists first
79+ if [[ ! -d " $directory " ]]; then
80+ echo " Directory does not exist: $directory " >&2
81+ return 1
82+ fi
83+
84+ # Test write permissions by creating a temporary file
85+ local test_file=" $directory /.dotfiles_write_test_$$ "
86+ if touch " $test_file " 2> /dev/null; then
87+ rm -f " $test_file "
88+ return 0
89+ else
90+ echo " No write permission for directory: $directory " >&2
91+ return 1
92+ fi
93+ }
94+
95+ # Validate macOS version compatibility
96+ validate_macos_version () {
97+ local current_version
98+ local major minor
99+ local min_major min_minor
100+
101+ # Get current macOS version
102+ current_version=$( sw_vers -productVersion 2> /dev/null)
103+ if [[ -z " $current_version " ]]; then
104+ echo " Unable to determine macOS version" >&2
105+ return 1
106+ fi
107+
108+ # Parse current version
109+ major=$( echo " $current_version " | cut -d. -f1)
110+ minor=$( echo " $current_version " | cut -d. -f2)
111+
112+ # Parse minimum version
113+ min_major=$( echo " $MIN_MACOS_VERSION " | cut -d. -f1)
114+ min_minor=$( echo " $MIN_MACOS_VERSION " | cut -d. -f2)
115+
116+ # Compare versions
117+ if [[ " $major " -gt " $min_major " ]] ||
118+ [[ " $major " -eq " $min_major " && " $minor " -ge " $min_minor " ]]; then
119+ return 0
120+ else
121+ echo " Unsupported macOS version: $current_version (minimum: $MIN_MACOS_VERSION )" >&2
122+ return 1
123+ fi
124+ }
125+
126+ # Validate essential directories for dotfiles installation
127+ validate_essential_directories () {
128+ local directories=(
129+ " $HOME "
130+ " /usr/local"
131+ " /opt"
132+ )
133+
134+ local failed_dirs=()
135+
136+ for dir in " ${directories[@]} " ; do
137+ if ! validate_directory_permissions " $dir " ; then
138+ failed_dirs+=(" $dir " )
139+ fi
140+ done
141+
142+ if [[ ${# failed_dirs[@]} -gt 0 ]]; then
143+ echo " Essential directory validation failed for: ${failed_dirs[*]} " >&2
144+ return 1
145+ fi
146+
147+ return 0
148+ }
149+
150+ # Run comprehensive prerequisite validation
151+ run_prerequisite_validation () {
152+ local validation_functions=(
153+ " validate_command_line_tools"
154+ " validate_network_connectivity"
155+ " validate_macos_version"
156+ " validate_essential_directories"
157+ )
158+
159+ local failed_validations=()
160+ local exit_code=0
161+
162+ echo " Running prerequisite validation checks..."
163+
164+ for validation_func in " ${validation_functions[@]} " ; do
165+ echo " Checking: ${validation_func# validate_} "
166+
167+ if ! " $validation_func " ; then
168+ failed_validations+=(" $validation_func " )
169+ exit_code=1
170+ else
171+ echo " ✓ Passed"
172+ fi
173+ done
174+
175+ if [[ $exit_code -eq 0 ]]; then
176+ echo " ✅ All prerequisite validation checks passed"
177+ else
178+ echo " ❌ Prerequisite validation failed:"
179+ for failed_func in " ${failed_validations[@]} " ; do
180+ echo " - ${failed_func# validate_} "
181+ done
182+ fi
183+
184+ return $exit_code
185+ }
0 commit comments