Skip to content

Instantly share code, notes, and snippets.

@callmephil
Created February 17, 2026 17:07
Show Gist options
  • Select an option

  • Save callmephil/ec1deac3f12920165b1cea238ea2c8d7 to your computer and use it in GitHub Desktop.

Select an option

Save callmephil/ec1deac3f12920165b1cea238ea2c8d7 to your computer and use it in GitHub Desktop.
pub upgrade
#!/bin/bash
# 1. Run flutter pub outdated and capture output
# We use --no-color to make text parsing reliable
echo "Analyzing outdated packages..."
OUTDATED_OUTPUT=$(flutter pub outdated --no-color)
# Check if the command was successful
if [ $? -ne 0 ]; then
echo "Error running 'flutter pub outdated'. Please check your project configuration."
exit 1
fi
# 2. Parse the output using awk
# We separate the output into two lists: direct dependencies and dev_dependencies
# We stop parsing if we hit "transitive" to avoid adding internal packages.
read -r -d '' PARSED_PACKAGES <<EOM
$(echo "$OUTDATED_OUTPUT" | awk '
BEGIN { section="" }
# Detect sections
/^direct dependencies:/ { section="direct"; next }
/^dev_dependencies:/ { section="dev"; next }
/^transitive/ { section="stop"; next } # Stop processing when hitting transitive deps
# Skip headers and empty lines
/^Package Name/ { next }
/^\[\*\]/ { next }
NF==0 { next }
# Process lines based on section
{
# $1 is the Package Name in the output columns
if (section == "direct") {
print "REG " $1
}
else if (section == "dev") {
print "DEV " $1
}
}
')
EOM
# 3. separate into variables
REGULAR_DEPS=""
DEV_DEPS=""
while IFS= read -r line; do
if [[ $line == REG* ]]; then
PKG_NAME=${line#REG }
REGULAR_DEPS="$REGULAR_DEPS $PKG_NAME"
elif [[ $line == DEV* ]]; then
PKG_NAME=${line#DEV }
DEV_DEPS="$DEV_DEPS $PKG_NAME"
fi
done <<< "$PARSED_PACKAGES"
# 4. Construct the commands
CMD_REG=""
CMD_DEV=""
if [ ! -z "$REGULAR_DEPS" ]; then
CMD_REG="flutter pub add$REGULAR_DEPS"
fi
if [ ! -z "$DEV_DEPS" ]; then
# Note: Using -d for dev dependencies
CMD_DEV="flutter pub add -d$DEV_DEPS"
fi
# 5. Execute Logic
if [ -z "$CMD_REG" ] && [ -z "$CMD_DEV" ]; then
echo "No outdated packages found."
exit 0
fi
echo "------------------------------------------------"
echo "Prepared Commands:"
FINAL_CMD=""
if [ ! -z "$CMD_REG" ]; then
echo "1. $CMD_REG"
FINAL_CMD="$CMD_REG"
fi
if [ ! -z "$CMD_DEV" ]; then
echo "2. $CMD_DEV"
if [ ! -z "$FINAL_CMD" ]; then
FINAL_CMD="$FINAL_CMD && $CMD_DEV"
else
FINAL_CMD="$CMD_DEV"
fi
fi
echo "------------------------------------------------"
echo "Executing updates..."
echo ""
# Execute the final combined command
eval "$FINAL_CMD"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment