Skip to content

Instantly share code, notes, and snippets.

@iqbalmineraltown
Last active April 28, 2022 02:42
Show Gist options
  • Save iqbalmineraltown/47b41ae269a8ecc8d92909ef8742cfb2 to your computer and use it in GitHub Desktop.
Save iqbalmineraltown/47b41ae269a8ecc8d92909ef8742cfb2 to your computer and use it in GitHub Desktop.
Generate Static Resource to Image Assets Files for Flutter. See first comment for usage
# Copyright 2018 Muhammad Iqbal(iqbalmineraltown.com)
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
# THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#!/bin/bash
targetfolder="./lib/assets"
targetfile="$targetfolder/assets.dart"
regex='s/[-_]([a-z0-9])/\U\1/gi'
# for OSX, replace sed with command below
# perl -nE 'say lcfirst join "", map {ucfirst lc} split /[^[:alnum:]]+/'
echo `pwd`
cd `pwd`
mkdir -p ./lib/assets
touch $targetfile
touch tmp
echo "class ImageAssets {" > $targetfile
for f in `find ./assets/images -maxdepth 1 -type f`; do
filename=`echo $f | cut -d'/' -f4 | cut -d'.' -f1`
varname=`echo $filename | sed -E $regex`
entry="static const String $varname = '${f:2}';"
echo " $entry" >> tmp;
done;
sort tmp >> $targetfile
printf '}\n\n' >> $targetfile
rm -f tmp
cd -
@iqbalmineraltown
Copy link
Author

iqbalmineraltown commented Dec 3, 2018

Pre-requisites

  • filename in snake_case or kebab-case
  • Tested on Linux Ubuntu
  • Tested on MacOSX with gnu-sed installed

How To

Run this on the root of Flutter Project. It will generate assets.dart under lib/assets folder

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