Oz

Hi all,


I have been getting to grips with MSBuild today, but seem to be having a problem doing something relatively simple!


I'd like to copy all my source folders to a certain folder BEFORE the build, then build the files that are in that folder. I've got the copying of source files working, then I modifiy one of the source files to replace a token with a property value. The problem I have is getting the copied source files to be used as the inputs to the compiler.


Does anyone have any suggestions I'm happy to post some code snippets of what I have if required. This seems like such a common thing to do, but the compiler always uses the orginal source files!


Many thanks in advance,




Mark



Re: Visual Studio MSBuild Copy Source Files to Folder, then Build Source From that Folder

sluggy

You have a solution file, right Are you building by invoking msbuild on that solution file Does that solution file have full (not relative) paths in it

The other possibility is that you have an incorrect path in your msbuild file, probably due to an incorrect string (property) substitution.





Re: Visual Studio MSBuild Copy Source Files to Folder, then Build Source From that Folder

Oz

Hi sluggy,

You were dead right - an incorrect string in my token replacement target. The transformation was working perfectly, but my token replacement was taking place on the original file, instead of the transformed file in the new location.

Thanks very much for taking the time to help me out.

Kind regards,


Mark






Re: Visual Studio MSBuild Copy Source Files to Folder, then Build Source From that Folder

Oz

My apologies sluggy - the problem is still present.


Let me give you some more information:


I have a csproj file, which I have customised to add extra build targets. My source file list is as follows. This is just dumped out by Visual Studio, and not edited by me:


Code Snippet

<ItemGroup>
<Compile Include="WindowsService.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="WindowsService.Designer.cs">
<DependentUpon>WindowsService.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="ProjectInstaller.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="ProjectInstaller.Designer.cs">
<DependentUpon>ProjectInstaller.cs</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>



Before the build target is invoked, the following two targets are invoked in order:


Code Snippet

<Target Name="CopySourceFilesToBuildDirectory">
<MakeDir Directories="$(BuildDir)" />
<Copy SourceFiles="@(Compile->'%(FullPath)')" DestinationFiles="@(Compile->'$(BuildDir)\%(RelativeDir)\%(FileName)%(Extension)')" />
</Target>



Code Snippet

<Target Name="TokeniseServiceProjectInstaller">
<FileUpdate Files="$(BuildDir)\ProjectInstaller.Designer.cs"
Regex="SERVICE_NAME"
ReplacementText="$(ServiceName)"
/>
<FileUpdate
Files="$(BuildDir)\ProjectInstaller.Designer.cs"
Regex="SERVICE_DISPLAY_NAME"
ReplacementText="$(ServiceDisplayName)"
/>
</Target>


As can be seen, the project source as definied by the "compile" items is copied to a folder specified by $(BuildDir), then the "ProjectInstaller.Designer.cs" file in $(BuildDir) is edited by the FileUpdate task (available from tigris). The "SERVICE_NAME" and "SERVICE_DISPLAY_NAME" tokens are replaced by property values.


The default build target then runs, but seems to be using the files from the source directory, instead of the modified files in $(BuildDir).



Mark





Re: Visual Studio MSBuild Copy Source Files to Folder, then Build Source From that Folder

Simon Dahlbacka

Well, your problem stems from the fact that the Csc task takes Sources="@(Compile)" (see Microsoft.Csharp.targets)

Unfortunately, AFAIK there isn't a way to "clear" an itemgroup and re-populate it, (if you happen to find one, I'd be interested to know about it)

As a hack, you *could* roll your own Csc task, copy-pasted and just replace Sources="@(Compile)" with an itemgroup that you populated with the "new" sources





Re: Visual Studio MSBuild Copy Source Files to Folder, then Build Source From that Folder

Oz

Hi Simon,


Thanks very much for your response. I was aware that the Csc task took the @(Compile) parameter. I was under the impression that the transform I applied when performing the file copy actually transformed the original ItemGroup.


This seems like such a common request to be able to do! I'm surpised MSBuild can't handle it!! Are there any MVPs / MSBuild developers around to comment on this at all


Kindest regards,



Mark





Re: Visual Studio MSBuild Copy Source Files to Folder, then Build Source From that Folder

Oz

Ok, well I've found a workaround, but it's pretty nasty!!

I've changed each "compile" tag in the compile itemgroup to be called "source". i.e.:

Code Snippet

<ItemGroup>
<Source Include="WindowsService.cs">
<SubType>Component</SubType>
</Source>
<Source Include="WindowsService.Designer.cs">
<DependentUpon>WindowsService.cs</DependentUpon>
</Source>
<Source Include="Program.cs" />
<Source Include="ProjectInstaller.cs">
<SubType>Component</SubType>
</Source>
<Source Include="ProjectInstaller.Designer.cs">
<DependentUpon>ProjectInstaller.cs</DependentUpon>
</Source>
<Source Include="Properties\AssemblyInfo.cs" />
</ItemGroup>


I then changed my useage of the copy task and grabbed the "CopiedFiles" parameter out and assigned it to, surprise surprise, @(Compile).

That means that the modified files are then passed to the compiler.

This seems mad! Is there really no way to re-assign/clear an item group. Some assignment along the lines of the following is exactly what I need:

@(Compile->'%(FullPath)' = @(Compile->'$(NewPath)\%(RelativeDir)\%(FileName)%(Extension)'




Re: Visual Studio MSBuild Copy Source Files to Folder, then Build Source From that Folder

Simon Dahlbacka

I was under the impression that the transform I applied when performing the file copy actually transformed the original ItemGroup.

Well, as you realized it doesn't, and most of the times, that's a Good Thing(tm)

This seems like such a common request to be able to do! I'm surpised MSBuild can't handle it!!


I've wondered the same, and some part of me still hopes I've overlooked something, but I doubt that it is really the case...