带有 .netstandard 2.0 项目的 ProjectCracker [英] ProjectCracker with .netstandard 2.0 projects

查看:22
本文介绍了带有 .netstandard 2.0 项目的 ProjectCracker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的团队最近从使用 .net 框架切换到为我们的 F# 库使用 .net 标准 2.0.我们有一些在我们的项目中运行的内部脚本来自动生成 Markdown 文档,这些脚本使用 F# Compiler Services SDK 来分析代码并检索类型元数据、文档注释等.

My team has recently switched from using .net framework to use .net standard 2.0 for our F# libraries. We have some internal scripts that we run on our projects to automatically generate markdown documentation, and these scripts use the F# Compiler Services SDK to analyze the code and retrieve the type metadata, documentation comments, etc.

我们正在使用 FSharp.Compiler.Service.ProjectCracker 库来读取我们的 .fsproj 文件并生成 FSharpProjectOptions 实例以在以下情况下使用运行 FSharpChecker.ParseAndCheckFileInProject 方法.但是,对于 .net 标准项目,我们在调用 ProjectCracker.GetProjectOptionsFromProjectFile 时会收到以下错误:

We are using the FSharp.Compiler.Service.ProjectCracker library to read our .fsproj files and generate the FSharpProjectOptions instance to use when running the FSharpChecker.ParseAndCheckFileInProject method. However, with the .net standard projects, we get the following error when calling ProjectCracker.GetProjectOptionsFromProjectFile:

System.Exception: Could not load project Example.fsproj in ProjectCollection. 
Available tools: 
["C:\Program Files (x86)\MSBuild\12.0\bin\amd64";
 "C:\Program Files (x86)\MSBuild\14.0\bin\amd64";
 "C:\Windows\Microsoft.NET\Framework64\v2.0.50727";
 "C:\Windows\Microsoft.NET\Framework64\v3.5";
 "C:\Windows\Microsoft.NET\Framework64\v4.0.30319"]. 

Message: The default XML namespace of the project must be the MSBuild XML namespace. 
If the project is authored in the MSBuild 2003 format, please add xmlns="http://schemas.microsoft.com/developer/msbuild/2003" to the <Project> element. 
If the project has been authored in the old 1.0 or 1.2 format, please convert it to MSBuild 2003 format.```

有没有办法使用 Project Cracker 库读取 .net 标准项目文件,或者我们是否需要手动解析文件并为我们的新库手动创建 FSharpProjectOptions?

Is there any way to read .net standard project files using the Project Cracker library, or will we need to manually parse the file and create the FSharpProjectOptions by hand for our new libraries?

推荐答案

看来标准的 ProjectCracker 包不能用于读取 .netstandard 项目文件.但是,已经有一个 NuGet 包可用于提供此功能,Dotnet.项目信息.此项目允许您读取 .NET 项目文件的所有变体,包括 .NET Framework 和 .NET Core,以及 project.json 和 .csproj/.fsproj 文件.

It appears that the standard ProjectCracker package cannot be used to read .netstandard project files. However, there is already a NuGet package available to provide this functionality, Dotnet.ProjInfo. This project allows you to read all the variations of .NET project files, including .NET Framework and .NET Core, as well as both project.json and .csproj/.fsproj files.

该项目作为 API 的文档并不十分完善(文档更适合命令行工具),但可以在 FsAutoComplete 库.

The project isn't extremely well documented as an API (the documentation is better for the command-line tool), but programmatic usage of the tool can be seen in the FsAutoComplete library.

对于 .netstandard 项目,您可以使用 getProjectInfos 函数,如下所示:

For a .netstandard project, you can use the getProjectInfos function, like so:

let getProjectInfo additionalMSBuildProps file =
    let projDir = Path.GetDirectoryName file
    let additionalInfo = 
            [ "OutputType"
              "IsTestProject"
              "Configuration"
              "IsPackable"
              MSBuildKnownProperties.TargetFramework
              "TargetFrameworkIdentifier"
              "TargetFrameworkVersion"
              "MSBuildAllProjects"
              "ProjectAssetsFile"
              "RestoreSuccess"
              "Configurations"
              "TargetFrameworks"
              "RunArguments"
              "RunCommand"
              "IsPublishable"
            ]

    let loggedMessages = System.Collections.Concurrent.ConcurrentQueue<string>()
    let msBuildExec = Dotnet.ProjInfo.Inspect.msbuild (Dotnet.ProjInfo.Inspect.MSBuildExePath.DotnetMsbuild "dotnet") (fun exePath args -> Utils.runProcess loggedMessages.Enqueue projDir exePath (args |> String.concat " "))
    let gp () = Dotnet.ProjInfo.Inspect.getProperties (["TargetPath"; "IsCrossTargetingBuild"; "TargetFrameworks"] @ additionalInfo)

    let additionalArgs = additionalMSBuildProps |> List.map (Dotnet.ProjInfo.Inspect.MSBuild.MSbuildCli.Property)

    let cliList = [Dotnet.ProjInfo.Inspect.getFscArgs; Dotnet.ProjInfo.Inspect.getResolvedP2PRefs; gp]

    file |> Dotnet.ProjInfo.Inspect.getProjectInfos loggedMessages.Enqueue msBuildExec cliList additionalArgs

这篇关于带有 .netstandard 2.0 项目的 ProjectCracker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆