From 3a89c4843fb4efc6385ce0ec82974fa5388aa4dc Mon Sep 17 00:00:00 2001 From: filzrev <103790468+filzrev@users.noreply.github.com> Date: Fri, 30 Aug 2024 09:25:07 +0900 Subject: [PATCH] feat: add AllowUnsafeBlocks supports for source based metadata generation --- docs/reference/docfx-json-reference.md | 7 ++++++ src/Docfx.Dotnet/CompilationHelper.cs | 30 ++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/docs/reference/docfx-json-reference.md b/docs/reference/docfx-json-reference.md index f0de2c02839..ff1e2dee49b 100644 --- a/docs/reference/docfx-json-reference.md +++ b/docs/reference/docfx-json-reference.md @@ -397,6 +397,13 @@ Specifies an optional set of MSBuild properties used when interpreting project f > [!Note] > Make sure to specify `"TargetFramework": ` in your docfx.json when the project is targeting for multiple platforms. +> [!Note] +> When generating metadata from source code files. +> Supported properties are limited to the following. +> - `DefineConstants` +> - `AllowUnsafeBlocks` +> If other properties are specified. These properties are ignored silently. + ### `noRestore` Do not run `dotnet restore` before building the projects. diff --git a/src/Docfx.Dotnet/CompilationHelper.cs b/src/Docfx.Dotnet/CompilationHelper.cs index 47da38b3fcb..75bf48f3e1e 100644 --- a/src/Docfx.Dotnet/CompilationHelper.cs +++ b/src/Docfx.Dotnet/CompilationHelper.cs @@ -62,7 +62,7 @@ public static Compilation CreateCompilationFromCSharpFiles(IEnumerable f return CS.CSharpCompilation.Create( assemblyName: null, - options: new CS.CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, xmlReferenceResolver: XmlFileResolver.Default), + options: GetCSharpCompilationOptions(msbuildProperties), syntaxTrees: syntaxTrees, references: GetDefaultMetadataReferences("C#").Concat(references)); } @@ -74,7 +74,7 @@ public static Compilation CreateCompilationFromCSharpCode(string code, IDictiona return CS.CSharpCompilation.Create( name, - options: new CS.CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, xmlReferenceResolver: XmlFileResolver.Default), + options: GetCSharpCompilationOptions(msbuildProperties), syntaxTrees: [syntaxTree], references: GetDefaultMetadataReferences("C#").Concat(references ?? [])); } @@ -86,7 +86,7 @@ public static Compilation CreateCompilationFromVBFiles(IEnumerable files return VB.VisualBasicCompilation.Create( assemblyName: null, - options: new VB.VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary, globalImports: GetVBGlobalImports(), xmlReferenceResolver: XmlFileResolver.Default), + options: GetVisualBasicCompilationOptions(msbuildProperties), syntaxTrees: syntaxTrees, references: GetDefaultMetadataReferences("VB").Concat(references)); } @@ -98,7 +98,7 @@ public static Compilation CreateCompilationFromVBCode(string code, IDictionary msbuildProperties) + { + var options = new CS.CSharpCompilationOptions( + OutputKind.DynamicallyLinkedLibrary, + xmlReferenceResolver: XmlFileResolver.Default); + + if (msbuildProperties.TryGetValue("AllowUnsafeBlocks", out var valueText) && bool.TryParse(valueText, out var allowUnsafe)) + { + options = options.WithAllowUnsafe(allowUnsafe); + } + + return options; + } + + private static VB.VisualBasicCompilationOptions GetVisualBasicCompilationOptions(IDictionary msbuildProperties) + { + return new VB.VisualBasicCompilationOptions( + OutputKind.DynamicallyLinkedLibrary, + globalImports: GetVBGlobalImports(), + xmlReferenceResolver: XmlFileResolver.Default); + } }