How to add all projects to a single solution with dotnet sln?
Asked Answered
C

5

15

Following examples from here I'm trying to execute

dotnet sln AllProjects.sln add **/*.csproj

But I get this error:

Could not find project or directory **/*.csproj.

Looks like wildcards are not working. What am I doing wrong?

Civic answered 25/8, 2018 at 12:28 Comment(5)
Whats the Operating System?Punak
Windows, PowerShellCivic
is your projects are in .net core?Counterforce
No, they are notCivic
Not 100% on this but I think the **/ is Linux based...try just (dot) ./* or (dot)(dot) if your backing up one levelPunak
C
13

I've missed this statement:

Globbing patterns are supported on Unix/Linux based terminals

My Windows PowerShell solution looks like this:

$projects = Get-ChildItem -Recurse | Where-Object { $_.Name -match '^.+\.(csproj|vbproj)$' }

$uniqueProjects = $projects | Group-Object -Property Name | Where Count -EQ 1 | select -ExpandProperty Group | % { $_.FullName }

Invoke-Expression -Command "dotnet new sln -n AllProjects"

$uniqueProjects | % { Invoke-Expression -Command "dotnet sln AllProjects.sln add ""$_""" }
Civic answered 25/8, 2018 at 17:8 Comment(2)
This should be voted down. Way too much work. The right answer is below: dotnet sln add (ls */.csproj)Bleach
Official documentation contains all the solutions much better than this. learn.microsoft.com/en-us/dotnet/core/tools/dotnet-slnHaug
K
32

For Windows, open PowerShell and run this command to add all projects to the solution file:

 dotnet sln add (ls -r **/*.csproj)
Kinaesthesia answered 21/3, 2021 at 15:38 Comment(5)
How can this be done in Linux?Diep
Running the above command throws the error: syntax error near unexpected token `(' because the '$' before the ls is missing. The correct syntax is: dotnet sln add $(ls -r **/*.csproj) which is working on (Git and Linux) bash.Guendolen
dotnet sln add $(ls -r */.csproj)Plutus
@Civic this is the correct answer. For linux/unix just use "dotnet sln add mysolution.sln */.csprojHaug
Works also with WSLMilling
C
13

I've missed this statement:

Globbing patterns are supported on Unix/Linux based terminals

My Windows PowerShell solution looks like this:

$projects = Get-ChildItem -Recurse | Where-Object { $_.Name -match '^.+\.(csproj|vbproj)$' }

$uniqueProjects = $projects | Group-Object -Property Name | Where Count -EQ 1 | select -ExpandProperty Group | % { $_.FullName }

Invoke-Expression -Command "dotnet new sln -n AllProjects"

$uniqueProjects | % { Invoke-Expression -Command "dotnet sln AllProjects.sln add ""$_""" }
Civic answered 25/8, 2018 at 17:8 Comment(2)
This should be voted down. Way too much work. The right answer is below: dotnet sln add (ls */.csproj)Bleach
Official documentation contains all the solutions much better than this. learn.microsoft.com/en-us/dotnet/core/tools/dotnet-slnHaug
R
7

On Windows you could also use the following command to recursively add all the projects in sub-directories to a pre-existing solution file:

FOR /R %i IN (*.csproj) DO dotnet sln add "%i"

Alternatively, if you need to (re)create solution files often then you could create a batch file with the following content, and then just run it whenever you need to:

dotnet new sln 
FOR /R %%i IN (*.csproj) DO dotnet sln add "%%i"

Please note that you need the extra % when trying to do this inside the batch file.

Rider answered 10/3, 2020 at 1:21 Comment(0)
S
5

I tried ls -r on git bash

dotnet sln add (ls -r **/*.csproj)

but it gives me

$ dotnet sln add (ls -r **\*.csproj)
bash: syntax error near unexpected token `('

And then i tried

dotnet sln add **/*.csproj

it worked for me on git bash(windows)

Sententious answered 1/7, 2022 at 2:15 Comment(3)
The simbol $ before the (ls...) is missing.Guendolen
On that section i wrote command with console output and that $ sign is not from command. The command you should execute is first command or last command in this answer. Second one is output of first oneSententious
dotnet sln add $(ls -r */.csproj)Plutus
T
1

For linux

find . -name "*.csproj" -print0 | xargs -0 dotnet sln add


find . -name "*.csproj" ! -name "exclude-this.csproj" -print0 | xargs -0 dotnet sln add

Towrope answered 18/10, 2023 at 23:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.