Wednesday, September 15, 2010

MSBuild: Copy a list of files to a list of directories

I have config files in a master directory (team does not allow svn:externals, which I believed was designed for this task, perhaps I did not set it up correctly) that I need to svn update and copy to my modules.


 <PropertyGroup>
  <LocalBranchPath>C:\branches\November<\LocalBranchPath>
  <LocalSourcePath>$(LocalBranchPath)\Source<\LocalSourcePath>
  <TortoisePath>C:\Program Files\TortoiseSVN\bin\TortoiseProc.exe<\TortoisePath>
  <ClientConfigPath>$(LocalSourcePath)\MasterDirectory<\ClientConfigPath>
  <TortoiseUpdate>&quot;$(TortoisePath)&quot; /closeonend:1 /command:update /path:<\TortoiseUpdate>
  <ReferencedAssembliesPath>$(LocalSourcePath)\ReferencedAssemblies<\ReferencedAssembliesPath>
  <DbProjLocalPath>$(LocalBranchPath)\Database\DBProjects<\DbProjLocalPath>
  <PropertyGroup>
<ItemGroup>
 <MyModules Include="$(LocalSourcePath)\CommonControls\LimitsInformation;
  $(LocalSourcePath)\CommonControls\BalanceInquiry;
  $(LocalSourcePath)\CommonControls\AuditTrail;
  $(LocalSourcePath)\Administration\Reassignment;
  $(LocalSourcePath)\IndividualControls\Configuration\ActivityList"
 />   
</ItemGroup>

 I need the config files in ClientConfigPath

to be copied to the subfolder of MyModules called ModuleName.UnitTest





let's get the svn update out of the way 





<Target Name="SvnUpdateClientConfigs">
 <Exec Command="$(TortoiseUpdate)&quot;$(ClientConfigPath)&quot;"
/><Target>

Very short and sweet, now the hard part, copying multiple files to multiple subdirectories. I had no idea FileName was a shortcut in this case for the final directory name only, it came in handy.





<Target Name="CopyClientConfigsBatched" Outputs="%(MyModules.FullPath)">
  <Message Text="@(MyModules -> '%(FullPath)\%(FileName).UnitTest')"/>
 <ItemGroup>
  <ClientConfigs
  Include="$(ClientConfigPath)\*.config"
  Exclude="$(ClientConfigPath)\P*.config" >
  </ClientConfigs>
 <ItemGroup>
 <Copy SourceFiles="@(ClientConfigs)" DestinationFolder="@(MyModules -> '%(FullPath)\%(FileName).UnitTest')"
   SkipUnchangedFiles="true"/>
<Target/>





The Outputs was necessary because Copy.DestinationFolder will not accept multiple items.
So first I build the list of files, which could have been done at the project level but this was the only target that needed it, and those files may not be present until the svn update was run.  The Exclude prevents PolicyCache.config from being part of the copy. SkipUnchangedFiles makes sure files that haven't changed aren't copied wasting time.




No comments:

Post a Comment