From 307f720ef5ca66d8af9e7e9d05a8d34a08698cfc Mon Sep 17 00:00:00 2001 From: duckdoom5 Date: Tue, 25 Feb 2025 16:15:41 +0100 Subject: [PATCH] Handle exception if `git` is not found --- src/Generator/Utils/Utils.cs | 77 ++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/src/Generator/Utils/Utils.cs b/src/Generator/Utils/Utils.cs index 48528579..44e777cf 100644 --- a/src/Generator/Utils/Utils.cs +++ b/src/Generator/Utils/Utils.cs @@ -84,7 +84,7 @@ namespace CppSharp { while (stringBuilder.Length > 0 && stringBuilder[0] == '_') stringBuilder.Remove(0, 1); - while (stringBuilder.Length > 0 && stringBuilder[stringBuilder.Length - 1] == '_') + while (stringBuilder.Length > 0 && stringBuilder[^1] == '_') stringBuilder.Remove(stringBuilder.Length - 1, 1); } } @@ -136,47 +136,54 @@ namespace CppSharp { public static NewLineType? GetNewLineTypeFromGitConfig(string outputDir) { - if (!bool.TryParse( - ProcessHelper.RunFrom(outputDir, - "git", "rev-parse --is-inside-work-tree", - out _, out _ - ), - out bool isInsideWorkTree)) + try { - return null; - } + if (!bool.TryParse( + ProcessHelper.RunFrom(outputDir, + "git", "rev-parse --is-inside-work-tree", + out _, out _ + ), + out bool isInsideWorkTree)) + { + return null; + } - if (!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(); + // 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; - } + 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(); + // 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, + return autoCrLf switch + { + "input" => NewLineType.LF, + "true" => NewLineType.CRLF, + "false" => NewLineType.Host, - _ => null - }; + _ => null + }; + } + catch (Exception) + { + return null; + } } } }