Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS 2017 reference local projects by filepath (like using global.json in VS 2015)

In VS 2015, we used to be able to specify a local path in global.json like so:

{
    “projects”: [ “src”, “test”, “C:\\path\\to\\other\\projects” ]
}

It would then add all the projects from that path to the current solution, allowing us to easily reference them from existing projects.

Now that VS 2017 has changed its' model to using csproj, and getting rid of project.json and global.json in the process, does anybody know of a way to this?

The best I've gotten is to manually include the other projects, one by one, into the solution. Then, in all the existing projects that need to reference it, I would have to edit their csproj to include them. This is really cumbersome compared to the previous way of simply including a filepath in one location.

Thanks for any help with this.

like image 578
Isaiah Lee Avatar asked Mar 10 '17 16:03

Isaiah Lee


1 Answers

Alright guys, it's May and we still don't have an official solution from Microsoft. I got something working using Powershell and the new .NET core CLI. There's already commands built into dotnet.exe to add/remove solutions from a project, so here's what I came up with.

Includes.json

{
    "Includes": [
        "C:\\projects\\SomeProjectA\\src",
        "C:\\git\\SomeProjectB\\src"
    ]
}

Add-Includes.ps1

echo "Beginning import of projects in Includes.json"

$JsonIncludes = (Get-Content -Raw -Path "Includes.json") | ConvertFrom-Json

$IncludePaths = $JsonIncludes.Includes;
foreach ($IncludePath in $IncludePaths) {

    $ProjectFiles = Get-ChildItem ($IncludePath + "\*") `
                    -Include *.csproj `
                    -Recurse `
                    | % {$_.FullName }

    foreach ($ProjectFile in $ProjectFiles) {
        dotnet sln add $ProjectFile
    }
}

Remove-Includes.ps1

echo "Beginning removal of projects in Includes.json"

$JsonIncludes = (Get-Content -Raw -Path "Includes.json") | ConvertFrom-Json

$IncludePaths = $JsonIncludes.Includes;
foreach ($IncludePath in $IncludePaths) {

    $ProjectFiles = Get-ChildItem ($IncludePath + "\*") `
                    -Include *.csproj `
                    -Recurse `
                    | % {$_.FullName }

    foreach ($ProjectFile in $ProjectFiles) {
        dotnet sln remove $ProjectFile
    }
}

It's a couple extra steps compared to using the old Global.json file, but it does what we need. To make it really convenient, add a solution folder and include the Includes.json so you can easily modify it from within Visual Studio.

Some notes:

  • The Add/Remove scripts are almost exactly the same, the only difference is the dotnet sln add/remove command. This can probably be cleaned up into one interactive script.
  • You could also change things so that instead of having a separate add/remove script, you simply read the Includes.json and compare it to what projects are currently in the solution by parsing the .sln file.

Just food for thought. Here's the repo if you want to clone/download: https://github.com/rushfive/VS2017-Includes

like image 126
Isaiah Lee Avatar answered Oct 06 '22 00:10

Isaiah Lee