-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoUpdateBatch.bat
202 lines (157 loc) · 5.83 KB
/
AutoUpdateBatch.bat
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
@echo off
setlocal enabledelayedexpansion
:: Set THIS_VERSION to the version of this batch file script
set THIS_VERSION=2.0.05
:: Set SCRIPT_NAME to the name of this batch file script
set SCRIPT_NAME=Auto Update Testing
:: Set GH_USER_NAME to your GitHub username here
set GH_USER_NAME=KSanders7070
:: Set GH_REPO_NAME to your GitHub repository name here
set GH_REPO_NAME=AUTO_UPDATE_BATCH_FILE
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
TITLE !SCRIPT_NAME! (v!THIS_VERSION!)
:SetUpTempDir
:: Setting up the Temp Directory
CD /D "%temp%"
IF exist "!GH_REPO_NAME!-UDPATE" RD /S /Q "!GH_REPO_NAME!-UDPATE"
MD "!GH_REPO_NAME!-UDPATE"
CD /D "!GH_REPO_NAME!-UDPATE"
:GetLatestVerNum
:: URL to fetch JSON data from GitHub API
set "GH_LATEST_RLS_PAGE=https://api.github.com/repos/!GH_USER_NAME!/!GH_REPO_NAME!/releases/latest"
set "URL_TO_DOWNLOAD=!GH_LATEST_RLS_PAGE!"
set "LATEST_VERSION="
:RedirectLooop
if exist response.json del /Q response.json
:: Use CURL to download the JSON data
curl -s -o response.json !URL_TO_DOWNLOAD!
if not exist "response.json" (
ECHO.
ECHO.
ECHO -------
ECHO ERROR
ECHO -------
ECHO.
ECHO Something went wrong with downloading the latest release information.
ECHO.
ECHO.
ECHO Press any key to continue with this version of the batch file or
ECHO just close this window...
ECHO NOTE-I will open the releases page for you to see if there is a newer version.
PAUSE>NUL
START "" "!GH_LATEST_RLS_PAGE!"
GOTO UpdateCleanUp
)
::Check if "exceeded" is present in the JSON, if so it likely means the API Call limit has been reached.
findstr /C:"exceeded" response.json
if "%errorlevel%"=="0" (
ECHO.
ECHO.
ECHO -------
ECHO ERROR
ECHO -------
ECHO.
ECHO While trying to get the latest version number for this batch file from GitHub,
ECHO I found that the number of requests has been exceeded.
ECHO You can try again in a while.
ECHO.
ECHO.
ECHO Press any key to continue with this version of the batch file or
ECHO just close this window...
ECHO NOTE-I will open the releases page for you to see if there is a newer version.
PAUSE>NUL
START "" "!GH_LATEST_RLS_PAGE!"
GOTO UpdateCleanUp
)
:: Check if "tag_name" is present in the JSON.
findstr /C:"tag_name" response.json
if "%errorlevel%"=="0" (
:: tag_name Found in file which means there was no redirect or this is the final redirect
:: page and has the version number on it.
:: Extract the text between the second set of quotes and remove the first character (usually a lower case v).
for /f "tokens=2 delims=:" %%a in ('findstr /C:"tag_name" response.json') do (
set "LATEST_VERSION=%%~a"
set "LATEST_VERSION=!LATEST_VERSION:~3,-2!"
)
) else (
:: tag_name was not found which means that this is likely a redirect page.
:: Extract the line that has "https://api." and grab the URL between the second set of quotes.
:: Feed this URL back through the loop to see if this one redirects too. If it does, keep following until tag_name is found.
for /f "tokens=1,* delims=" %%a in ('findstr /C:"https://api." response.json') do (
set "URL_TO_DOWNLOAD=%%~a"
set "URL_TO_DOWNLOAD=!URL_TO_DOWNLOAD:~10,-2!"
)
goto RedirectLooop
)
:DoYouHaveLatest
:: If the current version matches the latest version available, contine on with normal code.
if /i "!THIS_VERSION!"=="!LATEST_VERSION!" goto UpdateCleanUp
:UpdateAvailablePrompt
cls
ECHO.
ECHO.
ECHO * * * * * * * * * * * * *
ECHO UPDATE AVAILABLE
ECHO * * * * * * * * * * * * *
ECHO.
ECHO.
ECHO GITHUB VERSION: !LATEST_VERSION!
ECHO YOUR VERSION: !THIS_VERSION!
ECHO.
ECHO.
ECHO.
ECHO CHOICES:
ECHO.
ECHO U - MANUALLY DOWNLOAD THE NEWEST BATCH FILE UPDATE AND USE THAT FILE.
ECHO.
ECHO C - CONTINUE USING THIS FILE.
ECHO.
ECHO.
ECHO.
SET UPDATE_CHOICE=NO_CHOICE_MADE
SET /p UPDATE_CHOICE=Please type either M, or C and press Enter:
if /I %UPDATE_CHOICE%==U GOTO UPDATE
if /I %UPDATE_CHOICE%==C GOTO UpdateCleanUp
goto UpdateAvailablePrompt
:UPDATE
set GH_LATEST_RLS_PAGE=https://github.com/!GH_USER_NAME!/!GH_REPO_NAME!/releases/latest
CLS
START "" "!GH_LATEST_RLS_PAGE!"
ECHO.
ECHO.
ECHO GO TO THE FOLLOWING WEBSITE, DOWNLOAD AND USE THE LATEST VERSION OF %~nx0
ECHO.
ECHO !GH_LATEST_RLS_PAGE!
ECHO.
ECHO Press any key to exit...
pause>nul
exit
:UpdateCleanUp
cls
CD /D "%temp%"
IF exist "!GH_REPO_NAME!-UDPATE" RD /S /Q "!GH_REPO_NAME!-UDPATE"
:: Ensures the directory is back to where this batch file is hosted.
CD /D "%~dp0"
:RestOfCode
CLS
:: Here is where your normal code will go after the update process is done.
ECHO.
ECHO.
ECHO VERSION CHECK COMPLETE, Do some other stuff now.
ECHO.
ECHO.
pause
EXIT