using System;
using System.Linq;
using System.Reflection;
namespace YuncangTrace.LocalService.Services;
///
/// 构建期注入的环境信息 —— 和前端 VITE_BACKEND 一个思路:
/// 开发 / 生产由**构建**决定,不靠用户手填。
///
/// - Debug 构建 :环境 = Development,默认后端 http://127.0.0.1:7300(与前端 dev 代理一致);
/// - Release 构建 :环境 = Production,默认后端 https://packs.example.com(占位,打包时覆盖);
/// - 打包时覆盖 :dotnet build -c Release -p:ServerBaseUrl=https://你的域名。
///
/// 值由 YuncangTrace.LocalService.csproj 通过 写进程序集,
/// 因此 exe 自己就带着"它是哪个环境"的信息,不需要额外的配置文件。
///
public static class BuildInfo
{
/// Debug 构建的默认后端(= 前端 dev 代理的后端 127.0.0.1:7300)
public const string DevServer = "http://127.0.0.1:7300";
/// Release 构建的默认后端(占位,打包时用 -p:ServerBaseUrl= 覆盖)
public const string ProdServerPlaceholder = "https://packs.example.com";
/// 环境名:Development / Production
public static string Environment { get; } = ReadMetadata("AppEnvironment", "Development");
/// 当前构建的默认后端地址
public static string DefaultServer { get; } = Normalize(ReadMetadata("ServerBaseUrl", DevServer));
/// 是不是开发(Debug)构建
public static bool IsDevelopment =>
Environment.Equals("Development", StringComparison.OrdinalIgnoreCase);
/// 给人看的环境名(登录窗里显示)
public static string EnvironmentLabel => IsDevelopment ? "开发环境" : "生产环境";
/// --server=... / YUNCANG_SERVER 的运行时覆盖(排在构建默认值之前)
private static string ReadMetadata(string key, string fallback)
{
try
{
var value = Assembly.GetEntryAssembly()
?.GetCustomAttributes()
.FirstOrDefault(a => string.Equals(a.Key, key, StringComparison.OrdinalIgnoreCase))
?.Value;
return string.IsNullOrWhiteSpace(value) ? fallback : value.Trim();
}
catch
{
return fallback;
}
}
private static string Normalize(string url)
{
var value = url.Trim();
if (!value.Contains("://", StringComparison.Ordinal)) value = "http://" + value;
return value.TrimEnd('/');
}
}