1.1.0 未驗證
This commit is contained in:
1 parent
2638c5929d
commit
4a72bd132a
31 files changed
+790
-123
No files matched your search
@@ -0,0 +1,69 @@
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using UnityEditor;
|
||||
using UnityEditor.Build;
|
||||
using UnityEditor.Build.Reporting;
|
||||
using UnityEngine;
|
||||
using Debug = UnityEngine.Debug;
|
||||
|
||||
// Build 前把 git commit 短碼寫到 Assets/BuildInfo/Resources/build_id.txt (不進 git),執行時由 BuildInfo.Id 讀出、記進 config 的 buildId
|
||||
// 工作區不乾淨 (有未提交的改動) 會在後面加 "-dirty" 並警告 : 正式出包前請先全部 commit,出包後對該 commit 打 tag
|
||||
public class BuildIdWriter : IPreprocessBuildWithReport
|
||||
{
|
||||
const string Folder = "Assets/BuildInfo/Resources";
|
||||
const string FilePath = Folder + "/build_id.txt";
|
||||
|
||||
public int callbackOrder => 0;
|
||||
|
||||
public void OnPreprocessBuild(BuildReport report)
|
||||
{
|
||||
string hash = Git("rev-parse --short HEAD");
|
||||
string id = string.IsNullOrEmpty(hash) ? "unknown" : hash;
|
||||
|
||||
bool dirty = false;
|
||||
foreach (string line in Git("status --porcelain").Split('\n'))
|
||||
{
|
||||
string l = line.Trim();
|
||||
if (l.Length == 0) continue;
|
||||
dirty = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (dirty)
|
||||
{
|
||||
id += "-dirty";
|
||||
Debug.LogWarning("BuildIdWriter : 工作區有未提交的改動,buildId 加上 -dirty。正式出包請先 commit 再 Build。");
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(Folder);
|
||||
File.WriteAllText(FilePath, id);
|
||||
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
|
||||
Debug.Log($"BuildIdWriter : buildId = {id}");
|
||||
}
|
||||
|
||||
static string Git(string arguments)
|
||||
{
|
||||
try
|
||||
{
|
||||
ProcessStartInfo info = new ProcessStartInfo("git", arguments)
|
||||
{
|
||||
WorkingDirectory = Path.GetDirectoryName(Application.dataPath),
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
};
|
||||
using (Process p = Process.Start(info))
|
||||
{
|
||||
string output = p.StandardOutput.ReadToEnd();
|
||||
p.WaitForExit();
|
||||
return p.ExitCode == 0 ? output.Trim() : "";
|
||||
}
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Debug.LogWarning($"BuildIdWriter : 執行 git 失敗 ({e.Message}),buildId 會是 unknown");
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user