有一个 sln 文件,可以列出该 sln 下所有项目的所有文件吗? [英] Having a sln file, possible to list all files from all projects under that sln?

查看:43
本文介绍了有一个 sln 文件,可以列出该 sln 下所有项目的所有文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法或扩展可以执行以下操作:

Is there a way or an extension that does the following:

有一个 *.sln 文件,我可以列出该 sln 下所有项目或所有解决方案文件夹中的所有文件吗?

Having a *.sln file, can I list all files from all projects or all solution folders under that sln?

因此,如果我执行 dotnet sln list,那么我会从该解决方案文件中获取项目列表,但我需要的是获取所有文件.

So if I do dotnet sln list then I get the list of the projects from that solution file, but what I need is to get all files.

我真正需要的是将所有这些文件复制到不同的文件夹中.

My real need is to copy all those files into a different folder.

推荐答案

这是一个基于 msbuild 的解决方案,它实际上相当简单且非侵入性.例如在解决方案目录下创建一个文件CopySource.targets,内容:

Here's an msbuild-based solution which is actually fairly simple and also non-intrusive. Create a file CopySource.targets in the solution directory for instance, contents:

<Project>
  <Target Name="CopySourcesUsed" AfterTargets="AfterCompile">
    <PropertyGroup>
      <DestinationDirectory>c:	empdest</DestinationDirectory>
      <SourceDirectory>$([System.IO.Path]::GetDirectoryName($(MSBuildProjectDirectory)))</SourceDirectory>
    </PropertyGroup>
    <ItemGroup>
      <CompileSources Condition="!$([System.Text.RegularExpressions.Regex]::IsMatch('%(Identity)', `.*Assembly(Attributes|Info).cs`))" Include="@(Compile)"/>
      <AllSources Include="@(CompileSources);@(EmbeddedResource);$(MSBuildProjectFile)" />
      <AllSources>
        <Dest>$([System.String]::new('%(FullPath)').Replace($(SourceDirectory), $(DestinationDirectory)))</Dest>
      </AllSources>
    </ItemGroup>
    <Message Importance="High" Text="%(AllSources.FullPath) -> %(AllSources.Dest)"/>
    <Copy SourceFiles="@(AllSources)" DestinationFiles="@(AllSources->'%(Dest)')" />
  </Target>
</Project>

然后像这样运行构建

dotnet msbuild mysolution.sln /p:CustomAfterMicrosoftCSharpTargets=/path/to/CopySource.targets

(注意 dotnet build 也适用于我,但可能不适用于旧版本,不知道)

(note dotnet build also works for me but might not in older versions, don't know)

这将在每个项目文件中导入 ListSources.Targets 并在编译后运行 CopySourcesUsed 目标.在编译之前某处也可以工作,但是你必须弄清楚什么时候:编译之后所有的项目肯定都已经收集好了.例如,AllSources 项填充了编译源文件和嵌入的资源.正如您所看到的,Compile 项被过滤以排除生成的 AssemblyAttributes/AssemblyInfo 文件,因为您可能不想要这些文件.另一种方法可能是在添加这些文件之前运行此目标,但同样:这更棘手.您可能还想复制其他内容 (xaml/config/...),在这种情况下,您必须弄清楚要包含的项目的名称.首先检查它是否不是简单地列在 csproj 中.否则,以诊断详细程度运行构建可以告诉您名称:dotnet msbuild mysolution.sln/v:diag.

This will import ListSources.Targets in each project file and run the CopySourcesUsed target after compilation. Before compilation somewhere will also work, but then you have to figure out exactly when: after compilation all items definitely have been gathered. As an example the AllSources item is filled with the compilation source files and the embedded resources. As you can see the Compile item is filtered to exclude the generated AssemblyAttributes/AssemblyInfo files since you probably don't want those. An alternative might be to have this target ran before those files are added, but again: that's just trickier. There can be other things you want to copy (xaml/config/...) in which case you have to figure out the name of the item to include. First check if it's not simply listed in the csproj. Else running the build with diagnostic verbosity can tell you the name: dotnet msbuild mysolution.sln /v:diag.

不是在命令行中注入这个逻辑,你可以在你拥有的每个项目中导入文件,或者使用另一个扩展点,比如通过将它放在某个 ImportAfter 目录中来使其在系统范围内,所有这些解决方案都可以在这里找到所以可能.

Instead of injecting this logic on the commandline you could instead import the file in each project you have, or use another extension point like making it system-wide by putting it in some ImportAfter directory, all these solutions can be found here on SO probably.

这篇关于有一个 sln 文件,可以列出该 sln 下所有项目的所有文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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