Skip to content
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

feat: Upgrade to .NET Core #8279

Merged
merged 8 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Test
on:
pull_request:
branches: [ dev, main ]
branches: [ dev, main, feature/* ]
push:
branches: [ dev ]
branches: [ dev, feature/* ]

jobs:
test:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.4.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic" Version="4.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="4.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Features" Version="4.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Features" Version="4.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Features" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Features" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.4.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="System.Composition" Version="6.0.0" />
<PackageReference Include="System.Collections.Immutable" Version="6.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,28 @@
namespace Microsoft.DocAsCode.Metadata.ManagedReference
{
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;

using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;

using Microsoft.DocAsCode.Common;

using Microsoft.DocAsCode.Exceptions;
using Microsoft.Win32;
using CS = Microsoft.CodeAnalysis.CSharp;
using VB = Microsoft.CodeAnalysis.VisualBasic;

internal static class CompilationUtility
{
private static readonly Lazy<MetadataReference> MscorlibMetadataReference = new Lazy<MetadataReference>(() => MetadataReference.CreateFromFile(typeof(object).Assembly.Location));
private static readonly Lazy<MetadataReference> SystemMetadataReference = new Lazy<MetadataReference>(() => MetadataReference.CreateFromFile(typeof(EditorBrowsableAttribute).Assembly.Location));

public static Compilation CreateCompilationFromCsharpCode(string code)
public static Compilation CreateCompilationFromCsharpCode(string code, string name = "cs.temp.dll", params MetadataReference[] references)
{
try
{
var tree = CS.SyntaxFactory.ParseSyntaxTree(code);
var compilation = CS.CSharpCompilation.Create(
"cs.temp.dll",
return CS.CSharpCompilation.Create(
name,
options: new CS.CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
syntaxTrees: new[] { tree },
references: new[] { MscorlibMetadataReference.Value, SystemMetadataReference.Value });
return compilation;
references: GetNetFrameworkMetadataReferences().Concat(references));
}
catch (Exception e)
{
Expand All @@ -39,17 +34,16 @@ public static Compilation CreateCompilationFromCsharpCode(string code)
}
}

public static Compilation CreateCompilationFromVBCode(string code)
public static Compilation CreateCompilationFromVBCode(string code, string name = "cs.temp.dll", params MetadataReference[] references)
yufeih marked this conversation as resolved.
Show resolved Hide resolved
{
try
{
var tree = VB.SyntaxFactory.ParseSyntaxTree(code);
var compilation = VB.VisualBasicCompilation.Create(
return VB.VisualBasicCompilation.Create(
"vb.temp.dll",
yufeih marked this conversation as resolved.
Show resolved Hide resolved
options: new VB.VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
syntaxTrees: new[] { tree },
references: new[] { MscorlibMetadataReference.Value });
return compilation;
references: GetNetFrameworkMetadataReferences().Concat(references));
}
catch (Exception e)
{
Expand All @@ -63,14 +57,9 @@ public static Compilation CreateCompilationFromAssembly(IEnumerable<string> asse
{
try
{
var paths = assemblyPaths.ToList();
//TODO: "mscorlib" should be ignored while extracting metadata from .NET Core/.NET Framework
paths.Add(typeof(object).Assembly.Location);
var assemblies = (from path in paths
select MetadataReference.CreateFromFile(path)).ToList();
var options = new CS.CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
var complilation = CS.CSharpCompilation.Create("EmptyProjectWithAssembly", new SyntaxTree[] { }, assemblies, options);
return complilation;
var references = GetNetFrameworkMetadataReferences().Concat(assemblyPaths.Select(path => MetadataReference.CreateFromFile(path)));
return CS.CSharpCompilation.Create("EmptyProjectWithAssembly", new SyntaxTree[] { }, references, options);
}
catch (Exception e)
{
Expand Down Expand Up @@ -112,5 +101,17 @@ private static string GetAbbreviateString(string input, int length = 20)
if (string.IsNullOrEmpty(input) || input.Length <= 20) return input;
return input.Substring(0, length) + "...";
}

private static MetadataReference[] GetNetFrameworkMetadataReferences()
{
using var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full");
var installPath = key?.GetValue("InstallPath")?.ToString() ?? throw new DocfxException("Cannot compile project, make sure .NET Framework is installed.");

return new[]
{
MetadataReference.CreateFromFile(Path.Combine(installPath, "mscorlib.dll")),
MetadataReference.CreateFromFile(Path.Combine(installPath, "System.dll")),
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Build.Locator" Version="1.5.3" />
<PackageReference Include="Microsoft.Build.Locator" Version="1.5.5" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ private EnvironmentScope GetScope()
}
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/");
MSBuildLocator.RegisterDefaults();
return null;
}
}
catch (Exception e)
Expand Down
2 changes: 1 addition & 1 deletion test/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<TargetFrameworks>net472</TargetFrameworks>
<TargetFrameworks>net6.0;net472</TargetFrameworks>
<LangVersion>Latest</LangVersion>

<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ [Not exist link2](link2.md)
Assert.Equal("Test", results[0].Uid);
Assert.Equal("Hello", results[0].Metadata["remarks"]);
Assert.Equal(2, results[0].LinkToFiles.Count);
Assert.Equal("~/link.md", results[0].LinkToFiles.ElementAt(0));
Assert.True(results[0].LinkToFiles.OrderBy(f => f).SequenceEqual(new[] { "~/link.md", "~/link2.md", }));
Assert.Single(results[0].LinkToUids);
Assert.Equal("NotExistUid", results[0].LinkToUids.ElementAt(0));
Assert.Equal(2, results[0].FileLinkSources.Count);
Expand Down
4 changes: 2 additions & 2 deletions test/Microsoft.DocAsCode.Common.Tests/RelativePathTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@ public void TestUrlEncode(string path, string expected)
[InlineData("../a%2Bb/c/d", "../a+b/c/d")]
[InlineData("a%253fb", "a%3fb")]
[InlineData("a%2fb", "a%2fb")]
[InlineData("%2A%2F%3A%3C%3E%3F%5C%7C", "%2A%2F%3A%3C%3E%3F%5C%7C")] //*/:<>?\|
[InlineData("%2a%2f%3a%3c%3e%3f%5c%7c", "%2a%2f%3a%3c%3e%3f%5c%7c")]
[InlineData("%2A%2F%3A%3F%5C%7C", "%2A%2F%3A%3F%5C%7C")] //*/:?\|
[InlineData("%2a%2f%3a%3f%5c%7c", "%2a%2f%3a%3f%5c%7c")]
public void TestUrlDecode(string path, string expected)
{
Assert.Equal(expected, ((RelativePath)path).UrlDecode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,24 +260,7 @@ public class Test2

private static Compilation CreateCompilationFromCSharpCode(string code, params MetadataReference[] references)
{
return CreateCompilationFromCSharpCode(code, "test.dll", references);
}

private static Compilation CreateCompilationFromCSharpCode(string code, string assemblyName, params MetadataReference[] references)
{
var tree = SyntaxFactory.ParseSyntaxTree(code);
var defaultReferences = new List<MetadataReference> { MetadataReference.CreateFromFile(typeof(object).Assembly.Location), MetadataReference.CreateFromFile(typeof(EditorBrowsableAttribute).Assembly.Location) };
if (references != null)
{
defaultReferences.AddRange(references);
}

var compilation = CSharpCompilation.Create(
assemblyName,
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
syntaxTrees: new[] { tree },
references: defaultReferences);
return compilation;
return CompilationUtility.CreateCompilationFromCsharpCode(code, "test.dll", references);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,10 @@ public void Function<T>()

";
// act
MetadataItem output = RoslynIntermediateMetadataExtractor.GenerateYamlMetadata(CreateCompilationFromCSharpCode(code, "test.dll"));
MetadataItem output = RoslynIntermediateMetadataExtractor.GenerateYamlMetadata(CompilationUtility.CreateCompilationFromCsharpCode(code, "test.dll"));

// assert
Assert.NotNull(output);
}

private static Compilation CreateCompilationFromCSharpCode(string code, string assemblyName, params MetadataReference[] references)
{
var tree = SyntaxFactory.ParseSyntaxTree(code);
var defaultReferences = new List<MetadataReference> { MetadataReference.CreateFromFile(typeof(object).Assembly.Location), MetadataReference.CreateFromFile(typeof(EditorBrowsableAttribute).Assembly.Location) };
if (references != null)
{
defaultReferences.AddRange(references);
}

var compilation = CSharpCompilation.Create(
assemblyName,
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
syntaxTrees: new[] { tree },
references: defaultReferences);
return compilation;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2622,7 +2622,7 @@ public TestAttribute([Test(3), Test(4)] object obj){}
}
}
";
MetadataItem output = GenerateYamlMetadata(CreateCompilationFromCSharpCode(code, MetadataReference.CreateFromFile(typeof(System.ComponentModel.TypeConverterAttribute).Assembly.Location)));
MetadataItem output = GenerateYamlMetadata(CreateCompilationFromCSharpCode(code));
var @class = output.Items[0].Items[0];
Assert.NotNull(@class);
Assert.Equal("TestAttribute", @class.DisplayNames[SyntaxLanguage.CSharp]);
Expand Down Expand Up @@ -3284,24 +3284,7 @@ public class Foo

private static Compilation CreateCompilationFromCSharpCode(string code, params MetadataReference[] references)
{
return CreateCompilationFromCSharpCode(code, "test.dll", references);
}

private static Compilation CreateCompilationFromCSharpCode(string code, string assemblyName, params MetadataReference[] references)
{
var tree = SyntaxFactory.ParseSyntaxTree(code);
var defaultReferences = new List<MetadataReference> { MetadataReference.CreateFromFile(typeof(object).Assembly.Location), MetadataReference.CreateFromFile(typeof(EditorBrowsableAttribute).Assembly.Location) };
if (references != null)
{
defaultReferences.AddRange(references);
}

var compilation = CSharpCompilation.Create(
assemblyName,
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
syntaxTrees: new[] { tree },
references: defaultReferences);
return compilation;
return CompilationUtility.CreateCompilationFromCsharpCode(code, "test.dll", references);
}

private static Assembly CreateAssemblyFromCSharpCode(string code, string assemblyName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ End Sub
End Class
End Namespace
";
MetadataItem output = GenerateYamlMetadata(CreateCompilationFromVBCode(code, MetadataReference.CreateFromFile(typeof(System.ComponentModel.TypeConverterAttribute).Assembly.Location)));
MetadataItem output = GenerateYamlMetadata(CreateCompilationFromVBCode(code));
Assert.Single(output.Items);
var type = output.Items[0].Items[0];
Assert.NotNull(type);
Expand Down Expand Up @@ -1727,39 +1727,7 @@ End Namespace

private static Compilation CreateCompilationFromVBCode(string code, params MetadataReference[] references)
{
return CreateCompilationFromVBCode(code, "test.dll", references);
}

private static Compilation CreateCompilationFromVBCode(string code, string assemblyName, params MetadataReference[] references)
{
var tree = SyntaxFactory.ParseSyntaxTree(code);
var defaultReferences = new List<MetadataReference> { MetadataReference.CreateFromFile(typeof(object).Assembly.Location) };
if (references != null)
{
defaultReferences.AddRange(references);
}

var compilation = VisualBasicCompilation.Create(
assemblyName,
options: new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
syntaxTrees: new[] { tree },
references: defaultReferences);
return compilation;
}

private static Assembly CreateAssemblyFromVBCode(string code, string assemblyName)
{
// MemoryStream fails when MetadataReference.CreateFromAssembly with error: Empty path name is not legal
var compilation = CreateCompilationFromVBCode(code);
EmitResult result;
using (FileStream stream = new FileStream(assemblyName, FileMode.Create))
{
result = compilation.Emit(stream);
}

Assert.True(result.Success, string.Join(",", result.Diagnostics.Select(s => s.GetMessage())));
return Assembly.LoadFile(Path.GetFullPath(assemblyName));
return CompilationUtility.CreateCompilationFromVBCode(code, "test.dll", references);
}

}
}
6 changes: 0 additions & 6 deletions test/docfx.Tests/docfx.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\src\Shared\base.props" />

<ItemGroup>
<None Update="Assets\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand All @@ -13,8 +11,4 @@
<ProjectReference Include="..\..\src\docfx\docfx.csproj" />
<ProjectReference Include="..\Microsoft.DocAsCode.Tests.Common\Microsoft.DocAsCode.Tests.Common.csproj" />
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
</Project>