Skip to content

Instantly share code, notes, and snippets.

@CodeEagle
Last active October 24, 2022 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save CodeEagle/44e4e379e7b93ee08afc76e30c719c25 to your computer and use it in GitHub Desktop.
Save CodeEagle/44e4e379e7b93ee08afc76e30c719c25 to your computer and use it in GitHub Desktop.
Generator for Assets.xcassets , Xcode Run Script
#!/bin/bash
# Generate UIImage extension for images assets, using in Xcode run script
base="$SRCROOT/${TARGET_NAME}"
assertPath="$base/Assets.xcassets"
fileName="$base/ImageResource.swift"
content="import UIKit\nextension UIImage {\n"
prefix="lo_"
DealWithFile() {
entry=$1
IFS='/' read -r -a components <<< "$entry"
for index in "${!components[@]}"
do
local item="${components[index]}"
if [[ $item == *".imageset"* ]]
then
imageName=${item/.imageset/}
seperator=""
if [[ $imageName == *" "* ]]; then
seperator=" "
elif [[ $imageName == *"-"* ]]; then
seperator="-"
elif [[ $imageName == *"_"* ]]; then
seperator="_"
elif [[ $imageName == *"."* ]]; then
seperator="."
fi
echo "seperator $seperator"
IFS="$seperator" read -r -a vnames <<< "$imageName"
echo "vnames $vnames"
cname=""
for kk in "${!vnames[@]}"
do
name="${vnames[kk]}"
if [ "$kk" == 0 ]
then
cname+=$(echo "$name" | awk '{print tolower($0)}')
else
cname+="$(tr '[:lower:]' '[:upper:]' <<< ${name:0:1})${name:1}"
fi
done
echo "cname $cname"
content+=" static var $prefix$cname: UIImage { return UIImage(named: \"$imageName\")! }\n"
fi
done
}
ImageResourceGenerator() {
for entry in $1/*
do
echo "entry: $entry"
if [[ -d $entry ]]
then
IFS='/' read -r -a components <<< "$entry"
LENGTH=${#components[@]} # Get the length.
LAST_POSITION=$((LENGTH - 1)) # Subtract 1 from the length.
lastComponent=${components[${LAST_POSITION}]}
echo "lastComponent: $lastComponent"
if [[ $lastComponent == *"."* ]]
then
echo "deal file $entry"
DealWithFile "$entry"
else
echo "deal dir $entry"
ImageResourceGenerator "$entry"
fi
fi
done
}
ImageResourceGenerator "$assertPath"
content+="}"
echo "// Generate By Code" | tee "$fileName"
echo -e "$content" >> "$fileName"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment