Skip to content
Merged
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.CreateSourceFile(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
Loading