Skip to content

Commit

Permalink
Search for global.json up directory tree
Browse files Browse the repository at this point in the history
Skip already installed sdks
  • Loading branch information
slang25 committed Sep 5, 2019
1 parent 0a1c48f commit ba1f294
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 13 deletions.
@@ -1,4 +1,5 @@
using System.IO;
using System.Linq;
using System.Text.Json;

namespace DotNet.InstallSdk.Acquirables.GlobalJson
Expand All @@ -11,13 +12,24 @@ public GlobalJsonFileLocator(ITextWriter _)

public GlobalJsonParseResult Parse(string file = "global.json")
{
if (!File.Exists(file))
return GlobalJsonParseResult.Failure($"A {file} file could not be found in the current directory.");

var contents = File.ReadAllText(file);
var result = JsonSerializer.Deserialize<GlobalJsonFile>(contents);
var dir = new DirectoryInfo(Directory.GetCurrentDirectory());

while (dir != null)
{
var searchFiles = dir.GetFiles(file);
if (!searchFiles.Any())
{
dir = dir.Parent;
continue;
}

var contents = File.ReadAllText(Path.Combine(dir.ToString(), file));
var result = JsonSerializer.Deserialize<GlobalJsonFile>(contents);

return GlobalJsonParseResult.Success(result);
}

return GlobalJsonParseResult.Success(result);
return GlobalJsonParseResult.Failure($"A {file} file could not be found.");
}
}
}
1 change: 1 addition & 0 deletions src/DotNetInstallSdk/DotNetInstallSdk.csproj
Expand Up @@ -22,6 +22,7 @@
<ItemGroup>
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.3.4" />
<PackageReference Include="System.Text.Json" Version="4.6.0-preview7.19362.9" />
<PackageReference Include="SimpleExec" Version="6.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(Configuration)' == 'Debug'">
Expand Down
5 changes: 2 additions & 3 deletions src/DotNetInstallSdk/InstallSdkTool.cs
@@ -1,4 +1,3 @@
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
Expand All @@ -8,12 +7,12 @@ namespace DotNet.InstallSdk
{
public static class InstallSdkTool
{
public static void Run(Acquirable acquirable, ConsoleTextWriter writer)
public static async Task RunAsync(Acquirable acquirable, ITextWriter writer)
{
try
{
var sdkAcquirer = new SdkAcquirer(new HttpClient(), writer, new InstallerLauncher(), new PlatformIdentifier());
sdkAcquirer.Acquire(acquirable).GetAwaiter().GetResult();
await sdkAcquirer.Acquire(acquirable);
}
catch (FileNotFoundException e)
{
Expand Down
9 changes: 5 additions & 4 deletions src/DotNetInstallSdk/Program.cs
@@ -1,4 +1,5 @@
using DotNet.InstallSdk.Acquirables;
using System.Threading.Tasks;
using DotNet.InstallSdk.Acquirables;
using DotNet.InstallSdk.Acquirables.GlobalJson;
using McMaster.Extensions.CommandLineUtils;

Expand All @@ -16,7 +17,7 @@ public static int Main(string[] args)
[Option("-L|--latest", Description = "Install the latest non-preview version of the .NET Core SDK")]
public bool Latest { get; } = false;
*/
private void OnExecute()
private async Task OnExecuteAsync()
{
var writer = new ConsoleTextWriter();

Expand All @@ -29,8 +30,8 @@ private void OnExecute()
{
a = new GlobalJsonVersion(writer);
}
InstallSdkTool.Run(a, writer);

await InstallSdkTool.RunAsync(a, writer);
}
}
}
31 changes: 31 additions & 0 deletions src/DotNetInstallSdk/SdkAcquirer.cs
Expand Up @@ -8,6 +8,7 @@
using System.Text.Json;
using System.Threading.Tasks;
using DotNet.InstallSdk.Acquirables;
using static SimpleExec.Command;

namespace DotNet.InstallSdk
{
Expand All @@ -32,6 +33,12 @@ public async Task Acquire(Acquirable acquirable)
if (!result.IsSuccess)
return;

if (await CheckSdkExists(result.Version))
{
_textWriter.WriteLine($"SDK version {result.Version} is already installed.");
return;
}

using var channelResponse = await JsonDocument.ParseAsync(await _httpClient.GetStreamAsync(result.ChannelJson));

var file = channelResponse
Expand Down Expand Up @@ -113,5 +120,29 @@ void CheckHash(string filePath, string fileHash)
if (hashString != fileHash)
_textWriter.WriteLine("The downloaded file contents did not match expected hash.");
}

static async Task<bool> CheckSdkExists(string version)
{
try
{
var dotnetInfo =
(await ReadAsync("dotnet", "--info", noEcho: true)).Split(new[] {'\r', '\n'},
StringSplitOptions.RemoveEmptyEntries);

var existingSdks =
dotnetInfo
.SkipWhile(x => !x.Contains(".NET Core SDKs installed:")) // TODO localize?
.TakeWhile(x => !string.IsNullOrWhiteSpace(x))
.Select(x => x.Split(' ', StringSplitOptions.RemoveEmptyEntries))
.Where(x => x.Length > 0)
.Select(x => x.First());

return existingSdks.Contains(version);
}
catch
{
return false; // If we fail to detect installed sdks, just assume we need to aquire it
}
}
}
}

0 comments on commit ba1f294

Please sign in to comment.