-
Notifications
You must be signed in to change notification settings - Fork 61
[Bootstrapper] Fetch information from dotnet feeds #391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
71777a1
8c10e2d
ec96336
5ca0b5c
941bb43
80beb9c
9d02fa9
59ee658
c623aa7
6a89e20
12a155a
cbdb765
f46bccc
58580f2
9838ae3
3bbdddc
cbc2b7d
4df438d
7b35bd6
a9a47f5
1697e1e
822f5e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.CommandLine.Parsing; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.DotNet.Tools.Bootstrapper.Commands | ||
{ | ||
public abstract class CommandBase | ||
{ | ||
protected ParseResult _parseResult; | ||
|
||
protected CommandBase(ParseResult parseResult) | ||
{ | ||
_parseResult = parseResult; | ||
} | ||
|
||
public abstract int Execute(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.CommandLine; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.DotNet.Tools.Bootstrapper.Commands | ||
{ | ||
internal static class Common | ||
{ | ||
internal static Option<bool> AllowPreviewsOptions = new Option<bool>( | ||
"--allow-previews", | ||
description: "Include pre-release sdk versions"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.CommandLine.Parsing; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.Deployment.DotNet.Releases; | ||
using Spectre.Console; | ||
|
||
namespace Microsoft.DotNet.Tools.Bootstrapper.Commands.Search; | ||
|
||
internal class SearchCommand( | ||
ParseResult parseResult) : CommandBase(parseResult) | ||
{ | ||
private string _channel = parseResult.ValueForArgument(SearchCommandParser.ChannelArgument); | ||
private bool _allowPreviews = parseResult.ValueForOption(SearchCommandParser.AllowPreviewsOption); | ||
public override int Execute() | ||
{ | ||
List<Product> productCollection = [.. ProductCollection.GetAsync().Result]; | ||
edvilme marked this conversation as resolved.
Show resolved
Hide resolved
|
||
productCollection = [.. | ||
productCollection.Where(product => !product.IsOutOfSupport() && (product.SupportPhase != SupportPhase.Preview || _allowPreviews))]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @baronfel Did we decide to only support in support installs? I understand why we'd want to do that, but it seems to limit the product use cases. I would rather emit a warning than block the behavior, but maybe that's ill-advised. |
||
|
||
if (!string.IsNullOrEmpty(_channel)) | ||
{ | ||
productCollection = [.. productCollection.Where(product => product.ProductVersion.Equals(_channel, StringComparison.OrdinalIgnoreCase))]; | ||
edvilme marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
foreach (Product product in productCollection) | ||
{ | ||
string productHeader = $"{product.ProductName} {product.ProductVersion}"; | ||
Console.WriteLine(productHeader); | ||
|
||
Table productMetadataTable = new Table() | ||
.AddColumn("Version") | ||
.AddColumn("Release Date") | ||
.AddColumn("Latest SDK") | ||
.AddColumn("Runtime") | ||
.AddColumn("ASP.NET Runtime") | ||
.AddColumn("Windows Desktop Runtime"); | ||
|
||
List<ProductRelease> releases = product.GetReleasesAsync().Result.ToList() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For future reference in the above issue again: Does this cache, and does it handle no internet/timeouts well? The responsibility of ownership here is interesting. Ideally this command could work offline if it has cached information. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I think this does cache, and handles updating and working offline too: |
||
.Where(relase => !relase.IsPreview || _allowPreviews).ToList(); | ||
|
||
foreach (ProductRelease release in releases) | ||
{ | ||
// Get release.Sdks latest version | ||
var latestSdk = release.Sdks | ||
.OrderByDescending(sdk => sdk.Version) | ||
.FirstOrDefault(); | ||
|
||
productMetadataTable.AddRow( | ||
release.Version.ToString(), | ||
release.ReleaseDate.ToString("d", CultureInfo.CurrentUICulture), | ||
latestSdk?.DisplayVersion ?? "N/A", | ||
release.Runtime?.DisplayVersion ?? "N/A", | ||
release.AspNetCoreRuntime?.DisplayVersion ?? "N/A", | ||
release.WindowsDesktopRuntime?.DisplayVersion ?? "N/A"); | ||
} | ||
AnsiConsole.Write(productMetadataTable); | ||
Console.WriteLine(); | ||
} | ||
|
||
return 0; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.CommandLine; | ||
using System.CommandLine.Invocation; | ||
using System.CommandLine.Parsing; | ||
|
||
namespace Microsoft.DotNet.Tools.Bootstrapper.Commands.Search; | ||
|
||
internal class SearchCommandParser | ||
{ | ||
internal static Argument<string> ChannelArgument = new Argument<string>( | ||
name: "channel", | ||
description: "The channel to list sdks for. If not specified, all sdks will be listed.") | ||
{ | ||
Arity = ArgumentArity.ZeroOrOne | ||
}; | ||
|
||
internal static Option<bool> AllowPreviewsOption = Common.AllowPreviewsOptions; | ||
|
||
private static readonly Command Command = ConstructCommand(); | ||
public static Command GetCommand() => Command; | ||
private static Command ConstructCommand() | ||
{ | ||
Command command = new("search", "Search for SDKs available for installation."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: It will be easier to keep these strings in the localize file if we start now, but I also don't want to block on this. |
||
command.AddArgument(ChannelArgument); | ||
command.AddOption(AllowPreviewsOption); | ||
|
||
command.Handler = CommandHandler.Create((ParseResult parseResult) => | ||
{ | ||
return new SearchCommand(parseResult).Execute(); | ||
}); | ||
|
||
return command; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.IO; | ||
using System.Text.Json; | ||
using Microsoft.Deployment.DotNet.Releases; | ||
|
||
namespace Microsoft.DotNet.Tools.Bootstrapper | ||
{ | ||
/// <summary> | ||
/// Provides utility methods for working with global.json files in a directory tree. | ||
/// </summary> | ||
internal static class GlobalJsonUtilities | ||
{ | ||
/// <summary> | ||
/// Finds the nearest global.json file path by traversing up the directory tree starting from the specified base path. | ||
/// </summary> | ||
/// <param name="basePath">The starting directory path to search for the global.json file.</param> | ||
/// <returns> | ||
/// The full path to the nearest global.json file if found; otherwise, null. | ||
/// </returns> | ||
public static string GetNearestGlobalJsonPath(string basePath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is good for now, I think the runtime implemented some global json logic and it loks like CliTemplateEngineHost also did. We should probably try to share that implementation in the future, and I can create an issue for that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
// Example implementation: Traverse up the directory tree to find the nearest global.json file. | ||
string currentPath = basePath; | ||
while (!string.IsNullOrEmpty(currentPath)) | ||
{ | ||
string globalJsonPath = Path.Combine(currentPath, "global.json"); | ||
if (File.Exists(globalJsonPath)) | ||
{ | ||
return globalJsonPath; | ||
} | ||
currentPath = Directory.GetParent(currentPath)?.FullName; | ||
} | ||
// If no global.json file is found, return null. | ||
return null; | ||
} | ||
|
||
/// <summary> | ||
/// Retrieves the contents of the nearest global.json file as a JsonElement by traversing up the directory tree. | ||
/// </summary> | ||
/// <param name="basePath">The starting directory path to search for the global.json file.</param> | ||
/// <returns> | ||
/// A JsonElement representing the contents of the nearest global.json file. | ||
/// </returns> | ||
/// <exception cref="FileNotFoundException">Thrown if no global.json file is found in the directory tree.</exception> | ||
/// <exception cref="JsonException">Thrown if global.json cannot be parsed as json</exception> | ||
public static JsonElement GetNearestGlobalJson(string basePath) | ||
{ | ||
string globalJsonPath = GetNearestGlobalJsonPath(basePath); | ||
|
||
if (globalJsonPath == null) | ||
{ | ||
throw new FileNotFoundException("No global.json file found in the directory tree."); | ||
} | ||
|
||
return JsonDocument.Parse(File.ReadAllText(globalJsonPath)).RootElement; | ||
} | ||
/// <summary> | ||
/// Retrieves the major and minor version of the .NET SDK specified in the nearest global.json file | ||
/// within the given base path. | ||
/// </summary> | ||
/// <param name="basePath">The base directory path to search for the global.json file.</param> | ||
/// <returns> | ||
/// A string representing the major and minor version (e.g., "6.0") of the .NET SDK specified | ||
/// in the global.json file. | ||
/// </returns> | ||
/// <exception cref="KeyNotFoundException"> | ||
/// Thrown when the required keys ("tools" or "dotnet") are not found in the global.json file. | ||
/// </exception> | ||
public static string GetMajorVersionToInstallInDirectory(string basePath) | ||
{ | ||
try | ||
{ | ||
// Get the nearest global.json file. | ||
JsonElement globalJson = GlobalJsonUtilities.GetNearestGlobalJson(basePath); | ||
string sdkVersion = globalJson | ||
.GetProperty("tools") | ||
.GetProperty("dotnet") | ||
.ToString(); | ||
|
||
ReleaseVersion version = ReleaseVersion.Parse(sdkVersion); | ||
Console.WriteLine($"Found version {version.Major}.{version.Minor} in global.json"); | ||
return $"{version.Major}.{version.Minor}"; | ||
} | ||
catch (KeyNotFoundException e) | ||
{ | ||
throw new KeyNotFoundException("The specified key was not found in the global.json", e); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The releases.json only changes about once a month. You should consider caching the file on disk and only update it if there's a new version available.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be using the same etag-based cache invalidation system that @nagilson has for the VSCode extension. ETags are the way to handle cache invalidation of HTTP-delivered resources, and we shouldn't be reinventing the wheel.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does https://github.com/dotnet/deployment-tools cache it? I would hope it does so, but at a glance it looks like it does not. I tis what is interfacing with the web request caller API. I would also hope that it handles proxies well. As well as timeouts. If it does not, I would almost question why that should not be implemented over there instead of here. Definitely wouldn't block on this for this PR but would create another issue from it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From what I've seen, I don't think that it does. I do agree that it would make a lot of sense to implement it there