Skip to content

Commit

Permalink
reworked after review
Browse files Browse the repository at this point in the history
  • Loading branch information
bitsandfoxes committed Mar 11, 2022
1 parent 0621f61 commit f09bbf1
Show file tree
Hide file tree
Showing 15 changed files with 279 additions and 161 deletions.
2 changes: 1 addition & 1 deletion src/Sentry.Unity.Editor.iOS/BuildPostProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static void OnPostProcessBuild(BuildTarget target, string pathToProject)
sentryXcodeProject.AddNativeOptions(options);
sentryXcodeProject.AddSentryToMain(options);

var sentryCliOptions = SentryCliOptions.LoadCliOptions();
var sentryCliOptions = SentryEditorOptions.LoadEditorOptions();
if (sentryCliOptions.IsValid(logger))
{
SentryCli.CreateSentryProperties(pathToProject, sentryCliOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Sentry.Unity.Editor.Android
public class AndroidManifestConfiguration : IPostGenerateGradleAndroidProject
{
private readonly Func<SentryUnityOptions?> _getOptions;
private readonly Func<SentryCliOptions?> _getSentryCliOptions;
private readonly Func<SentryEditorOptions?> _getSentryCliOptions;
private readonly IUnityLoggerInterceptor? _interceptor;

private readonly bool _isDevelopmentBuild;
Expand All @@ -24,7 +24,7 @@ public class AndroidManifestConfiguration : IPostGenerateGradleAndroidProject

public AndroidManifestConfiguration()
: this(() => ScriptableSentryUnityOptions.LoadSentryUnityOptions(BuildPipeline.isBuildingPlayer),
() => SentryCliOptions.LoadCliOptions(),
() => SentryEditorOptions.LoadEditorOptions(),
isDevelopmentBuild: EditorUserBuildSettings.development,
scriptingImplementation: PlayerSettings.GetScriptingBackend(BuildTargetGroup.Android))
{
Expand All @@ -33,7 +33,7 @@ public AndroidManifestConfiguration()
// Testing
internal AndroidManifestConfiguration(
Func<SentryUnityOptions?> getOptions,
Func<SentryCliOptions?> getSentryCliOptions,
Func<SentryEditorOptions?> getSentryCliOptions,
IUnityLoggerInterceptor? interceptor = null,
bool isDevelopmentBuild = false,
ScriptingImplementation scriptingImplementation = ScriptingImplementation.IL2CPP)
Expand Down
35 changes: 0 additions & 35 deletions src/Sentry.Unity.Editor/ConfigurationWindow/DebugSymbolsTab.cs

This file was deleted.

54 changes: 54 additions & 0 deletions src/Sentry.Unity.Editor/ConfigurationWindow/EditorOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using UnityEditor;
using UnityEngine;

namespace Sentry.Unity.Editor.ConfigurationWindow
{
internal static class EditorOptions
{
internal static void Display(SentryEditorOptions editorOptions)
{
editorOptions.UploadSymbols = EditorGUILayout.BeginToggleGroup(
new GUIContent("Upload Symbols", "Whether debug symbols should be uploaded automatically " +
"on release builds."),
editorOptions.UploadSymbols);

editorOptions.UploadDevelopmentSymbols = EditorGUILayout.Toggle(
new GUIContent("Upload Dev Symbols", "Whether debug symbols should be uploaded automatically " +
"on development builds."),
editorOptions.UploadDevelopmentSymbols);

EditorGUILayout.EndToggleGroup();

editorOptions.Auth = EditorGUILayout.TextField(
new GUIContent("Auth Token", "The authorization token from your user settings in Sentry"),
editorOptions.Auth);

editorOptions.Organization = EditorGUILayout.TextField(
new GUIContent("Org Slug", "The organization slug in Sentry"),
editorOptions.Organization);

editorOptions.Project = EditorGUILayout.TextField(
new GUIContent("Project Name", "The project name in Sentry"),
editorOptions.Project);

EditorGUILayout.Space();
EditorGUI.DrawRect(EditorGUILayout.GetControlRect(false, 1), Color.gray);
EditorGUILayout.Space();

editorOptions.AddSentryToWindowsPlayer = EditorGUILayout.Toggle(
new GUIContent("Add Sentry to Windows Player", "If enabled the SDK will " +
"compile the Windows Player from source and add Sentry to it."),
editorOptions.AddSentryToWindowsPlayer);

editorOptions.MSBuildPath = EditorGUILayout.TextField(
new GUIContent("MSBuild Path", "The path to MSBuild, if left empty the SDK will " +
"try to locate it."),
editorOptions.MSBuildPath);

editorOptions.VSWherePath = EditorGUILayout.TextField(
new GUIContent("VSWhere Path", "The path to VSWhere used to locate MSBuild. If " +
"left empty the SDK will try to locate it."),
editorOptions.VSWherePath);
}
}
}
8 changes: 4 additions & 4 deletions src/Sentry.Unity.Editor/ConfigurationWindow/SentryWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static SentryWindow OpenSentryWindow()
protected virtual string SentryOptionsAssetName { get; } = ScriptableSentryUnityOptions.ConfigName;

public ScriptableSentryUnityOptions Options { get; private set; } = null!; // Set by OnEnable()
public SentryCliOptions CliOptions { get; private set; } = null!; // Set by OnEnable()
public SentryEditorOptions EditorOptions { get; private set; } = null!; // Set by OnEnable()

public event Action<ValidationError> OnValidationError = _ => { };

Expand All @@ -46,7 +46,7 @@ private void Awake()

CheckForAndConvertJsonConfig();
Options = LoadOptions();
CliOptions = SentryCliOptions.LoadCliOptions();
EditorOptions = SentryEditorOptions.LoadEditorOptions();
}

private ScriptableSentryUnityOptions LoadOptions()
Expand Down Expand Up @@ -147,7 +147,7 @@ private void OnGUI()
OptionsConfigurationTab.Display(Options);
break;
case 5:
DebugSymbolsTab.Display(CliOptions);
ConfigurationWindow.EditorOptions.Display(EditorOptions);
break;
default:
break;
Expand All @@ -169,7 +169,7 @@ private void OnLostFocus()
Validate();

EditorUtility.SetDirty(Options);
EditorUtility.SetDirty(CliOptions);
EditorUtility.SetDirty(EditorOptions);
AssetDatabase.SaveAssets();
}

Expand Down
7 changes: 0 additions & 7 deletions src/Sentry.Unity.Editor/IEditorApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,12 @@ namespace Sentry.Unity.Editor
internal interface IEditorApplication
{
string ApplicationContentsPath { get; }
EditorApplication.CallbackFunction Update { get; set; }
}

internal sealed class EditorApplicationAdapter : IEditorApplication
{
public static readonly EditorApplicationAdapter Instance = new();

public string ApplicationContentsPath => EditorApplication.applicationContentsPath;

public EditorApplication.CallbackFunction Update
{
get => EditorApplication.update;
set => EditorApplication.update = value;
}
}
}
17 changes: 15 additions & 2 deletions src/Sentry.Unity.Editor/Native/BuildPostProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,20 @@ internal static void OnPostProcessBuild(BuildTarget target, string executablePat
return;
}

SentryWindowsPlayer.Build(options, executablePath);
var editorOptions = SentryEditorOptions.LoadEditorOptions();
if (editorOptions.AddSentryToWindowsPlayer)
{
if (!File.Exists(editorOptions.MSBuildPath))
{
logger.LogDebug("Failed to find 'MSBuild' at '{0}'. Trying to locate.", editorOptions.MSBuildPath);
MSBuildLocator.SetMSBuildPath(editorOptions, options.DiagnosticLogger);
}

var windowsPlayerBuilder = SentryWindowsPlayer.Create(options.DiagnosticLogger);
windowsPlayerBuilder.AddNativeOptions();
windowsPlayerBuilder.AddSentryToMain();
windowsPlayerBuilder.Build(editorOptions.MSBuildPath, executablePath);
}

var projectDir = Path.GetDirectoryName(executablePath);
AddCrashHandler(logger, projectDir);
Expand All @@ -65,7 +78,7 @@ private static void AddCrashHandler(IDiagnosticLogger logger, string projectDir)

private static void UploadDebugSymbols(IDiagnosticLogger logger, string projectDir, string executableName)
{
var cliOptions = SentryCliOptions.LoadCliOptions();
var cliOptions = SentryEditorOptions.LoadEditorOptions();
if (!cliOptions.IsValid(logger))
{
return;
Expand Down
93 changes: 93 additions & 0 deletions src/Sentry.Unity.Editor/Native/MSBuildLocator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Sentry.Extensibility;
using UnityEditor.PackageManager;

namespace Sentry.Unity.Editor.Native
{
internal static class MSBuildLocator
{
public static void SetMSBuildPath(SentryEditorOptions editorOptions, IDiagnosticLogger? logger)
{
if (!File.Exists(editorOptions.VSWherePath))
{
logger?.LogDebug("Failed to find 'VSWhere' at '{0}'. Trying to locate.", editorOptions.VSWherePath);
SetVSWherePath(editorOptions, logger);
}

logger?.LogDebug("Using 'VSWhere' at '{0}' to locate MSBuild.", editorOptions.VSWherePath);

var vsWhereOutput = "";
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = editorOptions.VSWherePath,
Arguments = "-latest -requires Microsoft.Component.MSBuild -find MSBuild\\**\\Bin\\MSBuild.exe",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};

process.OutputDataReceived += (sender, args) => vsWhereOutput += args.Data;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();

logger?.LogDebug("VSWhere returned with: '{0}'", vsWhereOutput);

if (!File.Exists(vsWhereOutput))
{
throw new FileNotFoundException($"Failed to locate 'MSBuild'.");
}

editorOptions.MSBuildPath = vsWhereOutput;
}

internal static void SetVSWherePath(SentryEditorOptions editorOptions, IDiagnosticLogger? logger)
{
logger?.LogDebug("Requesting packages from Client.");

var packageListRequest = Client.List(true);
while (!packageListRequest.IsCompleted)
{
// TODO: timeout - can't use Task.Run because it has to be on the main thread
}

if (packageListRequest.Status == StatusCode.Success)
{
logger?.LogDebug("Successfully retrieved installed packages.");

var visualstudioPackage = packageListRequest.Result.FirstOrDefault(p => p.name == "com.unity.ide.visualstudio");
if (visualstudioPackage != null)
{
logger?.LogDebug("Located com.unity.ide.visualstudio at '{0}'", visualstudioPackage.resolvedPath);

var vsWherePath = Path.Combine(visualstudioPackage.resolvedPath, "Editor", "VSWhere", "vswhere.exe");
if (File.Exists(vsWherePath))
{
logger?.LogDebug("Setting 'VSWhere' to '{0}'", vsWherePath);
editorOptions.VSWherePath = vsWherePath;
}
else
{
throw new FileNotFoundException($"Failed to locate 'VSWhere' at {vsWherePath}");
}
}
else
{
throw new Exception("Failed to locate the 'com.unity.ide.visualstudio' package.");
}
}
else
{
throw new Exception("Failed to retrieve installed packages.");
}
}
}
}
Loading

0 comments on commit f09bbf1

Please sign in to comment.