10000 Make `dotnet project convert` interactive by jjonescz · Pull Request #49660 · dotnet/sdk · GitHub
[go: up one dir, main page]

Skip to content

Make dotnet project convert interactive #49660

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

Merged
merged 15 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add option for keeping source files
  • Loading branch information
jjonescz committed Jul 4, 2025
commit d9fb0a72e2d0124babb687b9e5c9b2e7f5dca5c6
10 changes: 10 additions & 0 deletions src/Cli/dotnet/Commands/CliCommandStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,16 @@ Tool '{1}' (version '{2}') was successfully installed. Entry is added to the man
<data name="ProjectConvertAppFullName" xml:space="preserve">
<value>Convert a file-based program to a project-based program.</value>
</data>
<data name="CmdOptionKeepSourceDescription" xml:space="preserve">
<value>Whether to keep source files intact (otherwise, they are deleted after conversion).</value>
</data>
<data name="ProjectConvertConfirmKeepSourceFiles" xml:space="preserve">
<value>Keep source files?</value>
</data>
<data name="ProjectConvertNeedsConfirmation" xml:space="preserve">
<value>Conversion command needs to confirm whether to keep source files. Run in interactive mode or use the "--keep-source" command-line option to confirm.</value>
<comment>{Locked="--keep-source"}</comment>
</data>
<data name="ProjectManifest" xml:space="preserve">
<value>PROJECT_MANIFEST</value>
</data>
Expand Down
36 changes: 32 additions & 4 deletions src/Cli/dotnet/Commands/Project/Convert/ProjectConvertCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.CommandLine;
using Microsoft.Build.Evaluation;
using Microsoft.DotNet.Cli.Commands.Run;
using Microsoft.DotNet.Cli.Extensions;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.TemplateEngine.Cli.Commands;

Expand All @@ -17,18 +18,23 @@ internal sealed class ProjectConvertCommand(ParseResult parseResult) : CommandBa

public override int Execute()
{
// Check the entry point file path.
string file = Path.GetFullPath(_file);
if (!VirtualProjectBuildingCommand.IsValidEntryPointPath(file))
{
throw new GracefulException(CliCommandStrings.InvalidFilePath, file);
}

// Determine the output directory.
string targetDirectory = _outputDirectory ?? Path.ChangeExtension(file, null);
if (Directory.Exists(targetDirectory))
{
throw new GracefulException(CliCommandStrings.DirectoryAlreadyExists, targetDirectory);
}

// Determine whether to keep the source files.
bool keepSourceFiles = ShouldKeepSourceFiles();

// Find directives (this can fail, so do this before creating the target directory).
var sourceFile = VirtualProjectBuildingCommand.LoadSourceFile(file);
var directives = VirtualProjectBuildingCommand.FindDirectives(sourceFile, reportAllErrors: !_force, errors: null);
Expand All @@ -40,8 +46,12 @@ public override int Execute()

var targetFile = Path.Join(targetDirectory, Path.GetFileName(file));

// If there were any directives, remove them from the file.
if (directives.Length != 0)
// Process the entry point file.
if (keepSourceFiles)
{
File.Copy(file, targetFile);
}
else if (directives.Length != 0)
{
VirtualProjectBuildingCommand.RemoveDirectivesFromFile(directives, sourceFile.Text, targetFile);
File.Delete(file);
Expand All @@ -57,7 +67,7 @@ public override int Execute()
using var writer = new StreamWriter(stream, Encoding.UTF8);
VirtualProjectBuildingCommand.WriteProjectFile(writer, directives, isVirtualProject: false);

// Copy over included items.
// Copy or move over included items.
foreach (var item in includeItems)
{
string targetItemFullPath = Path.Combine(targetDirectory, item.RelativePath);
Expand All @@ -70,7 +80,14 @@ public override int Execute()

string targetItemDirectory = Path.GetDirectoryName(targetItemFullPath)!;
Directory.CreateDirectory(targetItemDirectory);
File.Copy(item.FullPath, targetItemFullPath);
if (keepSourceFiles)
{
File.Copy(item.FullPath, targetItemFullPath);
}
else
{
File.Move(item.FullPath, targetItemFullPath);
}
}

return 0;
Expand Down Expand Up @@ -118,4 +135,15 @@ public override int Execute()
}
}
}

private bool ShouldKeepSourceFiles()
{
bool? keepSourceFiles =
_parseResult.HasOption(ProjectConvertCommandParser.KeepSourceOption)
? _parseResult.GetValue(ProjectConvertCommandParser.KeepSourceOption)
: InteractiveConsole.Confirm(CliCommandStrings.ProjectConvertConfirmKeepSourceFiles, _parseResult);
return keepSourceFiles is { } value
? value
: throw new GracefulException(CliCommandStrings.ProjectConvertNeedsConfirmation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ internal sealed class ProjectConvertCommandParser
Arity = ArgumentArity.Zero,
};

public static readonly Option<bool> KeepSourceOption = new("--keep-source")
{
Description = CliCommandStrings.CmdOptionKeepSourceDescription,
Arity = ArgumentArity.ZeroOrOne,
};

public static Command GetCommand()
{
Command command = new("convert", CliCommandStrings.ProjectConvertAppFullName)
{
FileArgument,
SharedOptions.OutputOption,
KeepSourceOption,
ForceOption,
CommonOptions.InteractiveOption(),
};

command.SetAction((parseResult) => new ProjectConvertCommand(parseResult).Execute());
Expand Down
2 changes: 1 addition & 1 deletion src/Cli/dotnet/Commands/Tool/Execute/ToolExecuteCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,6 @@ public override int Execute()
private bool UserAgreedToRunFromSource(PackageId packageId, NuGetVersion version, PackageSource source)
{
string promptMessage = string.Format(CliCommandStrings.ToolDownloadConfirmationPrompt, packageId, version.ToString(), source.Source);
return InteractiveConsole.Confirm(promptMessage, _parseResult);
return InteractiveConsole.Confirm(promptMessage, _parseResult) == true;
}
}
15 changes: 15 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
0