using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; public sealed class CheckDataPlatformSources : Task { [Required] public string Server { get; set; } [Required] public string Baseline { get; set; } private readonly HashSet legacyCalls = new HashSet(StringComparer.Ordinal); private static readonly Regex Connection = new Regex(@"\b\w*(?:scopeFactory|sourceFactory)\s*\.\s*GetScopeAsync\s*\(", RegexOptions.IgnoreCase); private static readonly Regex Method = new Regex(@"^\s*(?:public|private|protected|internal)\s+[^\n]*?\s(\w+)\s*\(", RegexOptions.Multiline); private static readonly Regex Literal = new Regex("\"\"\"[\\s\\S]*?\"\"\"|@\"(?:\"\"|[^\"])*\"|\"(?:\\\\.|[^\"\\\\\\r\\n])*\""); private static readonly Regex Operation = new Regex(@"\b(FROM|JOIN|UPDATE|INSERT\s+(?:IGNORE\s+)?INTO|REPLACE\s+INTO|DELETE\s+FROM|REFERENCES|ALTER\s+TABLE|CREATE\s+TABLE(?:\s+IF\s+NOT\s+EXISTS)?)\s+`?(\w+)", RegexOptions.IgnoreCase); public override bool Execute() { foreach (string line in File.ReadLines(Baseline).Where(x => x.Length > 0 && !x.StartsWith("#"))) { var cols = line.Split('\t'); legacyCalls.Add(cols[0] + "|" + cols[1]); } string plugin = Path.Combine(Server, "Plugins", "Admin.NET.Plugin.AiDOP"); foreach (string area in "Order Production Supply ProcurementExecution Manufacturing MaterialWarehouse FinishedWarehouse WorkOrder SmartOps".Split(' ')) Scan(Path.Combine(plugin, area), "business", plugin); foreach (string dir in new[] { plugin + ".DataPlatform.Core", Path.Combine(Server, "Admin.NET.DataPlatform.Worker") }) { foreach (string project in Directory.GetFiles(dir, "*.csproj")) if (Regex.IsMatch(File.ReadAllText(project), @"]*[/\\]Admin\.NET\.Plugin\.AiDOP\.csproj", RegexOptions.IgnoreCase)) Reject(project, "中台核心或 Worker 引用了业务插件"); Scan(dir, "core", dir); } Scan(Path.Combine(plugin, "DataPlatform", "UnifiedIngress"), "ingress", plugin); foreach (string path in Directory.GetFiles(Path.Combine(Server, "Admin.NET.Web.Entry", "UpdateScripts"), "*.sql")) { var source = File.ReadAllText(path); if (source.IndexOf("mdp_ingest_", StringComparison.OrdinalIgnoreCase) < 0 && source.IndexOf("mdp_connector_", StringComparison.OrdinalIgnoreCase) < 0) continue; foreach (var sql in source.Split(';')) Sql(path, sql, "ddl"); } if (!Log.HasLoggedErrors) Log.LogMessage(MessageImportance.High, "S0-08 数据中台源码与迁移边界检查通过"); return !Log.HasLoggedErrors; } private void Scan(string root, string area, string plugin) { foreach (string path in Directory.GetFiles(root, "*.cs", SearchOption.AllDirectories)) { if (path.Split(Path.DirectorySeparatorChar).Any(x => x == "bin" || x == "obj")) continue; string src = File.ReadAllText(path); if (area == "core" && Regex.IsMatch(src, @"\bAdmin\.NET\.Plugin\.AiDOP\.(Order|Production|Supply|ProcurementExecution|Manufacturing|MaterialWarehouse|FinishedWarehouse|WorkOrder|SmartOps)\b")) Reject(path, "中台核心或 Worker 直接引用业务命名空间"); bool receiver = Path.GetFileName(path) == "MdpPurchaseHandoffService.cs"; if (area == "business" || receiver) { if (Regex.IsMatch(src, @"\bMdpIngestRaw(?:Batch|Event)\b")) Reject(path, "业务或交接接收方引用了核心 Raw 实体"); } if (area == "business") { string relative = path.Substring(plugin.Length+1).Replace('\\','/'); var methods = Method.Matches(src).Cast().ToArray(); foreach (Match call in Connection.Matches(src)) { var method = methods.LastOrDefault(m => m.Index < call.Index); string key = relative + "|" + (method == null ? "" : method.Groups[1].Value); if (!legacyCalls.Remove(key)) Reject(path, "新增或重复的业务外库连接 " + key); } } foreach (Match literal in Literal.Matches(src)) Sql(path, literal.Value, receiver ? "receiver" : area); } } private static int Owner(string table) { if (table == "mdp_ingest_handoff_receipt" || table.StartsWith("mdp_stg_") || table.StartsWith("mdp_std_") || table.StartsWith("dwd_")) return 2; return table.StartsWith("mdp_ingest_") || table.StartsWith("mdp_connector_") ? 1 : 0; } private void Sql(string path, string sql, string area) { bool foreignKey = false; var owners = new HashSet(); var readers = new HashSet(); foreach (Match m in Operation.Matches(sql)) { string op = m.Groups[1].Value.ToUpperInvariant(); string table = m.Groups[2].Value.ToLowerInvariant(); int owner = Owner(table); if (owner == 0) continue; owners.Add(owner); if (op == "REFERENCES") foreignKey = true; if (op == "FROM" || op == "JOIN") readers.Add(owner); if ((area == "business" || area == "receiver") && owner == 1) Reject(path, "业务或交接接收方直接访问核心私表 " + table); if (area == "core" && owner == 2 && Regex.IsMatch(op, "^(UPDATE|INSERT|REPLACE|DELETE)")) Reject(path, "中台核心或 Worker 直接写数据服务表 " + table); } if (foreignKey && owners.Count > 1) Reject(path, "跨归属外键"); if (readers.Count > 1) Reject(path, "跨归属查询"); } private void Reject(string path, string message) { Log.LogError("S0-08: {0} ({1})", message, path); } }