Skip to content

Commit

Permalink
Merge pull request #52830 from aaronfranke/3.x-cs-format-mini
Browse files Browse the repository at this point in the history
[3.x] Some more C# formatting and style fixes
  • Loading branch information
akien-mga authored Sep 18, 2021
2 parents bb2772d + 298e29c commit 673612f
Show file tree
Hide file tree
Showing 46 changed files with 847 additions and 822 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ public class GodotBuildLogger : ILogger
public string Parameters { get; set; }
public LoggerVerbosity Verbosity { get; set; }

private StreamWriter _logStreamWriter;
private StreamWriter _issuesStreamWriter;
private int _indent;

public void Initialize(IEventSource eventSource)
{
if (null == Parameters)
throw new LoggerException("Log directory parameter not specified.");

var parameters = Parameters.Split(new[] {';'});
string[] parameters = Parameters.Split(new[] { ';' });

string logDir = parameters[0];

Expand All @@ -35,8 +39,8 @@ public void Initialize(IEventSource eventSource)
if (!Directory.Exists(logDir))
Directory.CreateDirectory(logDir);

logStreamWriter = new StreamWriter(logFile);
issuesStreamWriter = new StreamWriter(issuesFile);
_logStreamWriter = new StreamWriter(logFile);
_issuesStreamWriter = new StreamWriter(issuesFile);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -66,12 +70,12 @@ public void Initialize(IEventSource eventSource)
private void eventSource_ProjectStarted(object sender, ProjectStartedEventArgs e)
{
WriteLine(e.Message);
indent++;
_indent++;
}

private void eventSource_ProjectFinished(object sender, ProjectFinishedEventArgs e)
{
indent--;
_indent--;
WriteLine(e.Message);
}

Expand All @@ -87,7 +91,7 @@ private void eventSource_ErrorRaised(object sender, BuildErrorEventArgs e)
string errorLine = $@"error,{e.File.CsvEscape()},{e.LineNumber},{e.ColumnNumber}," +
$"{e.Code?.CsvEscape() ?? string.Empty},{e.Message.CsvEscape()}," +
$"{e.ProjectFile?.CsvEscape() ?? string.Empty}";
issuesStreamWriter.WriteLine(errorLine);
_issuesStreamWriter.WriteLine(errorLine);
}

private void eventSource_WarningRaised(object sender, BuildWarningEventArgs e)
Expand All @@ -102,7 +106,7 @@ private void eventSource_WarningRaised(object sender, BuildWarningEventArgs e)
string warningLine = $@"warning,{e.File.CsvEscape()},{e.LineNumber},{e.ColumnNumber}," +
$"{e.Code?.CsvEscape() ?? string.Empty},{e.Message.CsvEscape()}," +
$"{e.ProjectFile?.CsvEscape() ?? string.Empty}";
issuesStreamWriter.WriteLine(warningLine);
_issuesStreamWriter.WriteLine(warningLine);
}

private void eventSource_MessageRaised(object sender, BuildMessageEventArgs e)
Expand Down Expand Up @@ -136,35 +140,31 @@ private void WriteLineWithSenderAndMessage(string line, BuildEventArgs e)

private void WriteLine(string line)
{
for (int i = indent; i > 0; i--)
for (int i = _indent; i > 0; i--)
{
logStreamWriter.Write("\t");
_logStreamWriter.Write("\t");
}

logStreamWriter.WriteLine(line);
_logStreamWriter.WriteLine(line);
}

public void Shutdown()
{
logStreamWriter.Close();
issuesStreamWriter.Close();
_logStreamWriter.Close();
_issuesStreamWriter.Close();
}

private bool IsVerbosityAtLeast(LoggerVerbosity checkVerbosity)
{
return Verbosity >= checkVerbosity;
}

private StreamWriter logStreamWriter;
private StreamWriter issuesStreamWriter;
private int indent;
}

internal static class StringExtensions
{
public static string CsvEscape(this string value, char delimiter = ',')
{
bool hasSpecialChar = value.IndexOfAny(new[] {'\"', '\n', '\r', delimiter}) != -1;
bool hasSpecialChar = value.IndexOfAny(new[] { '\"', '\n', '\r', delimiter }) != -1;

if (hasSpecialChar)
return "\"" + value.Replace("\"", "\"\"") + "\"";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace GodotTools.Core
{
public static class StringExtensions
{
private static readonly string _driveRoot = Path.GetPathRoot(Environment.CurrentDirectory);

public static string RelativeToPath(this string path, string dir)
{
// Make sure the directory ends with a path separator
Expand Down Expand Up @@ -49,13 +51,11 @@ public static string NormalizePath(this string path)
return Path.DirectorySeparatorChar + path;
}

private static readonly string DriveRoot = Path.GetPathRoot(Environment.CurrentDirectory);

public static bool IsAbsolutePath(this string path)
{
return path.StartsWith("/", StringComparison.Ordinal) ||
path.StartsWith("\\", StringComparison.Ordinal) ||
path.StartsWith(DriveRoot, StringComparison.Ordinal);
path.StartsWith(_driveRoot, StringComparison.Ordinal);
}

public static string ToSafeDirName(this string dirName, bool allowDirSeparator = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,40 @@ namespace GodotTools.ProjectEditor
{
public class DotNetSolution
{
private string directoryPath;
private readonly Dictionary<string, ProjectInfo> projects = new Dictionary<string, ProjectInfo>();
private const string _solutionTemplate =
@"Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
{0}
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
{1}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2}
EndGlobalSection
EndGlobal
";

private const string _projectDeclaration =
@"Project(""{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}"") = ""{0}"", ""{1}"", ""{{{2}}}""
EndProject";

private const string _solutionPlatformsConfig =
@" {0}|Any CPU = {0}|Any CPU";

private const string _projectPlatformsConfig =
@" {{{0}}}.{1}|Any CPU.ActiveCfg = {1}|Any CPU
{{{0}}}.{1}|Any CPU.Build.0 = {1}|Any CPU";

private string _directoryPath;
private readonly Dictionary<string, ProjectInfo> _projects = new Dictionary<string, ProjectInfo>();

public string Name { get; }

public string DirectoryPath
{
get => directoryPath;
set => directoryPath = value.IsAbsolutePath() ? value : Path.GetFullPath(value);
get => _directoryPath;
set => _directoryPath = value.IsAbsolutePath() ? value : Path.GetFullPath(value);
}

public class ProjectInfo
Expand All @@ -29,22 +54,22 @@ public class ProjectInfo

public void AddNewProject(string name, ProjectInfo projectInfo)
{
projects[name] = projectInfo;
_projects[name] = projectInfo;
}

public bool HasProject(string name)
{
return projects.ContainsKey(name);
return _projects.ContainsKey(name);
}

public ProjectInfo GetProjectInfo(string name)
{
return projects[name];
return _projects[name];
}

public bool RemoveProject(string name)
{
return projects.Remove(name);
return _projects.Remove(name);
}

public void Save()
Expand All @@ -58,15 +83,15 @@ public void Save()

bool isFirstProject = true;

foreach (var pair in projects)
foreach (var pair in _projects)
{
string name = pair.Key;
ProjectInfo projectInfo = pair.Value;

if (!isFirstProject)
projectsDecl += "\n";

projectsDecl += string.Format(ProjectDeclaration,
projectsDecl += string.Format(_projectDeclaration,
name, projectInfo.PathRelativeToSolution.Replace("/", "\\"), projectInfo.Guid);

for (int i = 0; i < projectInfo.Configs.Count; i++)
Expand All @@ -79,15 +104,15 @@ public void Save()
projPlatformsCfg += "\n";
}

slnPlatformsCfg += string.Format(SolutionPlatformsConfig, config);
projPlatformsCfg += string.Format(ProjectPlatformsConfig, projectInfo.Guid, config);
slnPlatformsCfg += string.Format(_solutionPlatformsConfig, config);
projPlatformsCfg += string.Format(_projectPlatformsConfig, projectInfo.Guid, config);
}

isFirstProject = false;
}

string solutionPath = Path.Combine(DirectoryPath, Name + ".sln");
string content = string.Format(SolutionTemplate, projectsDecl, slnPlatformsCfg, projPlatformsCfg);
string content = string.Format(_solutionTemplate, projectsDecl, slnPlatformsCfg, projPlatformsCfg);

File.WriteAllText(solutionPath, content, Encoding.UTF8); // UTF-8 with BOM
}
Expand All @@ -97,37 +122,12 @@ public DotNetSolution(string name)
Name = name;
}

const string SolutionTemplate =
@"Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
{0}
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
{1}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2}
EndGlobalSection
EndGlobal
";

const string ProjectDeclaration =
@"Project(""{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}"") = ""{0}"", ""{1}"", ""{{{2}}}""
EndProject";

const string SolutionPlatformsConfig =
@" {0}|Any CPU = {0}|Any CPU";

const string ProjectPlatformsConfig =
@" {{{0}}}.{1}|Any CPU.ActiveCfg = {1}|Any CPU
{{{0}}}.{1}|Any CPU.Build.0 = {1}|Any CPU";

public static void MigrateFromOldConfigNames(string slnPath)
{
if (!File.Exists(slnPath))
return;

var input = File.ReadAllText(slnPath);
string input = File.ReadAllText(slnPath);

if (!Regex.IsMatch(input, Regex.Escape("Tools|Any CPU")))
return;
Expand All @@ -151,7 +151,7 @@ public static void MigrateFromOldConfigNames(string slnPath)
};

var regex = new Regex(string.Join("|", dict.Keys.Select(Regex.Escape)));
var result = regex.Replace(input, m => dict[m.Value]);
string result = regex.Replace(input, m => dict[m.Value]);

if (result != input)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static string SanitizeIdentifier(string identifier, bool allowEmpty)
return identifier;
}

static bool IsKeyword(string value, bool anyDoubleUnderscore)
private static bool IsKeyword(string value, bool anyDoubleUnderscore)
{
// Identifiers that start with double underscore are meant to be used for reserved keywords.
// Only existing keywords are enforced, but it may be useful to forbid any identifier
Expand All @@ -103,22 +103,22 @@ static bool IsKeyword(string value, bool anyDoubleUnderscore)
}
else
{
if (DoubleUnderscoreKeywords.Contains(value))
if (_doubleUnderscoreKeywords.Contains(value))
return true;
}

return Keywords.Contains(value);
return _keywords.Contains(value);
}

private static readonly HashSet<string> DoubleUnderscoreKeywords = new HashSet<string>
private static readonly HashSet<string> _doubleUnderscoreKeywords = new HashSet<string>
{
"__arglist",
"__makeref",
"__reftype",
"__refvalue",
};

private static readonly HashSet<string> Keywords = new HashSet<string>
private static readonly HashSet<string> _keywords = new HashSet<string>
{
"as",
"do",
Expand Down
15 changes: 8 additions & 7 deletions modules/mono/editor/GodotTools/GodotTools/Build/BuildInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public sealed class BuildInfo : Reference // TODO Remove Reference once we have
public string[] Targets { get; }
public string Configuration { get; }
public bool Restore { get; }
public Array<string> CustomProperties { get; } = new Array<string>(); // TODO Use List once we have proper serialization
// TODO Use List once we have proper serialization
public Array<string> CustomProperties { get; } = new Array<string>();

public string LogsDirPath => Path.Combine(GodotSharpDirs.BuildLogsDirs, $"{Solution.MD5Text()}_{Configuration}");

Expand All @@ -32,12 +33,12 @@ public override int GetHashCode()
unchecked
{
int hash = 17;
hash = hash * 29 + Solution.GetHashCode();
hash = hash * 29 + Targets.GetHashCode();
hash = hash * 29 + Configuration.GetHashCode();
hash = hash * 29 + Restore.GetHashCode();
hash = hash * 29 + CustomProperties.GetHashCode();
hash = hash * 29 + LogsDirPath.GetHashCode();
hash = (hash * 29) + Solution.GetHashCode();
hash = (hash * 29) + Targets.GetHashCode();
hash = (hash * 29) + Configuration.GetHashCode();
hash = (hash * 29) + Restore.GetHashCode();
hash = (hash * 29) + CustomProperties.GetHashCode();
hash = (hash * 29) + LogsDirPath.GetHashCode();
return hash;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static class BuildManager

private static void RemoveOldIssuesFile(BuildInfo buildInfo)
{
var issuesFile = GetIssuesFilePath(buildInfo);
string issuesFile = GetIssuesFilePath(buildInfo);

if (!File.Exists(issuesFile))
return;
Expand Down
Loading

0 comments on commit 673612f

Please sign in to comment.