TeamBuild 2010: Customized expandable parameters

TeamBuild 2010: Customized expandable parameters

Here’s a small post on how to add customized expandable parameters to a build workflow. What i mean by expandable parameters is parameters with a small + sign so that you can expand the parameter to fill its properties, the main line containing a summary of the values:

image

Nothing difficult, first create a class for your data:

public class VersioningSettings
{
    public string AssemblyInfoFileSpec { get; set; }

    public string AssemblyVersionFormat { get; set; }

    public string AssemblyFileVersionFormat { get; set; }

    public string AssemblyInformationalVersionFormat { get; set; }
}

Now we will decorate the properties to customized the display in the PropertyGrid. Here are some of the attributes we can use:

  • DisplayNameAttribute: the name to display instead of the property name.
  • DescriptionAttribute: a small description of the property displayed at the bottom of the PropertyGrid.
  • BrowsableAttribute: specifies whether the property should be displayed in the PropertyGrid.
  • RefreshPropertiesAttribute: specifies how the PropertyGrid should be refreshed when the value changes. The list of values can be found here.

Let’s add those attributes to our example:

public class VersioningSettings
{
    [DisplayName(@"AssemblyInfo Filespec")]
    [Description(@"Specifiy the search pattern for locating AssemblyInfo files - e.g. **\*AssemblyInfo*.cs.")]
    [RefreshProperties(RefreshProperties.All)]
    [Browsable(true)]
    public string AssemblyInfoFileSpec { get; set; }

    [DisplayName("AssemblyVersion Format")]
    [Description("Specify the AssemblyVersion format. Supported tokens are: $(current), $(increment), $(date:<format>).")]
    [RefreshProperties(RefreshProperties.All)]
    [Browsable(true)]
    public string AssemblyVersionFormat { get; set; }

    [DisplayName("AssemblyFileVersion Format")]
    [Description("Specify the AssemblyFileVersion format. Supported tokens are: $(current), $(increment), $(date:<format>).")]
    [RefreshProperties(RefreshProperties.All)]
    [Browsable(true)]
    public string AssemblyFileVersionFormat { get; set; }

    [DisplayName("AssemblyInformationalVersion Format")]
    [Description("Specify the AssemblyInformationalVersion format. Supported tokens are: $(version), $(fileversion), $(date:<format>).")]
    [RefreshProperties(RefreshProperties.All)]
    [Browsable(true)]
    public string AssemblyInformationalVersionFormat { get; set; }

    [Browsable(false)]
    public bool HasAssemblyInfoFileSpec
    {
        get
        {
            return !string.IsNullOrEmpty(this.AssemblyInfoFileSpec);
        }
    }
}

In the previous code i added the HasAssemblyInfoFileSpec property which is not visible in the PropertyGrid but which can be used in the workflow. Now we add attributes to the class:

  • SerializableAttribute: a class must be serializable to be used as a parameter in a workflow.
  • TypeConverterAttribute: specifies the type converter to use to display the class. In our case, we need to use the ExpandableObjectConverter type converter, this attribute is the one indicating the PropertyGrid that it should display a + and the objects’properties.

Here’s the result :

[Serializable]
[TypeConverter(typeof(ExpandableObjectConverter))]
public class VersioningSettings
{
    //...
}

To display a summary on our first line we should create our own TypeConverter inheriting from ExpandableObjectConverter and returning our summary using the ConvertTo method. Unfortunately there’s a bug in TeamExplorer and custom type converters cannot be used. This bug should be corrected in the next version of VisualStudio but for now we’ll have to use a workaround :(

As a workaround, we will override the ToString method on our class:

//...
public class VersioningSettings
{
    //...

    public override string ToString()
    {
        if (this.HasAssemblyInfoFileSpec)
        {
            var args = new List<string>();
            if (!string.IsNullOrEmpty(this.AssemblyVersionFormat))
            {
                args.Add(
                    string.Format("'{0}' for version", this.AssemblyVersionFormat)
                    );
            }

            if (!string.IsNullOrEmpty(this.AssemblyFileVersionFormat))
            {
                args.Add(
                    string.Format("'{0}' for file version", this.AssemblyFileVersionFormat)
                    );
            }

            if (!string.IsNullOrEmpty(this.AssemblyInformationalVersionFormat))
            {
                args.Add(
                    string.Format("'{0}' for informational version", this.AssemblyInformationalVersionFormat)
                    );
            }

            if (args.Count != 0)
            {
                return string.Format(
                    "Update AssemblyInfo files matching '{0}' using {1}",
                    this.AssemblyInfoFileSpec,
                    args.Aggregate((a1, a2) => a1 + ", " + a2)
                    );
            }
            else
            {
                return string.Format("Update AssemblyInfo files matching '{0}'", this.AssemblyInfoFileSpec);
            }
        }

        return string.Empty;
    }
}

Our class is now finished and can be used, just edit the workflow, add a reference to your assembly and add a new parameter. For deployment, you have to put the assembly in version control and configure the build controller. You can read my post on my UpdateAssemblyInfo activity for more details.

Carpe Diem.

You can find the complet code sample on codeplex: http://updateassemblyinfo.codeplex.com/SourceControl/changeset/view/84894#1658227

0 thoughts on “TeamBuild 2010: Customized expandable parameters

  1. Reply Pieter Gheysens Sep 8,2011 12:31

    Great post Guillaume! This was exactly what I was looking to customize the build process parameters.

Leave a Reply

  

  

  

CAPTCHA *