RizzeBattleRoyale/Assets/Editor/GenerateSupabaseSecretsPrebuild.cs
2025-10-15 20:28:33 +04:00

55 lines
2.0 KiB
C#

#if UNITY_EDITOR
using System;
using System.IO;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
[Serializable] class SupabaseClientSecrets { public string url; public string anonKey; }
public class GenerateSupabaseSecretsPrebuild : IPreprocessBuildWithReport
{
public int callbackOrder => 0;
public void OnPreprocessBuild(BuildReport report)
{
// 1) read from Cloud Build env (populated from project secrets)
var url = Environment.GetEnvironmentVariable("SUPABASE_URL");
var anonKey = Environment.GetEnvironmentVariable("SUPABASE_ANON");
// 2) local dev override (git-ignored)
var devPath = Path.Combine(Application.dataPath, "Editor/supabase.secrets.dev.json");
if (File.Exists(devPath))
{
try
{
var dev = JsonUtility.FromJson<SupabaseClientSecrets>(File.ReadAllText(devPath));
if (!string.IsNullOrWhiteSpace(dev.url)) url = dev.url;
if (!string.IsNullOrWhiteSpace(dev.anonKey)) anonKey = dev.anonKey;
}
catch { Debug.LogWarning("Invalid supabase.secrets.dev.json; ignoring."); }
}
if (string.IsNullOrWhiteSpace(url) || string.IsNullOrWhiteSpace(anonKey))
throw new BuildFailedException("Missing SUPABASE_URL or SUPABASE_ANON_KEY.");
var json = JsonUtility.ToJson(new SupabaseClientSecrets { url = url, anonKey = anonKey }, false);
var resDir = Path.Combine(Application.dataPath, "Resources");
Directory.CreateDirectory(resDir);
var outPath = Path.Combine(resDir, "SupabaseClientSecrets.json");
File.WriteAllText(outPath, json);
AssetDatabase.ImportAsset(RelativeToProject(outPath));
Debug.Log("Embedded SupabaseClientSecrets.json");
}
static string RelativeToProject(string abs)
{
var root = Application.dataPath[..^"Assets".Length];
return abs.Replace(root, "").Replace("\\", "/");
}
}
#endif