Add scripts for generating UIColor definitions from colors defined in asset catalog

This commit is contained in:
Wojciech Nagrodzki 2020-04-20 19:27:31 +02:00
parent 103b30bd6c
commit 3cfa18ec60
Signed by: wnagrodzki
GPG key ID: E9D0EB0302264569
3 changed files with 71 additions and 0 deletions

27
Colorsets/cp_if_different.sh Executable file
View file

@ -0,0 +1,27 @@
#!/bin/zsh
#======================================================================================
# FILE: cp_if_different.sh
#
# USAGE: cp_if_different.sh source_file target_file
#
# DESCRIPTION: Copies the contents of the source_file to the target_file
# if target_file content is different then source_file content.
# Useful for copying autogenerated Swift files to avoid
# recompilations due to changes of file's modification date.
#======================================================================================
SOURCE_LOCATION=$1
DESTINATION_LOCATION=$2
if test -f $DESTINATION_LOCATION; then
if cmp --silent $SOURCE_LOCATION $DESTINATION_LOCATION; then
# files are identical
exit 0
else
# files are different
rm $DESTINATION_LOCATION
fi
fi
cp $SOURCE_LOCATION $DESTINATION_LOCATION

View file

@ -0,0 +1,7 @@
#!/bin/zsh
GENERATED_FILE_LOCATION=`mktemp`
COLORS_FILE_LOCATION="UIColor+Colorsets.swift"
./uicolor_colorsets.py > $GENERATED_FILE_LOCATION
./cp_if_different.sh $GENERATED_FILE_LOCATION $COLORS_FILE_LOCATION

37
Colorsets/uicolor_colorsets.py Executable file
View file

@ -0,0 +1,37 @@
#!/usr/bin/env python3
"""UIColor colorset extension generator
Searches recursively for *.colorset directories and generates Swift code
containing color definitions via UIColor extension.
"""
import os
import pathlib
# find colors
color_names = []
for dirpath, dirnames, filenames in os.walk('.'):
pure_path = pathlib.PurePath(dirpath)
directory_name = pure_path.name
if '.colorset' in directory_name:
color_names.append(pure_path.stem)
# build color definitions
color_definitions = list(map(lambda x: "static let {} = UIColor(named: \"{}\")".format(x, x) , color_names))
# compose file content
file_content = "// ******** Autogenerated File ******** //\n\n"
file_content += "import UIKit\n\n"
file_content += "extension UIColor {\n\n"
for color_definition in color_definitions:
file_content += " " + color_definition + "!\n"
file_content += "}"
print(file_content)