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

Commit dd76fec

Browse files
committed
Implicit restore for build, pack, publish, run and test.
1 parent a3d6c2a commit dd76fec

File tree

13 files changed

+91
-21
lines changed

13 files changed

+91
-21
lines changed

src/dotnet/CommonOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,11 @@ public static Option VersionSuffixOption() =>
6565

6666
public static ArgumentsRule DefaultToCurrentDirectory(this ArgumentsRule rule) =>
6767
rule.With(defaultValue: () => PathUtility.EnsureTrailingSlash(Directory.GetCurrentDirectory()));
68+
69+
public static Option NoRestoreOption() =>
70+
Create.Option(
71+
"--no-restore",
72+
CommonLocalizableStrings.NoRestoreDescription,
73+
Accept.NoArguments());
6874
}
6975
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using Microsoft.DotNet.Tools.MSBuild;
7+
using Microsoft.DotNet.Tools.Restore;
8+
9+
namespace Microsoft.DotNet.Tools
10+
{
11+
public class RestoringCommand : MSBuildForwardingApp
12+
{
13+
private bool NoRestore { get; }
14+
15+
private IEnumerable<string> ArgsToForward { get; }
16+
17+
public RestoringCommand(IEnumerable<string> msbuildArgs, bool noRestore, string msbuildPath = null)
18+
: base(msbuildArgs, msbuildPath)
19+
{
20+
NoRestore = noRestore;
21+
ArgsToForward = msbuildArgs;
22+
}
23+
24+
public override int Execute()
25+
{
26+
if (ShouldRunImplicitRestore)
27+
{
28+
int exitCode = RestoreCommand.Run(ArgsToForward.ToArray());
29+
if (exitCode != 0)
30+
{
31+
return exitCode;
32+
}
33+
}
34+
35+
return base.Execute();
36+
}
37+
38+
private bool ShouldRunImplicitRestore => !NoRestore;
39+
}
40+
}

src/dotnet/commands/dotnet-build/BuildCommand.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.Collections.Generic;
5+
using System.Linq;
56
using Microsoft.DotNet.Cli.CommandLine;
67
using Microsoft.DotNet.Cli.Utils;
78
using Microsoft.DotNet.Tools.MSBuild;
9+
using Microsoft.DotNet.Tools;
810
using Microsoft.DotNet.Cli;
11+
using Microsoft.DotNet.Tools.Restore;
912
using Parser = Microsoft.DotNet.Cli.Parser;
1013

1114
namespace Microsoft.DotNet.Tools.Build
1215
{
13-
public class BuildCommand : MSBuildForwardingApp
16+
public class BuildCommand : RestoringCommand
1417
{
15-
public BuildCommand(IEnumerable<string> msbuildArgs, string msbuildPath = null)
16-
: base(msbuildArgs, msbuildPath)
18+
public BuildCommand(IEnumerable<string> msbuildArgs, bool noRestore, string msbuildPath = null)
19+
: base(msbuildArgs, noRestore, msbuildPath)
1720
{
1821
}
1922

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

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

47-
return new BuildCommand(msbuildArgs, msbuildPath);
50+
bool noRestore = appliedBuildOptions.HasOption("--no-restore");
51+
52+
return new BuildCommand(msbuildArgs, noRestore, msbuildPath);
4853
}
4954

5055
public static int Run(string[] args)

src/dotnet/commands/dotnet-build/BuildCommandParser.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public static Command Build() =>
3737
LocalizableStrings.NoDependenciesOptionDescription,
3838
Accept.NoArguments()
3939
.ForwardAs("/p:BuildProjectReferences=false")),
40+
CommonOptions.NoRestoreOption(),
4041
CommonOptions.VerbosityOption());
4142
}
4243
}

src/dotnet/commands/dotnet-msbuild/MSBuildForwardingApp.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public ProcessStartInfo GetProcessStartInfo()
5858
return ret;
5959
}
6060

61-
public int Execute()
61+
public virtual int Execute()
6262
{
6363
return GetProcessStartInfo().Execute();
6464
}

src/dotnet/commands/dotnet-pack/PackCommand.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
using Microsoft.DotNet.Cli.CommandLine;
66
using Microsoft.DotNet.Cli.Utils;
77
using Microsoft.DotNet.Tools.MSBuild;
8+
using Microsoft.DotNet.Tools;
89
using Microsoft.DotNet.Cli;
910
using System.Diagnostics;
1011
using Parser = Microsoft.DotNet.Cli.Parser;
1112

1213
namespace Microsoft.DotNet.Tools.Pack
1314
{
14-
public class PackCommand : MSBuildForwardingApp
15+
public class PackCommand : RestoringCommand
1516
{
16-
public PackCommand(IEnumerable<string> msbuildArgs, string msbuildPath = null)
17-
: base(msbuildArgs, msbuildPath)
17+
public PackCommand(IEnumerable<string> msbuildArgs, bool noRestore, string msbuildPath = null)
18+
: base(msbuildArgs, noRestore, msbuildPath)
1819
{
1920
}
2021

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

3132
var msbuildArgs = new List<string>()
3233
{
33-
"/t:pack"
34+
"/t:pack"
3435
};
3536

3637
msbuildArgs.AddRange(parsedPack.OptionValuesToBeForwarded());
3738

3839
msbuildArgs.AddRange(parsedPack.Arguments);
3940

40-
return new PackCommand(msbuildArgs, msbuildPath);
41+
bool noRestore = parsedPack.HasOption("--no-restore");
42+
43+
return new PackCommand(msbuildArgs, noRestore, msbuildPath);
4144
}
4245

4346
public static int Run(string[] args)

src/dotnet/commands/dotnet-pack/PackCommandParser.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public static Command Pack() =>
3939
"-s|--serviceable",
4040
LocalizableStrings.CmdServiceableDescription,
4141
Accept.NoArguments().ForwardAs("/p:Serviceable=true")),
42+
CommonOptions.NoRestoreOption(),
4243
CommonOptions.VerbosityOption());
4344
}
4445
}

src/dotnet/commands/dotnet-publish/Program.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
using Microsoft.DotNet.Cli;
66
using Microsoft.DotNet.Cli.CommandLine;
77
using Microsoft.DotNet.Cli.Utils;
8+
using Microsoft.DotNet.Tools;
89
using Microsoft.DotNet.Tools.MSBuild;
910
using Parser = Microsoft.DotNet.Cli.Parser;
1011

1112
namespace Microsoft.DotNet.Tools.Publish
1213
{
13-
public class PublishCommand : MSBuildForwardingApp
14+
public class PublishCommand : RestoringCommand
1415
{
15-
private PublishCommand(IEnumerable<string> msbuildArgs, string msbuildPath = null)
16-
: base(msbuildArgs, msbuildPath)
16+
private PublishCommand(IEnumerable<string> msbuildArgs, bool noRestore, string msbuildPath = null)
17+
: base(msbuildArgs, noRestore, msbuildPath)
1718
{
1819
}
1920

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

3839
msbuildArgs.AddRange(appliedPublishOption.Arguments);
3940

40-
return new PublishCommand(msbuildArgs, msbuildPath);
41+
bool noRestore = appliedPublishOption.HasOption("--no-restore");
42+
43+
return new PublishCommand(msbuildArgs, noRestore, msbuildPath);
4144
}
4245

4346
public static int Run(string[] args)

src/dotnet/commands/dotnet-publish/PublishCommandParser.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public static Command Publish() =>
4141
string value = o.Arguments.Any() ? o.Arguments.Single() : "true";
4242
return $"/p:SelfContained={value}";
4343
})),
44+
CommonOptions.NoRestoreOption(),
4445
CommonOptions.VerbosityOption());
4546
}
4647
}

src/dotnet/commands/dotnet-run/RunCommand.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using Microsoft.Build.Evaluation;
99
using Microsoft.DotNet.Cli.Utils;
10+
using Microsoft.DotNet.Tools;
1011
using Microsoft.DotNet.Tools.MSBuild;
1112
using Microsoft.DotNet.Tools.Run.LaunchSettings;
1213

@@ -19,6 +20,7 @@ public partial class RunCommand
1920
public bool NoBuild { get; private set; }
2021
public string Project { get; private set; }
2122
public IReadOnlyCollection<string> Args { get; private set; }
23+
public bool NoRestore { get; private set; }
2224

2325
private List<string> _args;
2426
private bool ShouldBuild => !NoBuild;
@@ -55,6 +57,7 @@ public RunCommand(string configuration,
5557
string project,
5658
string launchProfile,
5759
bool noLaunchProfile,
60+
bool noRestore,
5861
IReadOnlyCollection<string> args)
5962
{
6063
Configuration = configuration;
@@ -64,6 +67,7 @@ public RunCommand(string configuration,
6467
LaunchProfile = launchProfile;
6568
NoLaunchProfile = noLaunchProfile;
6669
Args = args;
70+
NoRestore = noRestore;
6771
}
6872

6973
public RunCommand MakeNewWithReplaced(string configuration = null,
@@ -72,6 +76,7 @@ public RunCommand MakeNewWithReplaced(string configuration = null,
7276
string project = null,
7377
string launchProfile = null,
7478
bool? noLaunchProfile = null,
79+
bool? noRestore = null,
7580
IReadOnlyCollection<string> args = null)
7681
{
7782
return new RunCommand(
@@ -81,6 +86,7 @@ public RunCommand MakeNewWithReplaced(string configuration = null,
8186
project ?? this.Project,
8287
launchProfile ?? this.LaunchProfile,
8388
noLaunchProfile ?? this.NoLaunchProfile,
89+
noRestore ?? this.NoRestore,
8490
args ?? this.Args
8591
);
8692
}
@@ -142,8 +148,7 @@ private void EnsureProjectIsBuilt()
142148
buildArgs.Add($"/p:TargetFramework={Framework}");
143149
}
144150

145-
var buildResult = new MSBuildForwardingApp(buildArgs).Execute();
146-
151+
var buildResult = new RestoringCommand(buildArgs, NoRestore).Execute();
147152
if (buildResult != 0)
148153
{
149154
Reporter.Error.WriteLine();

0 commit comments

Comments
 (0)