Jan 22

Building web projects using MSBuild and versioned by SubVersion a.k.a. SVN is quite challenging task Embarassed. On the end of process it's recommended to delete all .svn folders which are not mandatory for deployment. I thought this will be easy task to achieve, but I was wrong. MSBuild uses ItemGroup-s to recursively list files to user defined item. But you cannot recursively list directories to an item:

<ItemGroup>
  <
Files Include="$(TargetDir)\**\*"
/>
</
ItemGroup
>

I spend hours searching google and found "cool trick" to recursively delete folders:
1. Prepare item with all files
2. Prepare item with all folders -> extract root dir and directory from file item
3. Prepare item with .svn folders -> add ..\.svn to folder created at step 2. We need to go one level down because each folder may not contain files.
4. Remove folder created in step 3.
Final target for cleaning files look like: 

<Target Name="CleanTargetDir">
  <!-- 1. -->
  <
ItemGroup
>
    <
Files Include="$(TargetDir)\**\*"
/>
  </
ItemGroup
>
  <!-- 2. -->
  <
ItemGroup
>
    <
Folders Include="@(Files->'%(RootDir)%(Directory)')"
/>
  </
ItemGroup
>
  <!-- 3. -->
  <
ItemGroup
>
    <
FoldersToDelete Include="@(Folders->'%(RootDir)%(Directory)..\.svn')"
/>
  </
ItemGroup
>
  <!-- 4. -->
 
<RemoveDir Directories="@(FoldersToDelete)"/>
</
Target>

 If anyone have better solution I'll be glad to hear it.

 

Tags:
Comments are closed