Trouble with File.Replace

Mar 3, 2009 at 1:32 PM
I'm having some trouble using the File class with the Replace command. For a simple test all I'm trying to do is check-out my .csproj file, edit a value, publish the project, check the file back in. Here is currently what I have:

 <Target Name="AfterGet">
    <Message Text="In AfterGet"/>
    <MSBuild.ExtensionPack.VisualStudio.TfsSource TaskAction="Checkout"
                                                  ItemPath="$(SolutionRoot)\Main\Source\Alien\Source\Scfo.Alien\Scfo.Alien.csproj"
                                                  />
  </Target>
 
  <Target Name="BeforeCompile">
    <Message Text="In BeforeCompile"/>
    <MSBuild.ExtensionPack.FileSystem.File TaskAction="Replace"
                                           RegexPattern="&lt;PublishUrl&gt;.*?&lt;/PublishUrl&gt;"
                                           Replacement="&lt;PublishUrl&gt;\\devnet\clickonceroot\Alien\Development\&lt;/PublishUrl&gt;"
                                           Files="$(SolutionRoot)\Main\Source\Alien\Source\Scfo.Alien\Scfo.Alien.csproj"
                                           />

    <...Some other tasks that are removed from brevity...>
    
  </Target>
 
  <Target Name="AfterCompile">
    <Message Text="In AfterCompile" />
    <Message Text="Publishing build..." />
    <MSBuild Projects="$(SolutionRoot)\Main\Source\ALien\Source\Scfo.Alien\Scfo.Alien.csproj"
             Targets="Publish" />

    <Message Text="Checking Files back in..." />
    <MSBuild.ExtensionPack.VisualStudio.TfsSource TaskAction="Checkin"
                                                  ItemPath="$(SolutionRoot)\Main\Source\ALien\Source\Scfo.Alien\Scfo.Alien.csproj"
                                                  Comments="Testing"
                                                  ContinueOnError="true"
                                                  />
  </Target>

  <Target Name="BeforeOnBuildBreak"
          Condition="'$(IsDesktopBuild)'!='true'">
    <MSBuild.ExtensionPack.VisualStudio.TfsSource TaskAction="UndoCheckout"
                                                  ItemPath="$(SolutionRoot)\Main\Source\ALien\Source\Scfo.Alien\Scfo.Alien.csproj"

The .csproj file gets checked out successfully according to the build log:
Using "MSBuild.ExtensionPack.VisualStudio.TfsSource" task from assembly "C:\Program Files\MSBuild\ExtensionPack\MSBuild.ExtensionPack.dll".
Task "MSBuild.ExtensionPack.VisualStudio.TfsSource"
  Resolve TF.exe path
  TF.exe path resolved to: C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\..\IDE\tf.exe
  TF Operation: Checkout
  Executing C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\..\IDE\tf.exe checkout "E:\Builds\ALien\AlienDevelopment\Sources\Main\Source\Alien\Source\Scfo.Alien\Scfo.Alien.csproj"  /noprompt /recursive
 
  E:\Builds\ALien\AlienDevelopment\Sources\Main\Source\ALien\Source\Scfo.Alien:
  Scfo.Alien.csproj

The File.Replace action also appears to work successfully:
Using "MSBuild.ExtensionPack.FileSystem.File" task from assembly "C:\Program Files\MSBuild\ExtensionPack\MSBuild.ExtensionPack.dll".
Task "MSBuild.ExtensionPack.FileSystem.File"
  Processing File Collection
  Processing File: E:\Builds\ALien\AlienDevelopment\Sources\Main\Source\Alien\Source\Scfo.Alien\Scfo.Alien.csproj
Done executing task "MSBuild.ExtensionPack.FileSystem.File".
However, then script runs the "AfterCompile" target, the publish does not publish to the new location and the check-in action says there is no pending changes to check in.
Task "MSBuild.ExtensionPack.VisualStudio.TfsSource"
  Resolve TF.exe path
  TF.exe path resolved to: C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\..\IDE\tf.exe
  TF Operation: Checkin
  Executing C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\..\IDE\tf.exe checkin "E:\Builds\ALien\AlienDevelopment\Sources\Main\Source\ALien\Source\Scfo.Alien\Scfo.Alien.csproj" /comment:"Testing"  /noprompt /recursive
 
  E:\Builds\ALien\AlienDevelopment\Sources\Main\Source\ALien\Source\Scfo.Alien:
  Checking in edit: Scfo.Alien.csproj
  The following changes were not checked in because the items were not modified.
    Undoing edit: E:\Builds\ALien\AlienDevelopment\Sources\Main\Source\ALien\Source\Scfo.Alien\Scfo.Alien.csproj
E:\Builds\ALien\AlienDevelopment\BuildType\TFSBuild.proj : warning : Exit Code 1. Partial success: There are no remaining changes to check in.
Ultimately what I'm looking for is the ability to change some of the tags in the .csproj file and save those changes back into source control. Namely the <ApplicaitonVersion> and <MinimumRequiredVersion> tags. I understand that all of those tags can be passed into the
<MSBuild Projects="$(SolutionRoot)\Main\Source\ALien\Source\Scfo.Alien\Scfo.Alien.csproj"
             Targets="Publish" />
portion of the script. However, there are a few other files that I would like to modify in this similar manor that does not have this option of passing in properties. Am I missing something with the use of check-out a file, using the file.replace action and checking the file back in?

Any help would be greatly appreciated.

Stephen
Coordinator
Mar 3, 2009 at 3:47 PM
Does the content of Scfo.Alien.csproj actually get updated by the replace task?

Mike
Mar 3, 2009 at 4:48 PM
Mike,

Thanks for the quick reply. I'm fairly new to MSBuild scripts so forgive me for an ignorance.

After messing around some more this morning I was able to verify that the file .csproj file is indeed getting updated and in my latest test is now being pushed back into source control. I honestly for the life of me, don't know what changed to allow it to start working. The only thing I have changed in the script is to remove the "AfterCompile" publish target that runs before the "Checkin" action and moved it elsewhere. However, when I move it back now, it still works. I guess you can count this as user error.

One thing I did do was use the SDC Tasks and used the File.RegEx command to get it to work initially before going back to the ExtensionPack's File.Replace command. Not sure what that would have to do with anything, but I'm at a lose for why it is now working.

The other issue that was throwing me for a loop was if the .csfile file was getting updated, why wasn't the project being published to the directory that was changed with my File.Replace command. I didn't realize that MSBuild does not drop the published project to the PublishURL of the .csproj. It will only drop the published project to a publish location when it is supplied through a custom property.

Thanks for great tasks and keep up the good work.
May 6, 2009 at 5:13 PM
I'm having the same issue.

I'm using Studio 08 w/ TFS 08. I have a build script that builds several solutions. I also have a folder for shared assemblies that is used across all solutions. So basically I do a clean, get, build, and have aftercompilesolution target that I'm over writing. In there, I'm checking out the shared assemblies, copying the assemblies from the recently built solution to the shared assemblies folder, and checking in the shared assemblies. Everything runs fine all the way through, but out of the 30 or so assemblies, maybe 6 don't get updated. I get the "The following changes were not checked in because the items were not modified." for those assemblies. Not all the solutions get changed before the build, but those assemblies get copied from the $outdir to the shared assemblies folder, and checked in. I have tried to edit one of the solutions where the assembly wasn't getting checked in due to the previous message. Still same thing.

Any help would be appreciated

James
May 6, 2009 at 5:16 PM
Actually I don't have a issue with File.Replace like the OP. I have an issue with the checkin task. Don't know if I should start a new thread or not. If so, I apologize and start a new one.

Thanks,
James
Coordinator
May 6, 2009 at 7:00 PM
Hi James

Can you start a new discussion and we'll take it from there... thanks Mike