Skip to content

Instantly share code, notes, and snippets.

@Taosif7
Last active October 20, 2024 12:07
Show Gist options
  • Save Taosif7/0c0cf40274a654c9f3487c125c624ba4 to your computer and use it in GitHub Desktop.
Save Taosif7/0c0cf40274a654c9f3487c125c624ba4 to your computer and use it in GitHub Desktop.
A Bash Script to Copy flutter generated build outputs in a proper format
#!/bin/bash
# Check if pubspec.yaml file exists
if [ ! -f "pubspec.yaml" ]; then
echo "⚠️ Please run command in a Flutter project"
exit 1
fi
# Get the value of "name: value"
projectName=$(awk '/name:/ {print $2}' pubspec.yaml)
# Get the value of "version: value"
version=$(awk '/version:/ {print $2}' pubspec.yaml)
IFS='+' read -ra versionParts <<< "$version"
buildName=${versionParts[0]}
buildNumber=${versionParts[1]}
# Check if the Assets exist
apkFound=false
aabFound=false
[ -f build/app/outputs/flutter-apk/*.apk ] && apkFound=true || apkFound=false
[ -f build/app/outputs/bundle/*.aab ] && aabFound=true || aabFound=false
if ! $apkFound && ! $aabFound; then
echo "⚠️ No assets found to copy. Please generate APK/AAB and run again"
exit 1
fi
# Create filename format
desktopPath="$HOME/Desktop"
formattedDate=$(date +%m%d%y)
newFileName="$projectName"_v"$buildName"\("$buildNumber"\)_"$formattedDate"
# Copy APK
if $apkFound; then
cp build/app/outputs/flutter-apk/*.apk "$desktopPath/$newFileName.apk"
echo "✅ APK file successfully copied to desktop: $newFileName.apk"
fi
# Copy AAB
if $aabFound; then
cp build/app/outputs/bundle/*.aab "$desktopPath/$newFileName.aab"
echo "✅ AAB file successfully copied to desktop: $newFileName.aab"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment