You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Name: Flutter Release Builds# Description: Generate release builds for flutter app# Author: Rohan Joshi# Twitter: @rohanjsh#!/bin/bash#! DEFAULTS#! Change these to your own#! PASS --use-defaults FLAG, TO USE THESE DEFAULTS
default_num_flavors=2
default_flavors=("dev""lib/main_dev.dart""prod""lib/main_prod.dart")
default_build_types=("apk""appbundle""ipa")
# DESCRIPTION# Automates Flutter project builds with flavor and type options.# Prerequisites:# - Flutter installed and configured.# - Bash shell environment.# Options:# --use-defaults: Use default values.# --no-flavor: Build without flavors.# Usage:# - Default: ./build_script.sh# - Custom: ./build_script.sh --use-defaults# - No Flavor: ./build_script.sh --no-flavor# How To Guide:# - Run the script and follow prompts for custom builds.# - Number of flavors# - Flavor name and entry point for each flavor# - Build types to generate# - Script creates timestamped build directories.# Exit on any error, unbound variable, or error in a pipelineset -euo pipefail
#! +--------------------------+#! | GLOBAL VARIABLES |#! +--------------------------+# Define variables to store states of both flags
use_defaults=false
no_flavor=false
# Define available flags
valid_flags="use-defaults,h,no-flavor"# Get the current date and time
current_date_time=$(date "+%Y-%m-%d_%H-%M-%S")#+---------------------------------+#| Function Declarations |#+---------------------------------+#! Function to run flutter commands, ALTER COMMANDS AS NEEDED (project specific)run_flutter_commands() {
echo"🚀 Cleaning, fetching dependencies, and running build processes..."
flutter clean
flutter pub get
dart pub get
#! Uncomment the following line if you are using build_runner# dart run build_runner build --delete-conflicting-outputs
}
# Function to display an interactive menu for build typesselect_build_types() {
if [[ $use_defaults==true ]];then
build_types=("${default_build_types[@]}")
returnfiecho"️ Select the build type(s) you want to generate:"echo" 1. APK"echo" 2. AAB (App Bundle)"echo" 3. IPA"read -p "Enter your choice (comma-separated numbers): " build_type_choice
# Split the user input into an array of individual choices
IFS=','read -r -a build_type_choices <<<"$build_type_choice"
build_types=()
# Loop through each choice and validate itforchoicein"${build_type_choices[@]}";docase"$choice"in
1) build_types+=("apk") ;;
2) build_types+=("appbundle") ;;
3) build_types+=("ipa") ;;
*) echo"Invalid choice: '$choice'. Ignoring." ;;
esacdone# Check if any valid choices were selectedif [[ ${#build_types[@]}-eq 0 ]];thenecho"No valid build types selected. Exiting."&&exit 1
fi
}
# Function to build and copy filesbuild_and_copy_files() {
if [ "$1"=="apk" ];then
flutter build $1 --flavor $2 -t $3 --split-per-abi
else
flutter build $1 --flavor $2 -t $3fi# Create a directory for the build
mkdir -p builds/$current_date_time/[$2-$1]
cp -r $4* builds/$current_date_time/[$2-$1]
echo"✅ Build completed for $2 flavor and type $1"
}
#function to call if no flavor is selectedno_flavor_build(){
select_build_types
run_flutter_commands
# Directly run commands for each build typeforbuild_typein"${build_types[@]}";docase"$build_type"in"apk")
flutter build apk --split-per-abi
# Create a directory for the build
mkdir -p builds/$current_date_time/[no-flavor-$build_type]
# Copy files for each build type
cp -r build/app/outputs/flutter-apk/* builds/$current_date_time/[no-flavor-$build_type]
;;
"appbundle")
flutter build appbundle
# Create a directory for the build
mkdir -p builds/$current_date_time/[no-flavor-$build_type]
# Copy files over
cp -r build/app/outputs/bundle/* builds/$current_date_time/[no-flavor-$build_type]
;;
"ipa")
flutter build ipa
# Create a directory for the build
mkdir -p builds/$current_date_time/[no-flavor-$build_type]
# Copy files over
cp -r build/ios/ipa/* builds/$current_date_time/[no-flavor-$build_type]
;;
esac# Create a directory for the build
mkdir -p builds/$current_date_time/[no-flavor-$build_type]
# Copy files for each build typecase"$build_type"in"apk")
cp -r build/app/outputs/flutter-apk/* builds/$current_date_time/[no-flavor-$build_type]
;;
"appbundle")
cp -r build/app/outputs/bundle/* builds/$current_date_time/[no-flavor-$build_type]
;;
"ipa")
cp -r build/ios/ipa/* builds/$current_date_time/[no-flavor-$build_type]
;;
esacdoneecho"🎉 All builds completed successfully!, you can find all the builds in /builds folder"
open builds/$current_date_timeexit 0
}
#+-------------------------+#| Process flags |#+-------------------------+# Process options using getoptswhilegetopts":h-:" opt;docase$optin
-)
case$OPTARGin
use-defaults)
use_defaults=true
;;
h)
echo"Usage: $0 [--$valid_flags]"exit 0
;;
no-flavor)
no_flavor=true
;;
*)
echo"Invalid option: --$OPTARG">&2echo"Available options: $valid_flags">&2exit 1
;;
esac
;;
h)
echo"Usage: $0 [--$valid_flags]"exit 0
;;
\?)
echo"Invalid option: -$OPTARG">&2echo"Available options: $valid_flags">&2exit 1
;;
esacdone# Check if the no-flavor flag is setif [[ $no_flavor==true ]];then
no_flavor_build
fi#+-------------------------+#| Get arguments |#+-------------------------+# Check if the flag is set and take appropriate actionif [[ $use_defaults==true ]];then
num_flavors=$default_num_flavors
flavors=("${default_flavors[@]}")
select_build_types
else# if no-flavor, then skip this stepif [[ $no_flavor==false ]];thenread -p "🤔 Number of flavors? " num_flavors
#if flavor is 0, then call if [[ $num_flavors-eq 0 ]];then
no_flavor_build
fi# Array to store flavor informationdeclare -a flavors
# Prompt the user for flavor informationfor((i =1; i <= num_flavors; i++));doread -p "🍦 Flavor $i name: " flavor_name
read -p "📄 Entry point location of flavor $i (eg. lib/main_dev.dart): " main_dart_location
#check if the file exists#if it does not exist, then ask againwhile [[ !-f$main_dart_location ]];doecho"🚫 File $main_dart_location does not exist, try again"read -p "📄 Entry point location of flavor $i (eg. lib/main_dev.dart): " main_dart_location
done# Add flavor information to the array
flavors+=("$flavor_name""$main_dart_location")
done
select_build_types
fifi
run_flutter_commands
# Build and copy files for each flavorfor((i =0; i <${#flavors[@]}; i +=2));do
flavor_name=${flavors[i]}
main_dart_location=${flavors[i + 1]}# Build and copy files for each build typeforbuild_typein"${build_types[@]}";docase"$build_type"in"apk")
output_directory="build/app/outputs/flutter-apk/"
;;
"appbundle")
output_directory="build/app/outputs/bundle/"
;;
"ipa")
output_directory="build/ios/ipa/"
;;
esac# Build and copy files for the flavor and build type
build_and_copy_files $build_type$flavor_name$main_dart_location$output_directorydonedone
open builds/$current_date_timeecho"🎉 All builds completed successfully!, you can find all the builds in /builds folder"
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Beta Was this translation helpful? Give feedback.
All reactions