8000 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
Ask for the output directory
  • Loading branch information
jjonescz committed Jul 4, 2025
commit 4a56fc7dd1e2c8e6ea9b4237e842a779fc9957b4
4 changes: 4 additions & 0 deletions src/Cli/dotnet/Commands/CliCommandStrings.resx
< 10000 /div>
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,10 @@ Tool '{1}' (version '{2}') was successfully installed. Entry is added to the man
<data name="CmdOptionKeepSourceDescription" xml:space="preserve">
<value>Whether to keep source files intact (otherwise, they are deleted after conversion).</value>
</data>
<data name="ProjectConvertAskForOutputDirectory" xml:space="preserve">
<value>Specify the output directory ({0}):</value>
<comment>{0} is the default value</comment>
</data>
<data name="ProjectConvertConfirmKeepSourceFiles" xml:space="preserve">
<value>Keep source files?</value>
</data>
Expand Down
27 changes: 19 additions & 8 deletions src/Cli/dotnet/Commands/Project/Convert/ProjectConvertCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,7 @@ public override int Execute()
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.
string targetDirectory = DetermineOutputDirectory(file);
bool keepSourceFiles = ShouldKeepSourceFiles();

// Find directives (this can fail, so do this before creating the target directory).
Expand Down Expand Up @@ -136,6 +129,24 @@ public override int Execute()
}
}

private string DetermineOutputDirectory(string file)
{
string defaultValue = Path.ChangeExtension(file, null);
string defaultValueRelative = Path.GetRelativePath(relativeTo: Environment.CurrentDirectory, defaultValue);
string targetDirectory = _outputDirectory
?? InteractiveConsole.Ask(
string.Format(CliCommandStrings.ProjectConvertAskForOutputDirectory, defaultValueRelative),
_parseResult,
(path) => Directory.Exists(path) ? string.Format(CliCommandStrings.DirectoryAlreadyExists, Path.GetFullPath(path)) : null)
?? defaultValue;
if (Directory.Exists(targetDirectory))
{
throw new GracefulException(CliCommandStrings.DirectoryAlreadyExists, targetDirectory);
}

return Path.GetFullPath(targetDirectory);
}

private bool ShouldKeepSourceFiles()
{
bool? keepSourceFiles =
Expand Down
5 changes: 5 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.

5 changes: 5 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.

5 changes: 5 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.

5 changes: 5 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.

5 changes: 5 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.

5 changes: 5 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.

5 changes: 5 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.

5 changes: 5 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.pl.xlf

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

5 changes: 5 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.pt-BR.xlf

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

5 changes: 5 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.ru.xlf

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

5 changes: 5 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.tr.xlf

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

5 changes: 5 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.zh-Hans.xlf

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

5 changes: 5 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.zh-Hant.xlf

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

25 changes: 25 additions & 0 deletions src/Cli/dotnet/InteractiveConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,29 @@ static bool KeyMatches(ConsoleKeyInfo pressedKey, string valueKey)
valueKey.ToLowerInvariant().Substring(0, 1));
}
}

public static string? Ask(string question, ParseResult parseResult, Func<string?, string?> validate)
{
if (!parseResult.GetValue(CommonOptions.InteractiveOption()))
{
return null;
}

while (true)
{
Console.Write(question);
Console.Write(' ');

string? answer = Console.ReadLine();
answer = string.IsNullOrWhiteSpace(answer) ? null : answer.Trim();
if (validate(answer) is { } error)
{
Console.WriteLine(error);
}
else
{
return answer;
}
}
}
}
Loading
0