3

I would like to remove all blank lines (lines that just have some number of spaces and a newline character) from a list of files using MSBuild.

What is the best way to accomplish this?

I recognize that I could write a MSBuild plug in C# or VB.NET that will do this using simple Regex replacement, but would prefer a solution that doesn't need me to do this.

If there's a open source MSBuild plugin that does this - I'd welcome that solution as well.

Adam
  • 28,537
  • 15
  • 60
  • 73
  • 1
    Is an inline task out of the question? – Ritch Melton Nov 15 '11 at 22:35
  • Not at all --- I don't think I'm familiar with this --- would this be the ability to write C# in the MSBuild script file itself to accomplish this? – Adam Nov 15 '11 at 22:49
  • Yup, its basically a task (like you nixed), but inline. http://msdn.microsoft.com/en-us/library/dd722601.aspx – Ritch Melton Nov 15 '11 at 22:50
  • Apply [second RegEx from accepted solution](http://stackoverflow.com/questions/643113/regex-to-strip-comments-and-multi-line-comments-and-empty-lines) on [FileUpdate task](http://stackoverflow.com/questions/2026436/change-only-revision-number-in-assemblyinfo-cs-with-msbuild-fileupdate-task). (My answer was converted to comment) – Ludwo Nov 16 '11 at 07:53
  • 1
    I'm getting an error from FileUpdate saying that ReplacementText can't be null (empty string for the replacement) – Adam Nov 16 '11 at 16:01

1 Answers1

2

@Ludwo is right, you have to take into account white-space characters. Moreover to replace any text with empty string you need to use ReplacementTextEmpty property instead of passing empty string to ReplacementText property. So, the following target should solve the problem:

<Target Name="Minify">
  <ItemGroup>
    <File Include="**\*.cs" />
  </ItemGroup>
  <FileUpdate
    Files="@(File)"
    Regex="(\n\s*\n)+"
    Multiline="False"
    ReplacementTextEmpty="True"/>
</Target>

You just need to call the target via MSBuild:

msbuild MyProject.csproj /t:Minify
Alexander
  • 4,420
  • 7
  • 27
  • 42