Skip to content
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions src/dotnet-bootstrapper/BootstrapperCommandParser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
Expand All @@ -7,6 +7,9 @@
using System.CommandLine.Invocation;
using System.CommandLine.Parsing;
using System.Reflection;
using Microsoft.DotNet.Tools.Uninstall.Shared.Configs;
using Microsoft.DotNet.Tools.Bootstrapper.Commands.Search;
using Microsoft.DotNet.Tools.Bootstrapper.Commands.Install;

namespace Microsoft.DotNet.Tools.Bootstrapper
{
Expand All @@ -15,30 +18,18 @@ internal static class BootstrapperCommandParser
public static Parser BootstrapParser;

public static RootCommand BootstrapperRootCommand = new RootCommand("dotnet bootstrapper");

public static readonly Command VersionCommand = new Command("--version");

private static readonly Lazy<string> _assemblyVersion =
new Lazy<string>(() =>
{
var assembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();
var assemblyVersionAttribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
if (assemblyVersionAttribute == null)
{
return assembly.GetName().Version.ToString();
}
else
{
return assemblyVersionAttribute.InformationalVersion;
}
});
public static readonly Command HelpCommand = new("--help");

static BootstrapperCommandParser()
{
BootstrapperRootCommand.AddCommand(VersionCommand);
VersionCommand.Handler = CommandHandler.Create(() =>
BootstrapperRootCommand.AddCommand(CommandLineConfigs.VersionSubcommand);
BootstrapperRootCommand.AddCommand(SearchCommandParser.GetCommand());
BootstrapperRootCommand.AddCommand(InstallCommandParser.GetCommand());
BootstrapperRootCommand.AddCommand(CommandLineConfigs.RemoveCommand);
BootstrapperRootCommand.AddCommand(HelpCommand);
HelpCommand.Handler = CommandHandler.Create(() =>
{
Console.WriteLine(_assemblyVersion.Value);
Console.WriteLine(LocalizableStrings.BootstrapperHelp);
});

BootstrapParser = new CommandLineBuilder(BootstrapperRootCommand)
Expand Down
21 changes: 21 additions & 0 deletions src/dotnet-bootstrapper/CommandBase.cs
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
{
public abstract class CommandBase
{
protected ParseResult _parseResult;

protected CommandBase(ParseResult parseResult)
{
_parseResult = parseResult;
}

public abstract int Execute();
}
}
56 changes: 56 additions & 0 deletions src/dotnet-bootstrapper/Commands/Install/InstallCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.CommandLine.Parsing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Deployment.DotNet.Releases;

namespace Microsoft.DotNet.Tools.Bootstrapper.Commands.Install;

internal class InstallCommand(
ParseResult parseResult) : CommandBase(parseResult)
{
private string _channel = parseResult.ValueForArgument(InstallCommandParser.ChannelArgument);

public override int Execute()
{
string path = Environment.CurrentDirectory;
ProductCollection productCollection = ProductCollection.GetAsync().Result;
Product product = productCollection
.FirstOrDefault(p => string.IsNullOrEmpty(_channel) || p.ProductVersion.Equals(_channel, StringComparison.OrdinalIgnoreCase));

if (product == null)
{
Console.WriteLine($"No product found for channel: {_channel}");
return 1;
}

ProductRelease latestRelease = product.GetReleasesAsync().Result
.Where(release => !release.IsPreview)
.OrderByDescending(release => release.ReleaseDate)
.FirstOrDefault();

if (latestRelease == null)
{
Console.WriteLine($"No releases found for product: {product.ProductName}");
return 1;
}

Console.WriteLine($"Installing {product.ProductName} {latestRelease.Version}...");

foreach (ReleaseComponent component in latestRelease.Components)
{
Console.WriteLine($"Installing {component.Name} {component.DisplayVersion}...");

ReleaseFile releaseFile = component.Files.FirstOrDefault(file => file.Rid.Equals(Environment.OSVersion.Platform.ToString(), StringComparison.OrdinalIgnoreCase));

releaseFile.DownloadAsync(Path.Combine(path, ".net", component.Name)).Wait();

Console.WriteLine($"Downloaded {component.Name} {component.DisplayVersion} to {Path.Combine(path, component.Name)}");
}

return 0;
}
}
35 changes: 35 additions & 0 deletions src/dotnet-bootstrapper/Commands/Install/InstallCommandParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.Parsing;

namespace Microsoft.DotNet.Tools.Bootstrapper.Commands.Install;

internal class InstallCommandParser
{
internal static Argument<string> ChannelArgument = new Argument<string>(
name: "channel",
description: "The channel to install sdks for. If not specified, It will take the latest.")
{
Arity = ArgumentArity.ZeroOrOne
};

private static readonly Command Command = ConstructCommand();

public static Command GetCommand() => Command;

private static Command ConstructCommand()
{
Command command = new("install", "Install SDKs available for installation.");
command.AddArgument(ChannelArgument);
command.Handler = CommandHandler.Create((ParseResult parseResult) =>
{
return new InstallCommand(parseResult).Execute();
});
return command;
}
}
60 changes: 60 additions & 0 deletions src/dotnet-bootstrapper/Commands/Search/SearchCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.CommandLine.Parsing;
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.AllowPreviews);
public override int Execute()
{
List<Product> productCollection = [.. ProductCollection.GetAsync().Result];
Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

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

productCollection = [..
productCollection.Where(product => !product.IsOutOfSupport() && (product.SupportPhase != SupportPhase.Preview || _allowPreviews))];
Copy link
Member

Choose a reason for hiding this comment

The 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))];
}

foreach (Product product in productCollection)
{
string productHeader = $"{product.ProductName} {product.ProductVersion}";
Console.WriteLine(productHeader);

Table productMetadataTable = new Table()
.AddColumn("Version")
.AddColumn("Release Date")
.AddColumn("SDK")
.AddColumn("Runtime")
.AddColumn("ASP.NET Runtime")
.AddColumn("Windows Desktop Runtime");

List<ProductRelease> releases = product.GetReleasesAsync().Result.ToList()
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.Where(relase => !relase.IsPreview || _allowPreviews).ToList();

foreach (ProductRelease release in releases)
{
productMetadataTable.AddRow(
release.Version.ToString(),
release.ReleaseDate.ToString("yyyy-MM-dd"),
release.Sdks.FirstOrDefault()?.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;
}
}
35 changes: 35 additions & 0 deletions src/dotnet-bootstrapper/Commands/Search/SearchCommandParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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> AllowPreviews = new Option<bool>(
"--allow-previews",
description: "Allow preview releases to be listed.");

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.");
Copy link
Member

Choose a reason for hiding this comment

The 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(AllowPreviews);

command.Handler = CommandHandler.Create((ParseResult parseResult) =>
{
return new SearchCommand(parseResult).Execute();
});

return command;
}
}
72 changes: 72 additions & 0 deletions src/dotnet-bootstrapper/LocalizableStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/dotnet-bootstrapper/LocalizableStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="BootstrapperHelp" xml:space="preserve">
<value>Bootstrapper help text</value>
</data>
</root>
4 changes: 4 additions & 0 deletions src/dotnet-bootstrapper/dotnet-bootstrapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Setup.Configuration.Interop" Version="$(MicrosoftVisualStudioSetupConfigurationPackageVersion)" />
<PackageReference Include="NuGet.Versioning" Version="6.9.1" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20574.7" />
<PackageReference Include="System.CommandLine.Rendering" Version="0.3.0-alpha.20574.7" />
<PackageReference Include="System.Resources.Extensions" Version="8.0.0" />
<ProjectReference Include="..\dotnet-core-uninstall\dotnet-core-uninstall.csproj" />
Copy link
Member

@nagilson nagilson Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Why does this take a dependency on the uninstall tool? Is that for the 'uninstall' command? I would delegate that to another PR, although that command does seem to already exist.

<PackageReference Include="Microsoft.Deployment.DotNet.Releases" Version="1.0.1" />
</ItemGroup>

<ItemGroup>
Expand All @@ -39,6 +42,7 @@
<EmbeddedResource Update="LocalizableStrings.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>LocalizableStrings.Designer.cs</LastGenOutput>
<CustomToolNamespace>Microsoft.DotNet.Tools.Bootstrapper</CustomToolNamespace>
</EmbeddedResource>
</ItemGroup>

Expand Down
8 changes: 7 additions & 1 deletion src/dotnet-bootstrapper/xlf/LocalizableStrings.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/dotnet-bootstrapper/xlf/LocalizableStrings.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading