Here it is, what everyone has been waiting for.. an MS build implementation of fizz buzz.


<Project DefaultTargets="CleanupRecur"
        <Currval Condition="'$(Currval)' == ''">1</Currval>
        <FileBaseName Condition="'$(FileBaseName)' == ''">recurfile</FileBaseName>
        <NextIndex>$([MSBuild]::Add($(Currval), 1))</NextIndex>
        <Mod3 Condition="$([MSBuild]::Modulo($(Currval), 3)) == 0">true</Mod3>
        <Mod5 Condition="$([MSBuild]::Modulo($(Currval), 5)) == 0">true</Mod5>

    <Target Name="CopyFile">
        <Message Text = "$(NextIndex)" />
        <Copy Condition="$(Currval) &lt; 100"
        DestinationFiles="$(NextMsbuildFile)" />
    <Target Name="Fizz" DependsOnTargets="CopyFile">
        <Message Condition="'$(Mod3)' == 'true' AND '$(Mod5)' != 'true'" Text="Fizz" Importance="high"/>
        <Message Condition="'$(Mod5)' == 'true' AND '$(Mod3)' != 'true'" Text="Buzz" Importance="high"/>
        <Message Condition="'$(Mod3)' == 'true' AND '$(Mod5)' == 'true'" Text="FizzBuzz" Importance="high"/>
        <Message Condition="'$(Mod3)' != 'true' AND '$(Mod5)' != 'true'" Text="$(Currval)" Importance="high"/>
        <MSBuild Condition="$(Currval) &lt; 100" Projects="$(NextMsbuildFile)" Targets="CleanupRecur" Properties="Currval=$(NextIndex)"></MSBuild>

    <Target Name="CleanupRecur" DependsOnTargets="Fizz">
        <Delete Files="$(NextMsbuildFile)" />

You can run it as follows.


# Pass the minimal flag to avoid a bunch of extra msbuild output
msbuild /v:minimal .\fizz.proj


# The project works by getting the current value of the count from an environment variable "Currval".
# It evaluates the mod3 and mod5 of Currval

# It then makes a copy of its own project file, to avoid MSBuild detecting any circular dependencies
# It than executes MSBuild on the new project file, since it is a dependency, passing the incremented environment variable to the new project

# Then it cleans up the newly copied file

There you have it. A working implementation of fizzbuzz using MSBuild.