MsBuild: sort your Content item file names

I have been playing a bit with MsBuild to process a set of Content items sorted by file name. Content items are the items included in the project, and their Build Action set to Content. Below some sample code on how to achive this, just paste the code in a file sorttest.msbuild, and run as msbuild.exe sorttest.msbuild.

 

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="SortTest"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >

  <PropertyGroup>
    <SortCommand>$(WinDir)\System32\sort.exe</SortCommand>
    <ContentItemsSortInFile>$(TEMP)\sortfileIn.tmp</ContentItemsSortInFile>
    <ContentItemsSortOutFile>$(TEMP)\sortfileOut.tmp</ContentItemsSortOutFile>
  </PropertyGroup>

  <ItemGroup>
    <Content Include="SampleNorthwind\0002_content.testdata.sql" />
    <Content Include="SampleNorthwind\0001_initial.schema.sql" />
    <Content Include="SampleNorthwind\0004_employee-mobilephone.schema.sql" />
    <Content Include="SampleNorthwind\0003_constraints.schema.sql" />
  </ItemGroup>

  <Target Name="SortTest">
    <WriteLinesToFile
              File="$(ContentItemsSortInFile)"
              Lines="@(Content)"
              Overwrite="true"/>
    <Exec Command="$(SortCommand) $(ContentItemsSortInFile) > $(ContentItemsSortOutFile)"/>
    <ReadLinesFromFile File="$(ContentItemsSortOutFile)">
      <Output TaskParameter="Lines" ItemName="SortedContent"/>
    </ReadLinesFromFile>
    <!-- CLEAN UP TEMPORARY FILES -->
    <Delete Files="$(ContentItemsSortInFile)"/>
    <Delete Files="$(ContentItemsSortOutFile)"/>

    <!-- DO YOUR ACTION WITH SortedContent, FOR NOW, JUST WRITE IT OUT, ',' SEPARATED -->
    <Message Text="@(SortedContent,',')"/>
  </Target>
</Project>

UPDATED:

 

My collegue Dion Olsthoorn came with the following solution that is smarter:

  • Get the files from filesystem, is sorted by default
  • Determine all files NOT in project
  • Get again files from filesystem, exclude files NOT in project
  • Result is a set of sorted items as included in the project (as Content items)

Nifty nifty!

Replace this target in the code above:

<Target Name="SortTest" Outputs="%(Content.RelativeDir)">

                <CreateItem Include="%(Content.RelativeDir)*" Exclude="@(Content)">

                               <Output TaskParameter="Include" ItemName="ExcludedSortedContent"/>

                </CreateItem>

                <CreateItem Include="%(Content.RelativeDir)*" Exclude="@(ExcludedSortedContent)">

                               <Output TaskParameter="Include" ItemName="SortedContent"/>

                </CreateItem>

                <Message Text="@(SortedContent,',')" />

</Target>

Published Thursday, May 03, 2007 11:10 PM by svdoever
Filed under: ,

Comments

Monday, March 22, 2010 10:25 PM by Jardena

# re: MsBuild: sort your Content item file names

Sorry. People will buy anything that is one to a customer. Help me! I can not find sites on the: Buying stock cheap. I found only this - <a href="www.comune.farageradadda.bg.it/.../CheapStocks">sell stocks cheap</a>. Usually per assets, gasoline farms account can commemorate into data of stockings during the price of the cluster, cheap stocks. With the index of programmers being closed by the us-led mistake, a worth same spending has placed to cool, cheap stocks. With best wishes :eek:, Jardena from Central.

Wednesday, June 16, 2010 6:44 PM by Sid DeLuca

# re: MsBuild: sort your Content item file names

Thanks for the tip.  I never knew the sort.exe file existed!

After reading your post I was able to implement a check of the fileset for the build of a large application.  After compile, I pass the output of the MSBuild task to the ResolveAssemblyReference Task.  Next I pass the output of that into the GetAssemblyIdentity task to get the full fusion name of each compiled assembly.  I then write the contents of the item list to disk, sort it, and generate a checksum from the file (using MSBuild Extension pack FileSystem.File task).  If the checksum of the generated file is different than the master list checked into source control, then I know that someone has checked in a new component, or one with a different version and I send an e-mail to myself!  

Thursday, March 24, 2011 7:05 PM by generic

# re: MsBuild: sort your Content item file names

Surprisingly! It is like you understand my mind! You seem to know so much about this, just like you wrote the book in it or something. I think that you can do with some pics to drive the content home a bit, but other than that, this is informative blog post. A good read. I’ll definitely revisit again.

Wednesday, March 30, 2011 7:35 AM by weblogs.asp.net

# re: MsBuild: sort your Content item file names

Msbuild sort your content item file names.. Reposted it :)

Tuesday, April 26, 2011 10:27 PM by weblogs.asp.net

# re: MsBuild: sort your Content item file names

Msbuild sort your content item file names.. Reposted it :)

Thursday, June 09, 2011 9:43 PM by weblogs.asp.net

# re: MsBuild: sort your Content item file names

Msbuild sort your content item file names.. Awful :)

Thursday, January 05, 2012 11:00 AM by Jag Padda

# re: MsBuild: sort your Content item file names

Hi,

I've added an MSBuild task to my blog that does something similar to what you're attempting to achieve.

Thanks

Leave a Comment

(required) 
(required) 
(optional)
(required)