52 lines
2.0 KiB
C#
52 lines
2.0 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using UnityEditor;
|
|
using UnityEditor.Build;
|
|
using UnityEditor.Build.Reporting;
|
|
|
|
// Перед каждым билдом записывает в Assets/Resources/BuildInfo.txt: ветку, коммит и дату сборки (05.11).
|
|
// Игра показывает это в углу экрана рядом с версией (App/GameVersionLabel). После билда файл удаляется,
|
|
// чтобы не попадать в git.
|
|
public class BuildInfoWriter : IPreprocessBuildWithReport, IPostprocessBuildWithReport
|
|
{
|
|
const string Path = "Assets/Resources/BuildInfo.txt";
|
|
public int callbackOrder => 0;
|
|
|
|
public void OnPreprocessBuild(BuildReport report)
|
|
{
|
|
string branch = Or(Git("rev-parse --abbrev-ref HEAD")), commit = Or(Git("rev-parse --short HEAD"));
|
|
bool dirty = Git("status --porcelain --untracked-files=no").Length > 0;
|
|
string info = $"{branch} {commit}{(dirty ? "+" : "")} • {DateTime.Now:dd.MM.yyyy HH:mm}";
|
|
Directory.CreateDirectory("Assets/Resources");
|
|
File.WriteAllText(Path, info);
|
|
AssetDatabase.ImportAsset(Path);
|
|
UnityEngine.Debug.Log("[ВЕРСИЯ] Сборка: v" + PlayerSettings.bundleVersion + " • " + info);
|
|
}
|
|
|
|
public void OnPostprocessBuild(BuildReport report)
|
|
{
|
|
AssetDatabase.DeleteAsset(Path);
|
|
}
|
|
|
|
static string Git(string args)
|
|
{
|
|
try
|
|
{
|
|
var p = Process.Start(new ProcessStartInfo("git", args)
|
|
{
|
|
RedirectStandardOutput = true,
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true,
|
|
WorkingDirectory = Directory.GetCurrentDirectory()
|
|
});
|
|
string output = p.StandardOutput.ReadToEnd().Trim();
|
|
p.WaitForExit(5000);
|
|
return output;
|
|
}
|
|
catch { return ""; }
|
|
}
|
|
|
|
static string Or(string s) => string.IsNullOrEmpty(s) ? "?" : s;
|
|
}
|