Skip to content

Commit 1ad6f51

Browse files
authored
dotnet-suggest script: use appropriate line-endings. (#2186)
Use Windows line-endings when printing the PowerShell script, and Unix line-endings when printing the bash and zsh scripts.
1 parent 87704ce commit 1ad6f51

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/System.CommandLine.Suggest.Tests/SuggestionShellScriptHandlerTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public async Task It_should_print_bash_shell_script()
3838
await _configuration.InvokeAsync("script bash");
3939

4040
_configuration.Output.ToString().Should().Contain("_dotnet_bash_complete()");
41+
_configuration.Output.ToString().Should().NotContain("\r\n");
4142
}
4243

4344
[Fact]
@@ -48,6 +49,7 @@ public async Task It_should_print_powershell_shell_script()
4849
await _configuration.InvokeAsync("script powershell");
4950

5051
_configuration.Output.ToString().Should().Contain("Register-ArgumentCompleter");
52+
_configuration.Output.ToString().Should().Contain("\r\n");
5153
}
5254

5355
[Fact]
@@ -58,6 +60,7 @@ public async Task It_should_print_zsh_shell_script()
5860
await _configuration.InvokeAsync("script zsh");
5961

6062
_configuration.Output.ToString().Should().Contain("_dotnet_zsh_complete()");
63+
_configuration.Output.ToString().Should().NotContain("\r\n");
6164
}
6265
}
6366
}

src/System.CommandLine.Suggest/SuggestionShellScriptHandler.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,37 @@ public static void Handle(TextWriter output, ShellType shellType)
1313
switch (shellType)
1414
{
1515
case ShellType.Bash:
16-
PrintToConsoleFrom(output, "dotnet-suggest-shim.bash");
16+
PrintToConsoleFrom(output, "dotnet-suggest-shim.bash", useUnixLineEndings: true);
1717
break;
1818
case ShellType.PowerShell:
19-
PrintToConsoleFrom(output, "dotnet-suggest-shim.ps1");
19+
PrintToConsoleFrom(output, "dotnet-suggest-shim.ps1", useUnixLineEndings: false);
2020
break;
2121
case ShellType.Zsh:
22-
PrintToConsoleFrom(output, "dotnet-suggest-shim.zsh");
22+
PrintToConsoleFrom(output, "dotnet-suggest-shim.zsh", useUnixLineEndings: true);
2323
break;
2424
default:
2525
throw new SuggestionShellScriptException($"Shell '{shellType}' is not supported.");
2626
}
2727
}
2828

29-
private static void PrintToConsoleFrom(TextWriter output, string scriptName)
29+
private static void PrintToConsoleFrom(TextWriter output, string scriptName, bool useUnixLineEndings)
3030
{
3131
var assemblyLocation = Assembly.GetAssembly(typeof(SuggestionShellScriptHandler)).Location;
3232
var directory = Path.GetDirectoryName(assemblyLocation);
33-
output.Write(File.ReadAllText(Path.Combine(directory, scriptName)));
33+
string scriptContent = File.ReadAllText(Path.Combine(directory, scriptName));
34+
bool hasUnixLineEndings = !scriptContent.Contains("\r\n");
35+
if (hasUnixLineEndings != useUnixLineEndings)
36+
{
37+
if (useUnixLineEndings)
38+
{
39+
scriptContent = scriptContent.Replace("\r\n", "\n");
40+
}
41+
else
42+
{
43+
scriptContent = scriptContent.Replace("\n", "\r\n");
44+
}
45+
}
46+
output.Write(scriptContent);
3447
}
3548
}
3649
}

0 commit comments

Comments
 (0)