Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having a sln file, possible to list all files from all projects under that sln?

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

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

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.

like image 351
Adrian Iftode Avatar asked Oct 26 '18 06:10

Adrian Iftode


People also ask

What does .sln file contain?

sln file. The . sln file contains text-based information the environment uses to find and load the name-value parameters for the persisted data and the project VSPackages it references. When a user opens a solution, the environment cycles through the preSolution , Project , and postSolution information in the .

How do I see all project files in Visual Studio?

Open your project in Visual Studio > click the Show All Files button > expand the bin , Debug > select and right-click the parent folder > choose Include in Project option. 4).

Where is the .sln file?

sln" file is created in the parent folder, along with the hidden ". vs" folder.


Video Answer


2 Answers

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>

Then run a build like

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

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

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.

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.

like image 134
stijn Avatar answered Oct 16 '22 13:10

stijn


Not sure if this might help, but I belive Microsoft.Build.Evaluation might help you here. There is another post about what I think is your problem:

csproj how to get all resources

I dont know with what kind of projects you are working with, I would assume csproj. But maybe this helps even if thats not the case. You should be able to modify the given example so that you get all the files you need.

Side note: It would probably helop to have a little more information about what kind of projects we are talking about, how many and if there are excluded files etc.

like image 36
Benjamin Basmaci Avatar answered Oct 16 '22 14:10

Benjamin Basmaci