Versión configurada de VS.net para múltiples proyectos en una sola solución
-
21-09-2019 - |
Pregunta
Estoy intentando actualizar muchos proyectos diferentes en una solución para tener un nuevo número de versión.¿Existe una manera sencilla de sincronizar el número de versión en todas las opciones de versión de archivo y hacer clic una vez en todos los proyectos?
Respuesta
Finalmente resolvió el problema escribiendo una pequeña herramienta:
Sub Main()
Try
Console.WriteLine("Updating version numbers")
Dim strPath As String = System.AppDomain.CurrentDomain.BaseDirectory()
Dim strAppName As String = ""
Console.WriteLine(strPath)
If My.Application.CommandLineArgs.Count > 0 Then
Console.WriteLine(My.Application.CommandLineArgs(0))
strPath = My.Application.CommandLineArgs(0)
strAppName = My.Application.CommandLineArgs(1)
Else
strPath = "C:\Projects\APP\"
Console.WriteLine("Error loading settings")
End If
Dim strAssemblyInfoFile As String = strPath + "Properties\AssemblyInfo.cs"
If Not File.Exists(strAssemblyInfoFile) Then
strAssemblyInfoFile = strPath + "My Project\AssemblyInfo.vb"
End If
Console.WriteLine("Loading " + strAssemblyInfoFile)
Dim strFileContent As String
strFileContent = ReadFileText(strAssemblyInfoFile)
Dim AssemblyVersionRegex As New Regex("AssemblyVersion(?:Attribute)?\(\s*?""(?<version>(?<major>[0-9]+)\.(?<minor>[0-9]+)\.(?<build>[0-9]+)\.(?<revision>[0-9]+))""\s*?\)")
Dim strOldVersion As String = AssemblyVersionRegex.Match(strFileContent).Groups("version").Value
Dim oldVersion As New Version(strOldVersion)
Dim newVersion As New Version(oldVersion.Major.ToString + "." + oldVersion.Minor.ToString + "." + oldVersion.MajorRevision.ToString + "." + (oldVersion.MinorRevision + 1).ToString)
Dim strNewVersion As String = newVersion.ToString()
Console.WriteLine("Newversion " + strNewVersion)
'Replace oldversion to newversion
strFileContent = strFileContent.Replace(strOldVersion, strNewVersion)
File.WriteAllText(strAssemblyInfoFile, strFileContent)
Dim strProjectFile As String = strPath + strAppName + ".csproj"
If Not File.Exists(strProjectFile) Then
strProjectFile = strPath + strAppName + ".vbproj"
End If
Console.WriteLine("Loading " + strProjectFile)
strFileContent = File.ReadAllText(strProjectFile)
strFileContent = strFileContent.Replace(strOldVersion, strNewVersion)
Dim strOld As String = "<ApplicationRevision>" + oldVersion.MinorRevision.ToString() + "</ApplicationRevision>"
Dim strNew As String = "<ApplicationRevision>" + (oldVersion.MinorRevision + 1).ToString() + "</ApplicationRevision>"
strFileContent = strFileContent.Replace(strOld, strNew)
SaveFile(strProjectFile, strFileContent)
Console.WriteLine("Done")
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
Function ReadFileText(ByVal strFilePath As String) As String
Return File.ReadAllText(strFilePath)
End Function
Sub SaveFile(ByVal strFilePath As String, ByVal strData As String)
File.WriteAllText(strFilePath, strData)
End Sub
Solución
Normalmente almaceno los atributos de la versión ensamblada en un archivo separado. VersiónEnsamblaje.cs archivo y colóquelo en la carpeta raíz de mi solución.
Entonces yo enlace el archivo a cada proyecto:
- Menú contextual en Proyecto y elija "Agregar elemento existente"
- Seleccione el archivo de la carpeta raíz
- Haga clic en el menú desplegable junto al botón "Agregar" y seleccione "Agregar como enlace".
Desafortunadamente, no he encontrado una manera limpia en MSBuild de generar automáticamente el número de versión antes de que se compile la solución.(Creo que MSBuild solo tiene eventos por proyecto, no por solución. tal vez alguien más lo sepa actualizar: ver aquí para eventos previos a la compilación de toda la solución a través de msbuild)
En lugar de eso, uso nant para compilar la solución y uso el asminfo tarea de generar el VersiónEnsamblaje.cs archivo.