Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,9 @@ If not specified the file will be generated inside the default 'TestResults' dir
<data name="ProjectConvertDryRun" xml:space="preserve">
<value>Determines changes without actually modifying the file system</value>
</data>
<data name="ProjectConvertDeleteSource" xml:space="preserve">
<value>Delete the source file after conversion</value>
</data>
<data name="ProjectManifest" xml:space="preserve">
<value>PROJECT_MANIFEST</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ internal sealed class ProjectConvertCommandDefinition : Command
Arity = ArgumentArity.Zero,
};

public readonly Option<bool> DeleteSourceOption = new("--delete-source")
{
Description = CommandDefinitionStrings.ProjectConvertDeleteSource,
Arity = ArgumentArity.Zero,
};

public ProjectConvertCommandDefinition()
: base("convert", CommandDefinitionStrings.ProjectConvertAppFullName)
{
Expand All @@ -42,5 +48,6 @@ public ProjectConvertCommandDefinition()
Options.Add(ForceOption);
Options.Add(InteractiveOption);
Options.Add(DryRunOption);
Options.Add(DeleteSourceOption);
}
}

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

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

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

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

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

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

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

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

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

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

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

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

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

18 changes: 18 additions & 0 deletions src/Cli/dotnet/Commands/CliCommandStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,24 @@ Tool '{1}' (version '{2}') was successfully installed. Entry is added to the man
<value>Dry run: would create file: {0}</value>
<comment>{0} is the file full path.</comment>
</data>
<data name="ProjectConvertAskDeleteSource" xml:space="preserve">
<value>Delete the source file '{0}' after conversion?</value>
<comment>{0} is the source file path.</comment>
</data>
<data name="ProjectConvertDeletedSourceFile" xml:space="preserve">
<value>Deleted source file: {0}</value>
<comment>{0} is the source file path.</comment>
</data>
<data name="ProjectConvertWouldDeleteSourceFile" xml:space="preserve">
<value>Dry run: would delete source file: {0}</value>
<comment>{0} is the source file path.</comment>
</data>
<data name="ProjectConvertDeleteSourceChoiceYes" xml:space="preserve">
<value>Yes - delete the source file</value>
</data>
<data name="ProjectConvertDeleteSourceChoiceNo" xml:space="preserve">
<value>No - keep the source file</value>
</data>
<data name="ProjectsHeader" xml:space="preserve">
<value>Project(s)</value>
</data>
Expand Down
92 changes: 75 additions & 17 deletions src/Cli/dotnet/Commands/Project/Convert/ProjectConvertCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.FileBasedPrograms;
using Microsoft.DotNet.ProjectTools;
using Spectre.Console;

namespace Microsoft.DotNet.Cli.Commands.Project.Convert;

Expand All @@ -19,6 +20,7 @@ internal sealed class ProjectConvertCommand : CommandBase<ProjectConvertCommandD
private readonly string? _outputDirectory;
private readonly bool _force;
private readonly bool _dryRun;
private readonly bool _deleteSource;

public ProjectConvertCommand(ParseResult parseResult)
: base(parseResult)
Expand All @@ -27,6 +29,7 @@ public ProjectConvertCommand(ParseResult parseResult)
_outputDirectory = parseResult.GetValue(Definition.OutputOption)?.FullName;
_force = parseResult.GetValue(Definition.ForceOption);
_dryRun = parseResult.GetValue(Definition.DryRunOption);
_deleteSource = parseResult.GetValue(Definition.DeleteSourceOption);
}

public override int Execute()
Expand Down Expand Up @@ -102,6 +105,13 @@ public override int Execute()
CopyFile(item.FullPath, targetItemFullPath);
}

// Handle deletion of source file if requested.
bool shouldDelete = _deleteSource || TryAskForDeleteSource(file);
if (shouldDelete)
{
DeleteFile(file);
}

return 0;

void CreateDirectory(string path)
Expand Down Expand Up @@ -131,6 +141,19 @@ void CopyFile(string source, string target)
}
}

void DeleteFile(string path)
{
if (_dryRun)
{
Reporter.Output.WriteLine(CliCommandStrings.ProjectConvertWouldDeleteSourceFile, path);
}
else
{
File.Delete(path);
Reporter.Output.WriteLine(CliCommandStrings.ProjectConvertDeletedSourceFile, path);
}
}

IEnumerable<(string FullPath, string RelativePath)> FindIncludedItems()
{
string entryPointFileDirectory = PathUtilities.EnsureTrailingSlash(Path.GetDirectoryName(file)!);
Expand Down Expand Up @@ -262,24 +285,59 @@ private string DetermineOutputDirectory(string file)

private string? TryAskForOutputDirectory(string defaultValueRelative)
{
return InteractiveConsole.Ask<string?>(
string.Format(CliCommandStrings.ProjectConvertAskForOutputDirectory, defaultValueRelative),
_parseResult,
(path, out result, [NotNullWhen(returnValue: false)] out error) =>
{
if (Directory.Exists(path))
if (!_parseResult.GetValue<bool>(CommonOptions.InteractiveOptionName))
{
return null;
}

try
{
var prompt = new TextPrompt<string>(string.Format(CliCommandStrings.ProjectConvertAskForOutputDirectory, defaultValueRelative))
.AllowEmpty()
.Validate(path =>
{
result = null;
error = string.Format(CliCommandStrings.DirectoryAlreadyExists, Path.GetFullPath(path));
return false;
}
if (string.IsNullOrWhiteSpace(path))
{
return ValidationResult.Success();
}

if (Directory.Exists(path))
{
return ValidationResult.Error(string.Format(CliCommandStrings.DirectoryAlreadyExists, Path.GetFullPath(path)));
}

return ValidationResult.Success();
});

var answer = Spectre.Console.AnsiConsole.Prompt(prompt);
return string.IsNullOrWhiteSpace(answer) ? null : Path.GetFullPath(answer);
}
catch (Exception)
{
return null;
}
}

result = path is null ? null : Path.GetFullPath(path);
error = null;
return true;
},
out var result)
? result
: null;
private bool TryAskForDeleteSource(string sourceFile)
{
if (!_parseResult.GetValue<bool>(CommonOptions.InteractiveOptionName))
{
return false;
}

try
{
var choice = Spectre.Console.AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title($"[cyan]{Markup.Escape(string.Format(CliCommandStrings.ProjectConvertAskDeleteSource, Path.GetFileName(sourceFile)))}[/]")
.AddChoices([CliCommandStrings.ProjectConvertDeleteSourceChoiceYes, CliCommandStrings.ProjectConvertDeleteSourceChoiceNo])
);

return choice == CliCommandStrings.ProjectConvertDeleteSourceChoiceYes;
}
catch (Exception)
{
return false;
}
}
}
25 changes: 25 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.

Loading
Loading