2017-06-01 21:25:06 -07: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.
|
|
|
|
|
2017-10-24 15:50:43 -07:00
|
|
|
using System;
|
2017-06-01 21:25:06 -07:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using Microsoft.DotNet.Tools.MSBuild;
|
|
|
|
using Microsoft.DotNet.Tools.Restore;
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools
|
|
|
|
{
|
|
|
|
public class RestoringCommand : MSBuildForwardingApp
|
|
|
|
{
|
2017-10-24 15:50:43 -07:00
|
|
|
public RestoreCommand SeparateRestoreCommand { get; }
|
2017-06-01 21:25:06 -07:00
|
|
|
|
2017-10-24 15:50:43 -07:00
|
|
|
public RestoringCommand(
|
|
|
|
IEnumerable<string> msbuildArgs,
|
|
|
|
IEnumerable<string> parsedArguments,
|
|
|
|
IEnumerable<string> trailingArguments,
|
|
|
|
bool noRestore,
|
|
|
|
string msbuildPath = null)
|
|
|
|
: base(GetCommandArguments(msbuildArgs, parsedArguments, noRestore), msbuildPath)
|
|
|
|
{
|
|
|
|
SeparateRestoreCommand = GetSeparateRestoreCommand(parsedArguments, trailingArguments, noRestore, msbuildPath);
|
|
|
|
}
|
2017-06-01 21:25:06 -07:00
|
|
|
|
2017-10-24 15:50:43 -07:00
|
|
|
private static IEnumerable<string> GetCommandArguments(
|
|
|
|
IEnumerable<string> msbuildArgs,
|
|
|
|
IEnumerable<string> parsedArguments,
|
|
|
|
bool noRestore)
|
2017-06-02 23:32:53 -07:00
|
|
|
{
|
2017-10-24 15:50:43 -07:00
|
|
|
if (noRestore)
|
|
|
|
{
|
|
|
|
return msbuildArgs;
|
|
|
|
}
|
2017-06-02 23:32:53 -07:00
|
|
|
|
2017-10-24 15:50:43 -07:00
|
|
|
if (HasArgumentToExcludeFromRestore(parsedArguments))
|
2017-06-07 14:43:48 -07:00
|
|
|
{
|
2017-10-24 15:50:43 -07:00
|
|
|
return Prepend("/nologo", msbuildArgs);
|
2017-06-02 23:32:53 -07:00
|
|
|
}
|
2017-06-07 14:43:48 -07:00
|
|
|
|
2017-10-24 15:50:43 -07:00
|
|
|
return Prepend("/restore", msbuildArgs);
|
2017-06-02 23:32:53 -07:00
|
|
|
}
|
|
|
|
|
2017-10-24 15:50:43 -07:00
|
|
|
private static RestoreCommand GetSeparateRestoreCommand(
|
2017-06-13 00:04:25 -07:00
|
|
|
IEnumerable<string> parsedArguments,
|
2017-10-24 15:50:43 -07:00
|
|
|
IEnumerable<string> trailingArguments,
|
2017-06-13 00:04:25 -07:00
|
|
|
bool noRestore,
|
2017-10-24 15:50:43 -07:00
|
|
|
string msbuildPath)
|
2017-06-01 21:25:06 -07:00
|
|
|
{
|
2017-10-24 15:50:43 -07:00
|
|
|
if (noRestore || !HasArgumentToExcludeFromRestore(parsedArguments))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var restoreArguments = parsedArguments
|
|
|
|
.Where(a => !IsExcludedFromRestore(a))
|
|
|
|
.Concat(trailingArguments);
|
|
|
|
|
|
|
|
return RestoreCommand.FromArgs(
|
|
|
|
restoreArguments.ToArray(),
|
|
|
|
msbuildPath,
|
|
|
|
noLogo: false);
|
2017-06-01 21:25:06 -07:00
|
|
|
}
|
|
|
|
|
2017-10-24 15:50:43 -07:00
|
|
|
private static IEnumerable<string> Prepend(string argument, IEnumerable<string> arguments)
|
|
|
|
=> new[] { argument }.Concat(arguments);
|
|
|
|
|
|
|
|
private static bool HasArgumentToExcludeFromRestore(IEnumerable<string> arguments)
|
|
|
|
=> arguments.Any(a => IsExcludedFromRestore(a));
|
|
|
|
|
|
|
|
private static bool IsExcludedFromRestore(string argument)
|
|
|
|
=> argument.StartsWith("/p:TargetFramework=", StringComparison.Ordinal);
|
|
|
|
|
2017-06-01 21:25:06 -07:00
|
|
|
public override int Execute()
|
|
|
|
{
|
2017-10-24 15:50:43 -07:00
|
|
|
if (SeparateRestoreCommand != null)
|
2017-06-01 21:25:06 -07:00
|
|
|
{
|
2017-10-24 15:50:43 -07:00
|
|
|
int exitCode = SeparateRestoreCommand.Execute();
|
2017-06-01 21:25:06 -07:00
|
|
|
if (exitCode != 0)
|
|
|
|
{
|
|
|
|
return exitCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return base.Execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|