rubidium@7564: Option Explicit rubidium@7564: rubidium@7564: Dim FSO rubidium@7564: Set FSO = CreateObject("Scripting.FileSystemObject") rubidium@7564: rubidium@7564: Sub FindReplaceInFile(filename, to_find, replacement) rubidium@7564: Dim file, data rubidium@7564: Set file = FSO.OpenTextFile(filename, 1, 0, 0) rubidium@7564: data = file.ReadAll rubidium@7564: file.Close rubidium@7564: data = Replace(data, to_find, replacement) glx@9783: Set file = FSO.CreateTextFile(filename, -1, 0) rubidium@7564: file.Write data rubidium@7564: file.Close rubidium@7564: End Sub rubidium@7564: smatz@9457: Sub UpdateFile(modified, revision, version, cur_date, filename) rubidium@7564: FSO.CopyFile filename & ".in", filename smatz@9457: FindReplaceInFile filename, "@@MODIFIED@@", modified rubidium@7780: FindReplaceInFile filename, "@@REVISION@@", revision rubidium@7564: FindReplaceInFile filename, "@@VERSION@@", version rubidium@7564: FindReplaceInFile filename, "@@DATE@@", cur_date rubidium@7564: End Sub rubidium@7564: rubidium@7564: Sub UpdateFiles(version) glx@10283: Dim modified, revision, cur_date rubidium@7564: cur_date = DatePart("D", Date) & "." & DatePart("M", Date) & "." & DatePart("YYYY", Date) glx@10283: glx@10283: If InStr(version, Chr(9)) Then glx@10283: revision = Mid(version, InStr(version, Chr(9)) + 1) glx@10283: revision = Mid(revision, 1, InStr(revision, Chr(9)) - 1) glx@10283: modified = Mid(version, InStrRev(version, Chr(9)) + 1) glx@10283: version = Mid(version, 1, InStr(version, Chr(9)) - 1) glx@10283: Else glx@10283: revision = 0 glx@10283: modified = 1 glx@10283: End If rubidium@7780: smatz@9457: UpdateFile modified, revision, version, cur_date, "../src/rev.cpp" smatz@9457: UpdateFile modified, revision, version, cur_date, "../src/ottdres.rc" rubidium@7564: End Sub rubidium@7564: glx@8702: Function ReadRegistryKey(shive, subkey, valuename, architecture) glx@8702: Dim hiveKey, objCtx, objLocator, objServices, objReg, Inparams, Outparams glx@8702: glx@8702: ' First, get the Registry Provider for the requested architecture glx@8702: Set objCtx = CreateObject("WbemScripting.SWbemNamedValueSet") glx@8702: objCtx.Add "__ProviderArchitecture", architecture ' Must be 64 of 32 glx@8702: Set objLocator = CreateObject("Wbemscripting.SWbemLocator") glx@8702: Set objServices = objLocator.ConnectServer("","root\default","","",,,,objCtx) glx@8702: Set objReg = objServices.Get("StdRegProv") glx@8702: glx@8702: ' Check the hive and give it the right value glx@8702: Select Case shive glx@8702: Case "HKCR", "HKEY_CLASSES_ROOT" glx@8702: hiveKey = &h80000000 glx@8702: Case "HKCU", "HKEY_CURRENT_USER" glx@8702: hiveKey = &H80000001 glx@8702: Case "HKLM", "HKEY_LOCAL_MACHINE" glx@8702: hiveKey = &h80000002 glx@8702: Case "HKU", "HKEY_USERS" glx@8702: hiveKey = &h80000003 glx@8702: Case "HKCC", "HKEY_CURRENT_CONFIG" glx@8702: hiveKey = &h80000005 glx@8702: Case "HKDD", "HKEY_DYN_DATA" ' Only valid for Windows 95/98 glx@8702: hiveKey = &h80000006 glx@8702: Case Else glx@8702: MsgBox "Hive not valid (ReadRegistryKey)" glx@8702: End Select glx@8702: glx@8702: Set Inparams = objReg.Methods_("GetStringValue").Inparameters glx@8702: Inparams.Hdefkey = hiveKey glx@8702: Inparams.Ssubkeyname = subkey glx@8702: Inparams.Svaluename = valuename glx@8702: Set Outparams = objReg.ExecMethod_("GetStringValue", Inparams,,objCtx) glx@8702: glx@8702: ReadRegistryKey = Outparams.SValue glx@8702: End Function glx@8702: rubidium@7564: Function DetermineSVNVersion() glx@10283: Dim WshShell, version, branch, modified, revision, url, oExec, line, hash rubidium@7564: Set WshShell = CreateObject("WScript.Shell") rubidium@7564: On Error Resume Next rubidium@7564: glx@10431: revision = 0 glx@10431: rubidium@7564: ' Try TortoiseSVN rubidium@7564: ' Get the directory where TortoiseSVN (should) reside(s) rubidium@7564: Dim sTortoise glx@8702: ' First, try with 32-bit architecture glx@8702: sTortoise = ReadRegistryKey("HKLM", "SOFTWARE\TortoiseSVN", "Directory", 32) glx@10431: If sTortoise = "" Then glx@8702: ' No 32-bit version of TortoiseSVN installed, try 64-bit version (doesn't hurt on 32-bit machines, it returns nothing or is ignored) glx@8702: sTortoise = ReadRegistryKey("HKLM", "SOFTWARE\TortoiseSVN", "Directory", 64) glx@8702: End If rubidium@7564: glx@8702: ' If TortoiseSVN is installed, try to get the revision number glx@10431: If sTortoise <> "" Then glx@10431: Dim SubWCRev glx@10431: Set SubWCRev = WScript.CreateObject("SubWCRev.object") glx@10431: SubWCRev.GetWCInfo FSO.GetAbsolutePathName("../src"), 0, 0 glx@10431: revision = SubWCRev.Revision glx@10431: version = "r" & revision glx@10431: modified = 0 glx@10431: if SubWCRev.HasModifications then modified = 2 glx@10431: url = SubWCRev.Url glx@8702: End If rubidium@7564: rubidium@7564: ' Looks like there is no TortoiseSVN installed either. Then we don't know it. glx@10431: If revision = 0 Then rubidium@7564: ' Reset error and version rubidium@7564: Err.Clear rubidium@7564: version = "norev000" glx@10283: modified = 0 glx@10074: glx@10074: ' Set the environment to english glx@10074: WshShell.Environment("PROCESS")("LANG") = "en" glx@10074: rubidium@7564: ' Do we have subversion installed? Check immediatelly whether we've got a modified WC. rubidium@7564: Set oExec = WshShell.Exec("svnversion ../src") rubidium@7564: If Err.Number = 0 Then glx@9783: ' Wait till the application is finished ... glx@9783: Do While oExec.Status = 0 glx@9783: Loop rubidium@7564: glx@10074: line = OExec.StdOut.ReadLine() glx@10074: If line <> "exported" Then glx@10074: If InStr(line, "M") Then glx@10283: modified = 2 glx@10074: End If glx@10074: glx@10074: ' And use svn info to get the correct revision and branch information. glx@10074: Set oExec = WshShell.Exec("svn info ../src") glx@10074: If Err.Number = 0 Then glx@10074: Do glx@10074: line = OExec.StdOut.ReadLine() glx@10074: If InStr(line, "URL") Then glx@10074: url = line glx@10074: End If glx@10074: If InStr(line, "Last Changed Rev") Then glx@10283: revision = Mid(line, 19) glx@10283: version = "r" & revision glx@10074: End If glx@10074: Loop While Not OExec.StdOut.atEndOfStream glx@10074: End If ' Err.Number = 0 glx@10074: End If ' line <> "exported" glx@10074: End If ' Err.Number = 0 glx@10074: End If ' InStr(version, "$") rubidium@7564: rubidium@7564: If version <> "norev000" Then rubidium@7564: If InStr(url, "branches") Then glx@10283: url = Mid(url, InStr(url, "branches/") + 9) glx@10283: branch = Mid(url, 1, InStr(2, url, "/") - 1) rubidium@7564: End If glx@10074: Else ' version <> "norev000" glx@7917: ' svn detection failed, reset error and try git glx@7784: Err.Clear glx@10283: Set oExec = WshShell.Exec("git rev-parse --verify HEAD") rubidium@7975: If Err.Number = 0 Then glx@9783: ' Wait till the application is finished ... glx@9783: Do While oExec.Status = 0 glx@9783: Loop glx@7917: glx@10074: If oExec.ExitCode = 0 Then glx@10283: hash = oExec.StdOut.ReadLine() glx@10283: version = "g" & Mid(hash, 1, 8) glx@10074: Set oExec = WshShell.Exec("git diff-index --exit-code --quiet HEAD ../src") glx@10074: If Err.Number = 0 Then glx@10074: ' Wait till the application is finished ... glx@10074: Do While oExec.Status = 0 glx@10074: Loop glx@10074: glx@10074: If oExec.ExitCode = 1 Then glx@10283: modified = 2 glx@10074: End If ' oExec.ExitCode = 1 glx@10074: glx@10074: Set oExec = WshShell.Exec("git symbolic-ref HEAD") glx@10074: If Err.Number = 0 Then glx@10074: line = oExec.StdOut.ReadLine() glx@10074: line = Mid(line, InStrRev(line, "/") + 1) glx@10074: If line <> "master" Then glx@10283: branch = line glx@10074: End If ' line <> "master" glx@10074: End If ' Err.Number = 0 glx@10283: glx@10283: Set oExec = WshShell.Exec("git log --pretty=format:%s --grep=" & Chr(34) & "^(svn r[0-9]*)" & Chr(34) & " -1 ../src") glx@10283: if Err.Number = 0 Then glx@10283: revision = Mid(oExec.StdOut.ReadLine(), 7) glx@10283: revision = Mid(revision, 1, InStr(revision, ")") - 1) glx@10283: End If ' Err.Number = 0 glx@10074: End If ' Err.Number = 0 glx@10074: End If ' oExec.ExitCode = 0 glx@10074: End If ' Err.Number = 0 glx@10074: glx@10074: If version = "norev000" Then glx@10074: ' git detection failed, reset error and try mercurial (hg) glx@7917: Err.Clear glx@10163: Set oExec = WshShell.Exec("hg parents") glx@7784: If Err.Number = 0 Then glx@9783: ' Wait till the application is finished ... glx@9783: Do While oExec.Status = 0 glx@9783: Loop glx@10074: glx@10074: If oExec.ExitCode = 0 Then glx@10074: line = OExec.StdOut.ReadLine() glx@10283: hash = Mid(line, InStrRev(line, ":") + 1) glx@10283: version = "h" & Mid(hash, 1, 8) glx@10074: Set oExec = WshShell.Exec("hg status ../src") glx@10074: If Err.Number = 0 Then glx@10074: Do glx@10074: line = OExec.StdOut.ReadLine() glx@10074: If Len(line) > 0 And Mid(line, 1, 1) <> "?" Then glx@10283: modified = 2 glx@10074: Exit Do glx@10074: End If ' Len(line) > 0 And Mid(line, 1, 1) <> "?" glx@10074: Loop While Not OExec.StdOut.atEndOfStream glx@10074: glx@10074: Set oExec = WshShell.Exec("hg branch") glx@10074: If Err.Number = 0 Then glx@10074: line = OExec.StdOut.ReadLine() glx@10074: If line <> "default" Then glx@10283: branch = line glx@10074: End If ' line <> "default" glx@10074: End If ' Err.Number = 0 glx@10283: glx@10283: Set oExec = WshShell.Exec("hg log -r " & hash & ":0 -k " & Chr(34) & "svn" & Chr(34) & " -l 1 --template " & Chr(34) & "{desc}\n" & Chr(34) & " ../src") glx@10283: If Err.Number = 0 Then glx@10283: revision = Mid(OExec.StdOut.ReadLine(), 7) glx@10283: revision = Mid(revision, 1, InStr(revision, ")") - 1) glx@10283: End If ' Err.Number = 0 glx@10074: End If ' Err.Number = 0 glx@10074: End If ' oExec.ExitCode = 0 glx@10074: End If ' Err.Number = 0 glx@10074: End If ' version = "norev000" glx@10074: End If ' version <> "norev000" rubidium@7564: glx@10283: If modified = 2 Then glx@10283: version = version & "M" glx@10283: End If glx@10283: glx@10283: If branch <> "" Then glx@10283: version = version & "-" & branch glx@10283: End If glx@10283: glx@10283: If version <> "norev000" Then glx@10283: DetermineSVNVersion = version & Chr(9) & revision & Chr(9) & modified glx@10283: Else glx@10283: DetermineSVNVersion = version glx@10283: End If rubidium@7564: End Function rubidium@7564: glx@10283: Function IsCachedVersion(ByVal version) rubidium@7564: Dim cache_file, cached_version rubidium@7564: cached_version = "" rubidium@7564: Set cache_file = FSO.OpenTextFile("../config.cache.version", 1, True, 0) rubidium@7564: If Not cache_file.atEndOfStream Then rubidium@7564: cached_version = cache_file.ReadLine() rubidium@7564: End If rubidium@7564: cache_file.Close rubidium@7564: glx@10283: If InStr(version, Chr(9)) Then glx@10283: version = Mid(version, 1, Instr(version, Chr(9)) - 1) glx@10283: End If glx@10283: rubidium@7564: If version <> cached_version Then rubidium@7564: Set cache_file = fso.CreateTextFile("../config.cache.version", True) rubidium@7564: cache_file.WriteLine(version) rubidium@7564: cache_file.Close rubidium@7564: IsCachedVersion = False rubidium@7564: Else rubidium@7564: IsCachedVersion = True rubidium@7564: End If rubidium@7564: End Function rubidium@7564: rubidium@7564: Dim version rubidium@7564: version = DetermineSVNVersion rubidium@7564: If Not (IsCachedVersion(version) And FSO.FileExists("../src/rev.cpp") And FSO.FileExists("../src/ottdres.rc")) Then rubidium@7564: UpdateFiles version rubidium@7564: End If