2009-09-08 2 views
4

Я пытаюсь обновить множество различных проектов в решении, чтобы иметь новый номер версии. Есть ли простой способ синхронизировать номер версии во всех вариантах fileversion и clickonce?VS.net версия для нескольких проектов в одном решении

Ответ

Наконец решил проблему, написав небольшой инструмент:

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 
+0

Я должен рассказать немного больше о проблеме. Я ищу способ обновить номер версии clickonce и номер файла/проекта сразу. – CodingBarfield

ответ

5

Я обычно хранить версию сборки атрибутов в отдельном AssemblyVersion.cs файл и поместить его в корень папку моего решения.

Тогда я link the file to each project:

  1. Контекстное меню по проекту и выберите «Добавить существующий элемент»
  2. Выберите файл из корневой папки
  3. Нажмите на раскрывающееся меню рядом с «Добавить кнопку» и выберите «Добавить как ссылку»

К сожалению, я не нашел в MSBuild чистого пути для автоматического создания номера версии до того, как решение компилируется эс. (Я считаю, что MSBuild имеет только события в проект, а не на решение - может быть, кто-то там знает обновления: см here for solution-wide pre-build events through msbuild)

Вместо этого я использую NAnt для компиляции решения и использовать задачу в asminfo для генерации файла AssemblyVersion.cs.

+1

Вы можете использовать задачу AssemblyInfo вместе с задачей SvnVersion из http://msbuildtasks.tigris.org/, чтобы сгенерировать файл AssemblyVersion.cs для каждой сборки. –

Смежные вопросы