Skip to content

Instantly share code, notes, and snippets.

@Hesamedin
Last active July 29, 2022 19:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hesamedin/c960d9bcd80b7e1c2e582232e5e7320d to your computer and use it in GitHub Desktop.
Save Hesamedin/c960d9bcd80b7e1c2e582232e5e7320d to your computer and use it in GitHub Desktop.
Clean ios project of a Flutter project and Run it.
#!/bin/bash
set -e
echo "The script you are running is: $(dirname "$0")/$(basename "$0")"
function runDebug() {
flutter run iOS --debug --target=lib/main_dev.dart
}
function runRelease() {
flutter run iOS --target=lib/main_prod.dart
}
CURRENT_DIR="$PWD"
FOLDER_SYMLINKS="$CURRENT_DIR/ios/.symlinks"
FOLDER_PODS="$CURRENT_DIR/ios/Pods"
FILE_PODFILE_LOCK="$CURRENT_DIR/ios/Podfile.lock"
FILE_FLUTTER_PODSPEC="$CURRENT_DIR/ios/Flutter/Flutter.podspec"
FILE_EXPORT_ENV="$CURRENT_DIR/ios/Flutter/flutter_export_environment.sh"
FILE_GENERATED="$CURRENT_DIR/ios/Flutter/Generated.xcconfig"
ARRAY_FOLDERS=( "$FOLDER_SYMLINKS" "$FOLDER_PODS" )
ARRAY_FILES=( "$FILE_PODFILE_LOCK" "$FILE_FLUTTER_PODSPEC" "$FILE_EXPORT_ENV" "$FILE_GENERATED" )
# Iterate over folders and remove its contents
for folder in "${ARRAY_FOLDERS[@]}"
do
if [ -d "$folder" ]; then
rm -Rf "$folder"
echo "$folder deleted."
else
echo "$folder does not exist."
fi
done
# Iterate over files and remove them
for file in "${ARRAY_FILES[@]}"
do
if [ -f "$file" ]; then
rm "$file"
echo "$file deleted."
else
echo "$file does not exist."
fi
done
# Clean Flutter project
flutter clean
# Download dependencies
flutter pub get
# Run the app on the emulator.
# Make sure the emulator is running.
if [ "$1" = "-r" ]; then
echo "Release build is running..."
runRelease
else
echo "Debug build is running..."
runDebug
fi
@Hesamedin
Copy link
Author

The script cleans the ios folder by deleting all files and folders that are being used to cache data. Then runs the Flutter app.

The script assumes that there are two entries (main file) for the project one is being used if the build is a dev build and the other will be used if it is a production build. You need to pass -r to the command if it is a production build.

I usually put this file under the scripts folder at the root of the project. Use the following commands when you want to run it:

dev build:

sh scripts/clean_run_ios.sh 

prod build:

sh scripts/clean_run_ios.sh -r

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment