Skip to content
Closed
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
11 changes: 5 additions & 6 deletions documentation/general/dotnet-run-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,14 @@ Directives `sdk`, `package`, and `property` are translated into `<Project Sdk=".
Other directives result in a warning, reserving them for future use.

```cs
#:sdk Microsoft.NET.Sdk.Web
#:property TargetFramework=net11.0
#:property LangVersion=preview
#:package System.CommandLine=2.0.0-*
#:sdk Microsoft.NET.Sdk.Web
#:property TargetFramework net11.0
#:property LangVersion preview
#:package System.CommandLine 2.0.0-*
```

The value must be separated from the name of the directive by white space and any leading and trailing white space is not considered part of the value.
Any value can optionally have two parts separated by `=` or `/`
(the former is consistent with how properties are usually passed, e.g., `/p:Prop=Value`, and the latter is what the `<Project Sdk="Name/Version">` attribute uses).
Any value can optionally have two parts separated by a space (more whitespace characters could be allowed in the future).
The value of the first `#:sdk` is injected into `<Project Sdk="{0}">` with the separator (if any) replaced with `/`,
and the subsequent `#:sdk` directive values are split by the separator and injected as `<Sdk Name="{0}" Version="{1}" />` elements (or without the `Version` attribute if there is no separator).
It is an error if the first part (name) is empty (the version is allowed to be empty, but that results in empty `Version=""`).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

#nullable enable

using System.Collections.Immutable;
using System.CommandLine;
using System.IO;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.TemplateEngine.Cli.Commands;
Expand Down Expand Up @@ -35,13 +37,29 @@ public override int Execute()
throw new GracefulException(LocalizableStrings.DirectoryAlreadyExists, targetDirectory);
}

// Find directives (this can fail, so do this before creating the target directory).
var sourceFile = VirtualProjectBuildingCommand.LoadSourceFile(file);
var directives = VirtualProjectBuildingCommand.FindDirectives(sourceFile);

Directory.CreateDirectory(targetDirectory);

string projectFile = Path.Join(targetDirectory, Path.GetFileNameWithoutExtension(file) + ".csproj");
string projectFileText = VirtualProjectBuildingCommand.GetNonVirtualProjectFileText();
File.WriteAllText(path: projectFile, contents: projectFileText);
var targetFile = Path.Join(targetDirectory, Path.GetFileName(file));

// If there were any directives, remove them from the file.
if (directives.Length != 0)
{
VirtualProjectBuildingCommand.RemoveDirectivesFromFile(directives, sourceFile.Text, targetFile);
File.Delete(file);
}
else
{
File.Move(file, targetFile);
}

File.Move(file, Path.Join(targetDirectory, Path.GetFileName(file)));
string projectFile = Path.Join(targetDirectory, Path.GetFileNameWithoutExtension(file) + ".csproj");
using var stream = File.Open(projectFile, FileMode.Create, FileAccess.Write);
using var writer = new StreamWriter(stream, Encoding.UTF8);
VirtualProjectBuildingCommand.WriteProjectFile(writer, directives);

return 0;
}
Expand Down
16 changes: 16 additions & 0 deletions src/Cli/dotnet/commands/dotnet-run/LocalizableStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,20 @@ Make the profile names distinct.</value>
<data name="LaunchProfileDoesNotExist" xml:space="preserve">
<value>A launch profile with the name '{0}' doesn't exist.</value>
</data>
<data name="UnrecognizedDirective" xml:space="preserve">
<value>Unrecognized directive '{0}' at {1}.</value>
<comment>{0} is the directive name like 'package' or 'sdk', {1} is the file path and line number.</comment>
</data>
<data name="MissingDirectiveName" xml:space="preserve">
<value>Missing name of '{0}' at {1}.</value>
<comment>{0} is the directive name like 'package' or 'sdk', {1} is the file path and line number.</comment>
</data>
<data name="PropertyDirectiveMissingParts" xml:space="preserve">
<value>The property directive needs to have two parts separated by '=' like 'PropertyName=PropertyValue': {0}</value>
<comment>{0} is the file path and line number. {Locked="="}</comment>
</data>
<data name="PropertyDirectiveInvalidName" xml:space="preserve">
<value>Invalid property name at {0}. {1}</value>
<comment>{0} is the file path and line number. {1} is an inner exception message.</comment>
</data>
</root>
2 changes: 1 addition & 1 deletion src/Cli/dotnet/commands/dotnet-run/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public int Execute()
projectFactory = new VirtualProjectBuildingCommand
{
EntryPointFileFullPath = EntryPointFileFullPath,
}.CreateProjectInstance;
}.PrepareProjectInstance().CreateProjectInstance;
}

try
Expand Down
Loading