Last active
July 8, 2016 12:08
-
-
Save gerardroche/cd1dd9cb73b9c84c89f7a0def1ca7779 to your computer and use it in GitHub Desktop.
sublime-generate-package-file-icon-preferences
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -e | |
unset CDPATH | |
unset IFS | |
readonly SOURCE=$(readlink -nf "$0") | |
readonly SOURCE_PATH=$(dirname "$SOURCE") | |
readonly SOURCE_NAME=$(basename "$SOURCE") | |
show_usage() { | |
echo "usage: $0 [-h|--help] [--package] [--syntax]" | |
} | |
icon_preferences_file_content() { | |
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?> | |
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"> | |
<plist version=\"1.0\"> | |
<dict> | |
<key>scope</key> | |
<string>$1</string> | |
<key>settings</key> | |
<dict> | |
<key>icon</key> | |
<string>$2</string> | |
</dict> | |
</dict> | |
</plist>"; | |
} | |
create_icon_file() { | |
scope="$1" | |
file_type="$2" | |
out_file="$OUTPUT_DIR/$3" | |
mkdir -p "$(dirname "out_file")" | |
echo "$(icon_preferences_file_content "$scope" "$file_type")" > "$out_file" | |
echo " created icon preferences file: $3" | |
} | |
lowercase() { | |
echo "$(echo "$1" | tr '[:upper:]' '[:lower:]')" | |
} | |
filter_icon_file_type() { | |
echo "$1"\ | |
| tr '[:punct:]' '_'\ | |
| sed -e 's/ /_/g'\ | |
| sed -e 's/__*/_/g'\ | |
| sed -e 's/_$//' | |
} | |
GEN_PACKAGE_ICONS= | |
GEN_SYNTAX_ICONS= | |
OUTPUT_DIR="$(dirname "$SOURCE_PATH")/dist" | |
while test "$#" != 0; do | |
case "$1" in | |
--help|-h) show_usage; exit 0 ;; | |
--package) GEN_PACKAGE_ICONS=y ;; | |
--syntax) GEN_SYNTAX_ICONS=y ;; | |
# --output) OUTPUT_DIR="$2"; shift ;; | |
--) shift; break ;; # standard end of options list | |
-*) echo >&2 "$0: unknown option '$1'"; exit 1 ;; | |
*) echo >&2 "$0: unknown argument '$1'"; exit 1 ;; | |
esac | |
shift | |
done | |
if test -z "$GEN_PACKAGE_ICONS" && test -z "$GEN_SYNTAX_ICONS"; then | |
show_usage | |
exit 0 | |
fi | |
rm -f "$OUTPUT_DIR"/*/*.tmPreferences | |
mkdir -p "$OUTPUT_DIR" | |
for package in ./*; do | |
test -d "$package" || continue | |
echo "=> package: $package" | |
package_name="$(basename "$package")" | |
package_name_lc="$(lowercase "$package_name")" | |
package_icon_preferences_file="$package_name/Icon.tmPreferences" | |
package_icon_file_type="$(filter_icon_file_type "file_type_${package_name_lc}")" | |
echo " name: $package_name" | |
echo " icon file type: $package_icon_file_type" | |
echo " icon file: $package_icon_preferences_file" | |
package_scope= | |
for syntax_file in "$package"/*.sublime-syntax; do | |
test -f "$syntax_file" || continue | |
is_hidden_syntax="$(grep "^hidden: true" "$syntax_file" &> /dev/null)" | |
if test -n "$is_hidden_syntax"; then | |
echo "=> skipping syntax (hidden): $syntax_file..." | |
continue | |
fi | |
echo " syntax: ${syntax_file}" | |
syntax_name="$(grep "^name: " "${syntax_file}" | sed -e 's/^name\: //')" | |
syntax_name_lc="$(lowercase "$syntax_name")" | |
syntax_scope="$(grep "^scope: " "${syntax_file}" | sed -e 's/^scope\: //')" | |
syntax_icon_preferences_file="${package_name}/Icon ($syntax_name).tmPreferences" | |
syntax_icon_file_type="$(filter_icon_file_type "file_type_${package_name_lc}_${syntax_name_lc}")" | |
echo " name: $syntax_name" | |
echo " scope: $syntax_scope" | |
echo " icon file type: $syntax_icon_file_type" | |
echo " icon file: $syntax_icon_preferences_file" | |
if test "$GEN_SYNTAX_ICONS" = y; then | |
create_icon_file "$syntax_scope" "$syntax_icon_file_type" "$syntax_icon_preferences_file" | |
fi | |
test -z "$package_scope" || package_scope="${package_scope}, " | |
package_scope="${package_scope}${syntax_scope}" | |
# syntax_file_extensions="$(grep "^ - " "$syntax_file" &2> /dev/null)" | |
# syntax_file_extensions="$(echo "$syntax_file_extensions" | sed -e 's/ - //')" | |
# for syntax_file_extension in $syntax_file_extensions; do | |
# echo " extension=$syntax_file_extension" | |
# icon="${icon}_${syntax_file_extension}" | |
# icon_filename="${icon_filename} (${syntax_file_extension})" | |
# echo " icon: $icon" | |
# echo " icon file: $icon_filename" | |
# # icon_preferences_file_content "$scope" "$syntax_icon" | |
# done | |
done | |
echo " scope: $package_scope" | |
test ! -z "$package_scope" || continue | |
if test "$GEN_PACKAGE_ICONS"; then | |
create_icon_file "$package_scope" "$package_icon_file_type" "$package_icon_preferences_file" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment