-
Notifications
You must be signed in to change notification settings - Fork 868
/
MsBuildEnvironmentScope.cs
162 lines (142 loc) · 6.81 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// 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 System.Text.RegularExpressions;
using Microsoft.Build.Locator;
using Microsoft.DocAsCode.Common;
using Microsoft.DocAsCode.Exceptions;
public class MSBuildEnvironmentScope : IDisposable
{
private const string VSInstallDirKey = "VSINSTALLDIR";
private const string MSBuildExePathKey = "MSBUILD_EXE_PATH";
private static readonly Regex DotnetBasePathRegex = new Regex("Base Path:(.*)$", RegexOptions.Compiled | RegexOptions.Multiline);
private readonly EnvironmentScope _innerScope;
public MSBuildEnvironmentScope()
{
_innerScope = GetScope();
}
private EnvironmentScope GetScope()
{
var vsInstallDirEnv = Environment.GetEnvironmentVariable(VSInstallDirKey);
if (!string.IsNullOrEmpty(vsInstallDirEnv))
{
Logger.LogInfo($"Environment variable {VSInstallDirKey} is set to {vsInstallDirEnv}, it is used as the inner compiler.");
return null;
}
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();
if (instances.Count == 0)
{
// when no visual studio installed, try detect dotnet
// workaround for https://github.com/dotnet/docfx/issues/1752
var dotnetBasePath = GetDotnetBasePath();
if (dotnetBasePath != null)
{
Logger.LogInfo($"Using dotnet {dotnetBasePath + "MSBuild.dll"} as inner compiler.");
return new EnvironmentScope(new Dictionary<string, string>
{
[MSBuildExePathKey] = dotnetBasePath + "MSBuild.dll",
["MSBuildExtensionsPath"] = dotnetBasePath,
["MSBuildSDKsPath"] = dotnetBasePath + "Sdks"
});
}
}
else
{
// 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>
{
[VSInstallDirKey] = latest.VisualStudioRootPath,
["VisualStudioVersion"] = latest.Version.ToString(2),
});
}
else
{
Logger.LogWarning("Fail to find MSBuild >= 15.0 on machine. Please install Visual Studio 2017 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;
}
private string GetDotnetBasePath()
{
using (var outputStream = new MemoryStream())
{
using (var outputStreamWriter = new StreamWriter(outputStream))
{
try
{
CommandUtility.RunCommand(new CommandInfo
{
Name = "dotnet",
Arguments = "--info"
}, outputStreamWriter, timeoutInMilliseconds: 60000);
}
catch
{
// when error running dotnet command, consilder dotnet as not available
return null;
}
// writer streams have to be flushed before reading from memory streams
// make sure that streamwriter is not closed before reading from memory stream
outputStreamWriter.Flush();
var outputString = System.Text.Encoding.UTF8.GetString(outputStream.ToArray());
var matched = DotnetBasePathRegex.Match(outputString);
if (matched.Success)
{
return matched.Groups[1].Value.Trim();
}
return null;
}
}
}
public void Dispose()
{
_innerScope?.Dispose();
}
}
}