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

查看:75
本文介绍了拥有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:\temp\dest\</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天全站免登陆