2007-09-12 07:11:48 +00:00
Option Explicit
2009-08-21 20:21:05 +00:00
' $Id$
'
' 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/>.
2007-09-12 07:11:48 +00:00
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)
2008-08-02 19:48:54 +00:00
Set file = FSO.CreateTextFile(filename, -1, 0)
2007-09-12 07:11:48 +00:00
file.Write data
file.Close
End Sub
2018-04-12 18:59:03 +00:00
Sub UpdateFile(modified, isodate, version, cur_date, filename)
2007-09-12 07:11:48 +00:00
FSO.CopyFile filename & ".in", filename
2009-05-16 12:36:33 +00:00
FindReplaceInFile filename, "!!MODIFIED!!", modified
2018-04-12 18:59:03 +00:00
FindReplaceInFile filename, "!!ISODATE!!", isodate
2009-05-16 12:36:33 +00:00
FindReplaceInFile filename, "!!VERSION!!", version
FindReplaceInFile filename, "!!DATE!!", cur_date
2007-09-12 07:11:48 +00:00
End Sub
Sub UpdateFiles(version)
2018-04-12 18:59:03 +00:00
Dim modified, isodate, cur_date
2007-09-12 07:11:48 +00:00
cur_date = DatePart("D", Date) & "." & DatePart("M", Date) & "." & DatePart("YYYY", Date)
2008-10-23 02:52:11 +00:00
If InStr(version, Chr(9)) Then
2018-04-12 18:59:03 +00:00
isodate = Mid(version, InStr(version, Chr(9)) + 1)
modified = Mid(isodate, InStr(isodate, Chr(9)) + 1)
isodate = Mid(isodate, 1, InStr(isodate, Chr(9)) - 1)
2010-11-29 12:49:27 +00:00
modified = Mid(modified, 1, InStr(modified, Chr(9)) - 1)
2008-10-23 02:52:11 +00:00
version = Mid(version, 1, InStr(version, Chr(9)) - 1)
Else
2018-04-12 18:59:03 +00:00
isodate = 0
2008-10-23 02:52:11 +00:00
modified = 1
End If
2007-10-21 14:59:05 +00:00
2018-04-12 18:59:03 +00:00
UpdateFile modified, isodate, version, cur_date, "../src/rev.cpp"
UpdateFile modified, isodate, version, cur_date, "../src/os/windows/ottdres.rc"
2007-09-12 07:11:48 +00:00
End Sub
2018-04-12 18:59:03 +00:00
Function DetermineVersion()
Dim WshShell, branch, tag, modified, isodate, oExec, line, hash, shorthash
2007-09-12 07:11:48 +00:00
Set WshShell = CreateObject("WScript.Shell")
On Error Resume Next
2018-04-12 18:59:03 +00:00
modified = 1
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, 8)
' 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
2007-09-12 07:11:48 +00:00
2018-04-12 18:59:03 +00:00
If oExec.ExitCode = 1 Then
2008-10-23 02:52:11 +00:00
modified = 2
2018-04-12 18:59:03 +00:00
End If ' oExec.ExitCode = 1
2007-09-12 07:11:48 +00:00
2018-04-12 18:59:03 +00:00
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, "-", "")
2008-09-04 15:27:21 +00:00
End If ' Err.Number = 0
2007-09-12 07:11:48 +00:00
2018-04-12 18:59:03 +00:00
' Check branch
Err.Clear
Set oExec = WshShell.Exec("git symbolic-ref HEAD")
2010-05-26 03:31:05 +00:00
If Err.Number = 0 Then
2018-04-12 18:59:03 +00:00
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")
2008-09-04 15:27:21 +00:00
If Err.Number = 0 Then
' Wait till the application is finished ...
Do While oExec.Status = 0
Loop
2018-04-12 18:59:03 +00:00
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
2008-09-04 15:27:21 +00:00
End If ' Err.Number = 0
End If ' Err.Number = 0
2018-04-12 18:59:03 +00:00
End If ' oExec.ExitCode = 0
End If ' Err.Number = 0
2007-09-12 07:11:48 +00:00
2018-04-12 18:59:03 +00:00
If hash = "" And FSO.FileExists("../.ottdrev") Then
2010-11-30 12:02:19 +00:00
Dim rev_file
Set rev_file = FSO.OpenTextFile("../.ottdrev", 1, True, 0)
2018-04-12 18:59:03 +00:00
DetermineVersion = rev_file.ReadLine()
2010-11-30 12:02:19 +00:00
rev_file.Close()
2018-04-12 18:59:03 +00:00
ElseIf hash = "" Then
DetermineVersion = "norev000"
2010-11-29 12:49:27 +00:00
Else
2018-04-12 18:59:03 +00:00
Dim version
If tag <> "" Then
version = tag
ElseIf branch = "master" Then
version = isodate & "-g" & shorthash
Else
version = isodate & "-" & branch & "-g" & shorthash
2010-11-29 12:49:27 +00:00
End If
2008-10-23 02:52:11 +00:00
2018-04-12 18:59:03 +00:00
If modified = 2 Then
version = version & "M"
2010-11-29 12:49:27 +00:00
End If
2008-10-23 02:52:11 +00:00
2018-04-12 18:59:03 +00:00
DetermineVersion = version & Chr(9) & isodate & Chr(9) & modified & Chr(9) & hash
2008-10-23 02:52:11 +00:00
End If
2007-09-12 07:11:48 +00:00
End Function
2008-10-23 02:52:11 +00:00
Function IsCachedVersion(ByVal version)
2007-09-12 07:11:48 +00:00
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
2008-10-23 02:52:11 +00:00
If InStr(version, Chr(9)) Then
version = Mid(version, 1, Instr(version, Chr(9)) - 1)
End If
2007-09-12 07:11:48 +00:00
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
2008-12-31 16:53:43 +00:00
Function CheckFile(filename)
2009-01-01 18:18:17 +00:00
CheckFile = FSO.FileExists(filename)
If CheckFile Then CheckFile = (FSO.GetFile(filename).DateLastModified >= FSO.GetFile(filename & ".in").DateLastModified)
2008-12-31 16:53:43 +00:00
End Function
2007-09-12 07:11:48 +00:00
Dim version
2018-04-12 18:59:03 +00:00
version = DetermineVersion
2009-08-31 22:38:37 +00:00
If Not (IsCachedVersion(version) And CheckFile("../src/rev.cpp") And CheckFile("../src/os/windows/ottdres.rc")) Then
2007-09-12 07:11:48 +00:00
UpdateFiles version
End If