2013-03-05 18 views
9

Tôi đang sử dụng dự án thiết lập để xuất bản dự án của mình. Tôi muốn phiên bản của từng dự án giống như phiên bản thiết lập.Đặt số phiên bản AssemblyInfo với phiên bản thiết lập MSI

Tôi muốn thay đổi thuộc tính phiên bản thiết lập của mình trong Visual Studio và sau khi xây dựng, để tất cả các phiên bản dự án được cập nhật từ thuộc tính này, điều này có khả thi không?

+1

"phiên bản thiết lập" là gì? –

+0

@SimonMourier làm tôi bối rối lúc đầu, ảnh chụp màn hình thứ 2 của nó được hiển thị trong câu trả lời của tôi. –

+0

Ồ, thuộc tính "Phiên bản" này của dự án thiết lập không được viết trong tệp MSI cuối cùng, theo như tôi biết, vậy điểm là gì? –

Trả lời

26

Dự án có hội số phiên bản & File: (không phiên bản cài đặt tôi thay đổi nội dung câu hỏi của bạn cho phù hợp) enter image description here

Trả lời 1:

Nếu bạn muốn chắc số phiên bản dự án cài đặt thiết lập hội & Số phiên bản tệp bạn cần thực hiện với tập lệnh/exe được kích hoạt bởi bản dựng.

enter image description here

Bài viết này trên How To Update Assembly Version Number Automatically cho thấy một nửa số giải pháp ...

Từ nghiên cứu tôi đã làm điều đó là không thể sử dụng SetupVersion trong một PreBuildEvent. Không có một lệnh $ SetupVersion cho nó: http://msdn.microsoft.com/en-us/library/42x5kfw4(v=vs.80).aspx

Phải thay đổi PreBuildEvent từng xây dựng như trong this comment trong bài viết Mã dự án sử dụng lệnh -set: không phải là lý tưởng.

Giải pháp mà chúng tôi cần là một PreBuildEvent để gọi AssemblyInfoUtil.exe và đọc nó "ProductVersion" từ tệp dự án vdproj. Và sau đó cập nhật số phiên bản Assembly (s).

tôi đã sửa đổi mã từ bài viết để cho bạn thấy làm thế nào để đọc các phiên bản sản phẩm từ Setup.vdproj và đây là cách nó có thể được gọi từ một PreBuildEvent:

AssemblyInfoUtil.exe -setup:"C:\Program Files\MyProject1\Setup1\Setup1.vdproj" -ass:"C:\Program Files\MyProject1\AssemblyInfo.cs" 

Đây là mã sửa đổi :

using System; 
using System.IO; 
using System.Text; 

namespace AssemblyInfoUtil 
{ 
    class AssemblyInfoUtil 
    { 
    private static int incParamNum = 0;  
    private static string fileName = ""; 
    private static string setupfileName = "";  
    private static string versionStr = null;  
    private static bool isVB = false; 
    [STAThread] 
    static void Main(string[] args) 
    { 
     for (int i = 0; i < args.Length; i++) { 
      if (args[i].StartsWith("-setup:")) { 
      string s = args[i].Substring("-setup:".Length); 
      setupfileName = int.Parse(s); 
      } 
      else if (args[i].StartsWith("-ass:")) { 
      fileName = args[i].Substring("-ass:".Length); 
      } 
     } 

     //Jeremy Thompson showing how to detect "ProductVersion" = "8:1.0.0" in vdproj 
     string setupproj = System.IO.File.ReadAllText(setupfileName); 
     int startPosOfProductVersion = setupproj.IndexOf("\"ProductVersion\" = \"") +20; 
     int endPosOfProductVersion = setupproj.IndexOf(Environment.NewLine, startPosOfProductVersion) - startPosOfProductVersion; 
     string versionStr = setupproj.Substring(startPosOfProductVersion, endPosOfProductVersion); 
     versionStr = versionStr.Replace("\"", string.Empty).Replace("8:",string.Empty); 

     if (Path.GetExtension(fileName).ToLower() == ".vb") 
     isVB = true; 

     if (fileName == "") { 
     System.Console.WriteLine("Usage: AssemblyInfoUtil 
      <path to :Setup.vdproj file> and <path to AssemblyInfo.cs or AssemblyInfo.vb file> [options]"); 
     System.Console.WriteLine("Options: "); 
     System.Console.WriteLine(" -setup:Setup.vdproj file path"); 
     System.Console.WriteLine(" -ass:Assembly file path"); 
     return; 
     } 

     if (!File.Exists(fileName)) { 
     System.Console.WriteLine 
      ("Error: Can not find file \"" + fileName + "\""); 
     return; 
     } 

     System.Console.Write("Processing \"" + fileName + "\"..."); 
     StreamReader reader = new StreamReader(fileName); 
      StreamWriter writer = new StreamWriter(fileName + ".out"); 
     String line; 

     while ((line = reader.ReadLine()) != null) { 
     line = ProcessLine(line); 
     writer.WriteLine(line); 
     } 
     reader.Close(); 
     writer.Close(); 

     File.Delete(fileName); 
     File.Move(fileName + ".out", fileName); 
     System.Console.WriteLine("Done!"); 
    } 

    private static string ProcessLine(string line) { 
     if (isVB) { 
     line = ProcessLinePart(line, "<Assembly: AssemblyVersion(\""); 
     line = ProcessLinePart(line, "<Assembly: AssemblyFileVersion(\""); 
     } 
     else { 
     line = ProcessLinePart(line, "[assembly: AssemblyVersion(\""); 
     line = ProcessLinePart(line, "[assembly: AssemblyFileVersion(\""); 
     } 
     return line; 
    } 

    private static string ProcessLinePart(string line, string part) { 
     int spos = line.IndexOf(part); 
     if (spos >= 0) { 
     spos += part.Length; 
     int epos = line.IndexOf('"', spos); 
     string oldVersion = line.Substring(spos, epos - spos); 
     string newVersion = ""; 
     bool performChange = false; 

     if (incParamNum > 0) { 
      string[] nums = oldVersion.Split('.'); 
      if (nums.Length >= incParamNum && nums[incParamNum - 1] != "*") { 
      Int64 val = Int64.Parse(nums[incParamNum - 1]); 
      val++; 
      nums[incParamNum - 1] = val.ToString(); 
      newVersion = nums[0]; 
      for (int i = 1; i < nums.Length; i++) { 
       newVersion += "." + nums[i]; 
      } 
      performChange = true; 
      } 
     } 

     else if (versionStr != null) { 
      newVersion = versionStr; 
      performChange = true; 
     } 

     if (performChange) { 
      StringBuilder str = new StringBuilder(line); 
      str.Remove(spos, epos - spos); 
      str.Insert(spos, newVersion); 
      line = str.ToString(); 
     } 
     } 
     return line; 
    } 
    } 
} 

trả lời 2:

Cách suy nghĩ của tôi một cách tốt hơn là sử dụng lớp Shared Assembly Info thay vì các tệp lớp AssemblyInfo riêng lẻ.

Để thực hiện việc này, tạo một tệp trong thư mục giải pháp có tên SharedAssemblyInfo.cs và sau đó thêm liên kết trong mỗi dự án vào SharedAssemblyInfo.cs. Bạn cũng có thể di chuyển tệp SharedAssemblyInfo.cs được liên kết vào thư mục Thuộc tính để nó nằm cạnh nhau với AssemblyInfo.cs dành riêng cho từng dự án trong giải pháp, như được hiển thị bên dưới.

enter image description here

Đây là một tập tin SharedAssemblyInfo.cs mẫu:

using System; 
using System.Reflection; 
using System.Runtime.CompilerServices; 
using System.Runtime.InteropServices; 

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information 
// associated with an assembly. 
[assembly: AssemblyCompany("Saint Bart Technologies")] 
[assembly: AssemblyProduct("Demo")] 
[assembly: AssemblyCopyright("Copyright ? Saint Bart 2013")] 
[assembly: AssemblyTrademark("")] 

// Make it easy to distinguish Debug and Release (i.e. Retail) builds; 
// for example, through the file properties window. 
#if DEBUG 
[assembly: AssemblyConfiguration("Debug")] 
[assembly: AssemblyDescription("Flavor=Debug")] // a.k.a. "Comments" 
#else 
[assembly: AssemblyConfiguration("Retail")] 
[assembly: AssemblyDescription("Flavor=Retail")] // a.k.a. "Comments" 
#endif 

[assembly: CLSCompliant(true)] 

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components. If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type. 
[assembly: ComVisible(false)] 

// Note that the assembly version does not get incremented for every build 
// to avoid problems with assembly binding (or requiring a policy or 
// <bindingRedirect> in the config file). 
// 
// The AssemblyFileVersionAttribute is incremented with every build in order 
// to distinguish one build from another. AssemblyFileVersion is specified 
// in AssemblyVersionInfo.cs so that it can be easily incremented by the 
// automated build process. 
[assembly: AssemblyVersion("1.0.0.0")] 

// By default, the "Product version" shown in the file properties window is 
// the same as the value specified for AssemblyFileVersionAttribute. 
// Set AssemblyInformationalVersionAttribute to be the same as 
// AssemblyVersionAttribute so that the "Product version" in the file 
// properties window matches the version displayed in the GAC shell extension. 
[assembly: AssemblyInformationalVersion("1.0.0.0")] // a.k.a. "Product version" 

Đây là một tập tin AssemblyInfo.cs mẫu:

// Note: Shared assembly information is specified in SharedAssemblyInfo.cs 
using System.Reflection; 
using System.Runtime.CompilerServices; 
using System.Runtime.InteropServices; 
// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information 
// associated with an assembly. 
[assembly: AssemblyTitle("WindowsFormsApplication2")] 
// The following GUID is for the ID of the typelib if this project is exposed to COM 
[assembly: Guid("ffded14d-6c95-440b-a45d-e1f502476539")] 

Vì vậy, mỗi khi bạn muốn thay đổi tất cả các dự án Thông tin hội bạn có thể làm điều đó tại một chỗ. Tôi giả sử bạn sẽ muốn thiết lập phiên bản thiết lập MSI giống như số phiên bản lắp ráp, một bước thủ công.


Trả lời 3:

Cân nhắc việc chuyển sang sử dụng MSBuild nó có tất cả các loại lợi ích, nhưng tôi không chắc chắn nếu bạn có thời gian để nhặt nó lên ngay bây giờ.


Trả lời 4:

Assemblies có thể tự động tăng số build của họ bằng cách sử dụng cú pháp sau đây trong vòng dấu AssemblyInfo.cs:

[assembly: AssemblyVersion("1.0.0.*")] 

Đây là một phương pháp tốt vì điểm theo dõi số bản dựng là để có thể nhận ra các bản dựng khác nhau. Có một số thay đổi trước khi xây dựng thay đổi đánh bại mục đích này vì việc xây dựng chưa xảy ra.


trả lời 5:

Các CodeProject câu trả lời khác ở đây giả sử bạn muốn cập nhật ProductVersion, ProductCode, PackageCode trong Setup tập tin dự án MSI. Tôi không giải thích câu hỏi của bạn như vậy và theo chủ đề này có những vấn đề: pre-build event to change setup project's ProductVersion doesn't take effect until after the build

trả lời 6 (mới):

Có một vài TFS Xây dựng plugin để thiết lập "Thông tin hội": https://marketplace.visualstudio.com/items?itemName=bleddynrichards.Assembly-Info-Task https://marketplace.visualstudio.com/items?itemName=bool.update-assembly-info https://marketplace.visualstudio.com/items?itemName=ggarbuglia.setassemblyversion-task

+0

Câu trả lời hay nhưng Tôi đã tìm thấy một số vấn đề trong mẫu mã của câu trả lời 1. Không cần phải phân tích cú pháp thành số nguyên cho SetupfileName (lỗi biên dịch) và versionStr không được khai báo lại dưới dạng biến cục bộ (làm cho tập lệnh không làm gì mà không có bất kỳ lỗi !). –

1

Tôi không biết nếu điều này giải quyết vấn đề của bạn một cách hoàn hảo nhưng bạn có thể thực hiện một lớp học phổ biến với tất cả các thông tin configmanagment như:

public class VersionInfo{ 
    public const string cProductVersion = "1.0.0" 
    //other version info 
} 

Sau khi bạn có thể cập nhật tất cả AssemblyInfo.cs của bạn với các lớp mới :

[assembly: AssemblyVersion(VersionInfo.cProductVersion)] 

Tôi hy vọng điều này sẽ hữu ích.

+0

Xem câu trả lời số 2 của tôi cho cách chính thức để thực hiện việc này, hãy thử mặc dù –

+0

thx cho gợi ý, tôi sẽ thay đổi thiết lập dự án của riêng tôi sau giải pháp của bạn – yaens