diff --git a/Colorsets/cp_if_different.sh b/Colorsets/cp_if_different.sh new file mode 100755 index 0000000..cf2ca7d --- /dev/null +++ b/Colorsets/cp_if_different.sh @@ -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 diff --git a/Colorsets/generate_uicolor_colorsets.sh b/Colorsets/generate_uicolor_colorsets.sh new file mode 100755 index 0000000..e922bb7 --- /dev/null +++ b/Colorsets/generate_uicolor_colorsets.sh @@ -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 \ No newline at end of file diff --git a/Colorsets/uicolor_colorsets.py b/Colorsets/uicolor_colorsets.py new file mode 100755 index 0000000..83da480 --- /dev/null +++ b/Colorsets/uicolor_colorsets.py @@ -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)