Browse Source

Auto detect line endings

pull/1915/head
duckdoom5 4 months ago
parent
commit
ad8a3306d2
  1. 12
      src/Generator/Driver.cs
  2. 11
      src/Generator/Options.cs
  3. 20
      src/Generator/Utils/ProcessHelper.cs
  4. 15
      src/Generator/Utils/TextGenerator.cs
  5. 49
      src/Generator/Utils/Utils.cs

12
src/Generator/Driver.cs

@ -45,10 +45,22 @@ namespace CppSharp @@ -45,10 +45,22 @@ namespace CppSharp
}
if (Options.NoGenIncludeDirs != null)
{
foreach (var incDir in Options.NoGenIncludeDirs)
ParserOptions.AddIncludeDirs(incDir);
}
NewLineType newLineType = Options.OutputNewLineType;
if (Options.OutputNewLineType == NewLineType.Auto)
{
var sourceNewLineType = GeneratorHelpers.GetNewLineTypeFromGitConfig(Options.OutputDir);
newLineType = sourceNewLineType ?? NewLineType.Host;
}
TextGenerator.SetLineEndingType(newLineType);
}
public void Setup()
{
ValidateOptions();

11
src/Generator/Options.cs

@ -15,6 +15,15 @@ namespace CppSharp @@ -15,6 +15,15 @@ namespace CppSharp
FilePerUnit
}
public enum NewLineType
{
Auto, // Attempt to auto-detect the new line type using git repository settings of the output directory
Host, // Same as Environment.NewLine on the source generator host
LF,
CR,
CRLF,
}
public class DriverOptions
{
public DriverOptions()
@ -84,6 +93,8 @@ namespace CppSharp @@ -84,6 +93,8 @@ namespace CppSharp
public string OutputDir;
public NewLineType OutputNewLineType = NewLineType.Auto;
public bool OutputInteropIncludes;
public bool GenerateFunctionTemplates;
/// <summary>

20
src/Generator/Utils/ProcessHelper.cs

@ -7,8 +7,13 @@ namespace CppSharp.Utils @@ -7,8 +7,13 @@ namespace CppSharp.Utils
{
public static string Run(string path, string args, out int error, out string errorMessage)
{
using (var process = new Process())
return RunFrom(null, path, args, out error, out errorMessage);
}
public static string RunFrom(string workingDir, string path, string args, out int error, out string errorMessage)
{
using var process = new Process();
process.StartInfo.WorkingDirectory = workingDir;
process.StartInfo.FileName = path;
process.StartInfo.Arguments = args;
process.StartInfo.UseShellExecute = false;
@ -19,21 +24,21 @@ namespace CppSharp.Utils @@ -19,21 +24,21 @@ namespace CppSharp.Utils
var retout = new StringBuilder();
process.OutputDataReceived += (sender, outargs) =>
{
if (!string.IsNullOrEmpty(outargs.Data))
{
if (string.IsNullOrEmpty(outargs.Data))
return;
if (retout.Length > 0)
retout.AppendLine();
retout.Append(outargs.Data);
}
};
process.ErrorDataReceived += (sender, errargs) =>
{
if (!string.IsNullOrEmpty(errargs.Data))
{
if (string.IsNullOrEmpty(errargs.Data))
return;
if (reterror.Length > 0)
reterror.AppendLine();
reterror.Append(errargs.Data);
}
};
process.Start();
@ -48,5 +53,4 @@ namespace CppSharp.Utils @@ -48,5 +53,4 @@ namespace CppSharp.Utils
return retout.ToString();
}
}
}
}

15
src/Generator/Utils/TextGenerator.cs

@ -21,7 +21,7 @@ namespace CppSharp @@ -21,7 +21,7 @@ namespace CppSharp
public class TextGenerator : ITextGenerator
{
public static string NewLineChar = "\n";
public static string NewLineChar;
public const uint DefaultIndentation = 4;
@ -47,6 +47,19 @@ namespace CppSharp @@ -47,6 +47,19 @@ namespace CppSharp
return new TextGenerator(this);
}
public static void SetLineEndingType(NewLineType newLineType)
{
NewLineChar = newLineType switch
{
NewLineType.Host => Environment.NewLine,
NewLineType.CR => "\r",
NewLineType.LF => "\n",
NewLineType.CRLF => "\r\n",
NewLineType.Auto => throw new ArgumentException("Don't use Auto here.", nameof(newLineType)),
_ => throw new ArgumentOutOfRangeException(nameof(newLineType))
};
}
public void Write(string msg, params object[] args)
{
if (string.IsNullOrEmpty(msg))

49
src/Generator/Utils/Utils.cs

@ -4,6 +4,7 @@ using System.Linq; @@ -4,6 +4,7 @@ using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using CppSharp.Utils;
namespace CppSharp
{
@ -130,4 +131,52 @@ namespace CppSharp @@ -130,4 +131,52 @@ namespace CppSharp
return uri1.MakeRelativeUri(uri2).ToString();
}
}
public static class GeneratorHelpers
{
public static NewLineType? GetNewLineTypeFromGitConfig(string outputDir)
{
if (!bool.TryParse(
ProcessHelper.RunFrom(outputDir,
"git", "rev-parse --is-inside-work-tree",
out _, out _
),
out bool isInsideWorkTree))
{
return null;
}
if (!isInsideWorkTree)
return null;
// Check git config core.eol setting
var eolConfig = ProcessHelper.RunFrom(outputDir,
"git", "config --get core.eol",
out _, out _)
.Trim().ToLowerInvariant();
switch (eolConfig)
{
case "lf":
return NewLineType.LF;
case "crlf":
return NewLineType.CRLF;
}
// Otherwise check git config core.autocrlf setting
var autoCrLf = ProcessHelper.RunFrom(outputDir,
"git", "config --get core.autocrlf",
out _, out _)
.Trim().ToLowerInvariant();
return autoCrLf switch
{
"input" => NewLineType.LF,
"true" => NewLineType.CRLF,
"false" => NewLineType.Host,
_ => null
};
}
}
}

Loading…
Cancel
Save