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:
Jan 18

Lately I was trying to automate process of assigning assembly version for all my projects in solution. My tool of choice for build is MSBuild where is no appropriate support for file operations nor assembly version handling. I downloaded and installed MSBuild Community Task extensions which can be found at http://msbuildtasks.tigris.org/. Extension pack has FileUpdate task where you can find and replace specific text using regular expressions.

Build configuration file for assigning assembly version looks like:

<?xml version="1.0" encoding="utf-8"?>
<
Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
>
  <
Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"
/>
  <
PropertyGroup
>
    <
Major>3</Major
>
    <
Minor>5</Minor
>
    <
Build>1</Build
>
    <
Revision>0</Revision
>
  </
PropertyGroup
>
  <
ItemGroup
>
    <
AssemblyFile Include="Project1Folder\Properties\AssemblyInfo.cs"
/>
    <
AssemblyFile Include="Project2Folder\Properties\AssemblyInfo.cs"
/>
  </
ItemGroup
>
  <
Target Name="VersionUpdate"
>
    <
Message Text="Version: $(Major).$(Minor).$(Build).$(Revision)"
/>
   
<FileUpdate Files="@(AssemblyFile)"
        Regex='AssemblyVersion\("(\d+)\.(\d+)\.(\d+)\.(\d+)"\)
'
        ReplacementText='AssemblyVersion("$(Major).$(Minor).$(Build).$(Revision)")
'
        Condition=""
/>
    <
FileUpdate Files="@(AssemblyFile)
"
        Regex='AssemblyFileVersion\("(\d+)\.(\d+)\.(\d+)\.(\d+)"\)
'
        ReplacementText='AssemblyFileVersion("$(Major).$(Minor).$(Build).$(Revision)")
'
        Condition=""
/>
  </
Target
>
</
Project>

Tags: