116 lines
3.9 KiB
C#
116 lines
3.9 KiB
C#
|
|
using System.Collections.Generic;
|
||
|
|
using System.IO;
|
||
|
|
using System.Linq;
|
||
|
|
using System.Text;
|
||
|
|
using System.Text.RegularExpressions;
|
||
|
|
using Microsoft.CodeAnalysis;
|
||
|
|
using Microsoft.CodeAnalysis.CSharp;
|
||
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||
|
|
|
||
|
|
namespace wvdet.CodeChecker
|
||
|
|
{
|
||
|
|
public static class CheckerUtils
|
||
|
|
{
|
||
|
|
public static List<Result> CheckCodes()
|
||
|
|
{
|
||
|
|
var checkables = new List<ICheckable>
|
||
|
|
{
|
||
|
|
new CheckNamespace(),
|
||
|
|
new CheckClass(),
|
||
|
|
new CheckEnum(),
|
||
|
|
new CheckInterface(),
|
||
|
|
new CheckStruct(),
|
||
|
|
};
|
||
|
|
|
||
|
|
var results = new List<Result>();
|
||
|
|
var files = Directory.GetFiles("Assets", "*.cs", SearchOption.AllDirectories).Select(x => x.Replace("\\", "/"))
|
||
|
|
.ToList();
|
||
|
|
|
||
|
|
// files.Clear();
|
||
|
|
// files.Add(@"Assets\Hotfix\Scripts\_Table\Dubbing.cs");
|
||
|
|
|
||
|
|
foreach (var filepath in files)
|
||
|
|
{
|
||
|
|
if (IsUncheckFile(filepath))
|
||
|
|
continue;
|
||
|
|
|
||
|
|
var result = new Result();
|
||
|
|
var root = CSharpSyntaxTree.ParseText(File.ReadAllText(filepath)).GetCompilationUnitRoot();
|
||
|
|
checkables.ForEach(x => result.details.AddRange(x.Check(filepath, root)));
|
||
|
|
if (result.details.Count > 0)
|
||
|
|
{
|
||
|
|
result.filepath = filepath;
|
||
|
|
result.warningCount = result.details.Count(x => x.level == Level.Warning);
|
||
|
|
result.errorCount = result.details.Count(x => x.level == Level.Error);
|
||
|
|
result.details.Sort((a, b) => (int)b.level - (int)a.level);
|
||
|
|
results.Add(result);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return results;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static bool IsUncheckFile(string filepath)
|
||
|
|
{
|
||
|
|
//将不需要检查的代码过滤
|
||
|
|
//示例: if (Path.GetFileName(filepath) == "Res.cs")
|
||
|
|
// return true;
|
||
|
|
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static string ConvertPascalCase(string name)
|
||
|
|
{
|
||
|
|
if (!name.Contains(" "))
|
||
|
|
{
|
||
|
|
name = Regex.Replace(name, "(?<=[a-z])(?=[A-Z])", " ");
|
||
|
|
}
|
||
|
|
|
||
|
|
string s = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(name.ToLower())
|
||
|
|
.Replace(" ", "").Replace("_", "");
|
||
|
|
|
||
|
|
return s;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static bool IsChineseIdentifier(string identifier, int lineNumber, out Detail detail)
|
||
|
|
{
|
||
|
|
detail = null;
|
||
|
|
if (Regex.IsMatch(identifier, "[\u4e00-\u9fbb]"))
|
||
|
|
{
|
||
|
|
detail = new Detail
|
||
|
|
{
|
||
|
|
line = lineNumber,
|
||
|
|
guideline = "禁止使用中文命名",
|
||
|
|
};
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 获取Field的Identifier
|
||
|
|
/// </summary>
|
||
|
|
public static SyntaxToken GetIdentifier(this FieldDeclarationSyntax self)
|
||
|
|
{
|
||
|
|
var variableDec = self.DescendantNodes().OfType<VariableDeclarationSyntax>().FirstOrDefault();
|
||
|
|
var declarator = variableDec?.DescendantNodes().OfType<VariableDeclaratorSyntax>().FirstOrDefault();
|
||
|
|
return declarator.Identifier;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static PredefinedTypeSyntax GetPredefined(this FieldDeclarationSyntax self)
|
||
|
|
{
|
||
|
|
var variableDec = self.DescendantNodes().OfType<VariableDeclarationSyntax>().FirstOrDefault();
|
||
|
|
return variableDec?.DescendantNodes().OfType<PredefinedTypeSyntax>().FirstOrDefault();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static string GetFieldGreenText(this FieldDeclarationSyntax self)
|
||
|
|
{
|
||
|
|
var sb = new StringBuilder();
|
||
|
|
self.Modifiers.ToList().ForEach(m => sb.Append($"{m.Text.Trim()} "));
|
||
|
|
sb.Append(self.Declaration.GetText().ToString().Trim());
|
||
|
|
return sb.ToString();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|