Skip to content

Commit e78109b

Browse files
authored
Merge pull request #167 from filzrev/fix-generatedcode-line-endings
fix: Normalize line endings of generated source code
2 parents 5e8e7a2 + ccb9abd commit e78109b

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

src/ConsoleAppFramework/ConsoleAppGenerator.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
182182

183183
static void EmitConsoleAppTemplateSource(IncrementalGeneratorPostInitializationContext context)
184184
{
185-
context.AddSource("ConsoleApp.g.cs", ConsoleAppBaseCode.InitializationCode);
185+
context.AddSource("ConsoleApp.g.cs", ConsoleAppBaseCode.InitializationCode.ReplaceLineEndings());
186186
}
187187

188188
static void EmitConsoleAppRun(SourceProductionContext sourceProductionContext, CommandContext commandContext)
@@ -209,7 +209,7 @@ static void EmitConsoleAppRun(SourceProductionContext sourceProductionContext, C
209209
var withId = new Emitter.CommandWithId(null, command, -1);
210210
emitter.EmitRun(sb, withId, command.IsAsync);
211211
}
212-
sourceProductionContext.AddSource("ConsoleApp.Run.g.cs", sb.ToString());
212+
sourceProductionContext.AddSource("ConsoleApp.Run.g.cs", sb.ToString().ReplaceLineEndings());
213213

214214
var help = new SourceBuilder(0);
215215
help.AppendLine(ConsoleAppBaseCode.GeneratedCodeHeader);
@@ -218,7 +218,7 @@ static void EmitConsoleAppRun(SourceProductionContext sourceProductionContext, C
218218
var emitter = new Emitter();
219219
emitter.EmitHelp(help, command);
220220
}
221-
sourceProductionContext.AddSource("ConsoleApp.Run.Help.g.cs", help.ToString());
221+
sourceProductionContext.AddSource("ConsoleApp.Run.Help.g.cs", help.ToString().ReplaceLineEndings());
222222
}
223223

224224
static void EmitConsoleAppBuilder(SourceProductionContext sourceProductionContext, CollectBuilderContext collectBuilderContext)
@@ -267,7 +267,7 @@ static void EmitConsoleAppBuilder(SourceProductionContext sourceProductionContex
267267
var emitter = new Emitter();
268268
emitter.EmitBuilder(sb, commandIds, hasRun, hasRunAsync);
269269
}
270-
sourceProductionContext.AddSource("ConsoleApp.Builder.g.cs", sb.ToString());
270+
sourceProductionContext.AddSource("ConsoleApp.Builder.g.cs", sb.ToString().ReplaceLineEndings());
271271

272272
// Build Help
273273

@@ -279,7 +279,7 @@ static void EmitConsoleAppBuilder(SourceProductionContext sourceProductionContex
279279
var emitter = new Emitter();
280280
emitter.EmitHelp(help, commandIds!);
281281
}
282-
sourceProductionContext.AddSource("ConsoleApp.Builder.Help.g.cs", help.ToString());
282+
sourceProductionContext.AddSource("ConsoleApp.Builder.Help.g.cs", help.ToString().ReplaceLineEndings());
283283
}
284284

285285
static void EmitConsoleAppConfigure(SourceProductionContext sourceProductionContext, DllReference dllReference)
@@ -311,7 +311,7 @@ static void EmitConsoleAppConfigure(SourceProductionContext sourceProductionCont
311311
sb2.AppendLine("using Microsoft.Extensions.Hosting;");
312312
var emitter = new Emitter();
313313
emitter.EmitAsConsoleAppBuilder(sb2, dllReference);
314-
sourceProductionContext.AddSource("ConsoleAppHostBuilderExtensions.g.cs", sb2.ToString());
314+
sourceProductionContext.AddSource("ConsoleAppHostBuilderExtensions.g.cs", sb2.ToString().ReplaceLineEndings());
315315
}
316316

317317
using (sb.BeginBlock("internal static partial class ConsoleApp"))
@@ -321,7 +321,7 @@ static void EmitConsoleAppConfigure(SourceProductionContext sourceProductionCont
321321
emitter.EmitConfigure(sb, dllReference);
322322
}
323323

324-
sourceProductionContext.AddSource("ConsoleApp.Builder.Configure.g.cs", sb.ToString());
324+
sourceProductionContext.AddSource("ConsoleApp.Builder.Configure.g.cs", sb.ToString().ReplaceLineEndings());
325325
}
326326

327327
class CommandContext(Command? command, bool isAsync, DiagnosticReporter diagnosticReporter, InvocationExpressionSyntax node) : IEquatable<CommandContext>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace ConsoleAppFramework;
2+
3+
internal static class StringExtensions
4+
{
5+
#if NETSTANDARD2_0
6+
public static string ReplaceLineEndings(this string input)
7+
{
8+
#pragma warning disable RS1035
9+
return ReplaceLineEndings(input, Environment.NewLine);
10+
#pragma warning restore RS1035
11+
}
12+
13+
public static string ReplaceLineEndings(this string text, string replacementText)
14+
{
15+
text = text.Replace("\r\n", "\n");
16+
17+
if (replacementText != "\n")
18+
text = text.Replace("\n", replacementText);
19+
20+
return text;
21+
}
22+
#endif
23+
}

0 commit comments

Comments
 (0)