You've already forked Commercialization.topon
release: 1.4.14
This commit is contained in:
@@ -0,0 +1,245 @@
|
||||
#if UNITY_ANDROID
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using UnityEditor.Android;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Topon_Adapter.Editor
|
||||
{
|
||||
public sealed class ToponAndroidDebuggerDependencyPostProcessor : IPostGenerateGradleAndroidProject
|
||||
{
|
||||
private const string Tag = "[TopOn Build]";
|
||||
private const string DebuggerDependency = "com.anythink.sdk:debugger-ui:+";
|
||||
private const string VerbtoDependency = "com.verbto.tools:util:1.1.3";
|
||||
private const string DebuggerRepositoryUrl = "https://jfrog.anythinktech.com/artifactory/debugger";
|
||||
private const string DepsStart = "// TopOn Debugger UI Dependencies Start";
|
||||
private const string DepsEnd = "// TopOn Debugger UI Dependencies End";
|
||||
private const string ReposStart = "// TopOn Debugger UI Repository Start";
|
||||
private const string ReposEnd = "// TopOn Debugger UI Repository End";
|
||||
|
||||
public int callbackOrder => int.MaxValue;
|
||||
|
||||
public void OnPostGenerateGradleAndroidProject(string path)
|
||||
{
|
||||
var settings = ToponBuildSettingsStore.GetActiveForCurrentBuild();
|
||||
try
|
||||
{
|
||||
ProcessGeneratedProject(path, settings);
|
||||
}
|
||||
finally
|
||||
{
|
||||
ToponBuildSettingsStore.ClearActiveBuildSettings();
|
||||
}
|
||||
}
|
||||
|
||||
public static void ProcessGeneratedProject(string path, ToponBuildSettings settings)
|
||||
{
|
||||
if (settings == null)
|
||||
{
|
||||
settings = ToponBuildSettingsStore.CreateDefault();
|
||||
}
|
||||
|
||||
var gradleRoot = GetGradleRoot(path);
|
||||
var gradleFiles = new[]
|
||||
{
|
||||
Path.Combine(path, "build.gradle"),
|
||||
Path.Combine(gradleRoot, "launcher", "build.gradle"),
|
||||
Path.Combine(gradleRoot, "build.gradle")
|
||||
};
|
||||
|
||||
foreach (var gradleFile in gradleFiles)
|
||||
{
|
||||
StripDebuggerFromGradleFile(gradleFile);
|
||||
}
|
||||
|
||||
StripDebuggerFromGradleFile(Path.Combine(gradleRoot, "settings.gradle"));
|
||||
|
||||
if (settings.enableDebuggerUI)
|
||||
{
|
||||
InjectRepository(Path.Combine(gradleRoot, "settings.gradle"));
|
||||
InjectRepository(Path.Combine(gradleRoot, "build.gradle"));
|
||||
InjectDependencies(Path.Combine(path, "build.gradle"), settings.forceVerbtoUtilVersion);
|
||||
Debug.Log($"{Tag} DebugUI dependency enabled for this build.");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (settings.stripResolvedDebuggerArtifacts)
|
||||
{
|
||||
RemoveGeneratedDebuggerArtifacts(gradleRoot);
|
||||
}
|
||||
|
||||
Debug.Log($"{Tag} DebugUI dependency disabled for this build.");
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetGradleRoot(string unityLibraryPath)
|
||||
{
|
||||
#if UNITY_2019_3_OR_NEWER
|
||||
return Path.GetFullPath(Path.Combine(unityLibraryPath, ".."));
|
||||
#else
|
||||
return Path.GetFullPath(unityLibraryPath);
|
||||
#endif
|
||||
}
|
||||
|
||||
private static void StripDebuggerFromGradleFile(string filePath)
|
||||
{
|
||||
if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var content = File.ReadAllText(filePath);
|
||||
var original = content;
|
||||
content = RemoveMarkedBlock(content, DepsStart, DepsEnd);
|
||||
content = RemoveMarkedBlock(content, ReposStart, ReposEnd);
|
||||
content = RemoveDebuggerRepositoryBlocks(content);
|
||||
content = RemoveLinesContaining(content, "com.anythink.sdk:debugger-ui", "com.verbto.tools:util");
|
||||
|
||||
if (!string.Equals(original, content, StringComparison.Ordinal))
|
||||
{
|
||||
File.WriteAllText(filePath, content);
|
||||
}
|
||||
}
|
||||
|
||||
private static void InjectDependencies(string buildGradlePath, bool forceVerbtoUtilVersion)
|
||||
{
|
||||
if (!File.Exists(buildGradlePath))
|
||||
{
|
||||
Debug.LogWarning($"{Tag} build.gradle not found: {buildGradlePath}");
|
||||
return;
|
||||
}
|
||||
|
||||
var content = File.ReadAllText(buildGradlePath);
|
||||
var block = new StringBuilder();
|
||||
block.AppendLine(DepsStart);
|
||||
block.AppendLine($" implementation '{DebuggerDependency}'");
|
||||
if (forceVerbtoUtilVersion)
|
||||
{
|
||||
block.AppendLine($" implementation '{VerbtoDependency}'");
|
||||
}
|
||||
|
||||
block.AppendLine($" {DepsEnd}");
|
||||
|
||||
var pattern = new Regex(@"(dependencies\s*\{)");
|
||||
if (!pattern.IsMatch(content))
|
||||
{
|
||||
Debug.LogWarning($"{Tag} dependencies block not found: {buildGradlePath}");
|
||||
return;
|
||||
}
|
||||
|
||||
content = pattern.Replace(content, match => match.Groups[1].Value + "\n " + block, 1);
|
||||
File.WriteAllText(buildGradlePath, content);
|
||||
}
|
||||
|
||||
private static void InjectRepository(string gradlePath)
|
||||
{
|
||||
if (!File.Exists(gradlePath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var content = File.ReadAllText(gradlePath);
|
||||
var block = $"{ReposStart}\n maven {{ url '{DebuggerRepositoryUrl}' }}\n {ReposEnd}";
|
||||
Regex pattern;
|
||||
if (Path.GetFileName(gradlePath).Equals("settings.gradle", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
pattern = new Regex(@"(dependencyResolutionManagement\s*\{[\s\S]*?repositories\s*\{)");
|
||||
}
|
||||
else
|
||||
{
|
||||
block = $"{ReposStart}\n maven {{ url '{DebuggerRepositoryUrl}' }}\n {ReposEnd}";
|
||||
pattern = new Regex(@"(repositories\s*\{)");
|
||||
}
|
||||
|
||||
if (!pattern.IsMatch(content))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
content = pattern.Replace(content, match => match.Groups[1].Value + "\n " + block, 1);
|
||||
File.WriteAllText(gradlePath, content);
|
||||
}
|
||||
|
||||
private static void RemoveGeneratedDebuggerArtifacts(string gradleRoot)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(gradleRoot) || !Directory.Exists(gradleRoot))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var root = Path.GetFullPath(gradleRoot).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
|
||||
foreach (var path in Directory.GetFiles(root, "*", SearchOption.AllDirectories))
|
||||
{
|
||||
if (!ToponBuildSettingsStore.IsDebuggerArtifactFileName(Path.GetFileName(path)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var fullPath = Path.GetFullPath(path);
|
||||
if (!fullPath.StartsWith(root, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
File.Delete(fullPath);
|
||||
Debug.Log($"{Tag} Removed generated debugger artifact: {fullPath}");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Debug.LogWarning($"{Tag} Failed to remove generated debugger artifact: {exception.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string RemoveMarkedBlock(string content, string startMarker, string endMarker)
|
||||
{
|
||||
var pattern = new Regex($@"\s*{Regex.Escape(startMarker)}[\s\S]*?{Regex.Escape(endMarker)}\s*");
|
||||
return pattern.Replace(content, "\n");
|
||||
}
|
||||
|
||||
private static string RemoveDebuggerRepositoryBlocks(string content)
|
||||
{
|
||||
var escapedUrl = Regex.Escape(DebuggerRepositoryUrl);
|
||||
content = Regex.Replace(
|
||||
content,
|
||||
$@"\r?\n\s*maven\s*\{{\s*url\s*['""]{escapedUrl}['""][^\r\n]*\s*\}}",
|
||||
string.Empty);
|
||||
content = Regex.Replace(
|
||||
content,
|
||||
$@"\r?\n\s*maven\s*\{{\s*\r?\n\s*url\s*['""]{escapedUrl}['""][^\r\n]*\r?\n\s*\}}",
|
||||
string.Empty);
|
||||
return content;
|
||||
}
|
||||
|
||||
private static string RemoveLinesContaining(string content, params string[] markers)
|
||||
{
|
||||
var lines = content.Replace("\r\n", "\n").Replace('\r', '\n').Split('\n');
|
||||
var kept = new List<string>();
|
||||
foreach (var line in lines)
|
||||
{
|
||||
var remove = false;
|
||||
foreach (var marker in markers)
|
||||
{
|
||||
if (line.Contains(marker))
|
||||
{
|
||||
remove = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!remove)
|
||||
{
|
||||
kept.Add(line);
|
||||
}
|
||||
}
|
||||
|
||||
return string.Join("\n", kept);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user