Skip to content

Instantly share code, notes, and snippets.

@Git-I985
Last active September 29, 2024 20:11
Show Gist options
  • Save Git-I985/9b86358f2525cc3d0a1113ca2b9ffc03 to your computer and use it in GitHub Desktop.
Save Git-I985/9b86358f2525cc3d0a1113ca2b9ffc03 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
input_folder="./figma_export"
temp_export_folder="$(pwd)/temp_export"
export_zip_file_name="assets-icons-$(date +"%d-%m-%Y").zip"
assets_list_path=""
printf '\n'
echo 'Processing figma exported assets icons for AWS upload'
printf '\n'
if [ -d "$1" ]; then
input_folder=$(readlink -f $1)
elif [ -n "$1" ]; then
echo "Invalid input directory path $1"
exit 1
fi
if [ -f "$2" ]; then
assets_list_path=$(readlink -f $2)
elif [ -n "$2" ]; then
echo "Invalid assets list file path $2"
exit 1
fi
rm -rf "$temp_export_folder"
mkdir -p "$temp_export_folder"
if [ -n "$assets_list_path" ]; then
printf "Assets list file (%s) provided\n\n" "$assets_list_path"
else
printf "No assets list file provided, processing all *.svg files in %s\n\n" "$input_folder"
fi
last_two_parts() {
local full_path="$1"
echo "$full_path" | awk -F'/' '{print "../"$(NF-1)"/"$NF}'
}
last_three_parts() {
local full_path="$1"
echo "$full_path" | awk -F'/' '{print "../"$(NF-2)"/"$(NF-1)"/"$NF}'
}
update_svg_attributes() {
local svg_file="$1"
# Update width, height to 60 and set viewBox
sed -i.bak -E '1s/(width|height)="[^"]*"/\1="60"/g' "$svg_file"
# Optionally, remove the backup file created by sed (if needed)
rm -f "${svg_file}.bak"
}
printf "%-15s %-50s %-50s %s\n" "Asset" "Source" "Dest" "Dimensions before resize"
echo "--------------------------------------------------------------------------------------------------------------------------------"
for file in "$input_folder"/*.svg; do
asset=$(basename "$file" '.svg' | tr '+' '/');
if [ -n "$assets_list_path" ] && ! grep -q "$asset" "$assets_list_path"; then
continue
fi
dirname=$(echo $asset | tr '/' '_')
dest="$temp_export_folder/$dirname/default.svg"
mkdir "$(dirname $dest)"
cp $file $dest
dimensions=$(grep -m 1 '<svg.*width.*height' $dest | sed -E 's/.*width="([^"]+)".*height="([^"]+)".*/\1x\2/')
update_svg_attributes "$dest"
printf "%-15s %-50s %-50s %s\n" "$asset" "$(last_two_parts "$file")" "$(last_three_parts "$dest")" $dimensions
done
printf '\n'
if ! command -v zip > /dev/null; then
echo "Cannot zip, no zip command"
exit 1
fi
echo 'Zip...'
if [ -f "$export_zip_file_name" ]; then
echo "Zip $(pwd)/$export_zip_file_name already exist, it will be updated..."
rm -rf "$export_zip_file_name"
fi
cd "$temp_export_folder"
if zip -r -q "./../$export_zip_file_name" "."; then
cd ..
rm -rf "$temp_export_folder"
echo "Zip file created at $(pwd)/$export_zip_file_name"
if command -v open &> /dev/null; then
echo "Opening zip location..."
open -R "$export_zip_file_name"
fi
else
echo "Error: failed create zip $export_zip_file_name"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment