MSBuild is certainly on its way back (if was ever in demise). .NET Core will use it as its build tooling (going away from project.json and .xproj), of course it is still the build tooling for the “full” .NET framework and associated languages and also for native C++ projects. Furthermore it is open source since some time now, under the liberal MIT license.
Recently, I’ve being toying around with the idea to convert ItemGroup items into something else. On the way I needed to convert items using property and item functions and some other goo.
While the result (and even original intention) remains debatable, I thought it is a nice showcase for what could be done in MSBuild, without requiring custom tasks. If nothing more, as always, it is my personal bookkeeping of a technique (transforming ItemGroup items) that I now have written down for future reference.
OK, the setup is a as follows: I have a number Roslyn analyzers, provided as NuGet packages, that are
listed in the Analyzer
ItemGroup. In MSBuild syntax that item group looks like this:
Now, what I wanted to do is write a Target, that can read this ItemGroup and call nuget.exe install
for those analyzers
that originate from a NuGet package (note the my.dll
item, which is built locally, and should thus be ignored here).
First we need some definitions/properties:
Then, we need the actual Target that should install packages. Note that the following target, due to
MSBuild batching will be called once for
every item in the Analyzer
ItemGroup. This is actually the clue here, because modifying or transforming
one item at a time is much simpler then always attempting to deal with the complete ItemGroup as a whole
(e.g. using %{Analyzer.<Metadata>}
or @(Analyzer->...)
constructs, which don’t nest by the way).
See, that was easy ;-)
The only thing left to do, is to add this target as a dependency where required. In my case that was the
master “Build” target in the obligatory <topleveldir>\build.proj
file:
Again, note that the target is a simple dependency albeit it will be called multiple times, once for
each entry in the Analyzer
ItemGroup (again, due to MSBuild batching).