Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
executable file
·
275 lines (238 loc) · 8.42 KB

File metadata and controls

executable file
·
275 lines (238 loc) · 8.42 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#
# Deployment script for building complex GreaseMonkey userscripts.
#
# USAGE:
# ./buildUserscript.sh 'relative/path/to/basefile' 'name_of_basefile' 'relative/output/path'
#
# DESCRIPTION:
# Allows to split your userscript in several smaller files in which
# you can insert block-based commands.
# The script will be parsed and all recognized tags will be processed
# resulting in a single script file usable as GreaseMonkey userscript.
#
# At the moment there are two predefined core functions by default:
#
# (1) Merge-Blocks:
# Use /*%% PATH %%*/ anywhere in your script. This block will be
# replaced by content of the file specified as PATH. These sub files
# might contain another merge blocks as well.
# PATH must be relative to your base file.
#
# (2) Skip-Blocks:
# Use /*<SKIP>foo bar<SKIP>*/ to define a section which will be
# omitted when the output file is created.
#
# You can define additional rules in the config file.
# To do so, you must specify the open tag, the close tag and
# the name of the function, which will process the block.
# Then add the specified function to this file.
#
# NOTE:
# Even if this script was written as deployment system for java scripts,
# its basically not limited to it. Therefore, you can not just merge *.js files
# but any plaint text file like style sheets, html pages, text files or
# whatever you want to merge into your base file.
# Also works under WSL.
#
#######################################################################################
### UTILITIES ###
#######################################################################################
substr_between() {
printf '%s' "${1}" | grep -oP "(?<=${2:-''}).*?(?=${3:-"${2}"})"
}
str_escape() {
local str
for (( i=0; i<${#1}; i++ )); do
str="${str}\\${1:${i}:1}"
done
printf '%s' "${str}"
}
## Prepend a backslash to all given characters
## @param $1 - String to escape
## @param $2 - All characters to escape (case-sensitive)
## @example str_escape_given '<F*OO*~B#AR>' '*~#' outputs <F\*OO\*\~B\#AR>
str_escape_given() {
for (( i=0; i<${#1}; i++ )); do
hit=0
for (( j=0; j<${#2}; j++ )); do
if [[ "${1:${i}:1}" == "${2:${j}:1}" ]]; then
hit=1
break
fi
done
if [[ "${hit}" == 1 ]]; then
str="${str}\\${1:${i}:1}"
else
str=${str}"${1:${i}:1}"
fi
done
printf '%s' "${str}"
}
## Create empty files given as arguments. Folders are created as necessary on-the-fly.
## @param ...$1 - Files to creates
## @return {boolean}
mktouch() {
if [ $# -lt 1 ]; then
echo 'mktouch: Missing argument.'
return 1
fi
for f in "${@}"; do
mkdir -p -- "$(dirname -- "${f}")"
touch -- "${f}"
done
}
read_configs() {
while IFS=$'\n' read -a line; do
configs+=("${line}")
done < "${config_file}"
}
#######################################################################################
### PROCESS FUNCTIONS ###
#######################################################################################
## Function to process operation 'merge'
merge_blocks() {
local output
local tmp
# Replace function for the recursion
replace_blocks() {
local include_path
local content
local placeholder
local line
# build output file
while IFS='' read -r line; do
# search for a merge tag in current line
include_path="$(substr_between "${line}" "$(str_escape "${open_tag}")" "$(str_escape "${close_tag}")")"
if [ -n "${include_path}" ]; then
# replace tag with the content of the file the tag points to
content="$(cat "${include_path}")"
placeholder="${open_tag}${include_path}${close_tag}"
line="${line//"${placeholder}"/"${content}"}"
fi
# append current line to collected output
echo "${line}"
done < "${output_file}"
}
while : ; do
# read current content of the output file and replace all blocks with their actual content
output="$(replace_blocks "${output_file}" "${open_tag}" "${close_tag}")"
# write new content to the output file
echo "${output}" > "${output_file}"
# search for next merge tag
tmp="$(substr_between "${output}" "$(str_escape "${open_tag}")" "$(str_escape "${close_tag}")")"
# stop recursion output file contains no more blocks
if [ -z "${tmp}" ]; then break; fi
done
}
## Function to process operation 'skipBlocks'
remove_skip_blocks() {
open_tag="$(str_escape_given "${open_tag}" '*')"
close_tag="$(str_escape_given "${close_tag}" '*')"
local result="$(perl -0777 -pe "s{\R?${open_tag}.*?${close_tag}}{}sg" "${output_file}")"
echo "${result}" > "${output_file}"
return 0
}
## Function to process operation 'lineComments'
remove_line_comments() {
open_tag="$(str_escape_given "${open_tag}" "\\")"
sed -i "${start_at_line}"',$s,'"${open_tag}"'.*,,' "${output_file}"
return 0
}
## Function to process operation 'blockComments'
remove_block_comments() {
local result="$(perl -0777 -pe "s{\R?${open_tag}.*?${close_tag}}{}sg" "${output_file}")"
echo "${result}" > "${output_file}"
return 0
}
## Function to process operation 'docComments'
remove_doc_comments() {
open_tag="$(str_escape_given "${open_tag}" '*')"
close_tag="$(str_escape_given "${close_tag}" '*')"
local result="$(perl -0777 -pe "s{\R?${open_tag}.*?${close_tag}}{\n}sg" "${output_file}")"
echo "${result}" > "${output_file}"
return 0
}
## Function to process operation 'emptyLines'
remove_blank_lines() {
sed -i '/^[[:space:]]*$/d' "${output_file}"
return 0
}
## Function to process operation 'trimSpaces'
trim_spaces() {
sed -i 's/^[ \t]*//;s/[ \t]*$//' "${output_file}"
return 0
}
#######################################################################################
### BASIC FUNCTIONS ###
#######################################################################################
## Executes all configured block rules
process_rules() {
local configs=()
local isFirstSection=1
local sectionName
# transfer config lines into an array
read_configs
# declare all variables for each config section and execute the function given under key "fnc"
for line in "${configs[@]}"; do
# skip blank lines
if [[ "${line}" =~ ^[[:space:]]*$ ]]; then
continue
# skip comment lines
elif [[ "${line}" =~ ^[[:space:]]*#.*$ ]]; then
continue
# if current line is an key-value-pair
elif [[ "${line}" =~ ^([^=]+)=(.*)$ ]]; then
# execute keyname=value to create a variable
printf -v "${BASH_REMATCH[1]}" '%s' "${BASH_REMATCH[2]}"
# if current line defines a new section
elif [[ "${line}" =~ ^\[(.*)\]$ ]]; then
if [[ "${isFirstSection}" == 0 ]]; then
# execute the latest section
if [[ "${enabled}" == 'true' ]]; then
echo "Now executing: ${sectionName}"
"${fnc}"
fi
fi
sectionName="${BASH_REMATCH[1]}"
isFirstSection=0
# anything else is invalid
else
echo 'buildUserscript.sh: Config file is invalid! Exit.'
exit 1
fi
done
# finally we have to execute the last section as well
if [[ "${enabled}" == 'true' ]]; then
echo "Now executing: ${sectionName}"
"${fnc}"
fi
}
## Entry point
## @param $1 - Relative path to base file
## @param $2 - Relative path to output file
main() {
# store initial dir (to restore it later) and change to the directory with the base file
local _pwd="${PWD}"
cd "$(dirname "${1}")" || return 1
# store path to configs file
config_file="${_pwd}/buildUserscript.cfg"
# generate absolute path to the output file and copy content of the base file to it
local output_file="${_pwd}/${2}"
base_filename=$(basename "${1}")
mktouch "${output_file}"
cat "${base_filename}" > "${output_file}"
# process blocks
process_rules
# restore initial dir and finish task
cd "${_pwd}" || return 1
}
#######################################################################################
### EXECUTION ###
#######################################################################################
main "${@}"
##
## WSL example calls
##
## wsl ./buildUserscript.sh src/NuoFlix/EnhancedNuoFlix/base.js dist/NuoFlix/EnhancedNuoFlix/EnhancedNuoFlix.js
## wsl ./buildUserscript.sh src/FooPage/FooUserscript/base.txt dist/FooPage/FooUserscript/generatedResult.txt
##
Morty Proxy This is a proxified and sanitized view of the page, visit original site.