2017-11-21 20:10:06 -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.Collections.Generic;
|
2017-12-04 14:13:24 -08:00
|
|
|
using Microsoft.DotNet.Cli;
|
2018-01-11 13:54:02 -08:00
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
2018-01-13 09:40:48 -08:00
|
|
|
using Microsoft.DotNet.Tools;
|
2017-12-04 14:13:24 -08:00
|
|
|
using Microsoft.DotNet.ToolPackage;
|
2017-11-21 20:10:06 -08:00
|
|
|
using Microsoft.DotNet.PlatformAbstractions;
|
|
|
|
using Microsoft.Extensions.EnvironmentAbstractions;
|
|
|
|
|
2018-03-21 19:12:32 -07:00
|
|
|
namespace Microsoft.DotNet.Tools.Tool.Install
|
2017-11-21 20:10:06 -08:00
|
|
|
{
|
|
|
|
internal class ProjectRestorer : IProjectRestorer
|
|
|
|
{
|
2018-02-26 11:46:01 -08:00
|
|
|
private const string AnyRid = "any";
|
2018-01-28 13:35:04 -08:00
|
|
|
private readonly IReporter _reporter;
|
2018-03-06 16:15:27 -08:00
|
|
|
private readonly IReporter _errorReporter;
|
|
|
|
private readonly bool _forceOutputRedirection;
|
2018-01-17 19:26:52 -08:00
|
|
|
|
2018-01-28 13:35:04 -08:00
|
|
|
public ProjectRestorer(IReporter reporter = null)
|
2018-01-17 19:26:52 -08:00
|
|
|
{
|
2018-03-06 16:15:27 -08:00
|
|
|
_reporter = reporter ?? Reporter.Output;
|
|
|
|
_errorReporter = reporter ?? Reporter.Error;
|
|
|
|
_forceOutputRedirection = reporter != null;
|
2018-01-17 19:26:52 -08:00
|
|
|
}
|
|
|
|
|
2018-03-19 22:22:45 -07:00
|
|
|
public void Restore(FilePath project,
|
2018-01-28 13:35:04 -08:00
|
|
|
FilePath? nugetConfig = null,
|
2018-01-17 19:26:52 -08:00
|
|
|
string verbosity = null)
|
2017-11-21 20:10:06 -08:00
|
|
|
{
|
|
|
|
var argsToPassToRestore = new List<string>();
|
|
|
|
|
2018-01-28 13:35:04 -08:00
|
|
|
argsToPassToRestore.Add(project.Value);
|
|
|
|
if (nugetConfig != null)
|
2017-11-21 20:10:06 -08:00
|
|
|
{
|
|
|
|
argsToPassToRestore.Add("--configfile");
|
2018-01-28 13:35:04 -08:00
|
|
|
argsToPassToRestore.Add(nugetConfig.Value.Value);
|
2017-11-21 20:10:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
argsToPassToRestore.AddRange(new List<string>
|
|
|
|
{
|
|
|
|
"--runtime",
|
2018-04-24 10:19:27 -07:00
|
|
|
AnyRid
|
2017-11-21 20:10:06 -08:00
|
|
|
});
|
|
|
|
|
2018-04-02 14:44:43 -07:00
|
|
|
argsToPassToRestore.Add($"-verbosity:{verbosity ?? "quiet"}");
|
2018-01-17 19:26:52 -08:00
|
|
|
|
2017-11-21 20:10:06 -08:00
|
|
|
var command = new DotNetCommandFactory(alwaysRunOutOfProc: true)
|
2018-01-17 19:26:52 -08:00
|
|
|
.Create("restore", argsToPassToRestore);
|
|
|
|
|
2018-03-06 16:15:27 -08:00
|
|
|
if (verbosity == null || _forceOutputRedirection)
|
2018-01-17 19:26:52 -08:00
|
|
|
{
|
|
|
|
command = command
|
2018-03-06 16:15:27 -08:00
|
|
|
.OnOutputLine(line => WriteLine(_reporter, line, project))
|
|
|
|
.OnErrorLine(line => WriteLine(_errorReporter, line, project));
|
2018-01-17 19:26:52 -08:00
|
|
|
}
|
2017-11-21 20:10:06 -08:00
|
|
|
|
|
|
|
var result = command.Execute();
|
|
|
|
if (result.ExitCode != 0)
|
|
|
|
{
|
2018-01-28 13:35:04 -08:00
|
|
|
throw new ToolPackageException(LocalizableStrings.ToolInstallationRestoreFailed);
|
2017-11-21 20:10:06 -08:00
|
|
|
}
|
|
|
|
}
|
2018-03-06 16:15:27 -08:00
|
|
|
|
|
|
|
private static void WriteLine(IReporter reporter, string line, FilePath project)
|
|
|
|
{
|
|
|
|
line = line ?? "";
|
|
|
|
|
|
|
|
// Remove the temp project prefix if present
|
|
|
|
if (line.StartsWith($"{project.Value} : ", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
line = line.Substring(project.Value.Length + 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: MSBuild intentionally does not localize "warning" and "error" for diagnostic messages
|
|
|
|
if (line.StartsWith("warning ", StringComparison.Ordinal))
|
|
|
|
{
|
|
|
|
line = line.Yellow();
|
|
|
|
}
|
|
|
|
else if (line.StartsWith("error ", StringComparison.Ordinal))
|
|
|
|
{
|
|
|
|
line = line.Red();
|
|
|
|
}
|
|
|
|
|
|
|
|
reporter.WriteLine(line);
|
|
|
|
}
|
2017-11-21 20:10:06 -08:00
|
|
|
}
|
|
|
|
}
|