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
2008-06-03 18:35:58 +00:00
Sub UpdateFile(modified, revision, 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
FindReplaceInFile filename, "!!REVISION!!", revision
FindReplaceInFile filename, "!!VERSION!!", version
FindReplaceInFile filename, "!!DATE!!", cur_date
2007-09-12 07:11:48 +00:00
End Sub
Sub UpdateFiles(version)
2008-10-23 02:52:11 +00:00
Dim modified, revision, 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
revision = Mid(version, InStr(version, Chr(9)) + 1)
2010-11-29 12:49:27 +00:00
modified = Mid(revision, InStr(revision, Chr(9)) + 1)
2008-10-23 02:52:11 +00:00
revision = Mid(revision, 1, InStr(revision, 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
revision = 0
modified = 1
End If
2007-10-21 14:59:05 +00:00
2008-06-03 18:35:58 +00:00
UpdateFile modified, revision, version, cur_date, "../src/rev.cpp"
2009-08-31 22:38:37 +00:00
UpdateFile modified, revision, version, cur_date, "../src/os/windows/ottdres.rc"
2007-09-12 07:11:48 +00:00
End Sub
2008-03-15 22:43:28 +00:00
Function ReadRegistryKey(shive, subkey, valuename, architecture)
Dim hiveKey, objCtx, objLocator, objServices, objReg, Inparams, Outparams
' First, get the Registry Provider for the requested architecture
Set objCtx = CreateObject("WbemScripting.SWbemNamedValueSet")
objCtx.Add "__ProviderArchitecture", architecture ' Must be 64 of 32
Set objLocator = CreateObject("Wbemscripting.SWbemLocator")
Set objServices = objLocator.ConnectServer("","root\default","","",,,,objCtx)
Set objReg = objServices.Get("StdRegProv")
' Check the hive and give it the right value
Select Case shive
Case "HKCR", "HKEY_CLASSES_ROOT"
hiveKey = &h80000000
Case "HKCU", "HKEY_CURRENT_USER"
hiveKey = &H80000001
Case "HKLM", "HKEY_LOCAL_MACHINE"
hiveKey = &h80000002
Case "HKU", "HKEY_USERS"
hiveKey = &h80000003
Case "HKCC", "HKEY_CURRENT_CONFIG"
hiveKey = &h80000005
Case "HKDD", "HKEY_DYN_DATA" ' Only valid for Windows 95/98
hiveKey = &h80000006
Case Else
MsgBox "Hive not valid (ReadRegistryKey)"
End Select
Set Inparams = objReg.Methods_("GetStringValue").Inparameters
Inparams.Hdefkey = hiveKey
Inparams.Ssubkeyname = subkey
Inparams.Svaluename = valuename
Set Outparams = objReg.ExecMethod_("GetStringValue", Inparams,,objCtx)
ReadRegistryKey = Outparams.SValue
End Function
2007-09-12 07:11:48 +00:00
Function DetermineSVNVersion()
2010-11-30 12:02:19 +00:00
Dim WshShell, version, branch, modified, revision, clean_rev, url, oExec, line, hash
2007-09-12 07:11:48 +00:00
Set WshShell = CreateObject("WScript.Shell")
On Error Resume Next
2008-12-16 23:02:22 +00:00
revision = 0
2007-09-12 07:11:48 +00:00
' Try TortoiseSVN
' Get the directory where TortoiseSVN (should) reside(s)
Dim sTortoise
2008-03-15 22:43:28 +00:00
' First, try with 32-bit architecture
sTortoise = ReadRegistryKey("HKLM", "SOFTWARE\TortoiseSVN", "Directory", 32)
2010-06-28 13:52:09 +00:00
If sTortoise = "" Or IsNull(sTortoise) Then
2008-03-15 22:43:28 +00:00
' No 32-bit version of TortoiseSVN installed, try 64-bit version (doesn't hurt on 32-bit machines, it returns nothing or is ignored)
sTortoise = ReadRegistryKey("HKLM", "SOFTWARE\TortoiseSVN", "Directory", 64)
End If
2007-09-12 07:11:48 +00:00
2008-03-15 22:43:28 +00:00
' If TortoiseSVN is installed, try to get the revision number
2008-12-16 23:02:22 +00:00
If sTortoise <> "" Then
Dim SubWCRev
Set SubWCRev = WScript.CreateObject("SubWCRev.object")
2010-08-02 16:38:26 +00:00
SubWCRev.GetWCInfo FSO.GetAbsolutePathName("../"), 0, 0
2008-12-16 23:02:22 +00:00
revision = SubWCRev.Revision
version = "r" & revision
modified = 0
if SubWCRev.HasModifications then modified = 2
url = SubWCRev.Url
2008-03-15 22:43:28 +00:00
End If
2007-09-12 07:11:48 +00:00
' Looks like there is no TortoiseSVN installed either. Then we don't know it.
2008-12-16 23:02:22 +00:00
If revision = 0 Then
2007-09-12 07:11:48 +00:00
' Reset error and version
Err.Clear
version = "norev000"
2008-10-23 02:52:11 +00:00
modified = 0
2008-09-04 15:27:21 +00:00
' Set the environment to english
WshShell.Environment("PROCESS")("LANG") = "en"
2007-09-12 07:11:48 +00:00
' Do we have subversion installed? Check immediatelly whether we've got a modified WC.
2010-08-02 16:38:26 +00:00
Set oExec = WshShell.Exec("svnversion ../")
2007-09-12 07:11:48 +00:00
If Err.Number = 0 Then
2008-08-02 19:48:54 +00:00
' Wait till the application is finished ...
Do While oExec.Status = 0
Loop
2007-09-12 07:11:48 +00:00
2008-09-04 15:27:21 +00:00
line = OExec.StdOut.ReadLine()
If line <> "exported" Then
If InStr(line, "M") Then
2008-10-23 02:52:11 +00:00
modified = 2
2008-09-04 15:27:21 +00:00
End If
2007-09-12 07:11:48 +00:00
2008-09-04 15:27:21 +00:00
' And use svn info to get the correct revision and branch information.
2010-08-02 16:38:26 +00:00
Set oExec = WshShell.Exec("svn info ../")
2008-09-04 15:27:21 +00:00
If Err.Number = 0 Then
Do
line = OExec.StdOut.ReadLine()
If InStr(line, "URL") Then
url = line
End If
If InStr(line, "Last Changed Rev") Then
2008-10-23 02:52:11 +00:00
revision = Mid(line, 19)
version = "r" & revision
2008-09-04 15:27:21 +00:00
End If
Loop While Not OExec.StdOut.atEndOfStream
End If ' Err.Number = 0
End If ' line <> "exported"
End If ' Err.Number = 0
End If ' InStr(version, "$")
2007-09-12 07:11:48 +00:00
If version <> "norev000" Then
If InStr(url, "branches") Then
2010-12-25 08:22:55 +00:00
branch = Mid(url, InStr(url, "branches/") + 9)
End If
If InStr(url, "tags") Then
version = Mid(url, InStr(url, "tags/") + 5)
2007-09-12 07:11:48 +00:00
End If
2008-09-04 15:27:21 +00:00
Else ' version <> "norev000"
2007-11-18 22:39:02 +00:00
' svn detection failed, reset error and try git
2007-10-21 17:09:44 +00:00
Err.Clear
2008-10-23 02:52:11 +00:00
Set oExec = WshShell.Exec("git rev-parse --verify HEAD")
2007-11-26 23:28:29 +00:00
If Err.Number = 0 Then
2008-08-02 19:48:54 +00:00
' Wait till the application is finished ...
Do While oExec.Status = 0
Loop
2007-11-18 22:39:02 +00:00
2008-09-04 15:27:21 +00:00
If oExec.ExitCode = 0 Then
2008-10-23 02:52:11 +00:00
hash = oExec.StdOut.ReadLine()
version = "g" & Mid(hash, 1, 8)
2010-05-26 03:31:05 +00:00
' Make sure index is in sync with disk
Set oExec = WshShell.Exec("git update-index --refresh")
If Err.Number = 0 Then
' Wait till the application is finished ...
Do While oExec.Status = 0
WScript.Sleep 10
Loop
End If
2010-08-02 16:38:26 +00:00
Set oExec = WshShell.Exec("git diff-index --exit-code --quiet 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
If oExec.ExitCode = 1 Then
2008-10-23 02:52:11 +00:00
modified = 2
2008-09-04 15:27:21 +00:00
End If ' oExec.ExitCode = 1
Set oExec = WshShell.Exec("git symbolic-ref HEAD")
If Err.Number = 0 Then
line = oExec.StdOut.ReadLine()
line = Mid(line, InStrRev(line, "/") + 1)
If line <> "master" Then
2008-10-23 02:52:11 +00:00
branch = line
2008-09-04 15:27:21 +00:00
End If ' line <> "master"
End If ' Err.Number = 0
2008-10-23 02:52:11 +00:00
2011-01-18 21:27:30 +00:00
Set oExec = WshShell.Exec("git log --pretty=format:%s --grep=" & Chr(34) & "^(svn r[0-9]*)" & Chr(34) & " -1")
2008-10-23 02:52:11 +00:00
if Err.Number = 0 Then
revision = Mid(oExec.StdOut.ReadLine(), 7)
revision = Mid(revision, 1, InStr(revision, ")") - 1)
End If ' Err.Number = 0
2010-07-02 16:34:15 +00:00
If revision = "" Then
' No revision? Maybe it is a custom git-svn clone
' Reset error number as WshShell.Exec will not do that on success
Err.Clear
2011-01-18 21:27:30 +00:00
Set oExec = WshShell.Exec("git log --pretty=format:%b --grep=" & Chr(34) & "git-svn-id:.*@[0-9]*" & Chr(34) & " -1")
2010-07-02 16:34:15 +00:00
If Err.Number = 0 Then
revision = oExec.StdOut.ReadLine()
revision = Mid(revision, InStr(revision, "@") + 1)
revision = Mid(revision, 1, InStr(revision, " ") - 1)
End If ' Err.Number = 0
End If ' revision = ""
2011-01-18 21:27:30 +00:00
' 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
version = oExec.StdOut.ReadLine()
2011-05-07 15:13:52 +00:00
If Right(version, 2) = "^0" Then
version = Left(version, Len(version) - 2)
End If
2011-01-18 21:27:30 +00:00
branch = ""
End If ' oExec.ExitCode = 0
End If ' Err.Number = 0
2008-09-04 15:27:21 +00:00
End If ' Err.Number = 0
End If ' oExec.ExitCode = 0
End If ' Err.Number = 0
If version = "norev000" Then
' git detection failed, reset error and try mercurial (hg)
2007-11-18 22:39:02 +00:00
Err.Clear
2011-01-18 21:27:35 +00:00
Set oExec = WshShell.Exec("hg id -i")
2007-10-21 17:09:44 +00:00
If Err.Number = 0 Then
2008-08-02 19:48:54 +00:00
' Wait till the application is finished ...
Do While oExec.Status = 0
Loop
2008-09-04 15:27:21 +00:00
If oExec.ExitCode = 0 Then
line = OExec.StdOut.ReadLine()
2011-01-18 21:27:35 +00:00
hash = Left(line, 12)
2008-10-23 02:52:11 +00:00
version = "h" & Mid(hash, 1, 8)
2011-01-18 21:27:35 +00:00
' Check if a tag is currently checked out
Err.Clear
Set oExec = WshShell.Exec("hg id -t")
If Err.Number = 0 Then
line = oExec.StdOut.ReadLine()
2011-01-19 17:10:52 +00:00
If Len(line) > 0 And Right(line, 3) <> "tip" Then
2011-01-18 21:27:35 +00:00
version = line
branch = ""
2011-01-19 17:10:52 +00:00
End If ' Len(line) > 0 And Right(line, 3) <> "tip"
2011-01-18 21:27:35 +00:00
End If ' Err.Number = 0
Err.Clear
2010-08-02 16:38:26 +00:00
Set oExec = WshShell.Exec("hg status ../")
2008-09-04 15:27:21 +00:00
If Err.Number = 0 Then
Do
line = OExec.StdOut.ReadLine()
If Len(line) > 0 And Mid(line, 1, 1) <> "?" Then
2008-10-23 02:52:11 +00:00
modified = 2
2008-09-04 15:27:21 +00:00
Exit Do
End If ' Len(line) > 0 And Mid(line, 1, 1) <> "?"
Loop While Not OExec.StdOut.atEndOfStream
Set oExec = WshShell.Exec("hg branch")
If Err.Number = 0 Then
line = OExec.StdOut.ReadLine()
If line <> "default" Then
2008-10-23 02:52:11 +00:00
branch = line
2008-09-04 15:27:21 +00:00
End If ' line <> "default"
End If ' Err.Number = 0
2008-10-23 02:52:11 +00:00
2011-01-19 17:10:57 +00:00
Set oExec = WshShell.Exec("hg log -f -k " & Chr(34) & "(svn r" & Chr(34) & " -l 1 --template " & Chr(34) & "{desc|firstline}\n" & Chr(34) & " --cwd ../")
2008-10-23 02:52:11 +00:00
If Err.Number = 0 Then
2011-01-19 17:10:57 +00:00
line = oExec.StdOut.ReadLine()
If Left(line, 6) = "(svn r" Then
revision = Mid(line, 7)
revision = Mid(revision, 1, InStr(revision, ")") - 1)
End If 'Left(line, 6) = "(svn r"
2008-10-23 02:52:11 +00:00
End If ' Err.Number = 0
2011-01-19 17:10:57 +00:00
If revision = "" Then
' No rev? Maybe it is a custom hgsubversion clone
Err.Clear
Set oExec = WshShell.Exec("hg parent --template=" & Chr(34) & "{svnrev}" & Chr(34))
If Err.Number = 0 Then
revision = oExec.StdOut.ReadLine()
End If ' Err.Number = 0
End If ' revision = ""
2008-09-04 15:27:21 +00:00
End If ' Err.Number = 0
End If ' oExec.ExitCode = 0
End If ' Err.Number = 0
End If ' version = "norev000"
End If ' version <> "norev000"
2007-09-12 07:11:48 +00:00
2010-11-30 12:02:19 +00:00
If version = "norev000" And FSO.FileExists("../.ottdrev") Then
Dim rev_file
Set rev_file = FSO.OpenTextFile("../.ottdrev", 1, True, 0)
2010-11-29 12:49:27 +00:00
DetermineSVNVersion = rev_file.ReadLine()
2010-11-30 12:02:19 +00:00
rev_file.Close()
2010-11-29 12:49:27 +00:00
Else
If modified = 2 Then
version = version & "M"
End If
2008-10-23 02:52:11 +00:00
2010-11-29 12:49:27 +00:00
clean_rev = version
If branch <> "" Then
version = version & "-" & branch
End If
2008-10-23 02:52:11 +00:00
2010-11-29 12:49:27 +00:00
If version <> "norev000" Then
DetermineSVNVersion = version & Chr(9) & revision & Chr(9) & modified & Chr(9) & clean_rev
Else
DetermineSVNVersion = version
End If
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
version = DetermineSVNVersion
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