-
Notifications
You must be signed in to change notification settings - Fork 361
/
Copy pathRoslynBuildTask.cs
82 lines (73 loc) · 2.99 KB
/
RoslynBuildTask.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System;
using System.IO;
using System.Reflection;
#if NETCOREAPP
using System.Runtime.Loader;
#endif
namespace Microsoft.DotNet.Build.Tasks
{
public abstract partial class RoslynBuildTask : BuildTask
{
[Required]
public string RoslynAssembliesPath { get; set; }
public override bool Execute()
{
#if NETCOREAPP
AssemblyLoadContext currentContext = AssemblyLoadContext.GetLoadContext(Assembly.GetExecutingAssembly())!;
currentContext.Resolving += ResolverForRoslyn;
#else
AppDomain.CurrentDomain.AssemblyResolve += ResolverForRoslyn;
#endif
try
{
return ExecuteCore();
}
finally
{
#if NETCOREAPP
currentContext.Resolving -= ResolverForRoslyn;
#else
AppDomain.CurrentDomain.AssemblyResolve -= ResolverForRoslyn;
#endif
}
}
public abstract bool ExecuteCore();
#if NETCOREAPP
private Assembly ResolverForRoslyn(AssemblyLoadContext context, AssemblyName assemblyName)
{
return LoadRoslyn(assemblyName, path => context.LoadFromAssemblyPath(path));
}
#else
private Assembly ResolverForRoslyn(object sender, ResolveEventArgs args)
{
AssemblyName name = new(args.Name);
return LoadRoslyn(name, path => Assembly.LoadFrom(path));
}
#endif
private Assembly LoadRoslyn(AssemblyName name, Func<string, Assembly> loadFromPath)
{
const string codeAnalysisName = "Microsoft.CodeAnalysis";
const string codeAnalysisCsharpName = "Microsoft.CodeAnalysis.CSharp";
if (name.Name == codeAnalysisName || name.Name == codeAnalysisCsharpName)
{
Assembly asm = loadFromPath(Path.Combine(RoslynAssembliesPath!, $"{name.Name}.dll"));
Version resolvedVersion = asm.GetName().Version;
if (resolvedVersion < name.Version)
{
throw new Exception($"The minimum version required of Roslyn is '{name.Version}' and you are using '{resolvedVersion}' version of the Roslyn. You can update the sdk to get the latest version.");
}
// Being extra defensive but we want to avoid that we accidentally load two different versions of either
// of the roslyn assemblies from a different location, so let's load them both on the first request.
Assembly _ = name.Name == codeAnalysisName ?
loadFromPath(Path.Combine(RoslynAssembliesPath!, $"{codeAnalysisCsharpName}.dll")) :
loadFromPath(Path.Combine(RoslynAssembliesPath!, $"{codeAnalysisName}.dll"));
return asm;
}
return null;
}
}
}