Thursday, March 25, 2010

Open XML SDK 2.0 went RTM!

Microsoft finally released the so awaited version 2.0 of its Open XML SDK, which is supposed to radically improve the performances compared to v1.0 that was simply unusable!

I did not have time yet to test it out but I will try posting some results later on.

In the meantime, you can download it here

Tuesday, March 23, 2010

MSBuild - Cleaning a folder

It is a common need to clean the destination folder (deleting all files and folders) when you automate your deployment builds. Unfortunately, MSBuild does not have a built-in task that does it all for you, and googling about it does not give you a clear answer.

It is however pretty easy to do by combining the Delete and RemoveDir tasks together. Below is a code snippet that does it.

The code should be self-explanatory enough with the few comments.


<Project ToolsVersion="3.5" DefaultTargets="Default" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <!-- Folder to clean - Trailing slash required -->
        <DeleteRoot>D:\Test\Deploy\</DeleteRoot>
    </PropertyGroup>
    <Target Name="Default">
        <ItemGroup>
            <!-- Item to get all files recursively in the DeleteRoot folder -->
            <FilesToDelete Include="$(DeleteRoot)\**\*.*" />
            <!-- Item to get all folders from the files to be deleted -->
            <FoldersToDelete Include="%(FilesToDelete.RootDir)%(FilesToDelete.Directory)" Exclude="$(DeleteRoot)"/>
        </ItemGroup>
        <!-- Display what will be deleted -->
        <Message Text=" # @(FilesToDelete)" Importance="normal" />
        <Message Text=" # @(FoldersToDelete)" Importance="normal" />
        <!-- Delete the files -->
        <Delete Files="@(FilesToDelete)" Condition=" $(DeleteRoot)!=''" /> 
        <!-- Remove the folders -->
        <RemoveDir Directories="@(FoldersToDelete)" Condition="$(DeleteRoot)!=''" />
    </Target>
</Project>