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

前端 未结 1 1619
再見小時候
再見小時候 2021-01-18 10:39

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” ]
}         


        
相关标签:
1条回答
  • 2021-01-18 11:32

    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

    0 讨论(0)
提交回复
热议问题