Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a single value from a file in MSBuild

I'm trying to read a version number from a file in MSBuild:

<ItemGroup>
    <VersionFile Include="Properties\VERSION" />
</ItemGroup>
<Target Name="BeforeBuild">
    <ReadLinesFromFile File="@(VersionFile)">
        <Output TaskParameter="Lines" ItemName="VersionNumber" />
    </ReadLinesFromFile>
</Target>

I only need the first line of this file. How can I concatenate that value with another string in WriteLinesToFile? This does not work:

<WriteLinesToFile
    File="$(AssemblyVersionFile)"
    Lines="[assembly: AssemblyVersion(&quot;@(VersionNumber)&quot;)]" />

I get an error:

error MSB4012: The expression "[assembly: AssemblyVersion("@(VersionNumber)")]" cannot be used in this context. Item lists cannot be concatenated with other strings where an item list is expected. Use a semicolon to separate multiple item lists.`

like image 729
sourcenouveau Avatar asked Oct 08 '12 16:10

sourcenouveau


1 Answers

I'm not too familiar with MSBuild but changing the Output of ReadLinesFromFile to be a Property and using $ to access it in the WriteLinesToFile seems to work:

<Target Name="BeforeBuild">
    <ReadLinesFromFile File="@(VersionFile)">
        <Output TaskParameter="Lines" PropertyName="VersionNumber" />
    </ReadLinesFromFile>
    <WriteLinesToFile
        File="output.txt"
        Lines="[assembly: AssemblyVersion(&quot;$(VersionNumber)&quot;)]" />
</Target>
like image 119
Daisuke Shimamoto Avatar answered Nov 18 '22 08:11

Daisuke Shimamoto