Read text from file

Jan 7, 2010 at 9:51 AM

Hi I have some SQL script that I want to read from a file during build and inject it into another file.

The problem is that the ReadLinesFromFile block does not return any contents from the Some.Sql (yes there really is something in there) . I know this is from the Community .Tasks is there something similar in Extensions.

thanks

 

<Project DefaultTargets="DesktopBuild" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">

  <Import Project="$(MSBuildExtensionsPath)\ExtensionPack\MSBuild.ExtensionPack.tasks"/>
  <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\TeamBuild\Microsoft.TeamFoundation.Build.targets" />
  <Import Project="$(MSBuildExtensionsPath)\MSBuild.Community.Tasks.Targets" />

<!--Definition of the sql file to read the text from-->
<ItemGroup>
    <SourceSQLFile Include="C:\Temp\Some.sql"/>
</ItemGroup>

<Target Name="AfterCompile"  >
    <!--This task does not return anything from file seems not to work.-->
    <ReadLinesFromFile File="@(SourceSQLFile)">
      <Output TaskParameter="Lines" 
        PropertyName="FileContents" />
    </ReadLinesFromFile>

<!-- Output is FileContents:  -->
 <Message Text="FileContents: @(FileContents)" />

<!-- Replace the placeholder text in another file  -->
<MSBuild.ExtensionPack.FileSystem.File TaskAction="Replace" RegexPattern="-- MSBUILD_PLACEHOLDER" Replacement="@(FileContents, '%0a%0d')" Files="C:\Temp\SomeOther.sql"/>
 </Target>
</Project>

 

Coordinator
Jan 7, 2010 at 10:32 AM
Edited Jan 7, 2010 at 10:33 AM

Hi

 

I think you are using the ReadLinesFromFile task slightly incorrectly. the output is an ITaskItem, not Property, so...

<Target Name="AfterCompile"  >
    <!--This task does not return anything from file seems not to work.-->
    <ReadLinesFromFile File="@(SourceSQLFile)">
      <Output TaskParameter="Lines"
        ItemName="FileContents" />
    </ReadLinesFromFile>

<!-- Output is FileContents:  -->
 <Message Text="FileContents: %(FileContents.Identity)" />

Hope that helps.

Mike

Debugged using MSBuild Explorer Quick Build

 

Jan 7, 2010 at 11:14 AM

Hi Mike

Yes that does help, I am able to retrieve the text. To output to the Replace task I use the following format "@(FileContents, '%0a%0d')"
Using %(FileContents.Identity) only replaced my placeholder with the first line of my sql.

Thanks alot cheers

Peter

<ReadLinesFromFile File="@(DropAllSQLFile)">
      <Output TaskParameter="Lines"
        ItemName="FileContents" />
    </ReadLinesFromFile>

<MSBuild.ExtensionPack.FileSystem.File 
  TaskAction="Replace" 
  RegexPattern="-- MSBUILD_PLACEHOLDER" 
  Replacement="@(FileContents, '%0a%0d')"
  Files="C:\Temp\SourceOutput.sql"/>