ふるすたっくえんじにあの日記

ASP .NET MVC (C#)、.NET Framework、iOS (Objective-c) アプリ、Androidアプリ (Java)、AWSあたり

【Swift4】【Codable】 CodingKeys書くのだるいよね

とゆーわけで作りました。SwiftLint対応。

SOURCE_DIR="$PROJECT_DIR/Hoge/Models"
DEST_DIR="$PROJECT_DIR/Hoge/ModelExtensions"

find $DEST_DIR -name "*Ex.swift" -type f | while read FILE
do
    rm -f ${FILE}
done

find $SOURCE_DIR -name "*.swift" -type f | while read FILE
do
    NAME=$(basename ${FILE} | sed "s/.swift//g")
    DEST_FILE=$DEST_DIR"/"$NAME"Ex.swift"
    touch $DEST_FILE
    echo "extension $NAME {" >> "$DEST_FILE"
    echo "    enum CodingKeys: String, CodingKey {" >> "$DEST_FILE"

    while read line; do
	if ! echo $line | grep -q //; then
	if echo $line | grep -q let; then
            NAME=$(echo $line | sed -e "s/:.*$//g" -e "s/^.*let //g")
            SNAKED=$(echo $NAME | gsed -r -e 's/^([A-Z])/\L\1\E/' -e 's/([A-Z])/_\L\1\E/g')
            if [ $NAME = $SNAKED ]; then
	        echo "        case $NAME" >> "$DEST_FILE"
	    else
	        echo "        case $NAME = "'"'"$SNAKED"'"' >> "$DEST_FILE"
	    fi
        fi
        fi
    done < ${FILE}

    echo "    }" >> "$DEST_FILE"
    echo "}" >> "$DEST_FILE"
done