44
55echo " === Flutter Structurizr Development Environment Setup ==="
66
7+ # Detect if running as root
8+ if [ " $EUID " -eq 0 ]; then
9+ echo " Warning: Running as root. Flutter should be installed as a regular user."
10+ echo " Consider running this script as a non-root user for better security."
11+ echo " "
12+ fi
13+
714# Detect OS
815OS=" unknown"
916if [[ " $OSTYPE " == " linux-gnu" * ]]; then
2431
2532echo " Detected OS: $OS "
2633
34+ # Fix Flutter git safe directory if running as root
35+ fix_flutter_git_safe_directory () {
36+ if [ " $EUID " -eq 0 ] && [ -d " $1 " ]; then
37+ echo " Fixing Flutter git repository permissions..."
38+ git config --global --add safe.directory " $1 " || true
39+ fi
40+ }
41+
2742# 1. Install Flutter if not present
2843if ! command -v flutter & > /dev/null; then
2944 echo " Flutter not found. Installing Flutter..."
@@ -32,65 +47,86 @@ if ! command -v flutter &> /dev/null; then
3247 if command -v snap & > /dev/null; then
3348 echo " Installing Flutter via snap..."
3449 sudo snap install flutter --classic
50+ FLUTTER_PATH=" /snap/bin/flutter"
3551 else
3652 echo " Installing Flutter manually..."
3753 # Download and install Flutter
3854 FLUTTER_VERSION=" 3.19.0" # Latest stable as of now
55+ FLUTTER_DIR=" ${HOME} /development/flutter"
56+ if [ " $EUID " -eq 0 ]; then
57+ FLUTTER_DIR=" /opt/flutter"
58+ fi
59+
60+ mkdir -p " $( dirname " $FLUTTER_DIR " ) "
3961 curl -L " https://storage.googleapis.com/flutter_infra_release/releases/stable/linux/flutter_linux_${FLUTTER_VERSION} -stable.tar.xz" -o flutter.tar.xz
40- mkdir -p " $HOME /development"
41- tar xf flutter.tar.xz -C " $HOME /development"
62+ tar xf flutter.tar.xz -C " $( dirname " $FLUTTER_DIR " ) "
4263 rm flutter.tar.xz
4364
65+ FLUTTER_PATH=" ${FLUTTER_DIR} /bin/flutter"
66+
4467 # Add to PATH
45- export PATH=" $PATH :$HOME /development/flutter /bin"
68+ export PATH=" $PATH :${FLUTTER_DIR} /bin"
4669
4770 # Add to shell profile
4871 if [[ -f " $HOME /.bashrc" ]]; then
49- echo ' export PATH=" $PATH:$HOME/development/flutter/ bin" ' >> " $HOME /.bashrc"
72+ echo " export PATH=\"\ $ PATH:${FLUTTER_DIR} / bin\" " >> " $HOME /.bashrc"
5073 fi
5174 if [[ -f " $HOME /.zshrc" ]]; then
52- echo ' export PATH=" $PATH:$HOME/development/flutter/ bin" ' >> " $HOME /.zshrc"
75+ echo " export PATH=\"\ $ PATH:${FLUTTER_DIR} / bin\" " >> " $HOME /.zshrc"
5376 fi
5477
55- echo " Flutter installed to $HOME /development/flutter"
78+ # Fix git safe directory if needed
79+ fix_flutter_git_safe_directory " $FLUTTER_DIR "
80+
81+ echo " Flutter installed to $FLUTTER_DIR "
5682 echo " Please restart your terminal or run: source ~/.bashrc"
5783 fi
5884 elif [[ " $OS " == " macos" ]]; then
5985 if command -v brew & > /dev/null; then
6086 echo " Installing Flutter via Homebrew..."
6187 brew install flutter
88+ FLUTTER_PATH=" flutter"
6289 else
6390 echo " Installing Flutter manually..."
6491 # Download and install Flutter
6592 FLUTTER_VERSION=" 3.19.0" # Latest stable as of now
93+ FLUTTER_DIR=" ${HOME} /development/flutter"
94+
95+ mkdir -p " $( dirname " $FLUTTER_DIR " ) "
6696 curl -L " https://storage.googleapis.com/flutter_infra_release/releases/stable/macos/flutter_macos_${FLUTTER_VERSION} -stable.zip" -o flutter.zip
67- mkdir -p " $HOME /development"
68- unzip flutter.zip -d " $HOME /development"
97+ unzip flutter.zip -d " $( dirname " $FLUTTER_DIR " ) "
6998 rm flutter.zip
7099
100+ FLUTTER_PATH=" ${FLUTTER_DIR} /bin/flutter"
101+
71102 # Add to PATH
72- export PATH=" $PATH :$HOME /development/flutter /bin"
103+ export PATH=" $PATH :${FLUTTER_DIR} /bin"
73104
74105 # Add to shell profile
75106 if [[ -f " $HOME /.zshrc" ]]; then
76- echo ' export PATH=" $PATH:$HOME/development/flutter/ bin" ' >> " $HOME /.zshrc"
107+ echo " export PATH=\"\ $ PATH:${FLUTTER_DIR} / bin\" " >> " $HOME /.zshrc"
77108 elif [[ -f " $HOME /.bash_profile" ]]; then
78- echo ' export PATH=" $PATH:$HOME/development/flutter/ bin" ' >> " $HOME /.bash_profile"
109+ echo " export PATH=\"\ $ PATH:${FLUTTER_DIR} / bin\" " >> " $HOME /.bash_profile"
79110 fi
80111
81- echo " Flutter installed to $HOME /development/flutter "
112+ echo " Flutter installed to $FLUTTER_DIR "
82113 echo " Please restart your terminal or run: source ~/.zshrc"
83114 fi
84115 else
85116 echo " Unable to automatically install Flutter on this OS."
86117 echo " Please visit: https://flutter.dev/docs/get-started/install"
87118 exit 1
88119 fi
120+ else
121+ FLUTTER_PATH=" flutter"
89122fi
90123
91124# Update PATH for current session if Flutter was just installed
92- if [[ -d " $HOME /development/flutter/bin" ]] && [[ " :$PATH :" != * " :$HOME /development/flutter/bin:" * ]]; then
93- export PATH=" $PATH :$HOME /development/flutter/bin"
125+ if [[ -d " ${HOME} /development/flutter/bin" ]] && [[ " :$PATH :" != * " :${HOME} /development/flutter/bin:" * ]]; then
126+ export PATH=" $PATH :${HOME} /development/flutter/bin"
127+ fi
128+ if [[ -d " /opt/flutter/bin" ]] && [[ " :$PATH :" != * " :/opt/flutter/bin:" * ]]; then
129+ export PATH=" $PATH :/opt/flutter/bin"
94130fi
95131
96132# 2. Verify Flutter installation
101137
102138echo " Flutter is installed ✓"
103139
140+ # Fix git safe directory for existing Flutter installations
141+ if command -v flutter & > /dev/null; then
142+ FLUTTER_DIR=$( dirname $( dirname $( which flutter) ) )
143+ fix_flutter_git_safe_directory " $FLUTTER_DIR "
144+ fi
145+
104146# 3. Accept Android licenses and install dependencies
105147echo " Running flutter doctor to check environment..."
106148flutter doctor
@@ -116,17 +158,23 @@ echo "Installing system dependencies..."
116158
117159if [[ " $OS " == " linux" && " $DISTRO " == " debian" ]]; then
118160 echo " Installing Linux dependencies..."
119- sudo apt-get update
120- sudo apt-get install -y \
121- clang \
122- cmake \
123- git \
124- ninja-build \
125- pkg-config \
126- libgtk-3-dev \
127- libblkid-dev \
128- liblzma-dev \
129- lcov
161+ # Check for apt network connectivity
162+ if ! apt-get update 2> /dev/null; then
163+ echo " Warning: Unable to update package lists. Network connectivity issue?"
164+ echo " Skipping system dependency installation. You may need to install manually:"
165+ echo " sudo apt-get install -y clang cmake git ninja-build pkg-config libgtk-3-dev libblkid-dev liblzma-dev lcov"
166+ else
167+ sudo apt-get install -y \
168+ clang \
169+ cmake \
170+ git \
171+ ninja-build \
172+ pkg-config \
173+ libgtk-3-dev \
174+ libblkid-dev \
175+ liblzma-dev \
176+ lcov || echo " Some packages failed to install. Continuing..."
177+ fi
130178elif [[ " $OS " == " linux" && " $DISTRO " == " fedora" ]]; then
131179 echo " Installing Linux dependencies..."
132180 sudo dnf install -y \
@@ -135,14 +183,14 @@ elif [[ "$OS" == "linux" && "$DISTRO" == "fedora" ]]; then
135183 git \
136184 ninja-build \
137185 gtk3-devel \
138- lcov
186+ lcov || echo " Some packages failed to install. Continuing... "
139187elif [[ " $OS " == " macos" ]]; then
140188 echo " Installing macOS dependencies..."
141189 if ! command -v brew & > /dev/null; then
142190 echo " Installing Homebrew..."
143191 /bin/bash -c " $( curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh) "
144192 fi
145- brew install lcov cmake ninja
193+ brew install lcov cmake ninja || echo " Some packages failed to install. Continuing... "
146194fi
147195
148196# 5. Install Flutter dependencies
0 commit comments