Codechange: [AzurePipeline] add developer files section in manifest
[openttd-github.git] / projects / determineversion.vbs
bloba85a085f29955e964b986ffc07bb8cfbd69d31eb
1 Option Explicit
3 ' This file is part of OpenTTD.
4 ' OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
5 ' OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
6 ' See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 Dim FSO
9 Set FSO = CreateObject("Scripting.FileSystemObject")
11 Sub FindReplaceInFile(filename, to_find, replacement)
12 Dim file, data
13 Set file = FSO.OpenTextFile(filename, 1, 0, 0)
14 data = file.ReadAll
15 file.Close
16 data = Replace(data, to_find, replacement)
17 Set file = FSO.CreateTextFile(filename, -1, 0)
18 file.Write data
19 file.Close
20 End Sub
22 Sub UpdateFile(modified, isodate, version, cur_date, githash, istag, isstabletag, filename)
23 FSO.CopyFile filename & ".in", filename
24 FindReplaceInFile filename, "!!MODIFIED!!", modified
25 FindReplaceInFile filename, "!!ISODATE!!", isodate
26 FindReplaceInFile filename, "!!VERSION!!", version
27 FindReplaceInFile filename, "!!DATE!!", cur_date
28 FindReplaceInFile filename, "!!GITHASH!!", githash
29 FindReplaceInFile filename, "!!ISTAG!!", istag
30 FindReplaceInFile filename, "!!ISSTABLETAG!!", isstabletag
31 End Sub
33 Sub UpdateFiles(version)
34 Dim modified, isodate, cur_date, githash, istag, isstabletag
35 cur_date = DatePart("D", Date) & "." & DatePart("M", Date) & "." & DatePart("YYYY", Date)
37 If InStr(version, Chr(9)) Then
38 ' Split string into field with tails
39 isodate = Mid(version, InStr(version, Chr(9)) + 1)
40 modified = Mid(isodate, InStr(isodate, Chr(9)) + 1)
41 githash = Mid(modified, InStr(modified, Chr(9)) + 1)
42 istag = Mid(githash, InStr(githash, Chr(9)) + 1)
43 isstabletag = Mid(istag, InStr(istag, Chr(9)) + 1)
44 ' Remove tails from fields
45 version = Mid(version, 1, InStr(version, Chr(9)) - 1)
46 isodate = Mid(isodate, 1, InStr(isodate, Chr(9)) - 1)
47 modified = Mid(modified, 1, InStr(modified, Chr(9)) - 1)
48 githash = Mid(githash, 1, InStr(githash, Chr(9)) - 1)
49 istag = Mid(istag, 1, InStr(istag, Chr(9)) - 1)
50 Else
51 isodate = 0
52 modified = 1
53 githash = ""
54 istag = 0
55 isstabletag = 0
56 End If
58 UpdateFile modified, isodate, version, cur_date, githash, istag, isstabletag, "../src/rev.cpp"
59 UpdateFile modified, isodate, version, cur_date, githash, istag, isstabletag, "../src/os/windows/ottdres.rc"
60 End Sub
62 Function DetermineVersion()
63 Dim WshShell, branch, tag, modified, isodate, oExec, line, hash, shorthash
64 Set WshShell = CreateObject("WScript.Shell")
65 On Error Resume Next
67 modified = 0
68 hash = ""
69 shorthash = ""
70 branch = ""
71 isodate = ""
72 tag = ""
74 ' Set the environment to english
75 WshShell.Environment("PROCESS")("LANG") = "en"
77 Set oExec = WshShell.Exec("git rev-parse --verify HEAD")
78 If Err.Number = 0 Then
79 ' Wait till the application is finished ...
80 Do While oExec.Status = 0
81 Loop
83 If oExec.ExitCode = 0 Then
84 hash = oExec.StdOut.ReadLine()
85 shorthash = Mid(hash, 1, 10)
86 ' Make sure index is in sync with disk
87 Set oExec = WshShell.Exec("git update-index --refresh")
88 If Err.Number = 0 Then
89 ' StdOut and StdErr share a 4kB buffer so prevent it from filling up as we don't care about the output
90 oExec.StdOut.Close
91 oExec.StdErr.Close
92 ' Wait till the application is finished ...
93 Do While oExec.Status = 0
94 WScript.Sleep 10
95 Loop
96 End If
97 Set oExec = WshShell.Exec("git diff-index --exit-code --quiet HEAD ../")
98 If Err.Number = 0 Then
99 ' Wait till the application is finished ...
100 Do While oExec.Status = 0
101 Loop
103 If oExec.ExitCode = 1 Then
104 modified = 2
105 End If ' oExec.ExitCode = 1
107 Set oExec = WshShell.Exec("git show -s --pretty=format:%ci")
108 if Err.Number = 0 Then
109 isodate = Mid(oExec.StdOut.ReadLine(), 1, 10)
110 isodate = Replace(isodate, "-", "")
111 End If ' Err.Number = 0
113 ' Check branch
114 Err.Clear
115 Set oExec = WshShell.Exec("git symbolic-ref HEAD")
116 If Err.Number = 0 Then
117 line = oExec.StdOut.ReadLine()
118 branch = Mid(line, InStrRev(line, "/") + 1)
119 End If ' Err.Number = 0
121 ' Check if a tag is currently checked out
122 Err.Clear
123 Set oExec = WshShell.Exec("git name-rev --name-only --tags --no-undefined HEAD")
124 If Err.Number = 0 Then
125 ' Wait till the application is finished ...
126 Do While oExec.Status = 0
127 Loop
128 If oExec.ExitCode = 0 Then
129 tag = oExec.StdOut.ReadLine()
130 If Right(tag, 2) = "^0" Then
131 tag = Left(tag, Len(tag) - 2)
132 End If
133 End If ' oExec.ExitCode = 0
134 End If ' Err.Number = 0
135 End If ' Err.Number = 0
136 End If ' oExec.ExitCode = 0
137 End If ' Err.Number = 0
139 If hash = "" And FSO.FileExists("../.ottdrev") Then
140 Dim rev_file
141 Set rev_file = FSO.OpenTextFile("../.ottdrev", 1, True, 0)
142 DetermineVersion = rev_file.ReadLine()
143 rev_file.Close()
144 ElseIf hash = "" Then
145 DetermineVersion = "norev000"
146 modified = 1
147 Else
148 Dim version, hashprefix, istag, isstabletag
149 If modified = 0 Then
150 hashprefix = "-g"
151 ElseIf modified = 2 Then
152 hashprefix = "-m"
153 Else
154 hashprefix = "-u"
155 End If
157 If tag <> "" Then
158 version = tag
159 istag = 1
161 Set stable_regexp = New RegExp
162 stable_regexp.Pattern = "^[0-9.]*$"
163 If stable_regexp.Test(tag) Then
164 isstabletag = 1
165 Else
166 isstabletag = 0
167 End If
168 Else
169 version = isodate & "-" & branch & hashprefix & shorthash
170 istag = 0
171 isstabletag = 0
172 End If
174 DetermineVersion = version & Chr(9) & isodate & Chr(9) & modified & Chr(9) & hash & Chr(9) & istag & Chr(9) & isstabletag
175 End If
176 End Function
178 Function IsCachedVersion(ByVal version)
179 Dim cache_file, cached_version
180 cached_version = ""
181 Set cache_file = FSO.OpenTextFile("../config.cache.version", 1, True, 0)
182 If Not cache_file.atEndOfStream Then
183 cached_version = cache_file.ReadLine()
184 End If
185 cache_file.Close
187 If InStr(version, Chr(9)) Then
188 version = Mid(version, 1, Instr(version, Chr(9)) - 1)
189 End If
191 If version <> cached_version Then
192 Set cache_file = fso.CreateTextFile("../config.cache.version", True)
193 cache_file.WriteLine(version)
194 cache_file.Close
195 IsCachedVersion = False
196 Else
197 IsCachedVersion = True
198 End If
199 End Function
201 Function CheckFile(filename)
202 CheckFile = FSO.FileExists(filename)
203 If CheckFile Then CheckFile = (FSO.GetFile(filename).DateLastModified >= FSO.GetFile(filename & ".in").DateLastModified)
204 End Function
206 Dim version
207 version = DetermineVersion
208 If Not (IsCachedVersion(version) And CheckFile("../src/rev.cpp") And CheckFile("../src/os/windows/ottdres.rc")) Then
209 UpdateFiles version
210 End If