1

Problem

I have code like this in my msbuild files, all over the place:

<Message
    Text="$(NewLine)==============================================$(NewLine)"
    Importance="High" />
<Message
    Text="       CLEAN UP        "
    Importance="High" />
<Message
    Text="$(NewLine)==============================================$(NewLine)"
    Importance="High" />

Goal

now i would like to harmonize output and do reduce code redundancy and extract the message formatting logic to some kind of task or whatnot.

This is how i'm imagine the usage:

<Message Text="$(PrintInBox(CLEAN UP))" Importance="High"/>

so that i could also reuse it in

<Error Text="$(PrintInBox(CLEAN UP))"/>

If that is absolutely not possible, something like

<PrintInBox Text="CLEAN UP"/>

would be the next best thing.


What i found so far

The "best" thing i found so far is "inline tasks". but do i really need to write c# code to get the job done? It's just some simple string concatenation,...

Also using the custom task would be almost as cumbersome as duplicating the code all over, i guess it would look something like this:

<FormatInBox Text="CLEAN UP">
    <Output TaskParameter="Result" ItemName="FormattedText"/>
</FormatInBox>

<Message Text="@(FormattedText)" Importance="High"/>

or if i create more specific tasks:

<MessageInBox Text="CLEAN UP" Importance="High" />

and

<WarnInBox Text="CLEAN UP" Condition="..." />

and

<ErrorInBox Text="CLEAN UP" Condition = "..." />

but this will make it necessary to duplicate some of the parameters and pass them along. Also there's still code duplication across the MessageInBox, WarnInBox and ErrorInBox tasks.

Question

How can i achieve the simplest interface for the consumer? How can i reduce code-redundancy best? Is there really no better way then the one i found before? Can i create an msbuild task which consist of msbuild xml, not C# or VB.net or Javascript or whatnot?

1 Answer 1

1
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Box>==============================================&#10;       Text&#10;==============================================</Box>
  </PropertyGroup>

  <Target Name="Foo">
    <Message Text="$(Box.Replace(Text, CLEAN UP 1))" Importance="High" />
    <Warning Text="$(Box.Replace(Text, CLEAN UP 2))" />
    <Error Text="$(Box.Replace(Text, CLEAN UP 3))" />
  </Target>
</Project>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. But how can i call it and pass a parameter ("CLEAN UP")? As i understand it, CallTarget cannot be used to pass parameters. Also properties and item definitions are only visible to other targets once a target finishes.
Oh ok i now realised that i could define the Box property outside of the Foo target - "globally" so to speak. then i can use "$(Box.Replace(Text, CLEAN UP 3))" anywhere i like to.
@BatteryBackupUnit Yes, you can define or redefine it anywhere. Personally, I have a custom task and resource file with headers ran through taag, or sometimes even embedded cygwin version of FIGlet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.