User Interaction in MSBuild


Although most MSBuild applications will happily run with no user interaction, sometimes it may be useful to prompt a user with a confirmation or request a value for use in further, possibly conditional, processing.

In the following simple examples I’ll use a few tasks from the MSBuild Extension Pack to illustrate various options for interacting with the user.

Note that many of the tasks used here provide additional properties for customising their usage. Please consult the latest help file for full details.

Alert and Delay

Although it requires no physical interaction from the user, a simple combination of playing a sound, displaying a message and then giving the user a chance to read the message can provide a simple but valuable user experience.

Sample:

<!– Play an audible alert –>

<MSBuild.ExtensionPack.UI.Console TaskAction=”Beep” Repeat=”10″ Duration=”5000″ Frequency=”1000″/>

<!– or play a custom sound –>

<!– <MSBuild.ExtensionPack.Multimedia.Sound TaskAction=”Play” SoundFile=”C:\Windows\Media\notify.wav” Repeat=”10″/>–>

<!– Display a message –>

<Message Text=”Please take note of this important announcement” Importance=”High”/>

<!– Give the user time to read the message –>

<MSBuild.ExtensionPack.Framework.Thread TaskAction=”Sleep” Timeout=”5000″/>

Console Interaction

The Console class provides the ability to read a line of input from the user. This can also be used to pause the processing of the file until the user presses an [Enter] key.

Sample:

<!– Play an audible alert –>

<MSBuild.ExtensionPack.UI.Console TaskAction=”Beep”/>

<!– Ask the user to press enter when ready to continue –>

<MSBuild.ExtensionPack.UI.Console TaskAction=”ReadLine” UserPrompt=”Press [Enter] to continue…”/>

<!– Read input from the user –>

<MSBuild.ExtensionPack.UI.Console TaskAction=”ReadLine” UserPrompt=”Please enter your Name and press [Enter]” ToUpper=”true”>

<Output TaskParameter=”UserResponse” PropertyName=”Line”/>

</MSBuild.ExtensionPack.UI.Console>

<Message Text=”The user name is: $(Line)”/>

Output:

C:\msbuild UserInteraction.proj

Microsoft (R) Build Engine Version 3.5.30729.1

Build started 09/11/2008 17:01:09.

Project “C:\UserInteraction.proj” on node 0 (default targets).

Press [Enter] to continue…

Please enter your Name and press [Enter]

mike

The user name is: MIKE

Done Building Project “C:\UserInteraction.proj” (default targets).

Build succeeded.

WinForm Interaction

With so much console use in MSBuild, it can be a welcome break to switch to a Windows Form to interact with the user. The Dialog task provides a way for displaying prompts and gathering normal and sensitive input.

Sample:

<!– A simple message –>

<MSBuild.ExtensionPack.UI.Dialog TaskAction=”Show” Text=”Hello. Press OK to continue…”/>

<!– Give the user the option to cancel processing–>

<MSBuild.ExtensionPack.UI.Dialog TaskAction=”Show” Text=”Click cancel if you’ve had enough…” Button1Text=”Continue” Button2Text=”Cancel” Title=”You Decide!”>

<Output TaskParameter=”ButtonClickedText” PropertyName=”Clicked”/>

</MSBuild.ExtensionPack.UI.Dialog>

<Error Condition=”$(Clicked) == ‘Cancel'”/>

<Message Text=”User Clicked: $(Clicked)”/>

<!– A simple prompt for input –>

<MSBuild.ExtensionPack.UI.Dialog TaskAction=”Prompt” Title=”Information Required” Button2Text=”Cancel” Text=”Please enter your Name below”>

<Output TaskParameter=”ButtonClickedText” PropertyName=”Clicked”/>

<Output TaskParameter=”UserText” PropertyName=”Typed”/>

</MSBuild.ExtensionPack.UI.Dialog>

<Message Text=”User Clicked: $(Clicked)”/>

<Message Text=”User Typed: $(Typed)”/>

<!– A prompt for password input –>

<MSBuild.ExtensionPack.UI.Dialog TaskAction=”Prompt” Title=”Sensitive Information Required” Button2Text=”Cancel” Text=”Please enter your Password below” MessageColour=”Red” MaskText=”true”>

<Output TaskParameter=”ButtonClickedText” PropertyName=”Clicked”/>

<Output TaskParameter=”UserText” PropertyName=”Typed”/>

</MSBuild.ExtensionPack.UI.Dialog>

<Message Text=”User Clicked: $(Clicked)”/>

<Message Text=”User Typed: $(Typed)”/>

<OnError ExecuteTargets=”ErrorTarget”/>

Output:

clip_image002

clip_image004

Assuming I click Cancel, then all processing would stop:

C:\msbuild UserInteraction.proj

Microsoft (R) Build Engine Version 3.5.30729.1

Build started 09/11/2008 17:01:09.

Project “C:\UserInteraction.proj” on node 0 (default targets).

The user has terminated processing.

Done Building Project “C:\UserInteraction.proj” (default targets). — FAILED

Build FAILED.

If I clicked continue, we would see

clip_image006

clip_image008

Full Sample

<Project ToolsVersion=”3.5″ DefaultTargets=”Default” xmlns=”http://schemas.microsoft.com/developer/msbuild/2003″&gt;

<PropertyGroup>

<TPath>$(MSBuildProjectDirectory)\..\MSBuild.ExtensionPack.tasks</TPath>

<TPath Condition=”Exists(‘$(MSBuildProjectDirectory)\..\..\Common\MSBuild.ExtensionPack.tasks’)”>$(MSBuildProjectDirectory)\..\..\Common\MSBuild.ExtensionPack.tasks</TPath>

</PropertyGroup>

<Import Project=”$(TPath)”/>

<Target Name=”Default”>

<!– Play an audible alert –>

<MSBuild.ExtensionPack.UI.Console TaskAction=”Beep” Repeat=”10″ Duration=”5000″ Frequency=”1000″/>

<!– or play a custom sound –>

<!– <MSBuild.ExtensionPack.Multimedia.Sound TaskAction=”Play” SoundFile=”C:\Windows\Media\notify.wav” Repeat=”10″/>–>

<!– Display a message –>

<Message Text=”Please take note of this important announcement” Importance=”High”/>

<!– Give the user time to read the message –>

<MSBuild.ExtensionPack.Framework.Thread TaskAction=”Sleep” Timeout=”5000″/>

<!– Play an audible alert –>

<MSBuild.ExtensionPack.UI.Console TaskAction=”Beep”/>

<!– Ask the user to press enter when ready to continue –>

<MSBuild.ExtensionPack.UI.Console TaskAction=”ReadLine” UserPrompt=”Press [Enter] to continue…”/>

<!– Read input from the user –>

<MSBuild.ExtensionPack.UI.Console TaskAction=”ReadLine” UserPrompt=”Please enter your Name and press [Enter]” ToUpper=”true”>

<Output TaskParameter=”UserResponse” PropertyName=”Line”/>

</MSBuild.ExtensionPack.UI.Console>

<Message Text=”The user name is: $(Line)”/>

<!– A simple message –>

<MSBuild.ExtensionPack.UI.Dialog TaskAction=”Show” Text=”Hello. Press OK to continue…”/>

<!– Give the user the option to cancel processing–>

<MSBuild.ExtensionPack.UI.Dialog TaskAction=”Show” Text=”Click cancel if you’ve had enough…” Button1Text=”Continue” Button2Text=”Cancel” Title=”You Decide!”>

<Output TaskParameter=”ButtonClickedText” PropertyName=”Clicked”/>

</MSBuild.ExtensionPack.UI.Dialog>

<Error Condition=”$(Clicked) == ‘Cancel'”/>

<Message Text=”User Clicked: $(Clicked)”/>

<!– A simple prompt for input –>

<MSBuild.ExtensionPack.UI.Dialog TaskAction=”Prompt” Title=”Information Required” Button2Text=”Cancel” Text=”Please enter your Name below”>

<Output TaskParameter=”ButtonClickedText” PropertyName=”Clicked”/>

<Output TaskParameter=”UserText” PropertyName=”Typed”/>

</MSBuild.ExtensionPack.UI.Dialog>

<Message Text=”User Clicked: $(Clicked)”/>

<Message Text=”User Typed: $(Typed)”/>

<!– A prompt for password input –>

<MSBuild.ExtensionPack.UI.Dialog TaskAction=”Prompt” Title=”Sensitive Information Required” Button2Text=”Cancel” Text=”Please enter your Password below” MessageColour=”Red” MaskText=”true”>

<Output TaskParameter=”ButtonClickedText” PropertyName=”Clicked”/>

<Output TaskParameter=”UserText” PropertyName=”Typed”/>

</MSBuild.ExtensionPack.UI.Dialog>

<Message Text=”User Clicked: $(Clicked)”/>

<Message Text=”User Typed: $(Typed)”/>

<OnError ExecuteTargets=”ErrorTarget”/>

</Target>

<Target Name=”ErrorTarget”>

<Message Text=”The user has terminated processing.”/>

</Target>

</Project>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s