Skip to content

Commit ca164dd

Browse files
authored
Fix .NET Core functionality (#30)
Get .NET Core path from `dotnet --info`
1 parent d6d1a7c commit ca164dd

File tree

5 files changed

+132
-11
lines changed

5 files changed

+132
-11
lines changed

Directory.Build.rsp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/Restore
22
/ConsoleLoggerParameters:Verbosity=Minimal;Summary
33
/MaxCPUCount
4-
/NodeReuse:false
4+
/NodeReuse:false
5+
/RestoreProperty:NuGetInteractive=true

azure-pipelines.yml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,23 @@ steps:
2222
displayName: 'Set BuildConfiguration to Release for tagged commits'
2323
condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/v'))
2424

25-
- task: MSBuild@1
25+
- task: VSBuild@1
2626
displayName: 'Build Solution'
27+
continueOnError: true
2728
inputs:
28-
solution: 'MSBuildProjectCreator.sln'
29+
solution: '*.sln'
2930
platform: '$(BuildPlatform)'
3031
configuration: '$(BuildConfiguration)'
3132

33+
- task: VSBuild@1
34+
displayName: 'Retry Build Solution With Binary Logger'
35+
condition: succeededOrFailed()
36+
inputs:
37+
solution: '*.sln'
38+
platform: '$(BuildPlatform)'
39+
configuration: '$(BuildConfiguration)'
40+
msbuildArgs: '/bl:$(Build.SourcesDirectory)artifacts\msbuild.binlog'
41+
3242
- task: VSTest@2
3343
displayName: 'Run Unit Tests'
3444
inputs:
@@ -42,4 +52,6 @@ steps:
4252
displayName: 'Publish Artifacts'
4353
inputs:
4454
artifactName: 'drop'
45-
targetPath: 'artifacts'
55+
targetPath: 'artifacts'
56+
condition: succeededOrFailed()
57+

src/MSBuildProjectCreator.UnitTests/MSBuildProjectCreator.UnitTests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net46</TargetFramework>
3+
<TargetFrameworks>netcoreapp2.1;net472</TargetFrameworks>
44
<IsPackable>false</IsPackable>
55
<AssemblyName>Microsoft.Build.Utilities.ProjectCreation.UnitTests</AssemblyName>
66
<RootNamespace>Microsoft.Build.Utilities.ProjectCreation.UnitTests</RootNamespace>
@@ -11,7 +11,7 @@
1111
<PackageReference Include="Shouldly" />
1212
<PackageReference Include="xunit" />
1313
<PackageReference Include="xunit.runner.visualstudio" />
14-
14+
1515
<ProjectReference Include="..\MSBuildProjectCreator\MSBuildProjectCreator.csproj" />
1616
</ItemGroup>
1717
</Project>

src/MSBuildProjectCreator/MSBuildAssemblyResolver.cs

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
// Licensed under the MIT license.
44

55
using System;
6+
using System.Diagnostics;
67
using System.IO;
8+
#if !NETCORE
79
using System.Linq;
10+
#endif
811
using System.Reflection;
9-
#if NET46
12+
using System.Text.RegularExpressions;
13+
using System.Threading;
14+
#if !NETCORE
1015
using Microsoft.VisualStudio.Setup.Configuration;
1116
#endif
1217

@@ -17,9 +22,26 @@ namespace Microsoft.Build.Utilities.ProjectCreation
1722
/// </summary>
1823
public static class MSBuildAssemblyResolver
1924
{
25+
#if NETCORE
26+
private static readonly Regex DotNetBasePathRegex = new Regex(@"^ Base Path:\s+(?<Path>.*)$");
27+
#endif
28+
2029
private static readonly Lazy<string> MSBuildDirectoryLazy = new Lazy<string>(
2130
() =>
2231
{
32+
#if NETCORE
33+
string basePath;
34+
35+
if (!string.IsNullOrWhiteSpace(basePath = GetDotNetBasePath()))
36+
{
37+
return basePath;
38+
}
39+
40+
if (!string.IsNullOrWhiteSpace(basePath = Environment.GetEnvironmentVariable("MSBuildExtensionsPath")))
41+
{
42+
return basePath;
43+
}
44+
#else
2345
string visualStudioDirectory;
2446

2547
if (!string.IsNullOrWhiteSpace(visualStudioDirectory = Environment.GetEnvironmentVariable("VSINSTALLDIR")))
@@ -39,7 +61,7 @@ public static class MSBuildAssemblyResolver
3961
return path;
4062
}
4163
}
42-
#if NET46
64+
4365
if (!string.IsNullOrWhiteSpace(visualStudioDirectory = MSBuildAssemblyResolver.GetPathOfFirstInstalledVisualStudioInstance()))
4466
{
4567
return visualStudioDirectory;
@@ -81,6 +103,91 @@ public static Assembly AssemblyResolve(object sender, ResolveEventArgs args)
81103
return !assemblyName.FullName.Equals(AssemblyName.GetAssemblyName(fileInfo.FullName).FullName) ? null : Assembly.LoadFrom(fileInfo.FullName);
82104
}
83105

106+
#if NETCORE
107+
108+
private static string GetDotNetBasePath()
109+
{
110+
string basePath = null;
111+
112+
using (ManualResetEvent processExited = new ManualResetEvent(false))
113+
using (Process process = new Process
114+
{
115+
EnableRaisingEvents = true,
116+
StartInfo = new ProcessStartInfo
117+
{
118+
Arguments = "--info",
119+
CreateNoWindow = true,
120+
FileName = "dotnet",
121+
UseShellExecute = false,
122+
RedirectStandardError = true,
123+
RedirectStandardInput = true,
124+
RedirectStandardOutput = true,
125+
WorkingDirectory = Environment.CurrentDirectory,
126+
},
127+
})
128+
{
129+
process.StartInfo.EnvironmentVariables["DOTNET_CLI_UI_LANGUAGE"] = "en-US";
130+
131+
process.ErrorDataReceived += (sender, args) => { };
132+
133+
process.OutputDataReceived += (sender, args) =>
134+
{
135+
if (!String.IsNullOrWhiteSpace(args?.Data))
136+
{
137+
Match match = DotNetBasePathRegex.Match(args.Data);
138+
139+
if (match.Success && match.Groups["Path"].Success)
140+
{
141+
basePath = match.Groups["Path"].Value.Trim();
142+
}
143+
}
144+
};
145+
146+
process.Exited += (sender, args) => { processExited.Set(); };
147+
148+
try
149+
{
150+
if (!process.Start())
151+
{
152+
return null;
153+
}
154+
}
155+
catch
156+
{
157+
return null;
158+
}
159+
160+
process.StandardInput.Close();
161+
162+
process.BeginErrorReadLine();
163+
process.BeginOutputReadLine();
164+
165+
switch (WaitHandle.WaitAny(new WaitHandle[] { processExited }, TimeSpan.FromSeconds(5)))
166+
{
167+
case WaitHandle.WaitTimeout:
168+
break;
169+
170+
case 0:
171+
break;
172+
}
173+
174+
if (!process.HasExited)
175+
{
176+
try
177+
{
178+
process.Kill();
179+
}
180+
catch
181+
{
182+
}
183+
}
184+
185+
return basePath;
186+
}
187+
}
188+
189+
#endif
190+
84191
private static string GetMSBuildVersionDirectory(string version)
85192
{
86193
if (Version.TryParse(version, out Version visualStudioVersion) && visualStudioVersion.Major >= 16)
@@ -91,7 +198,7 @@ private static string GetMSBuildVersionDirectory(string version)
91198
return version;
92199
}
93200

94-
#if NET46
201+
#if !NETCORE
95202
private static string GetPathOfFirstInstalledVisualStudioInstance()
96203
{
97204
Tuple<Version, string> highestVersion = null;

src/MSBuildProjectCreator/MSBuildProjectCreator.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<ArtifactsPath>..\..\artifacts\$(MSBuildProjectName)</ArtifactsPath>
99
<CopyArtifactsAfterTargets>Pack</CopyArtifactsAfterTargets>
1010
<DefaultArtifactsFileMatch>*nupkg</DefaultArtifactsFileMatch>
11+
<DefineConstants Condition="'$(TargetFramework)' == 'netstandard2.0' Or '$(TargetFramework)' == 'netcoreapp2.1'">$(DefineConstants);NETCORE</DefineConstants>
1112
</PropertyGroup>
1213

1314
<PropertyGroup Label="Package properties">
@@ -23,15 +24,15 @@
2324
</PropertyGroup>
2425

2526
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' Or '$(TargetFramework)' == 'net46'">
26-
<PackageReference Include="Microsoft.Build" VersionOverride="15.9.20" />
27+
<PackageReference Include="Microsoft.Build" ExcludeAssets="Runtime" VersionOverride="15.9.20" />
2728
</ItemGroup>
2829

2930
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1' Or '$(TargetFramework)' == 'net472'">
3031
<PackageReference Include="Microsoft.Build" ExcludeAssets="Runtime" />
3132
</ItemGroup>
3233

3334
<ItemGroup Condition="'$(TargetFramework)' == 'net46' Or '$(TargetFramework)' == 'net472'">
34-
<PackageReference Include="Microsoft.VisualStudio.Setup.Configuration.Interop" Condition="'$(TargetFramework)' == 'net46'" />
35+
<PackageReference Include="Microsoft.VisualStudio.Setup.Configuration.Interop" />
3536
</ItemGroup>
3637

3738
<ItemGroup>

0 commit comments

Comments
 (0)