summaryrefslogtreecommitdiff
path: root/projects/determineversion.vbs
blob: a85a085f29955e964b986ffc07bb8cfbd69d31eb (plain)
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
Option Explicit

' This file is part of OpenTTD.
' 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.
' 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.
' 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/>.

Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")

Sub FindReplaceInFile(filename, to_find, replacement)
	Dim file, data
	Set file = FSO.OpenTextFile(filename, 1, 0, 0)
	data = file.ReadAll
	file.Close
	data = Replace(data, to_find, replacement)
	Set file = FSO.CreateTextFile(filename, -1, 0)
	file.Write data
	file.Close
End Sub

Sub UpdateFile(modified, isodate, version, cur_date, githash, istag, isstabletag, filename)
	FSO.CopyFile filename & ".in", filename
	FindReplaceInFile filename, "!!MODIFIED!!", modified
	FindReplaceInFile filename, "!!ISODATE!!", isodate
	FindReplaceInFile filename, "!!VERSION!!", version
	FindReplaceInFile filename, "!!DATE!!", cur_date
	FindReplaceInFile filename, "!!GITHASH!!", githash
	FindReplaceInFile filename, "!!ISTAG!!", istag
	FindReplaceInFile filename, "!!ISSTABLETAG!!", isstabletag
End Sub

Sub UpdateFiles(version)
	Dim modified, isodate, cur_date, githash, istag, isstabletag
	cur_date = DatePart("D", Date) & "." & DatePart("M", Date) & "." & DatePart("YYYY", Date)

	If InStr(version, Chr(9)) Then
		' Split string into field with tails
		isodate  = Mid(version, InStr(version, Chr(9)) + 1)
		modified = Mid(isodate, InStr(isodate, Chr(9)) + 1)
		githash  = Mid(modified, InStr(modified, Chr(9)) + 1)
		istag    = Mid(githash, InStr(githash, Chr(9)) + 1)
		isstabletag = Mid(istag, InStr(istag, Chr(9)) + 1)
		' Remove tails from fields
		version  = Mid(version, 1, InStr(version, Chr(9)) - 1)
		isodate  = Mid(isodate, 1, InStr(isodate, Chr(9)) - 1)
		modified = Mid(modified, 1, InStr(modified, Chr(9)) - 1)
		githash  = Mid(githash, 1, InStr(githash, Chr(9)) - 1)
		istag    = Mid(istag, 1, InStr(istag, Chr(9)) - 1)
	Else
		isodate = 0
		modified = 1
		githash = ""
		istag = 0
		isstabletag = 0
	End If

	UpdateFile modified, isodate, version, cur_date, githash, istag, isstabletag, "../src/rev.cpp"
	UpdateFile modified, isodate, version, cur_date, githash, istag, isstabletag, "../src/os/windows/ottdres.rc"
End Sub

Function DetermineVersion()
	Dim WshShell, branch, tag, modified, isodate, oExec, line, hash, shorthash
	Set WshShell = CreateObject("WScript.Shell")
	On Error Resume Next

	modified = 0
	hash = ""
	shorthash = ""
	branch = ""
	isodate = ""
	tag = ""

	' Set the environment to english
	WshShell.Environment("PROCESS")("LANG") = "en"

	Set oExec = WshShell.Exec("git rev-parse --verify HEAD")
	If Err.Number = 0 Then
		' Wait till the application is finished ...
		Do While oExec.Status = 0
		Loop

		If oExec.ExitCode = 0 Then
			hash = oExec.StdOut.ReadLine()
			shorthash = Mid(hash, 1, 10)
			' Make sure index is in sync with disk
			Set oExec = WshShell.Exec("git update-index --refresh")
			If Err.Number = 0 Then
				' StdOut and StdErr share a 4kB buffer so prevent it from filling up as we don't care about the output
				oExec.StdOut.Close
				oExec.StdErr.Close
				' Wait till the application is finished ...
				Do While oExec.Status = 0
					WScript.Sleep 10
				Loop
			End If
			Set oExec = WshShell.Exec("git diff-index --exit-code --quiet HEAD ../")
			If Err.Number = 0 Then
				' Wait till the application is finished ...
				Do While oExec.Status = 0
				Loop

				If oExec.ExitCode = 1 Then
					modified = 2
				End If ' oExec.ExitCode = 1

				Set oExec = WshShell.Exec("git show -s --pretty=format:%ci")
				if Err.Number = 0 Then
					isodate = Mid(oExec.StdOut.ReadLine(), 1, 10)
					isodate = Replace(isodate, "-", "")
				End If ' Err.Number = 0

				' Check branch
				Err.Clear
				Set oExec = WshShell.Exec("git symbolic-ref HEAD")
				If Err.Number = 0 Then
					line = oExec.StdOut.ReadLine()
					branch = Mid(line, InStrRev(line, "/") + 1)
				End If ' Err.Number = 0

				' Check if a tag is currently checked out
				Err.Clear
				Set oExec = WshShell.Exec("git name-rev --name-only --tags --no-undefined HEAD")
				If Err.Number = 0 Then
					' Wait till the application is finished ...
					Do While oExec.Status = 0
					Loop
					If oExec.ExitCode = 0 Then
						tag = oExec.StdOut.ReadLine()
						If Right(tag, 2) = "^0" Then
							tag = Left(tag, Len(tag) - 2)
						End If
					End If ' oExec.ExitCode = 0
				End If ' Err.Number = 0
			End If ' Err.Number = 0
		End If ' oExec.ExitCode = 0
	End If ' Err.Number = 0

	If hash = "" And FSO.FileExists("../.ottdrev") Then
		Dim rev_file
		Set rev_file = FSO.OpenTextFile("../.ottdrev", 1, True, 0)
		DetermineVersion = rev_file.ReadLine()
		rev_file.Close()
	ElseIf hash = "" Then
		DetermineVersion = "norev000"
		modified = 1
	Else
		Dim version, hashprefix, istag, isstabletag
		If modified = 0 Then
			hashprefix = "-g"
		ElseIf modified = 2 Then
			hashprefix = "-m"
		Else
			hashprefix = "-u"
		End If

		If tag <> "" Then
			version = tag
			istag = 1

			Set stable_regexp = New RegExp
			stable_regexp.Pattern = "^[0-9.]*$"
			If stable_regexp.Test(tag) Then
				isstabletag = 1
			Else
				isstabletag = 0
			End If
		Else
			version = isodate & "-" & branch & hashprefix & shorthash
			istag = 0
			isstabletag = 0
		End If

		DetermineVersion = version & Chr(9) & isodate & Chr(9) & modified & Chr(9) & hash & Chr(9) & istag & Chr(9) & isstabletag
	End If
End Function

Function IsCachedVersion(ByVal version)
	Dim cache_file, cached_version
	cached_version = ""
	Set cache_file = FSO.OpenTextFile("../config.cache.version", 1, True, 0)
	If Not cache_file.atEndOfStream Then
		cached_version = cache_file.ReadLine()
	End If
	cache_file.Close

	If InStr(version, Chr(9)) Then
		version = Mid(version, 1, Instr(version, Chr(9)) - 1)
	End If

	If version <> cached_version Then
		Set cache_file = fso.CreateTextFile("../config.cache.version", True)
		cache_file.WriteLine(version)
		cache_file.Close
		IsCachedVersion = False
	Else
		IsCachedVersion = True
	End If
End Function

Function CheckFile(filename)
	CheckFile = FSO.FileExists(filename)
	If CheckFile Then CheckFile = (FSO.GetFile(filename).DateLastModified >= FSO.GetFile(filename & ".in").DateLastModified)
End Function

Dim version
version = DetermineVersion
If Not (IsCachedVersion(version) And CheckFile("../src/rev.cpp") And CheckFile("../src/os/windows/ottdres.rc")) Then
	UpdateFiles version
End If