FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ionic/scripts/utils.inc at master · Java1024/ionic · GitHub
Java1024
ionic
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ionic
/
scripts
/
utils.inc
Copy path
More file actions
More file actions
Latest commit
History
History
History
280 lines (239 loc) · 7.04 KB
Breadcrumbs
ionic
/
scripts
/
utils.inc
Copy path
File metadata and controls
280 lines (239 loc) · 7.04 KB
Raw
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
276
277
278
279
280
#
bash utils from angularjs
#
This file provides:
#
- a default control flow
#
* initializes the environment
#
* able to mock "git push" in your script and in all sub scripts
#
* call a function in your script based on the arguments
#
- named argument parsing and automatic generation of the "usage" for your script
#
- intercepting "git push" in your script and all sub scripts
#
- utility functions
#
#
Usage:
#
- define the variable ARGS_DEF (see below) with the arguments for your script
#
- include this file using `source utils.inc` at the end of your script.
#
#
Default control flow:
#
0. Set the current directory to the directory of the script. By this
#
the script can be called from anywhere.
#
1. Parse the named arguments
#
2. If the parameter "git_push_dryrun" is set, all calls the `git push` in this script
#
or in child scripts will be intercepted so that the `--dry-run` and `--porcelain` is added
#
to show what the push would do but not actually do it.
#
3. If the parameter "verbose" is set, the `-x` flag will be set in bash.
#
4. The function "init" will be called if it exists
#
5. If the parameter "action" is set, it will call the function with the name of that parameter.
#
Otherwise the function "run" will be called.
#
#
Named Argument Parsing:
#
- The variable ARGS_DEF defines the valid command arguments
#
* Required args syntax: --paramName=paramRegex
#
* Optional args syntax: [--paramName=paramRegex]
#
* e.g. ARG_DEFS=("--required_param=(.+)" "[--optional_param=(.+)]")
#
- Checks that:
#
* all arguments match to an entry in ARGS_DEF
#
* all required arguments are present
#
* all arguments match their regex
#
- Afterwards, every paramter value will be stored in a variable
#
with the name of the parameter in upper case (with dash converted to underscore).
#
#
Special arguments that are always available:
#
- "--action=.*": This parameter will be used to dispatch to a function with that name when the
#
script is started
#
- "--git_push_dryrun=true": This will intercept all calls to `git push` in this script
#
or in child scripts so that the `--dry-run` and `--porcelain` is added
#
to show what the push would do but not actually do it.
#
- "--verbose=true": This will set the `-x` flag in bash so that all calls will be logged
#
#
Utility functions:
#
- readJsonProp
#
- replaceJsonProp
#
- resolveDir
#
- getVar
#
- serVar
#
- isFunction
#
always stop on errors
set
-
e
function
usage
{
echo
"Usage:
${0}
${ARG_DEFS[@]}
"
exit
1
}
function
parseArgs
{
local
REQUIRED_ARG_NAMES
=
()
#
-- helper functions
function
varName
{
#
everything to upper case and dash to underscore
echo
${1//-/_}
|
tr
'[:lower:]'
'[:upper:]'
}
function
readArgDefs
{
local
ARG_DEF
local
AD_OPTIONAL
local
AD_NAME
local
AD_RE
#
-- helper functions
function
parseArgDef
{
local
ARG_DEF_REGEX
=
"(\[?)--([^=]+)=(.*)"
if
[[ ! $
1
=
~ $
ARG_DEF_REGEX
]];
then
echo
"Internal error: arg def has wrong format: $ARG_DEF"
exit
1
fi
AD_OPTIONAL
=
"
${BASH_REMATCH[1]}
"
AD_NAME
=
"
${BASH_REMATCH[2]}
"
AD_RE
=
"
${BASH_REMATCH[3]}
"
if
[[ $
AD_OPTIONAL
]];
then
#
Remove last bracket for optional args.
#
Can't put this into the ARG_DEF_REGEX somehow...
AD_RE
=
${AD_RE%?}
fi
}
#
-- run
for
ARG_DEF
in
"
${ARG_DEFS[@]}
"
do
parseArgDef
$
ARG_DEF
local
AD_NAME_UPPER
=
$(
varName
$
AD_NAME
)
setVar
"
${AD_NAME_UPPER}
_OPTIONAL"
"$AD_OPTIONAL"
setVar
"
${AD_NAME_UPPER}
_RE"
"$AD_RE"
if
[[ ! $
AD_OPTIONAL
]];
then
REQUIRED_ARG_NAMES
+=
($
AD_NAME
)
fi
done
}
function
readAndValidateArgs
{
local
ARG_NAME
local
ARG_VALUE
local
ARG_NAME_UPPER
#
-- helper functions
function
parseArg
{
local
ARG_REGEX
=
"--([^=]+)=?(.*)"
if
[[ ! $
1
=
~ $
ARG_REGEX
]];
then
echo
"Can't parse argument $i"
usage
fi
ARG_NAME
=
"
${BASH_REMATCH[1]}
"
ARG_VALUE
=
"
${BASH_REMATCH[2]}
"
ARG_NAME_UPPER
=
$(
varName
$
ARG_NAME
)
}
function
validateArg
{
local
AD_RE
=
$(
getVar
${ARG_NAME_UPPER}_RE
)
if
[[ ! $
AD_RE
]];
then
echo
"Unknown option: $ARG_NAME"
usage
fi
if
[[ ! $
ARG_VALUE
=
~ ^
${AD_RE}
$ ]];
then
echo
"Wrong format: $ARG_NAME"
usage
;
fi
#
validate that the "action" option points to a valid function
if
[[ $
ARG_NAME
==
"action"
]] && !
isFunction
$
ARG_VALUE
;
then
echo
"No action $ARG_VALUE defined in this script"
usage
;
fi
}
#
-- run
for
i
in
"$@"
do
parseArg
$
i
validateArg
setVar
"
${ARG_NAME_UPPER}
"
"$ARG_VALUE"
done
}
function
checkMissingArgs
{
local
ARG_NAME
for
ARG_NAME
in
"
${REQUIRED_ARG_NAMES[@]}
"
do
ARG_VALUE
=
$(
getVar
$(
varName
$
ARG_NAME
))
if
[[ ! $
ARG_VALUE
]];
then
echo
"Missing: $ARG_NAME"
usage
;
fi
done
}
#
-- run
readArgDefs
readAndValidateArgs
"$@"
checkMissingArgs
}
#
getVar(varName)
function
getVar
{
echo
${!1}
}
#
setVar(varName, varValue)
function
setVar
{
eval
"$1=
\"
$2
\"
"
}
#
isFunction(name)
#
- to be used in an if, so return 0 if successful and 1 if not!
function
isFunction
{
if
[[ $(
type
-
t
$
1
)
==
"function"
]];
then
return
0
else
return
1
fi
}
#
readJsonProp(jsonFile, property)
#
- restriction: property needs to be on an own line!
function
readJsonProp
{
echo
$(
sed
-
En
's/.*"'
$
2
'"[ ]*:[ ]*"(.*)".*/\1/p'
$
1
)
}
#
replaceJsonProp(jsonFile, property, newValue)
#
- note: propertyRegex will be automatically placed into a
#
capturing group! -> all other groups start at index 2!
function
replaceJsonProp
{
replaceInFile
$
1
"
\"
$2
\"
:
\"
.*?
\"
"
"
\"
$2
\"
:
\"
$3
\"
"
}
#
replaceInFile(file, findPattern, replacePattern)
function
replaceInFile
{
perl
-
pi
-
e
"s/$2/$3/g;"
$
1
}
#
resolveDir(relativeDir)
#
- resolves a directory relative to the current script
function
resolveDir
{
echo
$(
cd
$
SCRIPT_DIR
;
cd
$
1
;
pwd
)
}
function
git_push_dryrun_proxy
{
echo
"## git push dryrun proxy enabled!"
export
ORIGIN_GIT
=
$(
which
git
)
function
git
{
local
ARGS
=
(
"$@"
)
local
RC
if
[[ $
1
==
"push"
]];
then
ARGS
+=
(
"--dry-run"
"--porcelain"
)
echo
"####### START GIT PUSH DRYRUN #######"
echo
"
${ARGS[@]}
"
fi
if
[[ $
1
==
"commit"
]];
then
echo
"
${ARGS[@]}
"
fi
$
ORIGIN_GIT
"
${ARGS[@]}
"
RC
=
$?
if
[[ $
1
==
"push"
]];
then
echo
"####### END GIT PUSH DRYRUN #######"
fi
return
$
RC
}
export
-
f
git
}
function
main
{
#
normalize the working dir to the directory of the script
cd
$(
dirname
$
0
);
SCRIPT_DIR
=
$(
pwd
)
ARG_DEFS
+=
(
"[--git-push-dryrun=(true|false)]"
"[--verbose=(true|false)]"
)
parseArgs
"$@"
#
--git_push_dryrun argument
if
[[ $
GIT_PUSH_DRYRUN
==
"true"
]];
then
git_push_dryrun_proxy
fi
#
--verbose argument
if
[[ $
VERBOSE
==
"true"
]];
then
set
-
x
fi
if
isFunction
init
;
then
init
"$@"
fi
#
jump to the function denoted by the --action argument,
#
otherwise call the "run" function
if
[[ $
ACTION
]];
then
$
ACTION
"$@"
else
run
"$@"
fi
}
main
"$@"
Back
|
FazBrowse Home
|
New Git URL