Skip to content

Instantly share code, notes, and snippets.

@jayarc
Created January 9, 2018 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayarc/c6d1c65119a1d87935ad0155a30a6f6f to your computer and use it in GitHub Desktop.
Save jayarc/c6d1c65119a1d87935ad0155a30a6f6f to your computer and use it in GitHub Desktop.
Prepare icons for build time.
#!/bin/bash
#################################
# Icon Dynamic Imports - BEGIN
#################################
echo "Building icons..."
# A reference of all the 2k+ icons that can be used here and a quick search for icons you're looking
# for can be found here: https://materialdesignicons.com/
# find . -name '*.vue' -type f -exec sh -c \
# 'executable -option1 -option2 ${0} > ${0%.ext1}.ext2' {} \;
# sed command: does icon="value" value have a . in it? If so, don't change this one, break
# Otherwise add this one to the list
# Reset icons used list for this build
rm icons-used.txt
touch icons-used.txt
rm icons-used-report.txt
touch icons-used-report.txt
rm ./src/used-icons/index.js
touch ./src/used-icons/index.js
if [[ $(uname -s) = *Linux* ]]; then
echo "Generating icons for ------------------ LINUX"
echo "Generating icons for ------------------ LINUX"
echo "Generating icons for ------------------ LINUX"
echo "Generating icons for ------------------ LINUX"
echo "Generating icons for ------------------ LINUX"
echo "Generating icons for ------------------ LINUX"
echo "Generating icons for ------------------ LINUX"
echo "Generating icons for ------------------ LINUX"
# Find all icon="something" refs in Vue templates, include can be {a,list,of,exts}
grep -ohRP 'icon="\K[^"]*' ./src --include=*.{vue,js} >> icons-used.txt
grep -ohRP "icon='\K[^']*" ./src --include=*.{vue,js} >> icons-used.txt
# Find all icon: "something" refs in amorphic data, include can be {a,list,of,exts}
grep -ohRP '"icon":\s"\K[^"]*' ./src --include=*.{json,js,vue} >> icons-used.txt
# Replace underscores with dashes
sed -i 's/_/-/g' icons-used.txt
# Remove iconName references, the (new) standard computed/method name for configurable icon components, so this all works.
sed -i '/iconName/d' icons-used.txt
sed -i '/icon/d' icons-used.txt
sed -i '/Icon/d' icons-used.txt
# Remove any lines that contain dots, and therefore were js references
sed -i '/\./d' icons-used.txt
# Remove any lines that contain done
sed -i '/\done-/d' icons-used.txt
# Remove duplicate lines
# awk '!seen[$0]++' icons-used.txt
# awk -i inplace '!seen[$0]++' icons-used.txt
# sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'
# cat icons-used.txt | sort | uniq -d
# sort -u icons-used.txt > icons-used.txt
ruby icons-remove-duplicates.rb
# Add edge case icons that are included in an even more dynamic way than is worth scripting around
# Add to this manually if you need to brute force in a new icons that isn't showing on the front-end.
echo "chevron-up" >> icons-used.txt
echo "chevron-down" >> icons-used.txt
echo "menu-up" >> icons-used.txt
echo "menu-down" >> icons-used.txt
echo "menu" >> icons-used.txt
# Loop through found used icons and copy them to ./src/used-icons while adding requires to ./src/used-icons/index.js for import.
cat icons-used.txt | while read line
do
# do something with $line here
echo "Adding used icon: $line"
cp ./src/compiled-icons/${line}.js ./src/used-icons
echo "require('./${line}')" >> ./src/used-icons/index.js
done
# Report all vue json icon needs in templates, include can be {a,list,of,exts}
grep -HnRP 'icon="\K[^"]*' ./src --include=*.{vue,js} >> icons-used-report.txt
# Report all vue json icon needs in amorphic data, include can be {a,list,of,exts}
grep -HnRP 'icon:\s"\K[^"]*' ./src --include=*.{json,js,vue} >> icons-used-report.txt
fi
if [[ $(uname -s) = *Darwin* ]]; then
echo "Generating icons for ------------------ DARWIN"
echo "Generating icons for ------------------ DARWIN"
echo "Generating icons for ------------------ DARWIN"
echo "Generating icons for ------------------ DARWIN"
echo "Generating icons for ------------------ DARWIN"
echo "Generating icons for ------------------ DARWIN"
echo "Generating icons for ------------------ DARWIN"
# Find all icon="something" refs in Vue templates, include can be {a,list,of,exts}
grep -ohRP 'icon="\K[^"]*' ./src --include=*.{vue,js} >> icons-used.txt
grep -ohRP "icon='\K[^']*" ./src --include=*.{vue,js} >> icons-used.txt
# Find all icon: "something" refs in amorphic data, include can be {a,list,of,exts}
grep -ohRP '"icon":\s"\K[^"]*' ./src --include=*.{json,js,vue} >> icons-used.txt
# Replace underscores with dashes
sed -i'icons-used.bak' -e 's/_/-/g' icons-used.txt
# Remove iconName references, the (new) standard computed/method name for configurable icon components, so this all works.
sed -i'icons-used.bak' -e '/iconName/d' icons-used.txt
sed -i'icons-used.bak' -e '/icon/d' icons-used.txt
sed -i'icons-used.bak' -e '/Icon/d' icons-used.txt
# Remove any lines that contain dots, and therefore were js references
sed -i'icons-used.bak' -e '/\./d' icons-used.txt
# Remove any lines that contain done
sed -i'icons-used.bak' -e '/\done-/d' icons-used.txt
# Remove duplicate lines
# awk '!seen[$0]++' icons-used.txt
# awk -i inplace '!seen[$0]++' icons-used.txt
# sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'
# cat icons-used.txt | sort | uniq -d
# sort -u icons-used.txt > icons-used.txt
ruby icons-remove-duplicates.rb
# Add edge case icons that are included in an even more dynamic way than is worth scripting around
# Add to this manually if you need to brute force in a new icons that isn't showing on the front-end.
echo "chevron-up" >> icons-used.txt
echo "chevron-down" >> icons-used.txt
echo "menu-up" >> icons-used.txt
echo "menu-down" >> icons-used.txt
echo "menu" >> icons-used.txt
# Loop through found used icons and copy them to ./src/used-icons while adding requires to ./src/used-icons/index.js for import.
cat icons-used.txt | while read line
do
# do something with $line here
echo "Adding used icon: $line"
cp ./src/compiled-icons/${line}.js ./src/used-icons
echo "require('./${line}')" >> ./src/used-icons/index.js
done
# Report all vue json icon needs in templates, include can be {a,list,of,exts}
grep -HnRP 'icon="\K[^"]*' ./src --include=*.{vue,js} >> icons-used-report.txt
# Report all vue json icon needs in amorphic data, include can be {a,list,of,exts}
grep -HnRP 'icon:\s"\K[^"]*' ./src --include=*.{json,js,vue} >> icons-used-report.txt
fi
#################################
# Icon Dynamic Imports - END
#################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment