forked from dotnet/docfx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMsBuildEnvironmentScope.cs
99 lines (84 loc) · 4.12 KB
/
MsBuildEnvironmentScope.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.DocAsCode.Metadata.ManagedReference
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Build.Locator;
using Microsoft.DocAsCode.Common;
using Microsoft.DocAsCode.Exceptions;
public class MSBuildEnvironmentScope : IDisposable
{
private const string MSBuildExePathKey = "MSBUILD_EXE_PATH";
private readonly EnvironmentScope _innerScope;
public MSBuildEnvironmentScope()
{
_innerScope = GetScope();
}
private EnvironmentScope GetScope()
{
var msbuildExePathEnv = Environment.GetEnvironmentVariable(MSBuildExePathKey);
if (!string.IsNullOrEmpty(msbuildExePathEnv))
{
Logger.LogInfo($"Environment variable {MSBuildExePathKey} is set to {msbuildExePathEnv}, it is used as the inner compiler.");
return null;
}
if (Type.GetType("Mono.Runtime") != null) // is mono
{
var assembly = typeof(System.Runtime.GCSettings).Assembly;
var assemblyDirectory = Path.GetDirectoryName(assembly.Location);
var monoDir = new DirectoryInfo(assemblyDirectory).Parent.FullName; // get mono directory
var msbuildBasePath = Path.Combine(monoDir, "msbuild", "15.0", "bin");
var msbuildPath = Path.Combine(msbuildBasePath, "MSBuild.dll");
if (!File.Exists(msbuildPath))
{
var message = $"Unable to find msbuild from {msbuildPath}, please try downloading latest mono to solve the issue.";
Logger.LogError(message);
throw new DocfxException(message);
}
Logger.LogInfo($"Using mono {msbuildPath} as inner compiler.");
MSBuildLocator.RegisterMSBuildPath(msbuildBasePath);
return new EnvironmentScope(new Dictionary<string, string>
{
[MSBuildExePathKey] = msbuildPath,
["MSBuildExtensionsPath"] = Path.Combine(monoDir, "xbuild"),
["MSBuildSDKsPath"] = Path.Combine(msbuildBasePath, "Sdks")
});
}
try
{
var instances = MSBuildLocator.QueryVisualStudioInstances().ToList();
// workaround for https://github.com/dotnet/docfx/issues/1969
// FYI https://github.com/dotnet/roslyn/issues/21799#issuecomment-343695700
var latest = instances.FirstOrDefault(a => a.Version.Major >= 15);
if (latest != null)
{
Logger.LogInfo($"Using msbuild {latest.MSBuildPath} as inner compiler.");
MSBuildLocator.RegisterInstance(latest);
return new EnvironmentScope(new Dictionary<string, string>
{
["VSINSTALLDIR"] = latest.VisualStudioRootPath,
// VS2019 defines VisualStudioVersion=16.0. Do not include minor version here to avoid breaking UWP projects.
// See: https://github.com/dotnet/docfx/issues/5011
["VisualStudioVersion"] = $"{latest.Version.Major}.0",
});
}
else
{
Logger.LogWarning("Fail to find MSBuild >= 15.0 on machine. Please install Visual Studio 2017 or above with MSBuild >= 15.0: https://visualstudio.microsoft.com/vs/");
}
}
catch (Exception e)
{
Logger.LogDiagnostic($"Have trouble locating MSBuild, if you meet issue similar to https://github.com/dotnet/docfx/issues/1969, try setting environment value VSINSTALLDIR and VisualStudioVersion as a workaround: {e.Message}");
}
return null;
}
public void Dispose()
{
_innerScope?.Dispose();
}
}
}