Skip to content

Instantly share code, notes, and snippets.

@danielgomezrico
Created May 2, 2024 21:20
Show Gist options
  • Save danielgomezrico/d7991f9e4e312e20dc160a786f812ca5 to your computer and use it in GitHub Desktop.
Save danielgomezrico/d7991f9e4e312e20dc160a786f812ca5 to your computer and use it in GitHub Desktop.
Flutter - Built Value - Build Runner: script to update pubscpec with the files to autogenerate
targets:
$default:
builders:
built_value_generator:built_value:generate_for:
generate_for:
- HERE
#!/bin/bash
# Updates pubspec.yml generate_for section with all files that
# should be included in the code generation process.
DIRECTORIES=("lib/" "test/")
TEMP_FILE="temp.yaml"
PUBSPEC_FILE="pubspec.yaml"
# Empty the temp file
> $TEMP_FILE
# Search for files containing 'part "*.dart"' in each directory and format the output
for DIRECTORY in "${DIRECTORIES[@]}"
do
grep -rl --include \*.dart "part '" $DIRECTORY | sort | awk '{gsub("//","/"); print " - " $0}' >> $TEMP_FILE
done
# Use awk to process the pubspec file line by line
awk -v temp_file="$TEMP_FILE" '
BEGIN {count=0; print_lines=1}
/generate_for:/ {count++}
count == 2 && /generate_for:/ {print; while ((getline line < temp_file) > 0) print line; count++; print_lines=0; next}
/^[^ ]/ {print_lines=1}
{if (print_lines) print}
' $PUBSPEC_FILE > "${PUBSPEC_FILE}.tmp" && mv "${PUBSPEC_FILE}.tmp" $PUBSPEC_FILE
# Remove the temporary file
rm $TEMP_FILE
# Display the result
echo "Updated pubspec.yaml:"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment