枚举 .csproj 中的文件(dotnet 核心) [英] Enumerate Files in a .csproj (dotnet core)

查看:31
本文介绍了枚举 .csproj 中的文件(dotnet 核心)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# 开发一个构建工具,它应该可以与 Visual Studio/MSBuild 一起使用.我想枚举属于 C# 项目的所有文件.项目格式是新的 (.NET Core) .csproj.

I am working on a build tool in C# that should work with Visual Studio / MSBuild. I want to enumerate all files that are part of a C# project. The project format is the new (.NET Core) .csproj.

描述项目系统的文档指出使用 MSBuild(文件格式)或通用项目系统(项目树).我对这两个 API 都不熟悉.查看这些各自项目的文档并没有立即帮助.

The documentation describing the Project System points at using MSBuild (file format) or Common Project System (project tree). I'm unfamiliar with both APIs. Looking at documentation for those respective projects is not immediately helpful.

专家可能知道,新的 .csproj 文件并未列出隐含在项目中的每个文件.另一方面,它可能会列出项目文件夹之外的链接"文件.我想确保我获得了所有被视为项目一部分的文件.

As the expert probably knows, the new .csproj file does not list every file that is implicitly part of the project. On the other hand it may list a 'linked' file that is outside the project folder. I want to make sure I get all files that are considered part of the project.

最终我想专注于特定文件类型 (.json),但我认为一般性问题值得提出.

Ultimately I want to focus on a particular file type (.json), but I thought the general question was worth asking.

总结:如何编写一个 C# 库来利用适当的包(希望很容易)枚举 csproj 中的所有文件?

推荐答案

Buildalyzer 是最容易使用的包,它针对 .NETStandard 2.0,使其跨平台.(Omnisharp 目前不提供用于处理工作区的 NuGet 包.Microsoft.CodeAnalysis 对获取正确的引用提出了挑战,并且仅限于 net46.)

Buildalyzer is the easiest package to use, and it targets .NETStandard 2.0 making it cross-platform. (Omnisharp does not currently offer a NuGet package for working with the workspace. And Microsoft.CodeAnalysis poses a challenge to get the correct references in place, and is limited to net46.)

using Buildalyzer;

private static IList<string> InlcudedProjectKeys = new[] { "None", "Compile", "Content", "EmbeddedResource" };

private static IEnumerable<string> EnumerateProjectFiles(string projectPath)
{
    AnalyzerManager manager = new AnalyzerManager();
    ProjectAnalyzer analyzer = manager.GetProject(projectPath);
    AnalyzerResults results = analyzer.Build();
    AnalyzerResult result = results.Single();

    // If only interested in C# files, check out:
    //string[] sourceFiles = result.SourceFiles;

    IReadOnlyDictionary<string, ProjectItem[]> items = result.Items;
    foreach (var item in items)
    {
        // Skip keys like ProjectReference that aren't for files
        if (!InlcudedProjectKeys.Contains(item.Key))
            continue;
        ProjectItem[] projectItems = item.Value;
        foreach (var projectItem in projectItems)
        {
            // The item spec for files will be the path relative to the project directory
            yield return projectItem.ItemSpec;
        }
    }
}

对于奖励积分,仅获取 *.json 文件:

And for bonus points, to get only *.json files:

var jsonFiles = EnumerateProjectFiles(projectPath)
    .Where(path => path.EndsWith(".json"))
    .ToArray();

感谢 Hitesh 链接到相关资源.

Thanks Hitesh for linking to relevant resources.

这篇关于枚举 .csproj 中的文件(dotnet 核心)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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