Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/pacman/bump_version.sh
1036 views
1
#!/bin/bash
2
3
#region globalvars
4
option=$1
5
abort=false
6
currentVersion=''
7
newVersion=''
8
currentDateTime=$(date +"%Y-%m-%d %T")
9
currentDate=$(date +"%Y-%m-%d")
10
11
#region functions
12
13
function showHelp {
14
echo 'Parameter invalid / missing.'
15
echo ''
16
echo 'usage:'
17
echo ''
18
echo ' bump_version [patch|minor|major]'
19
echo ''
20
}
21
22
function checkParameters {
23
# check if parameter provided
24
if [ -z $option ];
25
then
26
showHelp
27
abort=true
28
else
29
case $option in
30
patch)
31
# echo "patch"
32
;;
33
minor)
34
# echo "minor"
35
;;
36
major)
37
# echo "major"
38
;;
39
*)
40
showHelp
41
abort=true
42
return
43
;;
44
esac
45
echo "bump ${option} version"
46
fi
47
}
48
49
# use package.json as the source of truth
50
function read_current_version {
51
currentVersion=$(echo "$var1" | (grep version package.json | sed 's/.*"version": "\(.*\)".*/\1/'))
52
echo "currentVersion=${currentVersion}"
53
}
54
55
function get_new_version {
56
local currentMajor=$(echo "$var1" | (echo "${currentVersion}" | awk -F'.' '{print $1}'))
57
local currentMinor=$(echo "$var1" | (echo "${currentVersion}" | awk -F'.' '{print $2}'))
58
local currentPatch=$(echo "$var1" | (echo "${currentVersion}" | awk -F'.' '{print $3}'))
59
60
echo "${currentMajor} ${currentMinor} ${currentPatch}"
61
case $option in
62
patch)
63
let "newPatch = $currentPatch + 1"
64
newVersion="${currentMajor}.${currentMinor}.${newPatch}"
65
echo "newVersion=${newVersion}"
66
;;
67
minor)
68
let "newMinor = $currentMinor + 1"
69
newVersion="${currentMajor}.${newMinor}.0"
70
echo "newVersion=${newVersion}"
71
;;
72
major)
73
let "newMajor = $currentMajor + 1"
74
newVersion="${newMajor}.0.0"
75
echo "newVersion=${newVersion}"
76
;;
77
esac
78
}
79
80
function search_replace_in_file {
81
local file=$1
82
local search=$2
83
local replace=$3
84
85
sed -i "" "s/${search}/${replace}/g" $file
86
}
87
88
function write_new_version {
89
90
echo ""
91
echo "Update files to ${newVersion} (${currentDateTime}):"
92
93
echo " package.json"
94
local line="\"version\": \".*\""
95
local rep="\"version\": \"${newVersion}\""
96
sed -i "" "s/${line}/${rep}/g" package.json
97
98
# in package-lock.json we only want to replace the very first occurence
99
echo " package-lock.json"
100
sed -e "/${line}/{s//${rep}/;:a" -e '$!N;$!ba' -e '}' package-lock.json > tmp && mv tmp package-lock.json
101
102
103
# TODO: update last_update
104
echo " web-app-manifest.json"
105
sed -i "" "s/${line}/${rep}/g" web-app-manifest.json
106
107
# TODO: update last_update
108
echo " manifest.json"
109
sed -i "" "s/${line}/${rep}/g" manifest.json
110
111
# TODO: update last_update
112
echo " pacman-canvas.webapp"
113
sed -i "" "s/${line}/${rep}/g" pacman-canvas.webapp
114
115
echo " cache.manifest"
116
local line="# Pacman .*"
117
local rep="# Pacman ${newVersion} ${currentDateTime}"
118
sed -i "" "s/${line}/${rep}/g" cache.manifest
119
120
echo " index.htm"
121
local line="<span class=\"app-version\">.*<\/span>"
122
local rep="<span class=\"app-version\">${newVersion} (${currentDate})<\/span>"
123
sed -i "" "s/${line}/${rep}/g" index.htm
124
125
echo ""
126
echo "All files up to date. New Version ${newVersion} (${currentDateTime})."
127
echo "Don't forget to update README.md and to set the git tag."
128
}
129
130
function run {
131
# local option="${1}"
132
133
echo "run ${option}"
134
135
checkParameters
136
137
if [[ ${abort} == true ]];
138
then
139
return
140
fi
141
142
echo "bump version $option"
143
144
read_current_version
145
get_new_version
146
147
# TODO: user confirmation
148
149
write_new_version
150
}
151
152
#endregion
153
154
#region main
155
156
run
157
158
#endregion
159