From 9cbfaeb19571072687d0eb4c819bdad5c1b30853 Mon Sep 17 00:00:00 2001 From: DreamEnderKing Date: Tue, 30 Jan 2024 16:28:16 +0800 Subject: [PATCH] ci: :construction_worker: Add CI to publish hash.json of this repo. --- .github/preProcess/ExportHash.csx | 101 ++++++++++++++++++++++++++++++ .github/workflows/publish.yml | 24 +++++++ 2 files changed, 125 insertions(+) create mode 100644 .github/preProcess/ExportHash.csx create mode 100644 .github/workflows/publish.yml diff --git a/.github/preProcess/ExportHash.csx b/.github/preProcess/ExportHash.csx new file mode 100644 index 00000000..0d512dfe --- /dev/null +++ b/.github/preProcess/ExportHash.csx @@ -0,0 +1,101 @@ +using System.Text.Json; + +Dictionary MD5Data = new Dictionary(); + +public string ConvertAbsToRel(string basePath, string fullPath) +{ + if (fullPath.StartsWith(basePath)) + { + fullPath = fullPath.Replace(basePath, "."); + } + return fullPath; +} + +public string GetFileMd5Hash(string strFileFullPath) +{ + FileStream? fst = null; + try + { + fst = new FileStream(strFileFullPath, FileMode.Open, FileAccess.Read); + byte[] data = System.Security.Cryptography.MD5.Create().ComputeHash(fst); + + StringBuilder sBuilder = new StringBuilder(); + + for (int i = 0; i < data.Length; i++) + { + sBuilder.Append(data[i].ToString("x2")); + } + + fst.Close(); + return sBuilder.ToString().ToLower(); + } + catch (Exception) + { + if (fst != null) + fst.Close(); + if (File.Exists(strFileFullPath)) + return "conflict"; + return ""; + } + finally + { + } +} + +public static bool IsUserFile(string filename) +{ + if (filename.Contains("git") || filename.Contains("bin") || filename.Contains("obj")) + return true; + if (filename.EndsWith("sh") || filename.EndsWith("cmd")) + return true; + if (filename.EndsWith("gz")) + return true; + if (filename.Contains("AI.cpp") || filename.Contains("AI.py")) + return true; + if (filename.Contains("hash.json")) + return true; + if (filename.EndsWith("log")) + return true; + return false; +} + +public void SaveMD5Data() +{ + FileStream fs = new FileStream(@"D:\a\THUAI7\hash.json", FileMode.OpenOrCreate, FileAccess.ReadWrite); + StreamWriter sw = new StreamWriter(fs); + fs.SetLength(0); + var exp1 = from i in MD5Data + select new KeyValuePair(i.Key.Replace(Path.DirectorySeparatorChar, '/'), i.Value); + sw.Write(JsonSerializer.Serialize(exp1.ToDictionary())); + sw.Flush(); + sw.Dispose(); + fs.Dispose(); +} + +public void ScanDir(string dir) +{ + var d = new DirectoryInfo(dir); + foreach (var file in d.GetFiles()) + { + var relFile = ConvertAbsToRel(@"D:\a\THUAI7\", file.FullName); + // 用户自己的文件不会被计入更新hash数据中 + if (IsUserFile(relFile)) + continue; + var hash = GetFileMd5Hash(file.FullName); + if (MD5Data.Keys.Contains(relFile)) + { + if (MD5Data[relFile] != hash) + { + MD5Data[relFile] = hash; + } + } + else + { + MD5Data.Add(relFile, hash); + } + } + foreach (var d1 in d.GetDirectories()) { ScanDir(d1.FullName); } +} + +ScanDir(@"D:\a\THUAI7\"); +SaveMD5Data(); \ No newline at end of file diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 00000000..19b8c01f --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,24 @@ +name: publish +on: + push: + branches: [dev, main, master] + +jobs: + print-thuai7-json: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Setup .NET Core + uses: actions/setup-dotnet@v4 + with: + dotnet-version: 8.0.x + - name: Setup dotnet-script + run: dotnet tool install --global dotnet-script + - name: Generate THUAI7 hash.json + run: dotnet script .github/preProcess/ExportHash.csx + - name: Upload THUAI7 hash.json + uses: actions/upload-artifact@v4 + with: + name: hash.json + path: ./hash.json \ No newline at end of file