forked from Floflis/nftmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.sh
executable file
·236 lines (185 loc) · 5.78 KB
/
utils.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/bin/bash
GIST_URL='https://gist.githubusercontent.com/germanfr/6f48336523fa8a464362fb4249c63950/raw/utils.sh'
# ======================================
# Auto xlet discovery
# ======================================
json_prop () {
python3 -c "import sys, json; print(json.load(sys.stdin)['$1']);"
}
metadata=$(find . -name 'metadata.json' -print -quit)
get_xlet_meta () {
json_prop $1 < $metadata
}
get_xlet_type () {
file=$(find . -regextype posix-extended -regex ".*/(extension|applet|desklet).js" -print -quit)
echo $file | grep -o -E '(extension|applet|desklet)'
}
UUID=$(get_xlet_meta uuid)
xlet_type=$(get_xlet_type)
if [ -z "$xlet_type" ]; then
echo "Missing xlet Javascript files"
exit 1
fi
# ======================================
# Constants
# ======================================
install_dir="$HOME/.local/share/cinnamon/${xlet_type}s"
zip_name="${UUID}.zip"
COMMON_FILES=(
"$UUID/${xlet_type}.js"
"$UUID/metadata.json"
"$UUID/settings-schema.json"
"$UUID/po/"
"$UUID/icons/"
"$UUID/icon.png"
"$UUID/stylesheet.css"
$(find * -mindepth 2 -regextype posix-extended -regex ".*[1-9][0-9]*\.[0-9]+(\.[0-9]+)?/.*") # Files inside version folders
)
# Load options (files that will be packaged)
. ./pkg-options
# ======================================
# Operations
# ======================================
symlink_xlet () {
mkdir -p "$install_dir"
rm -rf "$install_dir/$UUID"
ln -rfs "$UUID" "$install_dir/"
}
install_theme () {
if [ "$2" == "dev" ]; then
symlink_xlet
else
spices_package &> /dev/null
mkdir -p "$install_dir"
rm -rf "$install_dir/$UUID"
unzip "$zip_name" -d "$install_dir"
rm "$zip_name"
fi
cd "$UUID"
cinnamon-json-makepot -i
echo "${xlet_type^} installed into $install_dir"
}
spices_package () {
rm -f "$zip_name"
local all_files="${COMMON_FILES[@]} ${PACKAGE_FILES[@]/#/${UUID}\/}"
# Remove duplcates
all_files=$(echo $all_files | tr ' ' '\n' | sort -u)
zip -r --symlinks "$zip_name" $all_files
for ef in "${EXTRA_FILES[@]}"; do
local filename=$(basename "$ef")
ln -rfs "$ef" "$UUID/$filename"
zip -r "$zip_name" "$UUID/$filename"
rm -rf "$UUID/$filename"
done
echo "Files compressed into $zip_name"
}
simplify_assets () {
simplify () {
scour -i "$1" -o "$2"\
--remove-metadata \
--enable-id-stripping \
--protect-ids-noninkscape \
--disable-simplify-colors
}
# Usage: print_progress PROGRESS TOTAL
print_progress () {
local n_cols=$(($(tput cols)-7))
local cols_completed=$(($1*n_cols/$2))
local percent_completed=$(($1*100/$2))
echo -n "$percent_completed% "
for ((i=0; i<$cols_completed; i++)) {
echo -n '#'
}
}
if type scour &> /dev/null ; then
if [ -f "$2" ]; then
local assets_list="$2"
else
if [[ -z "$ASSETS_DIR" ]]; then
echo "No assets folder in this $xlet_type"
exit 1
fi
cd "$UUID/"
local assets_list=$(find "$ASSETS_DIR/" -name '*.svg' -type f)
fi
# temp dir for the output (can't output to self)
local tmp_dir=$(mktemp -d)
local n_assets=$(echo "$assets_list" | wc -l)
local completed=0
for res in $assets_list ; do
echo -e "> Simplifying \e[34m$(basename $res)\e[0m"
print_progress $completed $n_assets
output=$(simplify "$res" "$tmp_dir/out.svg")
mv "$tmp_dir/out.svg" "$res"
echo -en '\033[2K\r' # clear old progress bar
echo " $output"
((completed=completed+1))
done
echo 'Simplify assets task finished'
else
echo 'scour not found'
fi
}
submit_to_spices () {
local url=$(get_xlet_meta url)
local name=$(get_xlet_meta name)
local version=$(get_xlet_meta version)
spices_package &> /dev/null
mv "$zip_name" "../spices-${xlet_type}s/$UUID/"
cd "../spices-${xlet_type}s/$UUID/"
git checkout master
git pull linuxmint master
git branch -D "$UUID"
git checkout -b "$UUID"
rm -rf files/*
unzip "$zip_name" -d files/ > /dev/null
rm "$zip_name"
git add files/
git status
git commit -m "Update $name v$version ($UUID)
Changelog at: $url/commits/master"
echo -n 'Push changes (git push)? [y/n] '
read confirm
if [[ $confirm =~ ^([yY]([eE][sS])?)?$ ]]
then git push -fu origin $UUID
fi
}
update_self () {
wget -O $(basename $0) $GIST_URL
}
show_help () {
local bold=$(tput bold)
local normal=$(tput sgr0)
local name=$(get_xlet_meta name)
echo "\
${bold}${name^^} ${xlet_type^^} HELP${normal}
Usage: ./$(basename $0) [COMMAND]
${bold}COMMANDS${normal}
install [dev] Install the theme into the system.
Add 'dev' to just create a symbolic link to here.
help Show help.
${bold}DEVELOPMENT COMMANDS${normal}
pkg Package files ready to be uploaded to the Cinnamon Spices.
simplify Optimize SVG assets for a smaller size and a better theme
performance stripping metadata and other stuff.
update Fetch the latest version available of this script.
"
}
# ======================================
# Start point
# ======================================
declare -A operations
operations[install]=install_theme
operations[help]=show_help
operations[pkg]=spices_package
operations[simplify]=simplify_assets
operations[submit]=submit_to_spices # Obscure
operations[update]=update_self
opname="$1"
opfunc="${operations[$opname]}"
if [[ -n "$opfunc" ]]
then $opfunc "$@"
else
echo "$opname: command not found"
show_help
fi