mirror of
https://github.com/wnagrodzki/XcodeToolkit.git
synced 2025-05-06 04:51:33 +02:00
Add scripts for generating UIColor definitions from colors defined in asset catalog
This commit is contained in:
parent
103b30bd6c
commit
3cfa18ec60
3 changed files with 71 additions and 0 deletions
27
Colorsets/cp_if_different.sh
Executable file
27
Colorsets/cp_if_different.sh
Executable 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
|
7
Colorsets/generate_uicolor_colorsets.sh
Executable file
7
Colorsets/generate_uicolor_colorsets.sh
Executable 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
37
Colorsets/uicolor_colorsets.py
Executable 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)
|
Loading…
Add table
Reference in a new issue