forked from OmniSharp/omnisharp-roslyn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c2bcdc
commit 00efa68
Showing
4 changed files
with
113 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using System.Composition; | ||
using System.IO; | ||
using OmniSharp.Services; | ||
|
||
namespace OmniSharp.MSBuild | ||
{ | ||
[Export, Shared] | ||
public class SdksPathResolver | ||
{ | ||
private const string MSBuildSDKsPath = nameof(MSBuildSDKsPath); | ||
private const string Sdks = nameof(Sdks); | ||
|
||
private readonly DotNetCliService _dotNetCli; | ||
|
||
[ImportingConstructor] | ||
public SdksPathResolver(DotNetCliService dotNetCli) | ||
{ | ||
_dotNetCli = dotNetCli; | ||
} | ||
|
||
public bool TryGetSdksPath(string projectFilePath, out string sdksPath) | ||
{ | ||
var projectFileDirectory = Path.GetDirectoryName(projectFilePath); | ||
|
||
var info = _dotNetCli.GetInfo(projectFileDirectory); | ||
|
||
if (info.IsEmpty || string.IsNullOrWhiteSpace(info.BasePath)) | ||
{ | ||
sdksPath = null; | ||
return false; | ||
} | ||
|
||
sdksPath = Path.Combine(info.BasePath, Sdks); | ||
|
||
if (Directory.Exists(sdksPath)) | ||
{ | ||
return true; | ||
} | ||
|
||
sdksPath = null; | ||
return false; | ||
} | ||
|
||
public IDisposable SetSdksPathEnvironmentVariable(string projectFilePath) | ||
{ | ||
if (!TryGetSdksPath(projectFilePath, out var sdksPath)) | ||
{ | ||
return NullDisposable.Instance; | ||
} | ||
|
||
var oldMSBuildSDKsPath = Environment.GetEnvironmentVariable(MSBuildSDKsPath); | ||
Environment.SetEnvironmentVariable(MSBuildSDKsPath, sdksPath); | ||
|
||
return new ResetSdksPathEnvironmentVariable(oldMSBuildSDKsPath); | ||
} | ||
|
||
private class NullDisposable : IDisposable | ||
{ | ||
public static IDisposable Instance { get; } = new NullDisposable(); | ||
|
||
public void Dispose() { } | ||
} | ||
|
||
private class ResetSdksPathEnvironmentVariable : IDisposable | ||
{ | ||
private readonly string _oldValue; | ||
|
||
public ResetSdksPathEnvironmentVariable(string oldValue) | ||
{ | ||
_oldValue = oldValue; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Environment.SetEnvironmentVariable(MSBuildSDKsPath, _oldValue); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters