-
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathGradleSetup.cs
More file actions
70 lines (58 loc) · 2.34 KB
/
GradleSetup.cs
File metadata and controls
70 lines (58 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System.IO;
using System.Text.RegularExpressions;
using Sentry.Extensibility;
namespace Sentry.Unity.Editor.Android;
internal class GradleSetup
{
private readonly IDiagnosticLogger _logger;
private const string AndroidMarker = "android {";
private const string SdkDependenciesFull = SdkDependencies + "\n}\n\n" + AndroidMarker;
public const string SdkDependencies = @"dependencies {
implementation(name: 'sentry-android-ndk-release', ext:'aar')
implementation(name: 'sentry-android-core-release', ext:'aar')
implementation(name: 'sentry-android-replay-release', ext:'aar')";
public const string DependenciesAddedMessage = "The Sentry Gradle dependencies have already been added.";
private readonly string _unityLibraryGradle;
public GradleSetup(IDiagnosticLogger logger, string gradleProjectPath)
{
_logger = logger;
_unityLibraryGradle = Path.Combine(gradleProjectPath, "unityLibrary", "build.gradle");
}
public void UpdateGradleProject()
{
_logger.LogInfo("Adding Sentry to the gradle project.");
var fileContent = LoadGradleScript(_unityLibraryGradle);
fileContent = AddSentryToGradle(fileContent);
File.WriteAllText(_unityLibraryGradle, fileContent);
}
public void ClearGradleProject()
{
_logger.LogInfo("Removing Sentry from the gradle project.");
var fileContent = LoadGradleScript(_unityLibraryGradle);
if (!fileContent.Contains(SdkDependenciesFull))
{
_logger.LogDebug("The Sentry Gradle dependencies have already been removed.");
return;
}
fileContent = fileContent.Replace(SdkDependenciesFull, AndroidMarker);
File.WriteAllText(_unityLibraryGradle, fileContent);
}
public string AddSentryToGradle(string fileContent)
{
if (fileContent.Contains(SdkDependenciesFull))
{
_logger.LogDebug(DependenciesAddedMessage);
return fileContent;
}
var regex = new Regex(Regex.Escape(AndroidMarker));
return regex.Replace(fileContent, SdkDependenciesFull, 1);
}
internal static string LoadGradleScript(string path)
{
if (!File.Exists(path))
{
throw new FileNotFoundException("Failed to find the gradle config.", path);
}
return File.ReadAllText(path);
}
}