Skip to content

Commit

Permalink
[new] 支持通过 -x lineEnding=xxx 指定行结束符,默认为Enviroment.NewLine
Browse files Browse the repository at this point in the history
  • Loading branch information
pirunxi committed Feb 19, 2024
1 parent df1486a commit 9cecf65
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/Luban.Core/BuiltinOptionNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ public static class BuiltinOptionNames
public const string PathValidatorRootDir = "rootDir";

public const string NamingConvention = "namingConvention";

public const string LineEnding = "lineEnding";
}
18 changes: 15 additions & 3 deletions src/Luban.Core/CodeTarget/CodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ public void Write(string line)
_lines.Add(line);
}

private static string GetLineEnding()
{
string endings = EnvManager.Current.GetOptionOrDefault("", BuiltinOptionNames.LineEnding, true, "").ToLowerInvariant();
switch (endings)
{
case "": return Environment.NewLine;
case "crlf": return "\r\n";
case "lf": return "\n";
case "cr": return "\r";
default: throw new Exception($"unknown line ending: {endings}");
}
}

public string ToResult(string header)
{
var sb = new StringBuilder(100 * 1024);
Expand All @@ -27,8 +40,7 @@ public string ToResult(string header)
{
sb.AppendLine(line);
}
sb.Replace("\r\n", "\n");
sb.Replace("\n\r", "\n");
return sb.ToString();
string lineEnding = GetLineEnding();
return sb.ToString().ReplaceLineEndings(lineEnding);
}
}

0 comments on commit 9cecf65

Please sign in to comment.