-
Notifications
You must be signed in to change notification settings - Fork 11
/
string_ops.sh
executable file
·171 lines (142 loc) · 3.63 KB
/
string_ops.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
#!/bin/bash
# Azurra string_ops v 0.1
# Run string modification ops on all theme folders
# WARNING: use with caution!
# PROGRAM VARS
OP='' # call appropriate string op when iterating on folders
VAL='' # used in search, append and replace ops
NEW_VAL='' # used in replace ops
# HELP
ops() {
echo "Available options:"
echo " -fc --find-in-colors <VALUE>"
echo " -fw --find-in-widgets <VALUE>"
echo " -rg --replace-in-gtk-files <VALUE> <NEW_VALUE>"
echo " -ri --replace-in-imports <VALUE> <NEW_VALUE>"
echo " -rw --replace-in-widgets <VALUE> <NEW_VALUE>"
echo " -ac --append-to-conf-file <VALUE>"
}
help() {
echo "Azurra string ops v0.1"
echo "Run string modification ops on all theme folders"
echo ""
ops
}
# UTILS
# $1: target $2: new value $3: filename
replace() {
sed -i "s@$1@$2@g" "$3"
}
# $1: target $2: filename
file_contains() {
result=$(grep "$1" "$2")
# 0 is OK, 1 is error
[[ ! -z $result ]] && return 0 || return 1
}
# iterate on all theme folders
run() {
for dir in *; do
if [ -f "$dir/theme.conf" ] # if has valid configuration
then
$OP "$dir" "$VAL" "$NEW_VAL"
elif [ -f "$dir/bundle.conf" ] # if is a bundle directory
then
for bundle_dir in "$dir"/*
do
if [ -f "$bundle_dir/theme.conf" ] # process bundled themes
then
$OP "$bundle_dir" "$VAL" "$NEW_VAL"
fi
done
fi
done
}
# OPERATIONS
colors_contains() {
local theme_dir="$1"
local value="$2"
if file_contains "$value" "$theme_dir/_colors.scss"; then
echo "Match in folder $theme_dir"
fi
}
widgets_contains() {
local theme_dir="$1"
local value="$2"
local hits=0
for FILE in "$theme_dir/widgets/"*; do
if [ ! -f "$FILE" ]; then continue; fi
if file_contains "$value" "$FILE"; then
echo "Match in file $FILE"
hits=$((hits+1))
fi
done
[ $hits -le 0 ] && echo "No matches found"
}
gtk_replace() {
local theme_dir="$1"
local value="$2"
local new_value="$3"
# replace in gtk*.scss files
[ -f "$theme_dir/gtk.scss" ] && replace "$value" "$new_value" "$theme_dir/gtk.scss"
[ -f "$theme_dir/gtk-light.scss" ] && replace "$value" "$new_value" "$theme_dir/gtk-light.scss"
[ -f "$theme_dir/gtk-dark.scss" ] && replace "$value" "$new_value" "$theme_dir/gtk-dark.scss"
echo "Replaced value in gtk*.scss files (if found)"
}
imports_replace() {
local theme_dir="$1"
local value="$2"
local new_value="$3"
replace "$value" "$new_value" "$theme_dir/_imports.scss"
echo "Replaced value in '$theme_dir/_imports.scss' (if found)"
}
widgets_replace() {
local theme_dir="$1"
local value="$2"
local new_value="$3"
# replace 'value' by 'new_value'
for FILE in "$theme_dir"/widgets/*.scss; do
[ -f $FILE ] && replace "$value" "$new_value" $FILE && echo "Replacing value in $FILE (if found)"
done
}
config_append() {
local theme_dir="$1"
local string="$2"
echo "$string" >> "$theme_dir/theme.conf"
}
OP="$!"
VAL="$2"
NEW_VAL="$3"
case $1 in
-fc|--find-in-colors)
OP="colors_contains"
;;
-fw|--find-in-widgets)
OP="widgets_contains"
;;
-rg|--replace-in-gtk-files)
OP="gtk_replace"
;;
-ri|--replace-in-imports)
OP="imports_replace"
;;
-rw|--replace-in-widgets)
OP="widgets_replace"
;;
-ac|--append-to-conf-file)
OP="config_append"
;;
*)
echo -n "Invalid operation. "
ops
exit 1
;;
esac
# warn user of potential catastrophic consequences
read -p "Files will be altered. Continue? (y/n): " -n 1 -r
echo "" # new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
run
else
echo "Operation aborted."
fi