Skip to content

Adding arch option to tool install command #19370

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

Merged
merged 3 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 23 additions & 0 deletions src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,29 @@ private IEnumerable<PackageSource> LoadNuGetSources(PackageSourceLocation packag
PackageSourceProvider packageSourceProvider = new PackageSourceProvider(settings);
defaultSources = packageSourceProvider.LoadPackageSources().Where(source => source.IsEnabled);


if (packageSourceLocation?.AdditionalSourceFeed?.Any() ?? false)
{
foreach (string source in packageSourceLocation?.AdditionalSourceFeed)
{
if (string.IsNullOrWhiteSpace(source))
{
continue;
}

PackageSource packageSource = new PackageSource(source);
if (packageSource.TrySourceAsUri == null)
{
_verboseLogger.LogWarning(string.Format(
LocalizableStrings.FailedToLoadNuGetSource,
source));
continue;
}

defaultSources = defaultSources.Append(packageSource);
}
}

if (!packageSourceLocation?.SourceFeedOverrides.Any() ?? true)
{
if (!defaultSources.Any())
Expand Down
16 changes: 10 additions & 6 deletions src/Cli/dotnet/NugetPackageDownloader/PackageSourceLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Extensions.EnvironmentAbstractions;

Expand All @@ -13,18 +12,23 @@ internal class PackageSourceLocation
public PackageSourceLocation(
FilePath? nugetConfig = null,
DirectoryPath? rootConfigDirectory = null,
string[] sourceFeedOverrides = null)
string[] sourceFeedOverrides = null,
string[] additionalSourceFeeds = null)
{
NugetConfig = nugetConfig;
RootConfigDirectory = rootConfigDirectory;
ExpandLocalFeedAndAssign(sourceFeedOverrides);
// Overrides other feeds
SourceFeedOverrides = ExpandLocalFeed(sourceFeedOverrides);
// Feeds to be using in addition to config
AdditionalSourceFeed = ExpandLocalFeed(additionalSourceFeeds);
}

public FilePath? NugetConfig { get; }
public DirectoryPath? RootConfigDirectory { get; }
public string[] SourceFeedOverrides { get; private set; }
public string[] AdditionalSourceFeed { get; private set; }

private void ExpandLocalFeedAndAssign(string[] sourceFeedOverrides)
private string[] ExpandLocalFeed(string[] sourceFeedOverrides)
{
if (sourceFeedOverrides != null)
{
Expand All @@ -42,11 +46,11 @@ private void ExpandLocalFeedAndAssign(string[] sourceFeedOverrides)
}
}

SourceFeedOverrides = localFeedsThatIsRooted;
return localFeedsThatIsRooted;
}
else
{
SourceFeedOverrides = Array.Empty<string>();
return Array.Empty<string>();
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/Cli/dotnet/ShellShim/AppHostShimMaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ internal class AppHostShellShimMaker : IAppHostShellShimMaker
private readonly IFilePermissionSetter _filePermissionSetter;
private const ushort WindowsGUISubsystem = 0x2;

public AppHostShellShimMaker(string appHostSourceDirectory = null, IFilePermissionSetter filePermissionSetter = null)
public AppHostShellShimMaker(string appHostSourceDirectory, IFilePermissionSetter filePermissionSetter = null)
{
_appHostSourceDirectory =
appHostSourceDirectory
?? Path.Combine(AppContext.BaseDirectory, "AppHostTemplate");
_appHostSourceDirectory = appHostSourceDirectory;

_filePermissionSetter =
filePermissionSetter
Expand Down
5 changes: 1 addition & 4 deletions src/Cli/dotnet/ShellShim/ShellShimRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools;
Expand All @@ -15,16 +14,14 @@ namespace Microsoft.DotNet.ShellShim
{
internal class ShellShimRepository : IShellShimRepository
{
private const string ApphostNameWithoutExtension = "apphost";

private readonly DirectoryPath _shimsDirectory;
private readonly IFileSystem _fileSystem;
private readonly IAppHostShellShimMaker _appHostShellShimMaker;
private readonly IFilePermissionSetter _filePermissionSetter;

public ShellShimRepository(
DirectoryPath shimsDirectory,
string appHostSourceDirectory = null,
string appHostSourceDirectory,
IFileSystem fileSystem = null,
IAppHostShellShimMaker appHostShellShimMaker = null,
IFilePermissionSetter filePermissionSetter = null)
Expand Down
4 changes: 2 additions & 2 deletions src/Cli/dotnet/ShellShim/ShellShimRepositoryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace Microsoft.DotNet.ShellShim
{
internal static class ShellShimRepositoryFactory
{
public static IShellShimRepository CreateShellShimRepository(DirectoryPath? nonGlobalLocation = null)
public static IShellShimRepository CreateShellShimRepository(string appHostSourceDirectory, DirectoryPath? nonGlobalLocation = null)
{
return new ShellShimRepository(nonGlobalLocation ?? GetShimLocation());
return new ShellShimRepository(nonGlobalLocation ?? GetShimLocation(), appHostSourceDirectory);
}

private static DirectoryPath GetShimLocation()
Expand Down
74 changes: 74 additions & 0 deletions src/Cli/dotnet/ShellShim/ShellShimTemplateFinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.NuGetPackageDownloader;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ToolPackage;
using Microsoft.Extensions.EnvironmentAbstractions;
using NuGet.Frameworks;
using LocalizableStrings = Microsoft.DotNet.Tools.Tool.Install.LocalizableStrings;

namespace Microsoft.DotNet.ShellShim
{
internal class ShellShimTemplateFinder
{
private readonly DirectoryPath _tempDir;
private readonly INuGetPackageDownloader _nugetPackageDownloader;
private readonly PackageSourceLocation _packageSourceLocation;

public ShellShimTemplateFinder(
INuGetPackageDownloader nugetPackageDownloader,
DirectoryPath tempDir,
PackageSourceLocation packageSourceLocation)
{
_tempDir = tempDir;
_nugetPackageDownloader = nugetPackageDownloader;
_packageSourceLocation = packageSourceLocation;
}

public async Task<string> ResolveAppHostSourceDirectoryAsync(string archOption, string targetFramework)
{
string rid;
var validRids = new string[] { "win-x64", "win-arm64", "osx-x64", "osx-arm64" };
if (string.IsNullOrEmpty(archOption))
{
if (!string.IsNullOrEmpty(targetFramework) && new NuGetFramework(targetFramework).Version < new Version("6.0")
&& (OperatingSystem.IsWindows() || OperatingSystem.IsMacOS()) && !RuntimeInformation.ProcessArchitecture.Equals(Architecture.X64))
{
rid = OperatingSystem.IsWindows() ? "win-x64" : "osx-x64";
}
else
{
// Use the default app host
return GetDefaultAppHostSourceDirectory();
}
}
else
{
rid = CommonOptions.ResolveRidShorthandOptionsToRuntimeIdentifier(null, archOption);
}

if (!validRids.Contains(rid))
{
throw new GracefulException(string.Format(LocalizableStrings.InvalidRuntimeIdentifier, rid, string.Join(" ", validRids)));
}

var packageId = new PackageId($"microsoft.netcore.app.host.{rid}");
var packagePath = await _nugetPackageDownloader.DownloadPackageAsync(packageId, packageSourceLocation: _packageSourceLocation);
var content = await _nugetPackageDownloader.ExtractPackageAsync(packagePath, _tempDir);

return Path.Combine(_tempDir.Value, "runtimes", rid, "native");
}

public static string GetDefaultAppHostSourceDirectory()
{
return Path.Combine(AppContext.BaseDirectory, "AppHostTemplate");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ namespace Microsoft.DotNet.Cli
{
internal static class ToolCommandRestorePassThroughOptions
{
public static Option DisableParallelOption = new ForwardedOption<bool>(
public static Option<bool> DisableParallelOption = new ForwardedOption<bool>(
"--disable-parallel",
LocalizableStrings.CmdDisableParallelOptionDescription)
.ForwardAs("--disable-parallel");

public static Option NoCacheOption = new ForwardedOption<bool>(
public static Option<bool> NoCacheOption = new ForwardedOption<bool>(
"--no-cache",
LocalizableStrings.CmdNoCacheOptionDescription)
.ForwardAs("--no-cache");

public static Option IgnoreFailedSourcesOption = new ForwardedOption<bool>(
public static Option<bool> IgnoreFailedSourcesOption = new ForwardedOption<bool>(
"--ignore-failed-sources",
LocalizableStrings.CmdIgnoreFailedSourcesOptionDescription)
.ForwardAs("--ignore-failed-sources");

public static Option InteractiveRestoreOption = new ForwardedOption<bool>(
public static Option<bool> InteractiveRestoreOption = new ForwardedOption<bool>(
"--interactive",
CommonLocalizableStrings.CommandInteractiveOptionDescription)
.ForwardAs(Constants.RestoreInteractiveOption);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,7 @@ For more reasons, including package naming enforcement, visit https://aka.ms/fai
<value>If you intended to install a global tool, add `--global` to the command.
If you would like to create a manifest, use `dotnet new tool-manifest`, usually in the repo root directory.</value>
</data>
</root>
<data name="InvalidRuntimeIdentifier" xml:space="preserve">
<value>The runtime identifier {0} is invalid. Valid runtime identifiers are: {1}.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.CommandLine;
using Microsoft.DotNet.Tools;
using Microsoft.DotNet.Tools.Tool.Common;
using LocalizableStrings = Microsoft.DotNet.Tools.Tool.Install.LocalizableStrings;

Expand Down Expand Up @@ -37,6 +38,9 @@ internal static class ToolInstallCommandParser

public static readonly Option<VerbosityOptions> VerbosityOption = CommonOptions.VerbosityOption();

// Don't use the common options version as we don't want this to be a forwarded option
public static readonly Option<string> ArchitectureOption = new Option<string>(new string[] { "--arch", "-a" }, CommonLocalizableStrings.ArchitectureOptionDescription);

public static Command GetCommand()
{
var command = new Command("install", LocalizableStrings.CommandDescription);
Expand All @@ -55,6 +59,7 @@ public static Command GetCommand()
command.AddOption(ToolCommandRestorePassThroughOptions.NoCacheOption);
command.AddOption(ToolCommandRestorePassThroughOptions.InteractiveRestoreOption);
command.AddOption(VerbosityOption);
command.AddOption(ArchitectureOption);

return command;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@
using System.CommandLine.Parsing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Transactions;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.NuGetPackageDownloader;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ShellShim;
using Microsoft.DotNet.ToolPackage;
using Microsoft.DotNet.Tools.Tool.Common;
using Microsoft.Extensions.EnvironmentAbstractions;
using NuGet.Common;
using NuGet.Frameworks;
using NuGet.Versioning;

namespace Microsoft.DotNet.Tools.Tool.Install
{
internal delegate IShellShimRepository CreateShellShimRepository(DirectoryPath? nonGlobalLocation = null);
internal delegate IShellShimRepository CreateShellShimRepository(string appHostSourceDirectory, DirectoryPath? nonGlobalLocation = null);
internal delegate (IToolPackageStore, IToolPackageStoreQuery, IToolPackageInstaller) CreateToolPackageStoresAndInstaller(
DirectoryPath? nonGlobalLocation = null,
IEnumerable<string> forwardRestoreArguments = null);
Expand All @@ -29,6 +34,7 @@ internal class ToolInstallGlobalOrToolPathCommand : CommandBase
private readonly IReporter _errorReporter;
private CreateShellShimRepository _createShellShimRepository;
private CreateToolPackageStoresAndInstaller _createToolPackageStoresAndInstaller;
private readonly ShellShimTemplateFinder _shellShimTemplateFinder;

private readonly PackageId _packageId;
private readonly string _packageVersion;
Expand All @@ -38,14 +44,16 @@ internal class ToolInstallGlobalOrToolPathCommand : CommandBase
private readonly bool _global;
private readonly string _verbosity;
private readonly string _toolPath;
private readonly string _architectureOption;
private IEnumerable<string> _forwardRestoreArguments;

public ToolInstallGlobalOrToolPathCommand(
ParseResult parseResult,
CreateToolPackageStoresAndInstaller createToolPackageStoreAndInstaller = null,
CreateShellShimRepository createShellShimRepository = null,
IEnvironmentPathInstruction environmentPathInstruction = null,
IReporter reporter = null)
IReporter reporter = null,
INuGetPackageDownloader nugetPackageDownloader = null)
: base(parseResult)
{
_packageId = new PackageId(parseResult.ValueForArgument<string>(ToolInstallCommandParser.PackageIdArgument));
Expand All @@ -56,13 +64,24 @@ public ToolInstallGlobalOrToolPathCommand(
_global = parseResult.ValueForOption<bool>(ToolAppliedOption.GlobalOptionAliases.First());
_verbosity = Enum.GetName(parseResult.ValueForOption<VerbosityOptions>(ToolInstallCommandParser.VerbosityOption));
_toolPath = parseResult.ValueForOption<string>(ToolAppliedOption.ToolPathOptionAlias);
_architectureOption = parseResult.ValueForOption<string>(ToolInstallCommandParser.ArchitectureOption);

_createToolPackageStoresAndInstaller = createToolPackageStoreAndInstaller ?? ToolPackageFactory.CreateToolPackageStoresAndInstaller;
_forwardRestoreArguments = parseResult.OptionValuesToBeForwarded(ToolInstallCommandParser.GetCommand());

_environmentPathInstruction = environmentPathInstruction
?? EnvironmentPathFactory.CreateEnvironmentPathInstruction();
_createShellShimRepository = createShellShimRepository ?? ShellShimRepositoryFactory.CreateShellShimRepository;
var tempDir = new DirectoryPath(Path.Combine(Path.GetTempPath(), "dotnet-tool-install"));
var configOption = parseResult.ValueForOption(ToolInstallCommandParser.ConfigOption);
var sourceOption = parseResult.ValueForOption(ToolInstallCommandParser.AddSourceOption);
var packageSourceLocation = new PackageSourceLocation(string.IsNullOrEmpty(configOption) ? null : new FilePath(configOption), additionalSourceFeeds: sourceOption);
var restoreAction = new RestoreActionConfig(DisableParallel: parseResult.ValueForOption(ToolCommandRestorePassThroughOptions.DisableParallelOption),
NoCache: parseResult.ValueForOption(ToolCommandRestorePassThroughOptions.NoCacheOption),
IgnoreFailedSources: parseResult.ValueForOption(ToolCommandRestorePassThroughOptions.IgnoreFailedSourcesOption),
Interactive: parseResult.ValueForOption(ToolCommandRestorePassThroughOptions.InteractiveRestoreOption));
nugetPackageDownloader ??= new NuGetPackageDownloader(tempDir, verboseLogger: new NullLogger(), restoreActionConfig: restoreAction);
_shellShimTemplateFinder = new ShellShimTemplateFinder(nugetPackageDownloader, tempDir, packageSourceLocation);

_reporter = (reporter ?? Reporter.Output);
_errorReporter = (reporter ?? Reporter.Error);
Expand Down Expand Up @@ -93,9 +112,11 @@ public override int Execute()
toolPath = new DirectoryPath(_toolPath);
}

string appHostSourceDirectory = _shellShimTemplateFinder.ResolveAppHostSourceDirectoryAsync(_architectureOption, _framework).Result;

(IToolPackageStore toolPackageStore, IToolPackageStoreQuery toolPackageStoreQuery, IToolPackageInstaller toolPackageInstaller) =
_createToolPackageStoresAndInstaller(toolPath, _forwardRestoreArguments);
IShellShimRepository shellShimRepository = _createShellShimRepository(toolPath);
IShellShimRepository shellShimRepository = _createShellShimRepository(appHostSourceDirectory, toolPath);

// Prevent installation if any version of the package is installed
if (toolPackageStoreQuery.EnumeratePackageVersions(_packageId).FirstOrDefault() != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ Tool '{1}' (version '{2}') was successfully installed.</source>
Nástroj {1} (verze {2}) byl úspěšně nainstalován.</target>
<note />
</trans-unit>
<trans-unit id="InvalidRuntimeIdentifier">
<source>The runtime identifier {0} is invalid. Valid runtime identifiers are: {1}.</source>
<target state="new">The runtime identifier {0} is invalid. Valid runtime identifiers are: {1}.</target>
<note />
</trans-unit>
<trans-unit id="LocalOptionDescription">
<source>Install the tool and add to the local tool manifest (default).</source>
<target state="translated">Nainstaluje nástroj a přidá ho do manifestu místního nástroje (výchozí).</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ Tool '{1}' (version '{2}') was successfully installed.</source>
Das Tool "{1}" (Version {2}) wurde erfolgreich installiert.</target>
<note />
</trans-unit>
<trans-unit id="InvalidRuntimeIdentifier">
<source>The runtime identifier {0} is invalid. Valid runtime identifiers are: {1}.</source>
<target state="new">The runtime identifier {0} is invalid. Valid runtime identifiers are: {1}.</target>
<note />
</trans-unit>
<trans-unit id="LocalOptionDescription">
<source>Install the tool and add to the local tool manifest (default).</source>
<target state="translated">Hiermit installieren Sie das Tool und fügen es dem lokalen Toolmanifest (Standard) hinzu.</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ Tool '{1}' (version '{2}') was successfully installed.</source>
La herramienta "{1}" (versión '{2}') se instaló correctamente.</target>
<note />
</trans-unit>
<trans-unit id="InvalidRuntimeIdentifier">
<source>The runtime identifier {0} is invalid. Valid runtime identifiers are: {1}.</source>
<target state="new">The runtime identifier {0} is invalid. Valid runtime identifiers are: {1}.</target>
<note />
</trans-unit>
<trans-unit id="LocalOptionDescription">
<source>Install the tool and add to the local tool manifest (default).</source>
<target state="translated">Instalar la herramienta y agregar al manifiesto de la herramienta local (predeterminado).</target>
Expand Down
Loading