GoRoS
GoRoS

Reputation: 5375

How to get an assembly's version in Team Build 2010 Workflow

Is there any way to get an assembly's version number like we previously used to do with Msbuild with Team Build 2010 Workflow? Here is a simple example of how we used to get an assemby's version with %(Info.Version).

  <Target Name="CheckFileVersion" DependsOnTargets="AfterDrop">
   <ItemGroup>
      <MyAssemblies Include='$(DropLocation)\$(BuildNumber)\Release\MyApp.exe' />
    </ItemGroup>
    <GetAssemblyIdentity AssemblyFiles="@(MyAssemblies)">
      <Output TaskParameter="Assemblies" ItemName="Info"/>
    </GetAssemblyIdentity>
  </Target>

I've found some methods to create custom Activities with many lines of code, but I think there must be a simpler way to do it.

Upvotes: 4

Views: 879

Answers (3)

Stanley.Goldman
Stanley.Goldman

Reputation: 4337

GoRoS had the right idea, but it only works correctly the first time.

In order to avoid this problem you have to run this code, or similar code in a different AppDomain from the Build Agent, Powershell and an InvokeProcessActivity can solve our problem.

GetAssemblyVersionNumber.ps1:

$error.clear()

if ($args.length -ne 1)
{
   Write-Error "Usage: GetAssemblyVersionNumber.ps1 <Assembly>"
   exit 1
}

# Now load the assembly
$assembly = [System.Reflection.Assembly]::Loadfile($args[0])

# Get name, version and display the results
$name = $assembly.GetName()
Write-Host $name.version

And the following gets added to your build process template

InvokeProcessActivity
    Name: Call Powershell Version Script
    Filename: "powershell.exe"
    Arguments: "-NonInteractive -NoProfile -Command " _
               + "c:\Builds\GetAssemblyVersionNumber.ps1" _
               + " '" + BuildDirectory + "\Binaries\Foobar.dll'"
    Handle Standard Output (stdOutput)

        AssignActivity
            To: VersionInfo
            Value: stdOutput

Upvotes: 3

GoRoS
GoRoS

Reputation: 5375

Finally I found a quick easy way to perform the operation I wanted. Using a simple Assign activity I've written the following code:

To:     VersionInfo
Value:  System.Reflection.Assembly.UnsafeLoadFrom(BuildDetail.DropLocation + _ 
        "\MyApplication.exe").GetName().Version.ToString()

After that, I can use VersionInfo variable to whatever I want. I recognize I'd prefer to avoid using reflection, but that's the easy and short way I was finding which doesn't use third party libraries or custom activities.

Upvotes: 1

Dylan Smith
Dylan Smith

Reputation: 22245

You can call out to MSBuild scripts from TFS Build Workflows using the MSBuild Workflow Activity and continue doing this the same way as always. Or you could create a custom Workflow Activity to do a similar thing. The best way will probably depend on what you intend to do with those version #'s.

You can read about how to get started creating a custom activity here: http://blogs.msdn.com/b/jimlamb/archive/2010/02/12/how-to-create-a-custom-workflow-activity-for-tfs-build-2010.aspx

Also there is the TFS Community Build Extensions which has a bunch of ready-made custom activities you can use. One of those is called AssemblyInfo which sounds promising: http://tfsbuildextensions.codeplex.com/documentation

Upvotes: 1

Related Questions