-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace-text
executable file
·117 lines (90 loc) · 3.09 KB
/
replace-text
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
#!/bin/bash
USAGE="Replaces some text and pushes that to master if requested in all repos. Meta variable REPO can be used for repository name.\n"
USAGE+="Usage: albert $(basename "$0") [ -s | -y | -d ] file \"search_string\" \"new_text\" \"commit_message\"\n"
USAGE+="Example: albert $(basename "$0") Jenkinsfile \"input 'Continue?'\" \"timeout(time: 1, unit: 'HOURS') { input 'Continue?' }\" \"Add timeout for Jenkins approval stages\"\n"
USAGE+="-s : Execute 'sbt clean test' for every repo which is changed\n"
USAGE+="-y : Execute 'yarn install && gulp test' for every repo which is changed\n"
USAGE+="-d : Dry run, changes aren't committed and pushed"
MY_DIR="$(dirname "$0")"
source "$MY_DIR/util"
# A POSIX variable
OPTIND=2 # Reset in case getopts has been used previously in the shell. Value is 2 because this is a sub-script.
# TODO: Set OPTIND to 1 when this script is called directly
YARN=0
SBT=0
DRY_RUN=0
while getopts "syd:" opt; do
case "$opt" in
y) YARN=1;
;;
s) SBT=1
;;
d) DRY_RUN=1;OPTIND=3;
;;
*) echo "Invalid option"
exit 1
;;
esac
done
shift $((OPTIND-1))
[[ $# != 4 ]] && { echo -e "$USAGE"; exit 1; } # Show usage when argument count is wrong
FILE="$1"
SEARCH_TEXT="$2"
NEW_TEXT="$3"
COMMIT_MSG="$4"
check_access_token
replace_text(){
SEPARATOR="~"
# Use another separator if input strings contain default separator
if [[ "$1$2" = *"$SEPARATOR"* ]]; then
SEPARATOR="%"
fi
gsed -i "s$SEPARATOR$1$SEPARATOR$2$SEPARATOR" $FILE
}
REPO_NAMES=$(get_repos)
for REPO in $REPO_NAMES; do
if [[ $REPO == "albert" ]]; then
continue
fi
echo "$(tput bold)=====$REPO=====$(tput sgr0)"
cd "$ROOT_DIR" || root_dir_not_found
[ -d "$REPO" ] || git clone [email protected]:AutoScout24/$REPO.git
cd "$REPO"
git_refresh
LOCAL_SEARCH_TEXT="${SEARCH_TEXT//REPO/$REPO}"
if grep -q "$LOCAL_SEARCH_TEXT" $FILE 2> /dev/null ; then
LOCAL_NEW_TEXT="${NEW_TEXT//REPO/$REPO}"
replace_text "$LOCAL_SEARCH_TEXT" "$LOCAL_NEW_TEXT"
if [[ $SBT == 1 ]] && [ -f build.sbt ]; then
if [ -f ./sbt ]; then
./sbt clean test
else
sbt clean test
fi
fi
if [[ $YARN == 1 ]] && [ -f package.json ]; then
yarn install
fi
git diff
read -p "Should i commit and create a PR? [y/n] " -n 1 -r; echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
git_commit_and_create_pr "$COMMIT_MSG" "$REPO" $DRY_RUN
else
git stash
read -p "Should i open the original file with vim? [y/n] " -n 1 -r; echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
vim $FILE
git diff
read -p "Should i commit and create a PR? [y/n]" -n 1 -r; echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
git_commit_and_create_pr "$COMMIT_MSG" "$REPO" $DRY_RUN
else
git stash
fi
else
git stash
fi
fi
fi
continue
done;