by Ewald Hofman
17. May 2010 07:06
In the series the following parts have been published
- Part 1: Introduction
- Part 2: Add arguments and variables
- Part 3: Use more complex arguments
- Part 4: Create your own activity
- Part 5: Increase AssemblyVersion
- Part 6: Use custom type for an argument
- Part 7: How is the custom assembly found
- Part 8: Send information to the build log
- Part 9: Impersonate activities (run under other credentials)
- Part 10: Include Version Number in the Build Number
- Part 11: Speed up opening my build process template
- Part 12: How to debug my custom activities
- Part 13: Get control over the Build Output
- Part 14: Execute a PowerShell script
- Part 15: Fail a build based on the exit code of a console application
- Part 16: Specify the relative reference path
In this post I will show you how you can create your own type for an argument, including an editor to change the value for the argument.
In this post I want to add an argument that holds an username and password. I will use this information to execute one of my activities with different credentials. Since I don’t want that the password is visible to anyone, I will create a new type called Credential. This has a UserName and Password property. In the properties window of the Process tab the Credential argument will only show the UserName. When I edit the argument a dialog is opened where I can enter the UserName and Password. If you want to make this option even more secure, you can even provide an encryption for the internal storage of the password. For the simplicity this is not implemented in this post.
At the end of the post, the classes, that are required for the solution, are attached for your convenience.
- Open the solution that is created in the following post: Customize Team Build 2010 – Part 4: Create your own activity.
- Create a new folder in the BuildTasks project called CustomType. In this folder all files required to support a custom type including the editor is stored.
Credential
In the credential class we will store the information that is required for the storage of the credentials. There is no magic in this class.
- Add a new class to this folder and call it Credential
- Add the UserName and Password property to this class.
- When editing the Process tab for the Build Definition, the ToString() function is called to retrieve the value. So override this function and return the UserName property.
- This will result in the following code
namespace BuildTasks.CustomType
{
public class Credential
{
public string UserName { get; set; }
public string Password { get; set; }
public override string ToString()
{
return UserName;
}
}
}
CredentialDialog
This dialog will be shown when an user edits the value for the argument in the Process tab of the Build Definition. It gives you the ability to provide a user friendly method of providing values.
- Add a new form to this folder and call it CredentialDialog
- Create an UI like this and call the two textboxes UserNameTextbox and PasswordTextbox.
- Now add the following code to the textbox:
using System.Windows.Forms;
namespace BuildTasks.CustomType
{
public partial class CredentialDialog : Form
{
public CredentialDialog()
{
InitializeComponent();
}
public string Password
{
get
{
return PasswordMaskedTextbox.Text;
}
set
{
PasswordMaskedTextbox.Text = value;
}
}
public string UserName
{
get
{
return UserNameTextbox.Text;
}
set
{
UserNameTextbox.Text = value;
}
}
}
}
CredentialEditor
This class contains the metadata for Visual Studio what you want to do when the user clicks on the ellipsis button of the argument
- Add a new class to the CustomType folder and call it CredentialEditor.
- This class overrides from the UITypeEditor class. You should override two methods, being EditValue and GetEditStyle. In the GetEditStyle you can tell Visual Studio to open up a combobox or a dialog. In this post I choose to open a dialog.
- In the EditValue you add the logic that is executed when the user clicks on the ellipsis button.
- The full code of the class is the following:
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.ComponentModel;
using System;
using System.Windows.Forms;
namespace BuildTasks.CustomType
{
class CredentialEditor : UITypeEditor
{
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (provider != null)
{
IWindowsFormsEditorService editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (editorService != null)
{
Credential credential = value as Credential;
using (CredentialDialog dialog = new CredentialDialog())
{
dialog.UserName = credential.UserName;
dialog.Password = credential.Password;
if (editorService.ShowDialog(dialog) == DialogResult.OK)
{
credential.UserName = dialog.UserName;
credential.Password = dialog.Password;
}
}
}
}
return value;
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
}
}
Metadata
Now we add a new argument with the correct type and provide metadata to the Build Process Template so Visual Studio knows it has to execute the logic that is specified in the CredentialEditor.
- First build the BuildTasks project so we have access to the new Credentials type.
- Now open the CustomTemplate.xaml Build Process Template
- Open the Arguments list and add a new argument called Credentials
- In the Argument Type choose for the “Browse for Types …” option
- In the Browse dialog, navigate to the BuildTasks reference and choose the Credential class.
- Click on OK to close the dialog
- Now locate the Metadata argument in the list to provide metadata for this argument. When you have found it, click on the ellipsis button. This will open the Metadata editor.
- Add the Credentials parameter. The most exciting we do in this dialog is providing the Editor value. You have to give it the value of the CredentialEditor class. You specify the reference to this class in the for of <full namespace>.<class>,<assembly name>. In our case, this is BuildTasks.CustomType.CredentialEditor,BuildTasks
- Save the CustomTemplate.xaml file and check it in.
Test the dialog
- Edit the build definition that is using the Build Process Template and go to the Process tab. You will now see a new argument called Credential.
- When you click on the ellipsis, you will see the new dialog.
- When you accept the values, you will see the following in the Credential argument
You can download the full solution at BuildProcess.zip. It will include the sources of every part and will continue to evolve.