Last active
April 12, 2025 00:05
-
-
Save sethladd/9f5cf1a3668cd89ac9bcda242a059c81 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script to automate Flutter SDK installation and setup for iOS development on macOS. | |
# Corrected URL finding logic for Intel vs Arm. V1.5 | |
# --- Configuration --- | |
FLUTTER_INSTALL_PARENT_DIR="${HOME}/development" # Default parent directory for Flutter SDK FOLDER | |
FLUTTER_DOCS_URL="https://docs.flutter.dev/get-started/install/macos/mobile-ios" | |
SCRIPT_VERSION="1.5" # Updated Version | |
REPORT_FILE="/tmp/flutter_setup_report_$(date +%s).txt" | |
# --- Colors for Output --- | |
COLOR_RESET='\033[0m' | |
COLOR_INFO='\033[0;34m' # Blue | |
COLOR_SUCCESS='\033[0;32m' # Green | |
COLOR_WARNING='\033[0;33m' # Yellow | |
COLOR_ERROR='\033[0;31m' # Red | |
COLOR_BOLD='\033[1m' | |
COLOR_CMD='\033[0;36m' # Cyan for commands | |
COLOR_UNDERLINE='\033[4m' | |
# --- Helper Functions --- | |
# (Helper functions log_info, log_success, log_warning, log_error, log_step, log_exec, ask_confirm, ask_confirm_command, add_line_to_file, add_to_report remain the same as v1.4) | |
# Function to print informational messages π¬ | |
log_info() { | |
echo -e "${COLOR_INFO}π¬ $1${COLOR_RESET}" | |
} | |
# Function to print success messages β π | |
log_success() { | |
echo -e "${COLOR_SUCCESS}β $1${COLOR_RESET}" | |
} | |
# Function to print warning messages β οΈ | |
log_warning() { | |
echo -e "${COLOR_WARNING}β οΈ $1${COLOR_RESET}" | |
} | |
# Function to print error messages βπ | |
log_error() { | |
echo -e "${COLOR_ERROR}β $1${COLOR_RESET}" | |
} | |
# Function to print step indicators π | |
log_step() { | |
echo -e "\n${COLOR_BOLD}π Step: $1${COLOR_RESET}" | |
} | |
# Function to log execution of simple/check commands π | |
log_exec() { | |
local command_str="$1" | |
echo -e "${COLOR_INFO}π Running: ${COLOR_CMD}${command_str}${COLOR_RESET}" | |
} | |
# Function to ask for simple yes/no confirmation π€ | |
ask_confirm() { | |
local prompt_message="$1" | |
while true; do | |
echo -en "${COLOR_WARNING}π€ ${prompt_message} (y/n): ${COLOR_RESET}" | |
read -r yn | |
echo | |
case $yn in | |
[Yy]* ) return 0;; # Yes | |
[Nn]* ) return 1;; # No | |
* ) echo "Oops! Please type 'y' for yes or 'n' for no.";; | |
esac | |
done | |
} | |
# Function to display a command and ask for confirmation to run it π | |
ask_confirm_command() { | |
local description="$1" | |
local command_str="$2" | |
echo -e "${COLOR_INFO}π About to: ${description}${COLOR_RESET}" | |
echo -e " Command: ${COLOR_CMD}${command_str}${COLOR_RESET}" | |
while true; do | |
echo -en "${COLOR_WARNING}π§ Run this command? (y/n): ${COLOR_RESET}" | |
read -r yn | |
echo | |
case $yn in | |
[Yy]* ) return 0;; # Yes | |
[Nn]* ) return 1;; # No | |
* ) echo "Oops! Please type 'y' for yes or 'n' for no.";; | |
esac | |
done | |
} | |
# Function to add line to file if it doesn't exist π | |
add_line_to_file() { | |
local line="$1" | |
local file="$2" | |
local check_cmd="grep -qF -- \"$line\" \"$file\"" | |
local add_cmd="echo \"$line\" >> \"$file\"" | |
# Ensure file exists first (low impact, no confirmation needed) | |
touch "$file" | |
log_exec "$check_cmd" | |
if ! eval $check_cmd; then | |
echo -e "${COLOR_INFO}π The line '${COLOR_CMD}${line}${COLOR_RESET}' is not in ${file}." | |
if ask_confirm_command "Add the missing line" "$add_cmd"; then | |
log_exec "$add_cmd" | |
if eval $add_cmd; then | |
log_success "Added line to $file." | |
echo "Added '$line' to $file" >> "$REPORT_FILE" | |
return 0 # Line added | |
else | |
log_error "Failed to add line to $file. Check permissions?" | |
echo "Failed adding '$line' to $file" >> "$REPORT_FILE" | |
return 1 # Failed to add | |
fi | |
else | |
log_info "Okay, skipping adding the line to $file." | |
echo "User skipped adding '$line' to $file" >> "$REPORT_FILE" | |
return 1 # Skipped | |
fi | |
else | |
log_info "Looks like that line already exists in $file. No changes needed there." | |
echo "Line '$line' already exists in $file" >> "$REPORT_FILE" | |
return 1 # Line already existed or skipped adding | |
fi | |
} | |
# Function to add text to the final report π | |
add_to_report() { | |
echo "$1" >> "$REPORT_FILE" | |
} | |
# --- Main Script --- | |
main() { | |
# --- Introduction --- | |
echo -e "${COLOR_BOLD}-----------------------------------------------------" | |
echo -e " Hello there! π Let's Get Flutter Set Up for iOS! " | |
echo -e " (Version ${SCRIPT_VERSION}) " | |
echo -e "-----------------------------------------------------${COLOR_RESET}" | |
echo -e "I'm a friendly script here to help install and configure the Flutter SDK" | |
echo -e "so you can start building beautiful iOS apps on your Mac. π±" | |
echo -e "I'll follow the steps from the official Flutter guide:" | |
echo -e "π ${FLUTTER_DOCS_URL}" | |
echo -e "\n${COLOR_BOLD}My Promise:${COLOR_RESET} I will show you ${COLOR_UNDERLINE}every command${COLOR_RESET} before I run it!" | |
echo -e "For simple checks, I'll just show it (π). For actions that change things," | |
echo -e "install software, use 'sudo', or might take time, I'll ask for your OK (π§)." | |
echo -e "\n${COLOR_BOLD}Here's the plan:${COLOR_RESET}" | |
# (Plan remains the same) | |
echo -e " 1. Check if Xcode is installed and ready." | |
echo -e " 2. Help you install Xcode manually if it's missing." | |
echo -e " 3. Make sure the Xcode license is accepted (might need your password!)." | |
echo -e " 4. Figure out if your Mac is Intel or Apple Silicon." | |
echo -e " 5. Find the newest Flutter SDK download link for you (Using updated logic!)." # Updated Step 5 description | |
echo -e " 6. Ask you where you'd like to put the Flutter folder (default: ${FLUTTER_INSTALL_PARENT_DIR})." | |
echo -e " 7. Download and unpack the Flutter SDK (like unzipping a file)." | |
echo -e " 8. Add Flutter to your system's PATH so you can run 'flutter' commands easily." | |
echo -e " 9. Download some extra bits Flutter needs for iOS ('flutter precache')." | |
echo -e "10. Check for a tool called CocoaPods (Flutter needs it!) and install it if needed." | |
echo -e "11. Maybe run 'pod setup' if we install CocoaPods with RubyGems." | |
echo -e "12. Offer to open the iOS Simulator for you." | |
echo -e "13. Run Flutter's built-in health check ('flutter doctor'). π©Ί" | |
echo -e "14. Give you a nice summary of everything done. π" | |
echo -e "\n${COLOR_WARNING}Heads up!${COLOR_RESET} Commands starting with 'sudo' need your admin password." | |
# Initialize Report File | |
echo "" > "$REPORT_FILE" | |
add_to_report "Flutter iOS Setup Report - $(date)" | |
add_to_report "======================================" | |
add_to_report "Script Version: $SCRIPT_VERSION" | |
add_to_report "Flutter Docs Reference: $FLUTTER_DOCS_URL" | |
if ! ask_confirm "Ready to start the Flutter setup adventure?"; then | |
log_info "Okay, maybe another time! Setup aborted. π" | |
exit 0 | |
fi | |
# --- 1. & 2. & 3. Check/Install Xcode & License --- | |
# (Xcode Check Section - Remains the same as v1.4) | |
log_step "Checking Xcode, Command Line Tools, and License..." | |
add_to_report "\n--- Xcode & License Check ---" | |
XCODE_CHECK_CMD="xcode-select -p" | |
log_exec "$XCODE_CHECK_CMD" | |
XCODE_PATH=$(eval $XCODE_CHECK_CMD 2>/dev/null) # Run the check | |
if [[ -z "$XCODE_PATH" || ! -d "$XCODE_PATH" ]]; then | |
log_warning "Hmm, 'xcode-select -p' didn't return a valid path." | |
# Instructions for manual install (no commands run here) | |
echo -e "\n${COLOR_BOLD}π¨ Action Needed: Install Xcode & Tools π¨${COLOR_RESET}" | |
# ... (Instructions remain the same) ... | |
echo -e " 1. Open the ${COLOR_BOLD}App Store${COLOR_RESET} app on your Mac." | |
echo -e " 2. Search for ${COLOR_BOLD}Xcode${COLOR_RESET} and click 'Install' or 'Update'." | |
echo -e " (This can take a while, grab a coffee! β)" | |
echo -e " 3. Once installed, open ${COLOR_BOLD}Terminal${COLOR_RESET} (like this window) and run this command (I'll ask before running it if needed later):" | |
echo -e " ${COLOR_CMD}sudo xcode-select --install${COLOR_RESET}" | |
echo -e " 4. Accept the Xcode license (I'll ask to run this command later too):" | |
echo -e " ${COLOR_CMD}sudo xcodebuild -license accept${COLOR_RESET}" | |
echo -e " 5. You might also need to ensure Xcode path is set correctly (usually automatic):" | |
echo -e " ${COLOR_CMD}sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer${COLOR_RESET}" | |
echo -e "\n (Sometimes opening the Xcode app once manually helps too!)" | |
if ! ask_confirm "Have you completed these Xcode steps (installing Xcode, running the install/accept commands)?"; then | |
log_error "Xcode setup is essential. Please complete the steps and run me again! π" | |
add_to_report "User indicated Xcode setup was not complete." | |
exit 1 | |
fi | |
# Re-check after user confirmation | |
log_exec "$XCODE_CHECK_CMD" | |
XCODE_PATH=$(eval $XCODE_CHECK_CMD 2>/dev/null) | |
if [[ -z "$XCODE_PATH" || ! -d "$XCODE_PATH" ]]; then | |
log_error "Still can't find the Xcode tools path after confirmation. Please double-check the installation and the 'xcode-select --switch' command if needed. Aborting for now. π₯" | |
add_to_report "Xcode check failed after user confirmation." | |
exit 1 | |
fi | |
fi | |
log_success "Xcode Command Line Tools seem to be at: $XCODE_PATH" | |
add_to_report "Xcode Command Line Tools Path: $XCODE_PATH" | |
# Check license specifically | |
LICENSE_CHECK_CMD="sudo xcodebuild -license check" | |
log_exec "$LICENSE_CHECK_CMD" | |
if ! sudo xcodebuild -license check > /dev/null 2>&1; then | |
log_warning "The license check indicates it hasn't been accepted." | |
LICENSE_ACCEPT_CMD="sudo xcodebuild -license accept" | |
if ask_confirm_command "Accept Xcode License (needs admin privileges)" "$LICENSE_ACCEPT_CMD"; then | |
log_exec "$LICENSE_ACCEPT_CMD" # Log execution just before running | |
if sudo xcodebuild -license accept; then | |
log_success "Xcode license accepted! β " | |
add_to_report "Accepted Xcode license via script." | |
else | |
log_error "Hmm, accepting the license failed. Please run 'sudo xcodebuild -license' manually in Terminal and follow its prompts. Aborting script. π" | |
add_to_report "Failed to accept Xcode license automatically." | |
exit 1 | |
fi | |
else | |
log_error "The Xcode license must be accepted. Please run 'sudo xcodebuild -license' manually. Aborting script. π" | |
add_to_report "User declined automatic license acceptance. Aborted." | |
exit 1 | |
fi | |
else | |
log_success "Xcode license is already accepted. Good to go! π" | |
add_to_report "Xcode license already accepted." | |
fi | |
# --- 4. Detect Architecture --- | |
# (Architecture Detection Section - Remains the same as v1.4) | |
log_step "Figuring out your Mac's processor type..." | |
add_to_report "\n--- System Architecture ---" | |
ARCH_CHECK_CMD="uname -m" | |
log_exec "$ARCH_CHECK_CMD" | |
ARCH=$(eval $ARCH_CHECK_CMD) | |
# FLUTTER_ARCH_ID is not needed anymore for URL construction logic change | |
if [[ "$ARCH" == "arm64" ]]; then | |
# FLUTTER_ARCH_ID="arm64" (kept for potential future use, but not in URL pattern directly) | |
log_info "Result: Apple Silicon Mac (arm64)! ο£Ώ" | |
add_to_report "Detected Architecture: Apple Silicon (arm64)" | |
elif [[ "$ARCH" == "x86_64" ]]; then | |
# FLUTTER_ARCH_ID="x64" (kept for potential future use) | |
log_info "Result: Intel Mac (x86_64)! π»" | |
add_to_report "Detected Architecture: Intel (x86_64)" | |
else | |
log_error "Oh, 'uname -m' returned an unexpected architecture: $ARCH. Can't proceed. π₯" | |
add_to_report "Unsupported architecture detected: $ARCH" | |
exit 1 | |
fi | |
# --- 5. Find Flutter SDK URL --- # MODIFIED SECTION | |
log_step "Searching the web for the latest Flutter SDK... π΅οΈ" | |
add_to_report "\n--- Flutter SDK Download URL ---" | |
log_info "Checking ${FLUTTER_DOCS_URL} for the download link..." | |
FLUTTER_ZIP_PATTERN_VERSIONED="" | |
FLUTTER_ZIP_PATTERN_STABLE="" | |
URL_EXCLUDE_FILTER="" # Used only for Intel | |
if [[ "$ARCH" == "arm64" ]]; then | |
log_info "Using patterns for Apple Silicon (arm64)..." | |
FLUTTER_ZIP_PATTERN_VERSIONED='https://storage.googleapis.com/flutter_infra_release/releases/stable/macos/flutter_macos_arm64_[0-9.]*-stable\.zip' | |
FLUTTER_ZIP_PATTERN_STABLE='https://storage.googleapis.com/flutter_infra_release/releases/stable/macos/flutter_macos_arm64_stable\.zip' | |
elif [[ "$ARCH" == "x86_64" ]]; then | |
log_info "Using patterns for Intel (x86_64), excluding 'arm64' results..." | |
# Pattern does NOT include _x64 or _intel | |
FLUTTER_ZIP_PATTERN_VERSIONED='https://storage.googleapis.com/flutter_infra_release/releases/stable/macos/flutter_macos_[0-9.]*-stable\.zip' | |
FLUTTER_ZIP_PATTERN_STABLE='https://storage.googleapis.com/flutter_infra_release/releases/stable/macos/flutter_macos_stable\.zip' | |
# We need to filter out the arm64 link if it matches the simpler pattern | |
URL_EXCLUDE_FILTER="arm64" | |
fi | |
# Fetch the page content once | |
HTML_CONTENT=$(curl -s "$FLUTTER_DOCS_URL") | |
FLUTTER_ZIP_URL="" | |
# Try finding the versioned link first | |
log_info "Attempting to find version-specific link..." | |
if [[ -n "$URL_EXCLUDE_FILTER" ]]; then | |
# For Intel, try pattern but exclude arm64 results | |
FLUTTER_ZIP_URL=$(echo "$HTML_CONTENT" | grep -Eo "$FLUTTER_ZIP_PATTERN_VERSIONED" | grep -v "$URL_EXCLUDE_FILTER" | head -n 1) | |
else | |
# For arm64, just use the pattern | |
FLUTTER_ZIP_URL=$(echo "$HTML_CONTENT" | grep -Eo "$FLUTTER_ZIP_PATTERN_VERSIONED" | head -n 1) | |
fi | |
# Fallback to generic stable link if versioned not found | |
if [[ -z "$FLUTTER_ZIP_URL" ]]; then | |
log_warning "Could not find version-specific link. Trying generic stable link..." | |
add_to_report "Fallback: Trying generic stable link pattern." | |
if [[ -n "$URL_EXCLUDE_FILTER" ]]; then | |
# For Intel fallback, exclude arm64 | |
FLUTTER_ZIP_URL=$(echo "$HTML_CONTENT" | grep -Eo "$FLUTTER_ZIP_PATTERN_STABLE" | grep -v "$URL_EXCLUDE_FILTER" | head -n 1) | |
else | |
# For arm64 fallback, just use the pattern | |
FLUTTER_ZIP_URL=$(echo "$HTML_CONTENT" | grep -Eo "$FLUTTER_ZIP_PATTERN_STABLE" | head -n 1) | |
fi | |
fi | |
# Final check and reporting | |
if [[ -z "$FLUTTER_ZIP_URL" ]]; then | |
log_error "Bummer! I couldn't automatically find the correct Flutter download link for your Mac architecture on ${FLUTTER_DOCS_URL}." | |
log_error "The website structure or link format might have changed. Please visit the URL manually, find the correct '*.zip' link for your Mac (${ARCH}), and install following their instructions." | |
add_to_report "Failed to find Flutter SDK download URL automatically using patterns:\nVersioned: ${FLUTTER_ZIP_PATTERN_VERSIONED}\nStable: ${FLUTTER_ZIP_PATTERN_STABLE}" | |
exit 1 | |
fi | |
log_success "Found the Flutter SDK link! π" | |
log_info "URL: ${FLUTTER_ZIP_URL}" | |
add_to_report "Found Flutter SDK URL: $FLUTTER_ZIP_URL" | |
# END OF MODIFIED SECTION | |
# --- 6. Ask Install Location --- | |
# (Remains the same as v1.4) | |
log_step "Where should we install Flutter?" | |
add_to_report "\n--- Installation Location ---" | |
echo -e "${COLOR_INFO}I need a place to put the Flutter SDK folder. Default is '~/development'.${COLOR_RESET}" | |
echo -en "${COLOR_INFO}π Enter the parent directory path [${FLUTTER_INSTALL_PARENT_DIR}]: ${COLOR_RESET}" | |
read user_install_dir | |
echo # Newline after read | |
FLUTTER_INSTALL_PARENT_DIR=${user_install_dir:-$FLUTTER_INSTALL_PARENT_DIR} | |
FLUTTER_SDK_DIR="${FLUTTER_INSTALL_PARENT_DIR}/flutter" | |
log_info "Okay, Flutter will be installed inside: ${FLUTTER_SDK_DIR}" | |
add_to_report "Target installation directory: ${FLUTTER_SDK_DIR}" | |
if [[ -d "$FLUTTER_SDK_DIR" ]]; then | |
log_warning "A folder named 'flutter' already exists at ${FLUTTER_INSTALL_PARENT_DIR}." | |
if ! ask_confirm "Is it okay to proceed? (Existing files might be replaced during extraction)"; then | |
log_info "Got it. Please choose a different location or manage the existing folder, then run me again. π" | |
add_to_report "Installation aborted due to existing directory conflict." | |
exit 1 | |
fi | |
log_info "Alright, proceeding with the existing location." | |
add_to_report "User confirmed potential overwrite of existing directory." | |
fi | |
# --- 7. Download and Extract Flutter SDK --- | |
# (Remains the same as v1.4) | |
log_step "Downloading and Unpacking Flutter... π" | |
add_to_report "\n--- Download & Extract ---" | |
TEMP_ZIP_FILE="/tmp/flutter_sdk_$(date +%s).zip" # Unique temp file name | |
MKDIR_CMD="mkdir -p \"$FLUTTER_INSTALL_PARENT_DIR\"" | |
DOWNLOAD_CMD="curl --progress-bar --fail --location --output \"$TEMP_ZIP_FILE\" \"$FLUTTER_ZIP_URL\"" | |
EXTRACT_CMD="unzip -oq \"$TEMP_ZIP_FILE\" -d \"$FLUTTER_INSTALL_PARENT_DIR\"" | |
CLEANUP_CMD="rm -f \"$TEMP_ZIP_FILE\"" | |
if [[ ! -d "$FLUTTER_INSTALL_PARENT_DIR" ]]; then | |
log_info "The parent directory '${FLUTTER_INSTALL_PARENT_DIR}' doesn't exist yet." | |
if ask_confirm_command "Create the directory '${FLUTTER_INSTALL_PARENT_DIR}'?" "$MKDIR_CMD"; then | |
log_exec "$MKDIR_CMD" | |
if mkdir -p "$FLUTTER_INSTALL_PARENT_DIR"; then | |
log_success "Created directory: ${FLUTTER_INSTALL_PARENT_DIR}" | |
add_to_report "Created directory ${FLUTTER_INSTALL_PARENT_DIR}." | |
else | |
log_error "Couldn't create directory ${FLUTTER_INSTALL_PARENT_DIR}. Check permissions? Aborting. π₯" | |
add_to_report "Failed to create directory ${FLUTTER_INSTALL_PARENT_DIR}." | |
exit 1 | |
fi | |
else | |
log_error "Can't proceed without the installation directory. Aborting." | |
add_to_report "Installation directory creation cancelled by user." | |
exit 1 | |
fi | |
fi | |
log_info "Ready to download Flutter SDK (this might take a few moments)..." | |
if ask_confirm_command "Download Flutter SDK" "$DOWNLOAD_CMD"; then | |
log_exec "$DOWNLOAD_CMD" | |
if eval $DOWNLOAD_CMD; then | |
log_success "Download complete!" | |
add_to_report "Downloaded Flutter SDK to $TEMP_ZIP_FILE." | |
log_info "Now unpacking Flutter into ${FLUTTER_INSTALL_PARENT_DIR}..." | |
if ask_confirm_command "Extract the downloaded Flutter SDK" "$EXTRACT_CMD"; then | |
log_exec "$EXTRACT_CMD" | |
if eval $EXTRACT_CMD; then | |
log_success "Flutter SDK extracted to ${FLUTTER_SDK_DIR}! π" | |
add_to_report "Successfully extracted Flutter SDK to ${FLUTTER_SDK_DIR}." | |
log_info "Cleaning up the downloaded zip file..." | |
if ask_confirm_command "Remove temporary download file" "$CLEANUP_CMD"; then | |
log_exec "$CLEANUP_CMD" | |
if eval $CLEANUP_CMD; then | |
log_success "Temporary file removed." | |
add_to_report "Removed temporary zip file $TEMP_ZIP_FILE." | |
else | |
log_warning "Couldn't remove the temporary zip file: $TEMP_ZIP_FILE. You might want to delete it manually later." | |
add_to_report "Failed to remove temporary zip file $TEMP_ZIP_FILE." | |
fi | |
else | |
log_info "Okay, keeping the zip file at ${TEMP_ZIP_FILE}." | |
add_to_report "User skipped removing temporary zip file." | |
fi | |
else | |
log_error "Extraction failed! Maybe the download was corrupted or there's a permissions issue? Aborting. π₯" | |
add_to_report "Flutter SDK extraction failed." | |
exit 1 | |
fi | |
else | |
log_info "Extraction cancelled. The downloaded file is still at ${TEMP_ZIP_FILE}." | |
add_to_report "Extraction cancelled by user." | |
exit 0 | |
fi | |
else | |
log_error "Download failed! Check your internet connection or the URL. Aborting. π₯" | |
log_exec "$CLEANUP_CMD" | |
eval $CLEANUP_CMD | |
add_to_report "Flutter SDK download failed." | |
exit 1 | |
fi | |
else | |
log_info "Download cancelled." | |
add_to_report "Download cancelled by user." | |
exit 0 | |
fi | |
# --- 8. Update PATH --- | |
# (Remains the same as v1.4) | |
log_step "Making Flutter easily accessible (Updating PATH)... πΊοΈ" | |
add_to_report "\n--- PATH Update ---" | |
FLUTTER_BIN_PATH="${FLUTTER_SDK_DIR}/bin" | |
EXPORT_LINE="export PATH=\"\$PATH:${FLUTTER_BIN_PATH}\"" | |
SHELL_CONFIG_FILE="" | |
CURRENT_SHELL=$(basename "$SHELL") | |
if [[ "$CURRENT_SHELL" == "zsh" ]]; then SHELL_CONFIG_FILE="${HOME}/.zshrc"; | |
elif [[ "$CURRENT_SHELL" == "bash" ]]; then | |
if [[ -f "${HOME}/.bash_profile" ]]; then SHELL_CONFIG_FILE="${HOME}/.bash_profile"; | |
elif [[ -f "${HOME}/.bashrc" ]]; then SHELL_CONFIG_FILE="${HOME}/.bashrc"; | |
else SHELL_CONFIG_FILE="${HOME}/.bash_profile"; fi | |
else | |
log_warning "Hmm, I don't recognize your shell ($CURRENT_SHELL). I can't automatically update your PATH." | |
add_to_report "Unsupported shell ($CURRENT_SHELL) for automatic PATH update." | |
fi | |
if [[ -n "$SHELL_CONFIG_FILE" ]]; then | |
log_info "Your shell config file seems to be: ${SHELL_CONFIG_FILE}" | |
if add_line_to_file "$EXPORT_LINE" "$SHELL_CONFIG_FILE"; then | |
echo -e "${COLOR_WARNING}β¨ REMEMBER: You need to close and reopen your Terminal window, or run '${COLOR_BOLD}source ${SHELL_CONFIG_FILE}${COLOR_RESET}' for this change to take effect! β¨${COLOR_RESET}" | |
fi | |
else | |
log_warning "Please add this line to your shell configuration file manually:" | |
echo -e " ${COLOR_CMD}${EXPORT_LINE}${COLOR_RESET}" | |
add_to_report "Manual PATH update required by user." | |
fi | |
log_info "Temporarily adding Flutter to PATH for this script session..." | |
export PATH="${FLUTTER_BIN_PATH}:${PATH}" | |
FLUTTER_CHECK_CMD="command -v flutter" | |
log_exec "$FLUTTER_CHECK_CMD" | |
if ! command -v flutter > /dev/null; then | |
log_error "Oh no! Even after trying to set the PATH, I can't find the 'flutter' command. Check install at ${FLUTTER_SDK_DIR}. Aborting. π₯" | |
add_to_report "Flutter command not found after installation and PATH export." | |
exit 1 | |
fi | |
log_success "Great! The 'flutter' command is working in this session." | |
add_to_report "Flutter command found in PATH for current session." | |
# --- 9. Run flutter precache --- | |
# (Remains the same as v1.4) | |
log_step "Getting extra tools Flutter needs for iOS... π οΈ" | |
add_to_report "\n--- Flutter Precache ---" | |
log_info "Flutter needs to download specific components for iOS development." | |
PRECACHE_CMD="flutter precache --ios" | |
if ask_confirm_command "Download iOS development artifacts" "$PRECACHE_CMD"; then | |
log_exec "$PRECACHE_CMD" | |
if flutter precache --ios; then | |
log_success "iOS artifacts downloaded successfully! π" | |
add_to_report "Ran 'flutter precache --ios' successfully." | |
else | |
log_warning "Hmm, 'flutter precache' finished, but maybe with some issues? Check the output above. 'flutter doctor' later might give more clues." | |
add_to_report "Ran 'flutter precache --ios' with warnings/errors." | |
fi | |
else | |
log_info "Okay, skipping precache for now. Flutter will download these later when needed." | |
add_to_report "Skipped 'flutter precache --ios'." | |
fi | |
# --- 10. & 11. Install CocoaPods --- | |
# (Remains the same as v1.4) | |
log_step "Checking for CocoaPods (a helper tool)... π¦" | |
add_to_report "\n--- CocoaPods Check & Install ---" | |
POD_CHECK_CMD="command -v pod" | |
log_exec "$POD_CHECK_CMD" | |
if ! command -v pod > /dev/null; then | |
log_warning "CocoaPods wasn't found. Flutter needs it for iOS projects." | |
INSTALL_CMD="" | |
USE_GEM=false | |
BREW_CHECK_CMD="command -v brew" | |
log_exec "$BREW_CHECK_CMD" | |
if command -v brew > /dev/null; then | |
log_info "I see you have Homebrew installed! That's usually the easiest way." | |
BREW_INSTALL_CMD="brew install cocoapods" | |
if ask_confirm_command "Install CocoaPods using Homebrew" "$BREW_INSTALL_CMD"; then | |
INSTALL_CMD="$BREW_INSTALL_CMD" | |
fi | |
else | |
log_info "Homebrew not found. We can try installing CocoaPods using RubyGems instead." | |
fi | |
if [[ -z "$INSTALL_CMD" ]]; then | |
GEM_INSTALL_CMD="sudo gem install cocoapods" | |
if ask_confirm_command "Install CocoaPods using RubyGems (needs admin privileges)" "$GEM_INSTALL_CMD"; then | |
INSTALL_CMD="$GEM_INSTALL_CMD" | |
USE_GEM=true | |
fi | |
fi | |
if [[ -n "$INSTALL_CMD" ]]; then | |
log_info "Alright, attempting to install CocoaPods..." | |
log_exec "$INSTALL_CMD" | |
if eval $INSTALL_CMD; then | |
log_success "CocoaPods installed successfully! π" | |
add_to_report "Installed CocoaPods using: $INSTALL_CMD" | |
if [[ "$USE_GEM" == true ]]; then | |
POD_SETUP_CMD="pod setup" | |
log_info "Since we used RubyGems, sometimes running 'pod setup' is needed." | |
if ask_confirm_command "Initialize CocoaPods repository (can take a while)" "$POD_SETUP_CMD"; then | |
log_info "Running 'pod setup' now... This might take some time! β³" | |
log_exec "$POD_SETUP_CMD" | |
if pod setup; then | |
log_success "'pod setup' completed!" | |
add_to_report "Ran 'pod setup' successfully." | |
else | |
log_warning "'pod setup' finished, but maybe with issues. Check the output." | |
add_to_report "'pod setup' failed or had warnings." | |
fi | |
else | |
log_info "Skipping 'pod setup'. If you have issues later, try running it manually." | |
add_to_report "Skipped 'pod setup'." | |
fi | |
fi | |
else | |
log_error "CocoaPods installation failed. π₯ Please try installing it manually. The command was: ${INSTALL_CMD}" | |
add_to_report "CocoaPods installation failed using: $INSTALL_CMD" | |
fi | |
else | |
log_warning "Okay, CocoaPods installation skipped. Remember, Flutter needs it for iOS builds!" | |
add_to_report "User skipped CocoaPods installation." | |
fi | |
else | |
log_success "Found CocoaPods!" | |
POD_VERSION_CMD="pod --version" | |
log_exec "$POD_VERSION_CMD" | |
POD_VERSION=$(pod --version) | |
log_info "Version: ${POD_VERSION} π" | |
add_to_report "CocoaPods found (version ${POD_VERSION})." | |
fi | |
# --- 12. Optionally Launch Simulator --- | |
# (Remains the same as v1.4) | |
log_step "Want to launch the iOS Simulator? π±" | |
add_to_report "\n--- iOS Simulator Launch ---" | |
SIM_LAUNCH_CMD="open -a Simulator" | |
if ask_confirm_command "Try opening the iOS Simulator app" "$SIM_LAUNCH_CMD"; then | |
log_info "Okay, attempting to launch the Simulator... Look for it in your Dock!" | |
log_exec "$SIM_LAUNCH_CMD" | |
if open -a Simulator; then | |
log_success "Simulator launch command sent!" | |
add_to_report "Attempted to launch iOS Simulator." | |
else | |
log_warning "The command to open the Simulator didn't seem to work right. Make sure Xcode is fully installed." | |
add_to_report "Command 'open -a Simulator' failed or had issues." | |
fi | |
else | |
log_info "No problem, you can open the Simulator later from Spotlight or Xcode." | |
add_to_report "Skipped launching Simulator." | |
fi | |
# --- 13. Run flutter doctor (final check) --- | |
# (Remains the same as v1.4) | |
log_step "Running Flutter Doctor for a final health check... π©Ί" | |
add_to_report "\n--- Final Flutter Doctor Run ---" | |
log_info "This checks everything: Flutter, Xcode, iOS tools, CocoaPods, and devices." | |
DOCTOR_CMD="flutter doctor" | |
if ask_confirm "Run 'flutter doctor' to see the status?"; then | |
log_exec "$DOCTOR_CMD" | |
mapfile -t DOCTOR_LINES < <(flutter doctor) | |
DOCTOR_OUTPUT=$(printf "%s\n" "${DOCTOR_LINES[@]}") | |
echo -e "$DOCTOR_OUTPUT" | |
add_to_report "\nFinal Flutter Doctor Output:\n${DOCTOR_OUTPUT}" | |
else | |
log_warning "Skipping 'flutter doctor'. Run it yourself later ('${DOCTOR_CMD}') to check the setup!" | |
add_to_report "Skipped final 'flutter doctor' run." | |
fi | |
# --- 14. Final Report --- | |
# (Remains the same as v1.4) | |
log_step "All Done! Here's Your Setup Summary π" | |
echo -e "${COLOR_SUCCESS}-----------------------------------------------------" | |
echo -e " β¨ Flutter iOS Setup Script Complete! β¨" | |
echo -e "-----------------------------------------------------${COLOR_RESET}" | |
echo -e "I've saved a detailed report of what I did here:" | |
echo -e " ${COLOR_BOLD}${REPORT_FILE}${COLOR_RESET}" | |
echo -e "\n${COLOR_BOLD}Quick Summary:${COLOR_RESET}" | |
grep -E "^(Script Version|Flutter Docs Reference|Xcode Command Line Tools Path|Xcode license|Detected Architecture|Found Flutter SDK URL|Target installation directory|Added/verified Flutter PATH|Ran 'flutter precache|Installed CocoaPods|Ran 'pod setup'|CocoaPods found|Attempted to launch iOS Simulator)" "$REPORT_FILE" | sed -e "s/^/ β /" | |
echo -e "\n${COLOR_BOLD}π What's Next?${COLOR_RESET}" | |
if [[ -n "$SHELL_CONFIG_FILE" ]]; then | |
echo -e " 1. ${COLOR_WARNING}VERY IMPORTANT:${COLOR_RESET} Close this Terminal window and open a new one, OR run '${COLOR_BOLD}source ${SHELL_CONFIG_FILE}${COLOR_RESET}' right now to make the 'flutter' command work everywhere!" | |
else | |
echo -e " 1. ${COLOR_WARNING}VERY IMPORTANT:${COLOR_RESET} Add Flutter to your PATH manually, then close and reopen your Terminal!" | |
fi | |
echo -e " 2. In the ${COLOR_BOLD}new Terminal${COLOR_RESET}, run ${COLOR_BOLD}flutter doctor${COLOR_RESET} again. Hopefully, everything is green! β " | |
echo -e " 3. Check connected devices/simulators: ${COLOR_BOLD}flutter devices${COLOR_RESET}" | |
echo -e " 4. Create your very first Flutter app: ${COLOR_BOLD}flutter create my_cool_app${COLOR_RESET}" | |
echo -e " 5. Go into your app's folder: ${COLOR_BOLD}cd my_cool_app${COLOR_RESET}" | |
echo -e " 6. Run your app on the Simulator or a connected iPhone: ${COLOR_BOLD}flutter run${COLOR_RESET} π" | |
echo -e "\nHappy Fluttering! π Check the official docs for more: ${FLUTTER_DOCS_URL}" | |
add_to_report "\n--- Script Finished ---" | |
} | |
# --- Run Script --- | |
main "$@" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment