MSBUILD: How to parse solution file to get project paths - msbuild

How can I get the list of project files from a solution when using MSBUILD?
For example getting all the .csproj from a .sln.

I was previously using MSBuild Community Tasks's GetSolutionProjects for this but unfortunately it has a dependency on .NET 3.5.
To accomplish this using a CodeTask (available since .NET 4) do the following:
<UsingTask TaskName="GetProjectsFromSolutionCodeTask" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup>
<Solution ParameterType="System.String" Required="true"/>
<Output ParameterType="Microsoft.Build.Framework.ITaskItem[]" Output="true"/>
</ParameterGroup>
<Task>
<Reference Include="System.Xml"/>
<Reference Include="Microsoft.Build"/>
<Using Namespace="Microsoft.Build.Construction"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
var _solutionFile = SolutionFile.Parse(Solution);
Output = _solutionFile.ProjectsInOrder
.Where(proj => proj.ProjectType == SolutionProjectType.KnownToBeMSBuildFormat)
.Select(proj => new TaskItem(proj.AbsolutePath))
.ToArray();
]]>
</Code>
</Task>
</UsingTask>
and invoke it like so:
<!-- Gets the projects composing the specified solution -->
<Target Name="GetProjectsFromSolution">
<GetProjectsFromSolutionCodeTask Solution="%(Solution.Fullpath)">
<Output ItemName="ProjectFiles" TaskParameter="Output"/>
</GetProjectsFromSolutionCodeTask >
</Target>
This will populate a ProjectFiles item collection with the absolute path of all the projects within the solution.
Please note: path to CodeTaskFactory varies by MSBuild version. Example here is for MSBuild 14.0.

Related

Retrieving the nupkg version number in msbuild

I've modified a csproj file in VS2017 to create a Nuget package when my .NET 4.5 project is release-built. The next automation step is to add this package to my private feed on a network share. Here are the commands I'm using:
<Exec Command="nuget.exe pack -Properties "Configuration=Release" -Symbols $(ProjectName).csproj" />
<Exec Command="nuget.exe add $(ProjectName).$(ProductVersion).nupkg -source \\MYSERVER\Nuget packages" />
Line 1 works and produces a nupkg file of the form productname.nn.nn.nn.nn.
However line 2 is not returning a value for the ProductVersion token (which was a guess on my part).
I've struggled to find a reference for MSBUILD tokens (that in itself would be useful to know), but what I really need to know is the correct MSBUILD token/variable/property for the the version - and that is the same value as the generated Nuget package.
I explored $(PackageVersion) suggested by Martin Ullrich, but it's not going to work with older projects even with Nuget 4.10 installed. Also I couldn't get Troopers sample to work how I wanted and I ended up with a variation on one of the posts here. I adapted it to give me the assembly version as opposed to the file version.
<UsingTask TaskName="GetVersionParts" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<AssemblyPath ParameterType="System.String" Required="true" />
<MajorVersion ParameterType="System.Int32" Output="true" />
<MinorVersion ParameterType="System.Int32" Output="true" />
<BuildVersion ParameterType="System.Int32" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.Reflection" />
<Code Type="Fragment" Language="cs">
<![CDATA[
Version v = AssemblyName.GetAssemblyName(this.AssemblyPath).Version;
this.MajorVersion = v.Major;
this.MinorVersion = v.Minor;
this.BuildVersion = v.Build;
]]>
</Code>
</Task>
</UsingTask>
<Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release'">
<Message Text="**** After-build process starting ****" />
<Exec Command="nuget.exe pack -Properties "Configuration=Release" -Symbols $(ProjectName).csproj" />
<GetVersionParts AssemblyPath="$(OutputPath)$(AssemblyName).dll">
<Output TaskParameter="MajorVersion" PropertyName="MajorVersionNumber" />
<Output TaskParameter="MinorVersion" PropertyName="MinorVersionNumber" />
<Output TaskParameter="BuildVersion" PropertyName="BuildVersionNumber" />
</GetVersionParts>
<Exec Command="nuget.exe add $(MSBuildProjectName).$(MajorVersionNumber).$(MinorVersionNumber).$(BuildVersionNumber).nupkg -source "\\My feed server\Shared location"" />
<Exec Command="move *.symbols.nupkg "\\My feed server\Shared location\Symbols"" />
<Message Text="**** After-build process completed ****" />
</Target>
Although this works, I can't help feeling that it should be easier.
Further reading
https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-inline-tasks
https://learn.microsoft.com/en-us/visualstudio/msbuild/common-msbuild-project-properties
https://learn.microsoft.com/en-us/nuget/tools/nuget-exe-cli-reference#add
You could get the assembly version with this macro. I call it before the postbuild event for example
<!--Macro to get the version -->
<Target Name="PostBuildMacros">
<GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
<Output TaskParameter="Assemblies" ItemName="CurrentAssembly" />
</GetAssemblyIdentity>
<ItemGroup>
<VersionNumber Include="%(CurrentAssembly.Version)" />
</ItemGroup>
</Target>
<!-- override PostBuildEvent to call PostBuildMacros -->
<PropertyGroup>
<PostBuildEventDependsOn>
$(PostBuildEventDependsOn);
PostBuildMacros;
</PostBuildEventDependsOn>
<PostBuildEvent>
...
</PostBuildEvent>
</PropertyGroup>

How to re-run property evaluation in MSBuild target?

I have a custom MSBuild target, partial snippet as follows ..
<Target Name="PublishHtm">
<PropertyGroup>
<PublishHtmTemplateContents>$([System.IO.File]::ReadAllText($(PublishHtmTemplatePath)))</PublishHtmTemplateContents>
<PublishHtm>$(PublishHtmTemplateContents)</PublishHtm>
</PropertyGroup>
<WriteLinesToFile Lines="$(PublishHtm)" File="$(PublishDir)\publish.htm" Overwrite="true"/>
</Target>
This is a rework attempt for this solution in that I'm trying to isolate this template to an external file. The template contains MSBuild property references such as $(ApplicationName). When doing this exactly as described in the linked solution, it works fine, but when loading the template in as a string, none of these property expressions are evaluated by the time it gets to the file.
<SPAN CLASS="BannerTextApplication">$(ApplicationName)</SPAN>
Is there an MSBuild expression/function I can use to get the string to be reevaluated given the context that the Target is being invoked?
BTW I'd rather not work around the problem using find/replace or regex replace, and stick with the MSBuild expression engine.
Using Visual Studio 2012 & .NET Framework 4.5.
Sorry for not getting back to this question for awhile.
Initially I thought that to solve this problem we'll need to bend MSBuild in very unusual way (plan for today was to write complex inline task which will do regex-replace in external file using Msbuild properties as tokens ). But I think this can be solved easier, using CDATA section, which is valid inside property definition:
Here is main script:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup>
<MyOtherProperty>$([System.DateTime]::Now)</MyOtherProperty>
<Version>1.0.1b</Version>
<ProjectName>MSBuild Rox</ProjectName>
<Author>Alexey Shcherbak</Author>
</PropertyGroup>
<Target Name="Build">
<ItemGroup>
<PropsToPass Include="MyOtherProperty=$(MyOtherProperty)" />
<PropsToPass Include="Version=$(Version)" />
<PropsToPass Include="ProjectName=$(ProjectName)" />
<PropsToPass Include="Author=$(Author)" />
</ItemGroup>
<MSBuild Projects="TransformHTML.Template.proj" Properties="#(PropsToPass)" />
</Target>
</Project>
And here is your template. It's not pure html, it's still msbuild file, but at least without ugly encoding for html tags in xml. It's just a block in CDATA
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Transform">
<PropertyGroup>
<HtmlProperty><![CDATA[
<body>
<div>$(MyOtherProperty)</div>
<div>$(Version)</div>
<div>$(ProjectName)</div>
<div>$(Author)</div>
</body>
]]></HtmlProperty>
</PropertyGroup>
<Target Name="Transform">
<Message Text="HtmlProperty: $(HtmlProperty)" Importance="High" />
</Target>
</Project>
Maybe it's not very elegant ( I personally don't like the section with #PropsToPass) but it'll do the job. You can put everything inline into single file and then you don't need to pass properties to MSBuild task. I don't like massive html-encoding from proposed "this solution" but I'd rather prefer to keep HTML template in the same script where it'll be transformed, just in nice html format, without encoding.
Single file example:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup>
<MyOtherProperty>$([System.DateTime]::Now)</MyOtherProperty>
<Version>1.0.1b</Version>
<ProjectName>MSBuild Rox</ProjectName>
<Author>Alexey Shcherbak</Author>
</PropertyGroup>
<Target Name="Build">
<PropertyGroup>
<HtmlProperty><![CDATA[
<body>
<div>$(MyOtherProperty)</div>
<div>$(Version)</div>
<div>$(ProjectName)</div>
<div>$(Author)</div>
</body>
]]></HtmlProperty>
</PropertyGroup>
<Message Text="HtmlProperty: $(HtmlProperty)" Importance="High" />
</Target>
</Project>
You can also download these files here
You can do it using Eval task
<Target Name="PublishHtm">
<PropertyGroup>
<PublishHtmTemplateContents>$([System.IO.File]::ReadAllText($(PublishHtmTemplatePath)))</PublishHtmTemplateContents>
<Eval Values="$(PublishHtmTemplateContents)">
<Output TaskParameter="Result" ItemName="EvalItemTemp"/>
</Eval>
<PublishHtm>%(EvalItemTemp.Identity)</PublishHtm>
</PropertyGroup>
<WriteLinesToFile Lines="$(PublishHtm)" File="$(PublishDir)\publish.htm" Overwrite="true"/>
</Target>
Actually the task does nothing except returning exactly the same value it received, however when you pass the returned value %(EvalItemTemp.Identity) to anywhere, msbuild does evaluation!
Eval task source:
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Choose>
<When Condition="'$(MSBuildToolsVersion)' == 'Current' OR $(MSBuildToolsVersion.Split('.')[0]) >= 14">
<PropertyGroup>
<TasksAssemblyName>Microsoft.Build.Tasks.Core.dll</TasksAssemblyName>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup>
<TasksAssemblyName>Microsoft.Build.Tasks.v$(MSBuildToolsVersion).dll</TasksAssemblyName>
</PropertyGroup>
</Otherwise>
</Choose>
<UsingTask TaskName="Eval" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\$(TasksAssemblyName)">
<ParameterGroup>
<Values ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="True" Output="False" />
<Result ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="False" Output="True" />
</ParameterGroup>
<Task>
<Code Type="Class" Language="cs" Source="$(MSBuildThisFileDirectory)TaskSource\EvalTask.cs"/>
</Task>
</UsingTask>
</Project>
Where TaskSource\EvalTask.cs is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.Diagnostics;
using System.Threading;
namespace Varonis.MSBuild.Tasks
{
public class Eval : Task
{
[Required]
public ITaskItem[] Values { get; set; }
[Output]
public ITaskItem[] Result { get; set; }
public override bool Execute()
{
Result = new TaskItem[Values.Length];
for (int i = 0; i < Values.Length; i++)
{
Result[i] = new TaskItem(Values[i].ItemSpec);
}
return true;
}
}
}

MSBuild UsingTask Resolve References

I feel like I've fixed this before, but I can't remember how.
I have a tasks file that looks like this (CustomTasks.tasks):
<UsingTask AssemblyFile="CustomTasks.dll" TaskName="MyCustomTask"/>
it references an assembly (namely Ionic.Zip.dll). Ionic.Zip.dll is not in the GAC (and I don't want it to be). It sits right next to my CustomTasks.dll.
I have a directory called MSBuild one level up from my sln file which has CustomTasks.tasks, CustomTasks.dll and Ionic.Zip.dll in it.
I have a csproj that references the tasks file and calls the custom task:
<Import Project="$(ProjectDir)\..\MSBuild\CustomTasks.tasks" />
<MyCustomTask ..... />
at build time, this yields:
The "MyCustomTask" task could not be loaded from the assembly ....MyCustomTasks.dll. Could not load file or assembly 'Ionic.Zip,......' or one of its dependencies.
Got tired and frustrated and took a direct approach...I don't think this is the same way I solved the problem previously...but maybe this will help someone else. Other, more elegant solutions are more than welcome.
<Target Name="BeforeBeforeBuild" BeforeTargets="BeforeBuild">
<HandleAssemblyResolve SearchPath="$(ProjectDir)\..\MSBuild\" />
</Target>
<UsingTask TaskName="HandleAssemblyResolve" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<SearchPath ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Reflection" />
<Code Type="Fragment" Language="cs">
<![CDATA[
AppDomain.CurrentDomain.AssemblyResolve += (sender, e) =>
{
var assemblySearchPath = Path.Combine(SearchPath, e.Name.Split(',')[0]);
if (File.Exists(assemblySearchPath)) return Assembly.LoadFrom(assemblySearchPath);
return null;
};
]]>
</Code>
</Task>
</UsingTask>
This is actually easy to fix. Put your custom build tasks and dependencies in a different folder. Then dependencies are loaded correctly.
For example like so:
<UsingTask AssemblyFile="..\BuildTools\CustomTasks.dll" TaskName="MyCustomTask"/>

Obtain file sizes in MSBuild script

Can I find out the sizes of a set of files from an MSBuild script without writing my own code?
MSBuild itself some metadata on individual items (for example, the last modified time at %(ModifiedTime)
), but no sizes. I can't see anything at http://msbuildextensionpack.com/.
Edit: based on Seva's answer, here's an inline task that returns the total size of an array of items:
<UsingTask TaskName="GetFileSize" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Files ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<TotalSize ParameterType="System.Int64" Output="true"/>
</ParameterGroup>
<Task>
<Using Namespace="System.IO"/>
<Code Type="Fragment" Language="cs"><![CDATA[
long l = 0;
foreach (var item in Files) {
var fi = new FileInfo(item.ItemSpec);
l += fi.Length;
}
TotalSize = l;
]]></Code>
</Task>
</UsingTask>
You can actually use a sub-set of .Net API within msbuild projects using inline tasks.
E.g. the following prints out a file size of a single file:
<UsingTask TaskName="GetFileSize" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<FileName Required="true" />
<FileSize ParameterType="System.Int64" Output="true"/>
</ParameterGroup>
<Task>
<Using Namespace="System.IO"/>
<Code Type="Fragment" Language="cs"><![CDATA[
FileInfo fi = new FileInfo(FileName);
FileSize = fi.Length;
]]></Code>
</Task>
</UsingTask>
<Target Name="PrintFileSize" >
<GetFileSize FileName="$(MyFileName)">
<Output TaskParameter="FileSize" PropertyName="MyFileSize" />
</GetFileSize>
<Message Text="file size of $(MyFileName) is $(MyFileSize)" />
</Target>

Copy Item's Metadata for batching

This is an extension to this question.
Suppose I'm creating items from other item's metadata:
<ItemGroup>
<A>
<files>f1;f2;f3;...</files>
<x>...</x>
<y>...</y>
<z>...</z>
...
</A>
<B Include="%(A.files)">
<x>%(A.x)</x>
<y>%(A.y)</y>
<z>%(A.z)</z>
...
</B>
</ItemGroup>
%(A.files) is a list of files separated by ;, such that for each A item I'm creating many B items (one for each file).
But frequently when I process B item I need the original's A item metadata. In this example I copied each metadata manually from A to B.
Is there a way to copy all of A's metadata to B without explicitly specifying each one of them?
Little late, but I like this solution better:
<B Include="#(A->Metadata('files'))" />
Full Example:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<A Include="A1">
<files>a1_file1.htm;a1_file2.htm</files>
<x>a1</x>
<y>b1</y>
<z>c1</z>
</A>
<A Include="A2">
<files>a2_file.proj</files>
<x>a2</x>
<y>b2</y>
<z>c2</z>
</A>
<B Include="#(A->Metadata('files'))" />
</ItemGroup>
<Target Name="Build">
<Message Text="A: #(A->'%(Identity) x:%(x) y:%(y) z:%(z) files:%(files)', '
')" />
<Message Text="B: #(B->'%(Identity) x:%(x) y:%(y) z:%(z) files:%(files)', '
')" />
</Target>
</Project>
Output:
Build:
A: A1 x:a1 y:b1 z:c1 files:a1_file1.htm;a1_file2.htm
A2 x:a2 y:b2 z:c2 files:a2_file.proj
B: a1_file1.htm x:a1 y:b1 z:c1 files:a1_file1.htm;a1_file2.htm
a1_file2.htm x:a1 y:b1 z:c1 files:a1_file1.htm;a1_file2.htm
a2_file.proj x:a2 y:b2 z:c2 files:a2_file.proj
As far as I can tell, MSBuild only copies metadata when there is a one-to-one mapping between the input and output item lists. In your case, you're starting with one item and expanding to many items. To get around this, I suggest using item batching:
<?xml version="1.0" encoding="iso-8859-1"?>
<Project
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
ToolsVersion="4.0"
DefaultTargets="Print">
<ItemGroup>
<A Include="A1">
<files>test.htm;test_sol1.htm</files>
<x>a1</x>
<y>b1</y>
<z>c1</z>
</A>
<A Include="A2">
<files>test.proj</files>
<x>a2</x>
<y>b2</y>
<z>c2</z>
</A>
</ItemGroup>
<Target Name="ExpandA">
<ItemGroup>
<ExpandedA Include="%(A.files)">
<Original>%(Identity)</Original>
</ExpandedA>
</ItemGroup>
</Target>
<Target
Name="CopyMetadata"
Outputs="%(ExpandedA.Identity)"
DependsOnTargets="ExpandA">
<PropertyGroup>
<ExpandedAIdentity>%(ExpandedA.Identity)</ExpandedAIdentity>
<ExpandedAOriginal>%(ExpandedA.Original)</ExpandedAOriginal>
</PropertyGroup>
<ItemGroup>
<ExpandedAMetadata Include="#(A)" Condition=" '%(Identity)' == '$(ExpandedAOriginal)' ">
<Expanded>$(ExpandedAIdentity)</Expanded>
</ExpandedAMetadata>
</ItemGroup>
</Target>
<Target Name="Print" DependsOnTargets="CopyMetadata">
<ItemGroup>
<B Include="#(ExpandedAMetadata->'%(Expanded)')" />
</ItemGroup>
<!--Use commas to illustrate that "files" has been expanded-->
<Message Text="A: %(A.files)" />
<Message Text="ExpandedA: #(ExpandedA, ',')" />
<Message Text="ExpandedAMetadata: #(ExpandedAMetadata, ',')" />
<Message Text="B: #(B->'%(Identity) x:%(x) y:%(y) z:%(z)', ',')" />
</Target>
</Project>
and the output of the "Print" target:
Print:
A: test.htm;test_sol1.htm
A: test.proj
ExpandedA: test.htm,test_sol1.htm,test.proj
ExpandedAMetadata: A1,A1,A2
B: test.htm x:a1 y:b1 z:c1,test_sol1.htm x:a1 y:b1 z:c1,test.proj x:a2 y:b2 z:c2
ExpandedA is similar to B in your original question; it is the expanded version of A but without any metadata. Then I run the CopyMetadata target once for each item in ExpandedA (thanks to item batching). Each run, the original A item is copied to the ExpandedAMetadata item group along with all of its metadata. The Original metadata is used to ensure that the correct A item is associated with each file. Finally, in the Print target, B is constructed using an item transformation, so that all the metadata from ExpandedAMetadata is copied over as well.
I'm not sure if I get what you need, but you can copy all A metedata to B as in the answer for related question:
<B Include="#(A)">
It should copy all metedata from A to B.
Here is a direct answer to the original question:
<!-- Copy all metadata from an input item to an output item -->
<UsingTask
TaskName="CopyMetadataTask"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<InputItem ParameterType="Microsoft.Build.Framework.ITaskItem" Required="true" />
<OutputItem ParameterType="Microsoft.Build.Framework.ITaskItem" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
InputItem.CopyMetadataTo(OutputItem);
]]>
</Code>
</Task>
</UsingTask>
You can use this task to copy metadata between items the way it was originally intended, and it can easily be adapted to multiple items on input/output, though doing so does induce issues whereby it is necessary to match the inputs and outputs 1/1, or make other compromises, though it can easily be adapted to the exact situation since you can modify the C# code however is necessary. E.g.:
<UsingTask
TaskName="CopyMetadataTask"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<InputItem ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<OutputItem ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
for (int i = 0; i < InputItem.Length; ++i)
{
InputItem[i].CopyMetadataTo(OutputItem[i]);
}
]]>
</Code>
</Task>
</UsingTask>
The task can then be called to copy metadata directly, for example (using the second version):
<ItemGroup>
<ItemListA Include="a;b;c" Metadata="MetadataValue" />
<ItemListB Include="d;e;f" OtherMetadata="OtherValue" />
</ItemGroup>
<CopyMetadataTask InputItem="#(ItemListA)" OutputItem="#(ItemListB)" />