-
-
Notifications
You must be signed in to change notification settings - Fork 673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use MSBuild OM for what it's good for #656
Conversation
when I import
Platform support There is still a lot of my manual code at this stage, Ultimately, if my lot of manual collection is no longer needed, I think it's good. |
Yes, sadly Microsoft.Build itself is not netstandard. But targeting netcoreapp2.1 (or 3.0) for this library may work well. Or you can multitarget netcoreapp2.1 and net472 easily too. I plan to look into this to find a good option.
I'm not sure what you mean. Do you mean that Microsoft.Build can't reliably tell you the compile items and references etc of a csproj when its new vs. legacy? It absolutely can. This is the same Microsoft.Build.dll that Visual Studio itself uses and what we use to build at the command line. It's the authority -- it doesn't get any more reliable/accurate than MSBuild itself. |
What is used in Visual Studio has no guarantee regarding the safety of Mac/Linux behavior. At present, XmlReader has been changed to a structured Reader, and there are no significant advantages. |
Yes there is. Microsoft.Build.dll is also used with As for "no significant advantages", you may feel differently once I make changes to this PR to run a design-time build. It will support a much wider variety of projects automatically than you could ever hope to support by hand-parsing XML. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm interested in the development, so I want to see the code that follows.
This pull request is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 5 days. |
The line count diff says it all: this is way simpler than what was happening before. And since we're using proper MSBuild/Roslyn APIs the fidelity is super high. I still need to implement the msbuild task version. And I suppose I need to bring back directory support to mpc.exe. Is that for Unity, @neuecc? Also, what is this support I saw for a comma-delimited list of paths to csproj files for? What's the scenario? Is that roughly equivalent to just running mpc.exe separately for each project file? |
61b4c04
to
75a184a
Compare
OK, I figured that mpc most likely does support directory scans to support Unity, so that's still supported. Also: Are the user-supplied preprocessor symbols that mpc allows you to supply only there to support unity scenarios? I'd like to drop support for them where an msbuild project file is given since it's not easy to support and I doubt is relevant except in Unity where no project file exists. Thoughts? |
fdcdd22
to
9737979
Compare
I went ahead and implemented the MSBuild task and redesigned the msbuild task nuget package to be self-executing. So now a project may include just this: <ItemGroup>
<PackageReference Include="messagepack" Version="2.1.143" />
<PackageReference Include="MessagePack.MSBuild.Tasks" Version="2.1.144-g9737979c90" PrivateAssets="all" />
</ItemGroup> And it automatically gets mpc run during the build and a file produced in the intermediate directory, consumable in the normal way: using System;
using MessagePack;
using MessagePack.Resolvers;
class Program
{
static void Main(string[] args)
{
var o = new SomeObject { SomeMember = "hi" };
var options = MessagePackSerializerOptions.Standard.WithResolver(
CompositeResolver.Create(
StandardResolver.Instance,
GeneratedResolver.Instance
));
byte[] b = MessagePackSerializer.Serialize(o, options);
var o2 = MessagePackSerializer.Deserialize<SomeObject>(b, options);
Console.WriteLine(o2.SomeMember);
}
}
[MessagePackObject]
public class SomeObject
{
[Key(0)]
public string SomeMember { get; set; }
} |
{ | ||
// Add this to your C# console app's Main method to give yourself | ||
// a CancellationToken that is canceled when the user hits Ctrl+C. | ||
var cts = new CancellationTokenSource(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use this.Context.CancellationToken
that provided by ConsoleAppFramework(and GenericHost's Console lifetime)
I think it's very good! Directory scan is, yes, for Unity. As for the MSBuild task, I don't know how it will be used (configured). By the way, what is the minimum code of use MSBuildWorkspace? <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Build.Locator" Version="1.2.6" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Framework" Version="16.0.461" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build" Version="16.0.461" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="3.6.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.6.0" />
</ItemGroup>
</Project> using Microsoft.Build.Locator;
using Microsoft.Build.Logging;
using Microsoft.CodeAnalysis.MSBuild;
using System.IO;
using System.Runtime.Loader;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
var instance = MSBuildLocator.RegisterDefaults();
AssemblyLoadContext.Default.Resolving += (assemblyLoadContext, assemblyName) =>
{
var path = Path.Combine(instance.MSBuildPath, assemblyName.Name + ".dll");
if (File.Exists(path))
{
return assemblyLoadContext.LoadFromAssemblyPath(path);
}
return null;
};
// my sample console app
var projectPath = @"C:\Users\neuecc\Source\Repos\ConsoleApp34\ConsoleApp34\ConsoleApp34.csproj";
var workspace = MSBuildWorkspace.Create();
var logger = new ConsoleLogger(Microsoft.Build.Framework.LoggerVerbosity.Quiet);
var project = await workspace.OpenProjectAsync(projectPath, logger, null, CancellationToken.None);
}
}
|
Thanks for the review, @neuecc. <PackageReference Include="Microsoft.Build.Locator" Version="1.2.6" ExcludeAssets="runtime" /> |
MSBuild and Roslyn has APIs that work xplat, on .NET Framework and .NET Core, that give a high fidelity representation of a CSharpCompilation (and even a VB one) so that we don't have to execute our own design-time builds or parse XML ourselves.
I've added a sample in the docs as you requested, @neuecc. |
To be honest, I don't understand it at all, but well, I'll think about it after I use it. |
In addition, I don't think anyone can use it in this ReadMe. |
@neuecc Thanks for the feedback. I do want this to be easy. The way I've designed it there won't typically be any need at all to modify msbuild files (as opposed to before where they had to add targets and tasks). |
To do:
Fixes #944