Tue, 2008-02-26 20:53 — carlos
[b]Objetive:[/b]
*Update automatically the revision number in the web.config file of a website in my project.
(I wanted to show the users looking at the remote server the revision they are looking at)
[b]Environment[/b]
* SVN installed in my local (c:\svn)
* Remote Windows Server that updates the trunk every X minutes
* Make sure your web.config has a section like: (inside the configuration branch)
[b]Solutions:[/b]
I created two macros.... one for adding the revision number on demand (selected project and press of a button) and the other one will write it every time the build command is called.
for the macro code click Read more....
[b]Macro to add SVN revision on demand[/b]
this is great when you have multiple websites in one project and you only want to update one of them....
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Imports System.Text.RegularExpressions
Imports System.Configuration
Imports System.Xml
Imports System.Web
Public Module Rev2Conf
Public Sub svn2conf()
'path to svn excecutable
Dim svnPath As String = "C:\svn\bin\svn.exe"
'gets the selected project
Dim proj As Project
Dim objProj As Object()
objProj = DTE.ActiveSolutionProjects
If objProj.Length = 0 Then
Exit Sub
End If
proj = DTE.ActiveSolutionProjects(0)
'gets the path of the web project to write to the web.config
Dim x As String = proj.ProjectItems.ContainingProject.FullName
Dim p As New System.Diagnostics.Process
'first update the root to get the latest revision
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.RedirectStandardError = True
p.StartInfo.FileName = svnPath
p.StartInfo.Arguments = "update"
p.StartInfo.WorkingDirectory = proj.Properties.Item("FullPath").Value()
p.Start()
p.WaitForExit()
p.StartInfo.Arguments = "info"
p.Start()
Dim strerror As String = p.StandardError().ReadToEnd()
Dim output As String = p.StandardOutput().ReadToEnd()
p.WaitForExit()
'get the revision
Dim re As New Regex("Revision:\s+(\d+)\s*")
Dim ver As String
ver = re.Match(output).Groups(1).Value
'add one so the number is not one revision behind
Dim version As Integer
version = Convert.ToInt32(ver) + 1
Dim file As New System.Configuration.ExeConfigurationFileMap()
file.ExeConfigFilename = proj.Properties.Item("FullPath").Value() + "\web.config"
Dim config As System.Configuration.Configuration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None)
'get section
Dim section As AppSettingsSection
section = config.GetSection("appSettings")
section.Settings("revision").Value = version.ToString()
section.SectionInformation.ForceSave = True
config.Save(ConfigurationSaveMode.Full)
End Sub
End Module
(the code above gets the selected website and adds the revision number)
The Other way of doing it is running it every time you build....
[b]Macro to add SVN revision number on build of project to all WebSites in the solution[/b]
add the following code to your EnvironmentEvents macro file
Private Sub BuildEvents_OnBuildProjConfigBegin(ByVal Project As String, ByVal ProjectConfig As String, ByVal Platform As String, ByVal SolutionConfig As String) Handles BuildEvents.OnBuildProjConfigBegin
'path to svn excecutable
Dim svnPath As String = "C:\svn\bin\svn.exe"
Dim file As New System.Configuration.ExeConfigurationFileMap()
file.ExeConfigFilename = Project + "\web.config"
'only apply revision Number to WebApplications
If FileIO.FileSystem.FileExists(file.ExeConfigFilename) Then
Dim p As New System.Diagnostics.Process
'first update the root to get the latest revision
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.RedirectStandardError = True
p.StartInfo.FileName = svnPath
p.StartInfo.Arguments = "update"
p.StartInfo.WorkingDirectory = Project
p.Start()
p.WaitForExit()
p.StartInfo.Arguments = "info"
p.Start()
Dim strerror As String = p.StandardError().ReadToEnd()
Dim output As String = p.StandardOutput().ReadToEnd()
p.WaitForExit()
'get the revision
Dim re As New Regex("Revision:\s+(\d+)\s*")
Dim ver As String
ver = re.Match(output).Groups(1).Value
'add one so the number is not one revision behind
Dim version As Integer
version = Convert.ToInt32(ver) + 1
Dim config As System.Configuration.Configuration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None)
'get config section
Dim section As AppSettingsSection
section = config.GetSection("appSettings")
section.Settings("revision").Value = version.ToString()
section.SectionInformation.ForceSave = True
config.Save(ConfigurationSaveMode.Full)
End If
End Sub
Comments
I don't want do add revision to Web.config when build fails
Private Sub BuildEvents_OnBuildProjConfigDone(ByVal Project As String, ByVal ProjectConfig As String, ByVal Platform As String, ByVal SolutionConfig As String, ByVal Success As Boolean) Handles BuildEvents.OnBuildProjConfigDone If (Success.Equals(True)) Then 'path to svn excecutable Dim svnPath As String = "C:\svn\bin\svn.exe" Dim file As New System.Configuration.ExeConfigurationFileMap() file.ExeConfigFilename = Project + "\web.config" 'only apply revision Number to WebApplications If FileIO.FileSystem.FileExists(file.ExeConfigFilename) Then Dim p As New System.Diagnostics.Process 'first update the root to get the latest revision p.StartInfo.UseShellExecute = False p.StartInfo.RedirectStandardOutput = True p.StartInfo.RedirectStandardError = True p.StartInfo.FileName = svnPath p.StartInfo.Arguments = "update" p.StartInfo.WorkingDirectory = Project p.Start() p.WaitForExit() p.StartInfo.Arguments = "info" p.Start() Dim strerror As String = p.StandardError().ReadToEnd() Dim output As String = p.StandardOutput().ReadToEnd() p.WaitForExit() 'get the revision Dim re As New Regex("Revision:\s+(\d+)\s*") Dim ver As String ver = re.Match(output).Groups(1).Value 'add one so the number is not one revision behind Dim version As Integer version = Convert.ToInt32(ver) + 1 Dim config As System.Configuration.Configuration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None) 'get config section Dim section As AppSettingsSection section = config.GetSection("appSettings") section.Settings("revision").Value = version.ToString() section.SectionInformation.ForceSave = True config.Save(ConfigurationSaveMode.Full) End If End If End Sub