Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process C# directives in file-based programs #47702

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 22 additions & 4 deletions src/Cli/dotnet/commands/Project/convert/ProjectConvertCommand.cs
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/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/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