9/7/2008 19:03
El Bruno
[MSBUILD] HowTo: Incrementar el numero de la version en cada Build (otra alternativa)

Buenas,
el amigo Luis nos comentó hace poco cómo incrementar el numero de versión en cada build basado en un post de otro crack: Martin Woodward. Como estoy en la línea del conformismo inconformista y los ejemplos anteriores no me bastaban, tuve que modificar un poco la tarea de TFS para que pueda incrementar bajo determinadas condiciones, los valores de Major Build, Minor Build, Revision, Build.
De esta forma el código de la nueva tarea permite que de acuerdo a las propiedades UpdateVersionMajor, UpdateVersionMinor, UpdateVersionRevision y UpdateVersionBuild se puede especificar que partes de la versión se desea incrementar con respecto a la versión anterior. Este es el código fuente de la tarea:
1 using System;
2 using System.IO;
3 using Microsoft.Build.Framework;
4 using Microsoft.Build.Utilities;
5
6 namespace ElBruno.TFS.Tasks
7 {
8 /// <summary>
9 /// A simple task to increment the number stored in a passed file.
10 /// </summary>
11 public class IncrementingNumber : Task
12 {
13 /// <summary>
14 /// When overridden in a derived class, executes the task.
15 /// </summary>
16 /// <returns>
17 /// true if the task successfully executed; otherwise, false.
18 /// </returns>
19 public override bool Execute()
20 {
21 IncrementVersionNumber();
22 return true;
23 }
24
25 /// <summary>
26 /// Increments the version number.
27 /// </summary>
28 public void IncrementVersionNumber()
29 {
30 // get version content
31 string fileContent = string.Empty;
32 if (File.Exists(VersionFile))
33 fileContent = File.ReadAllText(VersionFile);
34
35 // upgrade version number
36 if (!string.IsNullOrEmpty(fileContent))
37 {
38 string[] version = fileContent.Split('.');
39 // get current values
40 if (version.Length > 0)
41 NewVersionMajor = Convert.ToInt32(version[0]);
42 if (version.Length > 1)
43 NewVersionMinor = Convert.ToInt32(version[1]);
44 if (version.Length > 2)
45 NewVersionBuild = Convert.ToInt32(version[3]);
46 if (version.Length > 3)
47 NewVersionRevision = Convert.ToInt32(version[2]);
48
49 // increment if defined
50 if (UpdateVersionMajor)
51 NewVersionMajor++;
52 if (UpdateVersionMinor)
53 NewVersionMinor++;
54 if (UpdateVersionBuild)
55 NewVersionBuild++;
56 if (UpdateVersionRevision)
57 NewVersionRevision++;
58 }
59 else
60 {
61 NewVersionMajor = 1;
62 NewVersionMinor = 0;
63 NewVersionBuild = 0;
64 NewVersionRevision = 0;
65 }
66
67 // create new content
68 fileContent =
69 string.Format(@"{0}.{1}.{2}.{3}", NewVersionMajor, NewVersionMinor, NewVersionRevision, NewVersionBuild);
70
71 // delete file if exists
72 if (System.IO.File.Exists(VersionFile))
73 System.IO.File.Delete(VersionFile);
74
75 // create new file
76 System.IO.File.WriteAllText(VersionFile, fileContent);
77 VersionFileContent = fileContent;
78 }
79
80 #region Input Parameters
81
82 private string versionFile = "version.txt";
83
84 /// <summary>
85 /// Gets or sets the version file.
86 /// </summary>
87 /// <value>The version file.</value>
88 [Required]
89 public string VersionFile
90 {
91 get { return versionFile; }
92 set { versionFile = value; }
93 }
94
95 private string versionFileContent;
96
97 /// <summary>
98 /// Gets or sets the content of the version file.
99 /// </summary>
100 /// <value>The content of the version file.</value>
101 public string VersionFileContent
102 {
103 get { return versionFileContent; }
104 set { versionFileContent = value; }
105 }
106
107 /// <summary>
108 /// Gets or sets a value indicating whether [update version major].
109 /// </summary>
110 /// <value><c>true</c> if [update version major]; otherwise, <c>false</c>.</value>
111 public bool UpdateVersionMajor { get; set; }
112
113 /// <summary>
114 /// Gets or sets a value indicating whether [update version minor].
115 /// </summary>
116 /// <value><c>true</c> if [update version minor]; otherwise, <c>false</c>.</value>
117 public bool UpdateVersionMinor { get; set; }
118
119
120 /// <summary>
121 /// Gets or sets a value indicating whether [update version revision].
122 /// </summary>
123 /// <value>
124 /// <c>true</c> if [update version revision]; otherwise, <c>false</c>.
125 /// </value>
126 public bool UpdateVersionRevision { get; set; }
127
128 /// <summary>
129 /// Gets or sets a value indicating whether [update version build].
130 /// </summary>
131 /// <value><c>true</c> if [update version build]; otherwise, <c>false</c>.</value>
132 public bool UpdateVersionBuild { get; set; }
133
134 #endregion
135
136
137 // OUTPUT PARAMETERS
138 /// <summary>
139 /// Gets or sets the new version major.
140 /// </summary>
141 /// <value>The new version major.</value>
142 [Output]
143 public int NewVersionMajor { get; set; }
144 /// <summary>
145 /// Gets or sets the new version minor.
146 /// </summary>
147 /// <value>The new version minor.</value>
148 [Output]
149 public int NewVersionMinor { get; set; }
150 /// <summary>
151 /// Gets or sets the new version revision.
152 /// </summary>
153 /// <value>The new version revision.</value>
154 [Output]
155 public int NewVersionRevision { get; set; }
156 /// <summary>
157 /// Gets or sets the new version build.
158 /// </summary>
159 /// <value>The new version build.</value>
160 [Output]
161 public int NewVersionBuild { get; set; }
162
163 }
164 }
165
Finalmente dentro de nuestro proyecto de compilación, podemos agregar la siguiente seccion para incrementar el número de versión. En el siguiente ejemplo, se especifica que en cada compilación se incrementen los valores de Build y Revisión (línea 17) y deja los nuevos valores para la versión en las variables $(VersionMajor), $(VersionMinor), $(VersionBuild), $(VersionRevision).
Luego, en la línea 27, se crea un nuevo nombre de Build donde se aplica el nuevo número de versión.
1 <UsingTask AssemblyFile="$(MSBuildExtensionsPath)\ElBruno.TFS.Tasks.dll"
2 TaskName="ElBruno.TFS.Tasks.IncrementingNumber" />
3 <!--
4 =================================
5 INCREMENT VERSION NUMBER
6 ================================= -->
7
8 <PropertyGroup>
9 <VersionMajor>1</VersionMajor>
10 <VersionMinor>0</VersionMinor>
11 <VersionService>0</VersionService>
12 <VersionBuild>6</VersionBuild>
13 </PropertyGroup>
14 <Target Name="BuildNumberOverrideTarget">
15 <BuildStep TeamFoundationServerUrl="$(TeamFoundationServerUrl)" BuildUri="$(BuildUri)" Name="StartIncrement"
16 Message="Loading last build number from file "$(SolutionRoot)\ClickOnceDemo\version.txt"" Status="Succeeded" />
17
18 <!-- Increment number -->
19 <IncrementingNumber VersionFile="$(DropLocation)\buildnumber.txt" UpdateVersionBuild="true" UpdateVersionRevision="true" >
20 <Output TaskParameter="NewVersionMajor" PropertyName="VersionMajor" />
21 <Output TaskParameter="NewVersionMinor" PropertyName="VersionMinor" />
22 <Output TaskParameter="NewVersionBuild" PropertyName="VersionBuild" />
23 <Output TaskParameter="NewVersionRevision" PropertyName="VersionRevision" />
24
25 </IncrementingNumber>
26 <PropertyGroup>
27 <BuildNumber>$(BuildDefinitionName)_$(VersionMajor).$(VersionMinor).$(VersionBuild).$(VersionRevision)</BuildNumber>
28 </PropertyGroup>
29
30 <BuildStep TeamFoundationServerUrl="$(TeamFoundationServerUrl)" BuildUri="$(BuildUri)" Name="EndIncrement"
31 Message="Build number set to "$(BuildNumber)"" Status="Succeeded" />
32 </Target>
33
Saludos @ Marron
El Bruno
Crossposting from
ElBruno.com
Archivado en: Visual Studio Team System,VSTS,Team Foundation Server
Comparte este post: