代码之家  ›  专栏  ›  技术社区  ›  hitec

使用SVN版本标记ccnet中的内部版本

  •  40
  • hitec  · 技术社区  · 18 年前

    我在一个以SVN为源代码控制的示例项目中使用ccnet。ccnet配置为在每次签入时创建一个构建。ccnet使用msbuild生成源代码。

    我想使用最新版本号生成 AssemblyInfo.cs 编译时。 如何从Subversion中检索最新版本并使用ccnet中的值?

    编辑:我没有使用仅限nant的msbuild。

    12 回复  |  直到 8 年前
        1
  •  45
  •   skolima    15 年前

    CruiseControl.net 1.4.4现在有一个 Assembly Version Labeller 生成与.NET程序集属性兼容的版本号。

    在我的项目中,我将其配置为:

    <labeller type="assemblyVersionLabeller" incrementOnFailure="true" major="1" minor="2"/>
    

    (Caveat: assemblyVersionLabeller 在实际提交触发的生成发生之前,不会开始生成基于SVN修订的标签。)

    然后使用我的msbuild项目 MSBuildCommunityTasks.AssemblyInfo :

    <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
    <Target Name="BeforeBuild">
      <AssemblyInfo Condition="'$(CCNetLabel)' != ''" CodeLanguage="CS" OutputFile="Properties\AssemblyInfo.cs" 
      AssemblyTitle="MyTitle" AssemblyCompany="MyCompany" AssemblyProduct="MyProduct"
      AssemblyCopyright="Copyright ©  2009" ComVisible="false" Guid="some-random-guid"
      AssemblyVersion="$(CCNetLabel)" AssemblyFileVersion="$(CCNetLabel)"/>
    </Target>
    

    为了完整起见,使用nant而不是msbuild的项目也同样容易:

    <target name="setversion" description="Sets the version number to CruiseControl.Net label.">
        <script language="C#">
            <references>
                <include name="System.dll" />
            </references>
            <imports>
                <import namespace="System.Text.RegularExpressions" />
            </imports>
            <code><![CDATA[
                 [TaskName("setversion-task")]
                 public class SetVersionTask : Task
                 {
                  protected override void ExecuteTask()
                  {
                   StreamReader reader = new StreamReader(Project.Properties["filename"]);
                   string contents = reader.ReadToEnd();
                   reader.Close();
                   string replacement = "[assembly: AssemblyVersion(\"" + Project.Properties["CCNetLabel"] + "\")]";
                   string newText = Regex.Replace(contents, @"\[assembly: AssemblyVersion\("".*""\)\]", replacement);
                   StreamWriter writer = new StreamWriter(Project.Properties["filename"], false);
                   writer.Write(newText);
                   writer.Close();
                  }
                 }
                 ]]>
            </code>
        </script>
        <foreach item="File" property="filename">
            <in>
                <items basedir="..">
                    <include name="**\AssemblyInfo.cs"></include>
                </items>
            </in>
            <do>
                <setversion-task />
            </do>
        </foreach>
    </target>
    
        2
  •  14
  •   Markus Safar    10 年前

    你基本上有两个选择。要么编写一个简单的脚本,该脚本将从

    svn.exe info--修订头

    要获得版本号(然后生成assemblyinfo.cs几乎是直接的),或者只使用ccnet的plugin。这里是:

    SVN修订标签机 是一个插件 CruiseControl.net允许您 为您的 基于修订号生成 你的颠覆工作副本。这个 可以使用前缀和/或自定义 主要/次要版本号。

    http://code.google.com/p/svnrevisionlabeller/

    我更喜欢第一个选项,因为它只有大约20行代码:

    using System;
    using System.Diagnostics;
    
    namespace SvnRevisionNumberParserSample
    {
        class Program
        {
            static void Main()
            {
                Process p = Process.Start(new ProcessStartInfo()
                    {
                        FileName = @"C:\Program Files\SlikSvn\bin\svn.exe", // path to your svn.exe
                        UseShellExecute = false,
                        RedirectStandardOutput = true,
                        Arguments = "info --revision HEAD",
                        WorkingDirectory = @"C:\MyProject" // path to your svn working copy
                    });
    
                // command "svn.exe info --revision HEAD" will produce a few lines of output
                p.WaitForExit();
    
                // our line starts with "Revision: "
                while (!p.StandardOutput.EndOfStream)
                {
                    string line = p.StandardOutput.ReadLine();
                    if (line.StartsWith("Revision: "))
                    {
                        string revision = line.Substring("Revision: ".Length);
                        Console.WriteLine(revision); // show revision number on screen                       
                        break;
                    }
                }
    
                Console.Read();
            }
        }
    }
    
        3
  •  4
  •   Roddy    18 年前

    我已经编写了一个nant构建文件,它处理解析SVN信息和创建属性。然后,我将这些属性值用于各种构建任务,包括在构建上设置标签。我将这个目标与Lubos Hasko提到的SVN版本Labeller结合使用,效果很好。

    <target name="svninfo" description="get the svn checkout information">
        <property name="svn.infotempfile" value="${build.directory}\svninfo.txt" />
        <exec program="${svn.executable}" output="${svn.infotempfile}">
            <arg value="info" />
        </exec>
        <loadfile file="${svn.infotempfile}" property="svn.info" />
        <delete file="${svn.infotempfile}" />
    
        <property name="match" value="" />
    
        <regex pattern="URL: (?'match'.*)" input="${svn.info}" />
        <property name="svn.info.url" value="${match}"/>
    
        <regex pattern="Repository Root: (?'match'.*)" input="${svn.info}" />
        <property name="svn.info.repositoryroot" value="${match}"/>
    
        <regex pattern="Revision: (?'match'\d+)" input="${svn.info}" />
        <property name="svn.info.revision" value="${match}"/>
    
        <regex pattern="Last Changed Author: (?'match'\w+)" input="${svn.info}" />
        <property name="svn.info.lastchangedauthor" value="${match}"/>
    
        <echo message="URL: ${svn.info.url}" />
        <echo message="Repository Root: ${svn.info.repositoryroot}" />
        <echo message="Revision: ${svn.info.revision}" />
        <echo message="Last Changed Author: ${svn.info.lastchangedauthor}" />
    </target>
    
        4
  •  4
  •   Apurv    13 年前

    我发现 this 谷歌代码项目。这是 CCNET 用于生成标签的插件 CCNET .

    这个 DLL 被测试 CCNET 1.3 但它与 CCNET 1.4 为了我。我成功地使用这个插件来标记我的构建。

    现在把它传给 MSBuild

        5
  •  4
  •   Apurv    13 年前

    如果你喜欢在 MSBuild 侧身 CCNet 配置,看起来像 MSBube 社区任务扩展 SvnVersion 任务可能会起作用。

        6
  •  3
  •   Michael Stum    18 年前

    我目前正在通过预构建执行任务“手动”执行,使用我的 cmdnetsvnrev 工具,但如果有人知道更好的ccnet集成方式,我会很高兴听到:—)

        7
  •  3
  •   hitec    18 年前

    自定义csproj文件以自动生成assemblyinfo.cs
    http://www.codeproject.com/KB/dotnet/Customizing_csproj_files.aspx

    每次我们创建一个新的C项目, Visual Studio将 assemblyinfo.cs文件。文件 定义程序集元数据 其版本、配置或 制作人。

    找到了使用msbuild自动生成assemblyinfo.cs的上述技术。将很快发布样本。

        8
  •  3
  •   grimus    16 年前

    我不确定这是否适用于ccnet,但我已经创建了一个 SVN version plug-in 对于 Build Version Increment codeplex上的项目。这个工具非常灵活,可以设置为使用SVN版本为您自动创建版本号。它不需要编写任何代码或编辑XML,所以是的!

    希望这有帮助!

        9
  •  2
  •   R. Martinho Fernandes    17 年前

    我的方法是使用上述ccnet插件和nant echo任务生成 VersionInfo.cs 只包含版本属性的文件。我只需要包括 版本信息 文件到生成中

    echo任务只输出我给文件的字符串。

    如果存在类似的msbuild任务,则可以使用相同的方法。这是我使用的小南特任务:

    <target name="version" description="outputs version number to VersionInfo.cs">
      <echo file="${projectdir}/Properties/VersionInfo.cs">
        [assembly: System.Reflection.AssemblyVersion("$(CCNetLabel)")]
        [assembly: System.Reflection.AssemblyFileVersion("$(CCNetLabel)")]
      </echo>
    </target>
    

    试试这个:

    <ItemGroup>
        <VersionInfoFile Include="VersionInfo.cs"/>
        <VersionAttributes>
            [assembly: System.Reflection.AssemblyVersion("${CCNetLabel}")]
            [assembly: System.Reflection.AssemblyFileVersion("${CCNetLabel}")]
        </VersionAttributes>
    </ItemGroup>
    <Target Name="WriteToFile">
        <WriteLinesToFile
            File="@(VersionInfoFile)"
            Lines="@(VersionAttributes)"
            Overwrite="true"/>
    </Target>
    

    请注意,我对msbuild不是很熟悉,因此我的脚本可能不会开箱即用,需要更正…

        10
  •  2
  •   galaktor    17 年前

    基于skolimas解决方案,我更新了nant脚本以同时更新assemblyfileversion。感谢斯科利玛的密码!

    <target name="setversion" description="Sets the version number to current label.">
            <script language="C#">
                <references>
                        <include name="System.dll" />
                </references>
                <imports>
                        <import namespace="System.Text.RegularExpressions" />
                </imports>
                <code><![CDATA[
                         [TaskName("setversion-task")]
                         public class SetVersionTask : Task
                         {
                          protected override void ExecuteTask()
                          {
                           StreamReader reader = new StreamReader(Project.Properties["filename"]);
                           string contents = reader.ReadToEnd();
                           reader.Close();                     
                           // replace assembly version
                           string replacement = "[assembly: AssemblyVersion(\"" + Project.Properties["label"] + "\")]";
                           contents = Regex.Replace(contents, @"\[assembly: AssemblyVersion\("".*""\)\]", replacement);                                        
                           // replace assembly file version
                           replacement = "[assembly: AssemblyFileVersion(\"" + Project.Properties["label"] + "\")]";
                           contents = Regex.Replace(contents, @"\[assembly: AssemblyFileVersion\("".*""\)\]", replacement);                                        
                           StreamWriter writer = new StreamWriter(Project.Properties["filename"], false);
                           writer.Write(contents);
                           writer.Close();
                          }
                         }
                         ]]>
                </code>
            </script>
            <foreach item="File" property="filename">
                <in>
                        <items basedir="${srcDir}">
                                <include name="**\AssemblyInfo.cs"></include>
                        </items>
                </in>
                <do>
                        <setversion-task />
                </do>
            </foreach>
        </target>
    
        11
  •  2
  •   Community Mohan Dere    9 年前

    不知道我在哪里找到的。但我在网上“某处”找到了这个。

    这将在生成之前更新所有assemblyinfo.cs文件。

    很有魅力。所有我的exe和dll都显示为1.2.3.333(如果“333”当时是SVN版本)(assemblyinfo.cs文件中的原始版本列为“1.2.3.0”)。


    $(projectdir)(我的.sln文件所在的位置)

    $(svntoolpath)(指向svn.exe)

    是我的自定义变量,它们的声明/定义在下面没有定义。


    http://msbuildtasks.tigris.org/ 和/或 https://github.com/loresoft/msbuildtasks 具有(fileupdate和svnversion)任务。


      <Target Name="SubVersionBeforeBuildVersionTagItUp">
    
        <ItemGroup>
          <AssemblyInfoFiles Include="$(ProjectDir)\**\*AssemblyInfo.cs" />
        </ItemGroup>
    
        <SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="$(SVNToolPath)">
          <Output TaskParameter="Revision" PropertyName="MySubVersionRevision" />
        </SvnVersion>
    
        <FileUpdate Files="@(AssemblyInfoFiles)"
                Regex="(\d+)\.(\d+)\.(\d+)\.(\d+)"
                ReplacementText="$1.$2.$3.$(MySubVersionRevision)" />
      </Target>
    

    编辑——————————————————————————————————————————————

    当您的SVN版本号达到65534或更高时,上述可能会开始失败。

    见:

    Turn off warning CS1607

    这是解决方法。

    <FileUpdate Files="@(AssemblyInfoFiles)"
    Regex="AssemblyFileVersion\(&quot;(\d+)\.(\d+)\.(\d+)\.(\d+)"
    ReplacementText="AssemblyFileVersion(&quot;$1.$2.$3.$(SubVersionRevision)" />
    

    结果应该是:

    在Windows/Explorer/文件/属性中。

    程序集版本将为1.0.0.0。

    如果333是SVN版本,则文件版本将为1.0.0.333。

        12
  •  1
  •   Dan    17 年前

    小心。用于内部版本号的结构很短,因此您有一个上限来限制您的版本可以达到多高。

    在我们的例子中,我们已经超过了限制。

    如果尝试输入内部版本号99.99.99.599999,则文件版本属性实际上将显示为99.99.99.10175。