Skip to content

Commit 90971f3

Browse files
authored
Implement #:package manipulation for file-based apps (#49635)
1 parent 331313d commit 90971f3

File tree

64 files changed

+2248
-309
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2248
-309
lines changed

documentation/general/dotnet-run-file.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ To opt out, use `#:property PublishAot=false` directive in your `.cs` file.
102102

103103
Command `dotnet clean file.cs` can be used to clean build artifacts of the file-based program.
104104

105+
Commands `dotnet package add PackageName --file app.cs` and `dotnet package remove PackageName --file app.cs`
106+
can be used to manipulate `#:package` directives in the C# files, similarly to what the commands do for project-based apps.
107+
105108
## Entry points
106109

107110
If a file is given to `dotnet run`, it has to be an *entry-point file*, otherwise an error is reported.
@@ -378,8 +381,7 @@ We could also add `dotnet compile` command that would be the equivalent of `dotn
378381
`dotnet clean` could be extended to support cleaning all file-based app outputs,
379382
e.g., `dotnet clean --all-file-based-apps`.
380383

381-
Adding references via `dotnet package add`/`dotnet reference add` could be supported for file-based programs as well,
382-
i.e., the command would add a `#:package`/`#:project` directive to the top of a `.cs` file.
384+
More NuGet commands (like `dotnet nuget why` or `dotnet package list`) could be supported for file-based programs as well.
383385

384386
### Explicit importing
385387

src/Cli/dotnet/CliStrings.resx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,18 @@
273273
<data name="ProjectArgumentName" xml:space="preserve">
274274
<value>PROJECT</value>
275275
</data>
276+
<data name="ProjectOrFileArgumentName" xml:space="preserve">
277+
<value>PROJECT | FILE</value>
278+
</data>
276279
<data name="ProjectArgumentDescription" xml:space="preserve">
277280
<value>The project file to operate on. If a file is not specified, the command will search the current directory for one.</value>
278281
</data>
282+
<data name="ProjectOrFileArgumentDescription" xml:space="preserve">
283+
<value>The project file or C# file-based app to operate on. If a file is not specified, the command will search the current directory for a project file.</value>
284+
</data>
285+
<data name="FileArgumentDescription" xml:space="preserve">
286+
<value>The file-based app to operate on.</value>
287+
</data>
279288
<data name="CommonCmdFramework" xml:space="preserve">
280289
<value>FRAMEWORK</value>
281290
</data>
@@ -814,4 +823,4 @@ The default is 'false.' However, when targeting .NET 7 or lower, the default is
814823
<data name="SDKSchemaCommandDefinition" xml:space="preserve">
815824
<value>Display the command schema as JSON.</value>
816825
</data>
817-
</root>
826+
</root>

src/Cli/dotnet/Commands/CliCommandStrings.resx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,11 @@ Tool '{1}' (version '{2}') was successfully installed. Entry is added to the man
12801280
<data name="PackageRemoveSpecifyExactlyOnePackageReference" xml:space="preserve">
12811281
<value>Specify only one package reference to remove.</value>
12821282
</data>
1283+
<data name="DirectivesRemoved" xml:space="preserve">
1284+
<value>Removed '{0}' directives ({1}) for '{2}' from: {3}</value>
1285+
<comment>{0} is a directive kind (like '#:package'). {1} is number of removed directives.
1286+
{2} is directive key (e.g., package name). {3} is file path from which directives were removed.</comment>
1287+
</data>
12831288
<data name="PackagesCommandNameCollisionConclusion" xml:space="preserve">
12841289
<value>Command names conflict. Command names are case insensitive.
12851290
{0}</value>
@@ -1590,14 +1595,14 @@ Tool '{1}' (version '{2}') was successfully installed. Entry is added to the man
15901595
<value>Duplicate directives are not supported: {0} at {1}</value>
15911596
<comment>{0} is the directive type and name. {1} is the file path and line number.</comment>
15921597
</data>
1593-
<data name="InvalidOptionCombination" xml:space="preserve">
1594-
<value>Cannot combine option '{0}' and '{1}'.</value>
1595-
<comment>{0} and {1} are option names like '--no-build'.</comment>
1596-
</data>
15971598
<data name="InvalidOptionForStdin" xml:space="preserve">
15981599
<value>Cannot specify option '{0}' when also using '-' to read the file from standard input.</value>
15991600
<comment>{0} is an option name like '--no-build'.</comment>
16001601
</data>
1602+
<data name="InvalidOptionForFileBasedApp" xml:space="preserve">
1603+
<value>Cannot specify option '{0}' when operating on a file-based app.</value>
1604+
<comment>{0} is an option name like '--source'.</comment>
1605+
</data>
16011606
<data name="NoBinaryLogBecauseUpToDate" xml:space="preserve">
16021607
<value>Warning: Binary log option was specified but build will be skipped because output is up to date, specify '--no-cache' to force build.</value>
16031608
<comment>{Locked="--no-cache"}</comment>

src/Cli/dotnet/Commands/Hidden/Add/AddCommandParser.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#nullable disable
5-
64
using System.CommandLine;
75
using Microsoft.DotNet.Cli.Commands.Hidden.Add.Package;
86
using Microsoft.DotNet.Cli.Commands.Hidden.Add.Reference;
7+
using Microsoft.DotNet.Cli.Commands.Package;
98
using Microsoft.DotNet.Cli.Extensions;
109

1110
namespace Microsoft.DotNet.Cli.Commands.Hidden.Add;
@@ -14,11 +13,6 @@ internal static class AddCommandParser
1413
{
1514
public static readonly string DocsLink = "https://aka.ms/dotnet-add";
1615

17-
public static readonly Argument<string> ProjectArgument = new Argument<string>(CliStrings.ProjectArgumentName)
18-
{
19-
Description = CliStrings.ProjectArgumentDescription
20-
}.DefaultToCurrentDirectory();
21-
2216
private static readonly Command Command = ConstructCommand();
2317

2418
public static Command GetCommand()
@@ -33,7 +27,7 @@ private static Command ConstructCommand()
3327
Hidden = true
3428
};
3529

36-
command.Arguments.Add(ProjectArgument);
30+
command.Arguments.Add(PackageCommandParser.ProjectOrFileArgument);
3731
command.Subcommands.Add(AddPackageCommandParser.GetCommand());
3832
command.Subcommands.Add(AddReferenceCommandParser.GetCommand());
3933

src/Cli/dotnet/Commands/Hidden/Add/Package/AddPackageCommandParser.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#nullable disable
5-
64
using System.CommandLine;
75
using Microsoft.DotNet.Cli.Commands.Package;
86
using Microsoft.DotNet.Cli.Commands.Package.Add;
9-
using Microsoft.DotNet.Cli.Extensions;
107

118
namespace Microsoft.DotNet.Cli.Commands.Hidden.Add.Package;
129

@@ -32,20 +29,9 @@ private static Command ConstructCommand()
3229
command.Options.Add(PackageAddCommandParser.InteractiveOption);
3330
command.Options.Add(PackageAddCommandParser.PrereleaseOption);
3431
command.Options.Add(PackageCommandParser.ProjectOption);
32+
command.Options.Add(PackageCommandParser.FileOption);
3533

36-
command.SetAction((parseResult) =>
37-
{
38-
// this command can be called with an argument or an option for the project path - we prefer the option.
39-
// if the option is not present, we use the argument value instead.
40-
if (parseResult.HasOption(PackageCommandParser.ProjectOption))
41-
{
42-
return new PackageAddCommand(parseResult, parseResult.GetValue(PackageCommandParser.ProjectOption)).Execute();
43-
}
44-
else
45-
{
46-
return new PackageAddCommand(parseResult, parseResult.GetValue(AddCommandParser.ProjectArgument) ?? Directory.GetCurrentDirectory()).Execute();
47-
}
48-
});
34+
command.SetAction((parseResult) => new PackageAddCommand(parseResult).Execute());
4935

5036
return command;
5137
}

src/Cli/dotnet/Commands/Hidden/Remove/Package/RemovePackageCommandParser.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#nullable disable
5-
64
using System.CommandLine;
75
using Microsoft.DotNet.Cli.Commands.Package.Remove;
86

src/Cli/dotnet/Commands/Hidden/Remove/RemoveCommandParser.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#nullable disable
5-
64
using System.CommandLine;
75
using Microsoft.DotNet.Cli.Commands.Hidden.Remove.Package;
86
using Microsoft.DotNet.Cli.Commands.Hidden.Remove.Reference;
7+
using Microsoft.DotNet.Cli.Commands.Package;
98
using Microsoft.DotNet.Cli.Extensions;
109

1110
namespace Microsoft.DotNet.Cli.Commands.Hidden.Remove;
@@ -14,11 +13,6 @@ internal static class RemoveCommandParser
1413
{
1514
public static readonly string DocsLink = "https://aka.ms/dotnet-remove";
1615

17-
public static readonly Argument<string> ProjectArgument = new Argument<string>(CliStrings.ProjectArgumentName)
18-
{
19-
Description = CliStrings.ProjectArgumentDescription
20-
}.DefaultToCurrentDirectory();
21-
2216
private static readonly Command Command = ConstructCommand();
2317

2418
public static Command GetCommand()
@@ -33,7 +27,7 @@ private static Command ConstructCommand()
3327
Hidden = true
3428
};
3529

36-
command.Arguments.Add(ProjectArgument);
30+
command.Arguments.Add(PackageCommandParser.ProjectOrFileArgument);
3731
command.Subcommands.Add(RemovePackageCommandParser.GetCommand());
3832
command.Subcommands.Add(RemoveReferenceCommandParser.GetCommand());
3933

src/Cli/dotnet/Commands/New/DotnetCommandCallbacks.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal static bool AddPackageReference(string projectPath, string packageName,
2121
{
2222
commandArgs = commandArgs.Append(PackageAddCommandParser.VersionOption.Name).Append(version);
2323
}
24-
var addPackageReferenceCommand = new PackageAddCommand(AddCommandParser.GetCommand().Parse([.. commandArgs]), projectPath);
24+
var addPackageReferenceCommand = new PackageAddCommand(AddCommandParser.GetCommand().Parse([.. commandArgs]));
2525
return addPackageReferenceCommand.Execute() == 0;
2626
}
2727

0 commit comments

Comments
 (0)