Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Add Implicit restore to run/build/publish/pack/test #6762

Merged
merged 3 commits into from
Jun 8, 2017
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
3 changes: 3 additions & 0 deletions src/dotnet/CommonLocalizableStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -520,4 +520,7 @@
<data name="ShowHelpDescription" xml:space="preserve">
<value>Show help information.</value>
</data>
<data name="NoRestoreDescription" xml:space="preserve">
<value>Does not do an implicit restore when executing the command.</value>
</data>
</root>
6 changes: 6 additions & 0 deletions src/dotnet/CommonOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,11 @@ public static Option VersionSuffixOption() =>

public static ArgumentsRule DefaultToCurrentDirectory(this ArgumentsRule rule) =>
rule.With(defaultValue: () => PathUtility.EnsureTrailingSlash(Directory.GetCurrentDirectory()));

public static Option NoRestoreOption() =>
Create.Option(
"--no-restore",
CommonLocalizableStrings.NoRestoreDescription,
Accept.NoArguments());
}
}
39 changes: 39 additions & 0 deletions src/dotnet/commands/CommandWithRestoreOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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.Collections.Generic;
using System.Linq;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Tools.MSBuild;
using Microsoft.DotNet.Tools.Restore;

namespace Microsoft.DotNet.Tools
{
public static class CreateWithRestoreOptions
{
public static Command Command(
string name,
string help,
ArgumentsRule arguments,
params Option[] options)
{
return Create.Command(name, help, arguments, RestoreCommandParser.AddImplicitRestoreOptions(options));
}

public static Command Command(
string name,
string help,
ArgumentsRule arguments,
bool treatUnmatchedTokensAsErrors,
params Option[] options)
{
return Create.Command(
name,
help,
arguments,
treatUnmatchedTokensAsErrors,
RestoreCommandParser.AddImplicitRestoreOptions(options));
}
}
}
56 changes: 56 additions & 0 deletions src/dotnet/commands/RestoringCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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.Collections.Generic;
using System.Linq;
using Microsoft.DotNet.Tools.MSBuild;
using Microsoft.DotNet.Tools.Restore;

namespace Microsoft.DotNet.Tools
{
public class RestoringCommand : MSBuildForwardingApp
{
private bool NoRestore { get; }

private IEnumerable<string> ArgsToForward { get; }

private IEnumerable<string> ArgsToForwardToRestore()
{
var restoreArguments = ArgsToForward.Where(a =>
!a.StartsWith("/t:") &&
!a.StartsWith("/target:") &&
!a.StartsWith("/ConsoleLoggerParameters:") &&
!a.StartsWith("/clp:"));

if (!restoreArguments.Any(a => a.StartsWith("/v:") || a.StartsWith("/verbosity:")))
{
restoreArguments = restoreArguments.Concat(new string[] { "/v:q" });
}

return restoreArguments;
}

private bool ShouldRunImplicitRestore => !NoRestore;

public RestoringCommand(IEnumerable<string> msbuildArgs, bool noRestore, string msbuildPath = null)
: base(msbuildArgs, msbuildPath)
{
NoRestore = noRestore;
ArgsToForward = msbuildArgs;
}

public override int Execute()
{
if (ShouldRunImplicitRestore)
{
int exitCode = RestoreCommand.Run(ArgsToForwardToRestore().ToArray());
if (exitCode != 0)
{
return exitCode;
}
}

return base.Execute();
}
}
}
13 changes: 9 additions & 4 deletions src/dotnet/commands/dotnet-build/BuildCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.Linq;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.MSBuild;
using Microsoft.DotNet.Tools;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Tools.Restore;
using Parser = Microsoft.DotNet.Cli.Parser;

namespace Microsoft.DotNet.Tools.Build
{
public class BuildCommand : MSBuildForwardingApp
public class BuildCommand : RestoringCommand
{
public BuildCommand(IEnumerable<string> msbuildArgs, string msbuildPath = null)
: base(msbuildArgs, msbuildPath)
public BuildCommand(IEnumerable<string> msbuildArgs, bool noRestore, string msbuildPath = null)
: base(msbuildArgs, noRestore, msbuildPath)
{
}

Expand Down Expand Up @@ -44,7 +47,9 @@ public static BuildCommand FromArgs(string[] args, string msbuildPath = null)

msbuildArgs.Add($"/clp:Summary");

return new BuildCommand(msbuildArgs, msbuildPath);
bool noRestore = appliedBuildOptions.HasOption("--no-restore");

return new BuildCommand(msbuildArgs, noRestore, msbuildPath);
}

public static int Run(string[] args)
Expand Down
4 changes: 3 additions & 1 deletion src/dotnet/commands/dotnet-build/BuildCommandParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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.Collections.Generic;
using System.Linq;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Tools;
Expand All @@ -11,7 +12,7 @@ namespace Microsoft.DotNet.Cli
internal static class BuildCommandParser
{
public static Command Build() =>
Create.Command(
CreateWithRestoreOptions.Command(
"build",
LocalizableStrings.AppFullName,
Accept.ZeroOrMoreArguments()
Expand All @@ -37,6 +38,7 @@ public static Command Build() =>
LocalizableStrings.NoDependenciesOptionDescription,
Accept.NoArguments()
.ForwardAs("/p:BuildProjectReferences=false")),
CommonOptions.NoRestoreOption(),
CommonOptions.VerbosityOption());
}
}
2 changes: 1 addition & 1 deletion src/dotnet/commands/dotnet-msbuild/MSBuildForwardingApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public ProcessStartInfo GetProcessStartInfo()
return ret;
}

public int Execute()
public virtual int Execute()
{
return GetProcessStartInfo().Execute();
}
Expand Down
13 changes: 8 additions & 5 deletions src/dotnet/commands/dotnet-pack/PackCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.MSBuild;
using Microsoft.DotNet.Tools;
using Microsoft.DotNet.Cli;
using System.Diagnostics;
using Parser = Microsoft.DotNet.Cli.Parser;

namespace Microsoft.DotNet.Tools.Pack
{
public class PackCommand : MSBuildForwardingApp
public class PackCommand : RestoringCommand
{
public PackCommand(IEnumerable<string> msbuildArgs, string msbuildPath = null)
: base(msbuildArgs, msbuildPath)
public PackCommand(IEnumerable<string> msbuildArgs, bool noRestore, string msbuildPath = null)
: base(msbuildArgs, noRestore, msbuildPath)
{
}

Expand All @@ -30,14 +31,16 @@ public static PackCommand FromArgs(string[] args, string msbuildPath = null)

var msbuildArgs = new List<string>()
{
"/t:pack"
"/t:pack"
};

msbuildArgs.AddRange(parsedPack.OptionValuesToBeForwarded());

msbuildArgs.AddRange(parsedPack.Arguments);

return new PackCommand(msbuildArgs, msbuildPath);
bool noRestore = parsedPack.HasOption("--no-restore");

return new PackCommand(msbuildArgs, noRestore, msbuildPath);
}

public static int Run(string[] args)
Expand Down
5 changes: 4 additions & 1 deletion src/dotnet/commands/dotnet-pack/PackCommandParser.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
// 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.Collections.Generic;
using System.Linq;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Tools;
using LocalizableStrings = Microsoft.DotNet.Tools.Pack.LocalizableStrings;

namespace Microsoft.DotNet.Cli
{
internal static class PackCommandParser
{
public static Command Pack() =>
Create.Command(
CreateWithRestoreOptions.Command(
"pack",
LocalizableStrings.AppFullName,
Accept.ZeroOrMoreArguments(),
Expand Down Expand Up @@ -39,6 +41,7 @@ public static Command Pack() =>
"-s|--serviceable",
LocalizableStrings.CmdServiceableDescription,
Accept.NoArguments().ForwardAs("/p:Serviceable=true")),
CommonOptions.NoRestoreOption(),
CommonOptions.VerbosityOption());
}
}
11 changes: 7 additions & 4 deletions src/dotnet/commands/dotnet-publish/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools;
using Microsoft.DotNet.Tools.MSBuild;
using Parser = Microsoft.DotNet.Cli.Parser;

namespace Microsoft.DotNet.Tools.Publish
{
public class PublishCommand : MSBuildForwardingApp
public class PublishCommand : RestoringCommand
{
private PublishCommand(IEnumerable<string> msbuildArgs, string msbuildPath = null)
: base(msbuildArgs, msbuildPath)
private PublishCommand(IEnumerable<string> msbuildArgs, bool noRestore, string msbuildPath = null)
: base(msbuildArgs, noRestore, msbuildPath)
{
}

Expand All @@ -37,7 +38,9 @@ public static PublishCommand FromArgs(string[] args, string msbuildPath = null)

msbuildArgs.AddRange(appliedPublishOption.Arguments);

return new PublishCommand(msbuildArgs, msbuildPath);
bool noRestore = appliedPublishOption.HasOption("--no-restore");

return new PublishCommand(msbuildArgs, noRestore, msbuildPath);
}

public static int Run(string[] args)
Expand Down
5 changes: 4 additions & 1 deletion src/dotnet/commands/dotnet-publish/PublishCommandParser.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
// 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.Collections.Generic;
using System.Linq;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Tools;
using LocalizableStrings = Microsoft.DotNet.Tools.Publish.LocalizableStrings;

namespace Microsoft.DotNet.Cli
{
internal static class PublishCommandParser
{
public static Command Publish() =>
Create.Command(
CreateWithRestoreOptions.Command(
"publish",
LocalizableStrings.AppDescription,
Accept.ZeroOrMoreArguments(),
Expand Down Expand Up @@ -41,6 +43,7 @@ public static Command Publish() =>
string value = o.Arguments.Any() ? o.Arguments.Single() : "true";
return $"/p:SelfContained={value}";
})),
CommonOptions.NoRestoreOption(),
CommonOptions.VerbosityOption());
}
}
11 changes: 9 additions & 2 deletions src/dotnet/commands/dotnet-restore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public static RestoreCommand FromArgs(string[] args, string msbuildPath = null)
"/t:Restore"
};

if (!parsedRestore.HasOption("verbosity"))
if (!HasVerbosityOption(parsedRestore))
{
msbuildArgs.Add("/ConsoleLoggerParameters:Verbosity=Minimal");
}

msbuildArgs.AddRange(parsedRestore.OptionValuesToBeForwarded());

msbuildArgs.AddRange(parsedRestore.Arguments);

return new RestoreCommand(msbuildArgs, msbuildPath);
}

Expand All @@ -65,5 +65,12 @@ public static int Run(string[] args)

return cmd.Execute();
}

private static bool HasVerbosityOption(AppliedOption parsedRestore)
{
return parsedRestore.HasOption("verbosity") ||
parsedRestore.Arguments.Any(a => a.Contains("/v:")) ||
parsedRestore.Arguments.Any(a => a.Contains("/verbosity:"));
}
}
}
Loading