2017-12-04 14:13:24 -08:00
|
|
|
// 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 Microsoft.DotNet.Cli;
|
|
|
|
using Microsoft.DotNet.Cli.CommandLine;
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
|
|
|
using Microsoft.DotNet.Configurer;
|
|
|
|
using Microsoft.DotNet.ShellShim;
|
|
|
|
using Microsoft.DotNet.ToolPackage;
|
|
|
|
using Microsoft.Extensions.EnvironmentAbstractions;
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Install.Tool
|
|
|
|
{
|
|
|
|
public class InstallToolCommand : CommandBase
|
|
|
|
{
|
|
|
|
private static string _packageId;
|
|
|
|
private static string _packageVersion;
|
|
|
|
private static string _configFilePath;
|
|
|
|
private static string _framework;
|
2018-01-11 13:54:02 -08:00
|
|
|
private static string _source;
|
2018-01-19 17:15:34 -08:00
|
|
|
private static bool _global;
|
2017-12-04 14:13:24 -08:00
|
|
|
|
|
|
|
public InstallToolCommand(
|
|
|
|
AppliedOption appliedCommand,
|
|
|
|
ParseResult parseResult)
|
|
|
|
: base(parseResult)
|
|
|
|
{
|
|
|
|
if (appliedCommand == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(appliedCommand));
|
|
|
|
}
|
|
|
|
|
|
|
|
_packageId = appliedCommand.Arguments.Single();
|
|
|
|
_packageVersion = appliedCommand.ValueOrDefault<string>("version");
|
|
|
|
_configFilePath = appliedCommand.ValueOrDefault<string>("configfile");
|
|
|
|
_framework = appliedCommand.ValueOrDefault<string>("framework");
|
2018-01-11 13:54:02 -08:00
|
|
|
_source = appliedCommand.ValueOrDefault<string>("source");
|
2018-01-19 17:15:34 -08:00
|
|
|
_global = appliedCommand.ValueOrDefault<bool>("global");
|
2017-12-04 14:13:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override int Execute()
|
|
|
|
{
|
2018-01-19 17:15:34 -08:00
|
|
|
if (!_global)
|
|
|
|
{
|
|
|
|
throw new GracefulException(LocalizableStrings.InstallToolCommandOnlySupportGlobal);
|
|
|
|
}
|
2017-12-04 14:13:24 -08:00
|
|
|
|
2018-01-19 17:15:34 -08:00
|
|
|
var cliFolderPathCalculator = new CliFolderPathCalculator();
|
|
|
|
var executablePackagePath = new DirectoryPath(cliFolderPathCalculator.ExecutablePackagesPath);
|
|
|
|
var offlineFeedPath = new DirectoryPath(cliFolderPathCalculator.CliFallbackFolderPath);
|
2017-12-04 14:13:24 -08:00
|
|
|
|
2018-01-19 17:15:34 -08:00
|
|
|
var toolConfigurationAndExecutablePath = ObtainPackage(executablePackagePath, offlineFeedPath);
|
2017-12-04 14:13:24 -08:00
|
|
|
|
|
|
|
var shellShimMaker = new ShellShimMaker(executablePackagePath.Value);
|
2018-01-19 17:15:34 -08:00
|
|
|
var commandName = toolConfigurationAndExecutablePath.Configuration.CommandName;
|
2017-12-04 14:13:24 -08:00
|
|
|
shellShimMaker.EnsureCommandNameUniqueness(commandName);
|
|
|
|
|
|
|
|
shellShimMaker.CreateShim(
|
2018-01-19 17:15:34 -08:00
|
|
|
toolConfigurationAndExecutablePath.Executable.Value,
|
2017-12-04 14:13:24 -08:00
|
|
|
commandName);
|
|
|
|
|
|
|
|
EnvironmentPathFactory
|
|
|
|
.CreateEnvironmentPathInstruction()
|
|
|
|
.PrintAddPathInstructionIfPathDoesNotExist();
|
|
|
|
|
|
|
|
Reporter.Output.WriteLine(
|
2018-01-13 09:40:48 -08:00
|
|
|
string.Format(LocalizableStrings.InstallationSucceeded, commandName));
|
2017-12-04 14:13:24 -08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-19 17:15:34 -08:00
|
|
|
private static ToolConfigurationAndExecutablePath ObtainPackage(
|
|
|
|
DirectoryPath executablePackagePath,
|
|
|
|
DirectoryPath offlineFeedPath)
|
2017-12-04 14:13:24 -08:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2018-01-11 13:54:02 -08:00
|
|
|
FilePath? configFile = null;
|
|
|
|
if (_configFilePath != null)
|
|
|
|
{
|
|
|
|
configFile = new FilePath(_configFilePath);
|
|
|
|
}
|
|
|
|
|
2017-12-04 14:13:24 -08:00
|
|
|
var toolPackageObtainer =
|
|
|
|
new ToolPackageObtainer(
|
|
|
|
executablePackagePath,
|
2018-01-19 17:15:34 -08:00
|
|
|
offlineFeedPath,
|
2017-12-04 14:13:24 -08:00
|
|
|
() => new DirectoryPath(Path.GetTempPath())
|
|
|
|
.WithSubDirectories(Path.GetRandomFileName())
|
|
|
|
.WithFile(Path.GetRandomFileName() + ".csproj"),
|
|
|
|
new Lazy<string>(BundledTargetFramework.GetTargetFrameworkMoniker),
|
|
|
|
new PackageToProjectFileAdder(),
|
|
|
|
new ProjectRestorer());
|
|
|
|
|
|
|
|
return toolPackageObtainer.ObtainAndReturnExecutablePath(
|
2018-01-11 13:54:02 -08:00
|
|
|
packageId: _packageId,
|
|
|
|
packageVersion: _packageVersion,
|
2017-12-04 14:13:24 -08:00
|
|
|
nugetconfig: configFile,
|
2018-01-17 19:28:29 -08:00
|
|
|
targetframework: _framework,
|
|
|
|
source: _source);
|
2017-12-04 14:13:24 -08:00
|
|
|
}
|
2018-01-19 17:15:34 -08:00
|
|
|
|
2017-12-04 14:13:24 -08:00
|
|
|
catch (PackageObtainException ex)
|
|
|
|
{
|
|
|
|
throw new GracefulException(
|
|
|
|
message:
|
2018-01-13 09:40:48 -08:00
|
|
|
string.Format(LocalizableStrings.InstallFailedNuget,
|
|
|
|
ex.Message),
|
2017-12-04 14:13:24 -08:00
|
|
|
innerException: ex);
|
|
|
|
}
|
|
|
|
catch (ToolConfigurationException ex)
|
|
|
|
{
|
|
|
|
throw new GracefulException(
|
|
|
|
message:
|
2018-01-13 09:40:48 -08:00
|
|
|
string.Format(
|
|
|
|
LocalizableStrings.InstallFailedPackage,
|
|
|
|
ex.Message),
|
2017-12-04 14:13:24 -08:00
|
|
|
innerException: ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|