How can I get the automatic version of buildStamp CC.NET recorded in a global assembly file and specified by several .NET applications?

I am currently “automating the assembly” of our .NET solution (which contains many projects, at least 20. Some winForms, some web projects, all with different release configurations ... ugh!). I am using CruiseControl.NET (or CC.NET, whatevs) and nANT. In addition, DOS and Powershell and several other magic beans we do not need to go into :)

My goal is to create a build shortcut (in which I already have a half-working battle). The build label consists of the Min / Maj number and the SVN check number. This is good for us, and we are pleased with it. Now I need my .NET projects to reference the build number so that my QA members know what build number they are testing.

My version, denoting the nANT task, is as follows:

<project name="updateassemblyversion" default="updateassemblyversion">
<target name="updateassemblyversion" description="generates the version number">
    <echo message="Setting the build version to ${CCNetLabel}..." />
    <attrib file="AssemblyInfo.cs" readonly="false" />
    <asminfo output="AssemblyInfo.cs" language="CSharp">
        <imports>
            <import namespace="System" />
            <import namespace="System.Reflection" />
        </imports>
        <attributes>
            <attribute type="AssemblyVersionAttribute" value="${CCNetLabel}" />
            <attribute type="AssemblyFileVersionAttribute" value="${CCNetLabel}" />
        </attributes>
    </asminfo>
    <attrib file="AssemblyInfo.cs" readonly="true" />
</target>

In any case, I am trying to install the assembly on my build server. I read that it is not best practice to have 20+ AssemblyInfo.cs files for recording, so I manually created the GlobalyAssemblyInfo.cs file as a “solution item” that is associated with all my projects using “Add ... Existing item. .. Add a link. " I don't think this is what I need, though, since my version control will happen on the build server ...

, NANT ( ), , , , . AssemblyInfo.cs, versionstamps - . , , , , , , , "" script , . , , ? (. № 4).

:

  • 20 + AssemblyInfo.cs? ? , . , , Solution, , ?
  • GlobalAssemblyInfo.cs, nANT, , versionstamp it ?
  • GlobalAssemblyInfo ( ), , ? , , . AssemblyXYZ.cs( nANT) . ?
  • # 3 , CC.NET ( NANT?). , , VS GlobalAssemblyInfo.cs( nANT) 20+.NET ?
  • .NET () /versionstamp? , QA .

, :) !

+3
3

. , . , , , , .

nANT, "updateassemblyinfo.build":

<?xml version="1.0"?>
<project name="updateassemblyversion" default="updateassemblyversion">
    <target name="updateassemblyversion" description="generates the version number">
        <echo message="Setting the build version to ${CCNetLabel}..." />
        <attrib file="AssemblyInfo.cs" readonly="false" />
        <asminfo output="C:\CruiseControl\ProjectFolders\MyBigBadSolution\GlobalAssemblyInfo.cs" language="CSharp">
            <imports>
                <import namespace="System" />
                <import namespace="System.Reflection" />
            </imports>
            <attributes>
                <attribute type="AssemblyVersionAttribute" value="${CCNetLabel}" />
                <attribute type="AssemblyFileVersionAttribute" value="${CCNetLabel}" />
            </attributes>
        </asminfo>
        <attrib file="GlobalAssemblyInfo.cs" readonly="true" />
    </target>
</project>

, , : # Project Global AssemblyInfo

^ , . , , . AssemblyInfo.cs GlobalAssemblyInfo.cs. . . -, , .

5- " ", :

  • , AssemblyInfo ( ). . . .
  • CruiseControl.NET nANT, asminfo "output" GlobalAssemblyInfo.cs( ).
  • , . ( ), , . , , , .
  • , ANT. " ". .
  • , , :

    Assembly asm = Assembly.GetExecutingAssembly();
    this.Text = asm.GetName().Version.ToString();
    
+2

CC.NET... , NANT CC.NET, AssemblyInfo.cs . CC.NET .

using System.Collections.Generic; 
using System.IO; 
using Exortech.NetReflector; 
using ThoughtWorks.CruiseControl.Core;

namespace ccnet.AssemblyVersionUpdater.plugin {
[ReflectorType("assemblyRevisioner")]
  public class AssemblyRevisioner : ITask {
    public AssemblyRevisioner() {
    }

    [ReflectorProperty("rootFolder", Required = true)]
    public string RootFolder { get; set; }

    public void Run(IIntegrationResult result) {
        foreach (var file in GetAssemblyInfoFiles(RootFolder)) {
            var tmp = file + ".tmp";
            using (var sr = new StreamReader(file)) {
                using (var st = new StreamWriter(tmp)) {
                    string line;
                    var versionSet = false;
                    var fileVersionSet = false;
                    while ((line = sr.ReadLine()) != null) {
                        if (!versionSet && line.StartsWith("[assembly: AssemblyVersion(")) {
                            st.WriteLine("[assembly: AssemblyVersion(\"{0}\")]", result.Label);
                            versionSet = true;
                        } else {
                            if (!fileVersionSet && line.StartsWith("[assembly: AssemblyFileVersion(")) {
                                st.WriteLine("[assembly: AssemblyFileVersion(\"{0}\")]", result.Label);
                                fileVersionSet = true;
                            } else {
                                st.WriteLine(line);
                            }
                        }
                        st.Flush();
                    }
                    st.Close();
                }
                sr.Close();
            }
            File.SetAttributes(file, FileAttributes.Normal);
            File.Copy(tmp, file, true);
            File.Delete(tmp);
        }

    }

    private static IEnumerable<string> GetAssemblyInfoFiles(string b) {
        var result = new List<string>();
        var stack = new Stack<string>();
        stack.Push(b);
        while (stack.Count > 0) {
            var dir = stack.Pop();
            try {
                result.AddRange(Directory.GetFiles(dir, "AssemblyInfo.cs"));
                foreach (var dn in Directory.GetDirectories(dir)) {
                    stack.Push(dn);
                }
            } catch {
            }
        }
        return result;
    }

  } 
}

DLL CruiseControl.NET\server. , CC.NET. CC.NET:

<tasks>
   <assemblyRevisioner>
      <rootFolder>E:\Build\Source\Project1</rootFolder>
    </assemblyRevisioner>
     ...
</tasks>

AssemblyInfo.cs " " CC.NET . , , .

+2

1 - AssemblyInfo.cs. ( ) , COM.

2 - GlobalAssemblyInfo.cs( ) , . , . , . asminfo genera GlobalAssemblyInfo.cs ant, AssemblyVersion , .

3 - Personally, I like that the build process starts regardless of whether it is on the build server or only in the dev block. One reason is that it would be nice to be able to test the assembly without releasing the official assembly. Another one is that you may want to update the main dependency (version of SQL Server or something else) and see if everything is built, without actually updating the real build server.

4 - See answer 2

+2
source

All Articles