Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 183975d

Browse files
dgavedissianjamiebrynes7
authored andcommitted
Added RedirectedProcess.RunInAsync. Exposed GetPackagePath. (#662)
1 parent 3f16bde commit 183975d

File tree

6 files changed

+153
-53
lines changed

6 files changed

+153
-53
lines changed

spatialos.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "unity_gdk",
33
"project_version": "0.0.1",
4-
"sdk_version": "13.5.0",
4+
"sdk_version": "13.5.1-b8033-b6bcd-WORKER-SNAPSHOT",
55
"dependencies": [
6-
{"name": "standard_library", "version": "13.5.0"}
6+
{"name": "standard_library", "version": "13.5.1-b8033-b6bcd-WORKER-SNAPSHOT"}
77
]
8-
}
8+
}

workers/unity/Packages/com.improbable.gdk.core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
"dependencies": {
99
"com.improbable.gdk.tools": "0.1.3",
1010
"com.unity.entities": "0.0.12-preview.18",
11-
"com.unity.incrementalcompiler": "0.0.42-preview.21"
11+
"com.unity.incrementalcompiler": "0.0.42-preview.28"
1212
}
1313
}

workers/unity/Packages/com.improbable.gdk.tools/Common.cs

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Text;
66
using Improbable.Gdk.Tools.MiniJSON;
7+
using UnityEditor;
78
using UnityEngine;
89

910
namespace Improbable.Gdk.Tools
@@ -36,7 +37,6 @@ public static class Common
3637

3738
private static readonly string[] MacPaths = { UsrLocalBinDir, UsrLocalShareDir };
3839

39-
4040
static Common()
4141
{
4242
try
@@ -50,22 +50,26 @@ static Common()
5050
}
5151
}
5252

53+
internal static string GetThisPackagePath()
54+
{
55+
return GetPackagePath("com.improbable.gdk.tools");
56+
}
57+
5358
/// <summary>
5459
/// Finds the "file:" reference path from the package manifest.
5560
/// </summary>
56-
internal static string GetThisPackagePath()
61+
public static string GetPackagePath(string packageName)
5762
{
58-
const string gdkTools = "com.improbable.gdk.tools";
5963
var manifest = ParseDependencies(ManifestPath);
6064

61-
if (!manifest.TryGetValue(gdkTools, out var path))
65+
if (!manifest.TryGetValue(packageName, out var path))
6266
{
63-
throw new Exception($"The project manifest must reference '{gdkTools}'.");
67+
throw new Exception($"The project manifest must reference '{packageName}'.");
6468
}
6569

6670
if (!path.StartsWith("file:"))
6771
{
68-
throw new Exception($"The '{gdkTools}' package must exist on disk.");
72+
throw new Exception($"The '{packageName}' package must exist on disk.");
6973
}
7074

7175
path = path.Replace("file:", string.Empty);
@@ -150,5 +154,49 @@ private static string DiscoverLocation(string binarybaseName)
150154
Debug.LogError($"Could not discover location for {binarybaseName}");
151155
return string.Empty;
152156
}
157+
158+
/// <summary>
159+
/// Checks whether `dotnet` and `spatial` exist on the PATH.
160+
/// </summary>
161+
/// <returns></returns>
162+
public static bool CheckDependencies()
163+
{
164+
var hasDotnet = !string.IsNullOrEmpty(Common.DotNetBinary);
165+
var hasSpatial = !string.IsNullOrEmpty(Common.SpatialBinary);
166+
167+
if (hasDotnet && hasSpatial)
168+
{
169+
return true;
170+
}
171+
172+
var builder = new StringBuilder();
173+
174+
builder.AppendLine(
175+
"The SpatialOS GDK for Unity requires 'dotnet' and 'spatial' on your PATH to run its tooling.");
176+
builder.AppendLine();
177+
178+
if (!hasDotnet)
179+
{
180+
builder.AppendLine("Could not find 'dotnet' on your PATH.");
181+
}
182+
183+
if (!hasSpatial)
184+
{
185+
builder.AppendLine("Could not find 'spatial' on your PATH.");
186+
}
187+
188+
builder.AppendLine();
189+
builder.AppendLine("If these exist on your PATH, restart Unity and Unity Hub.");
190+
builder.AppendLine();
191+
builder.AppendLine("Otherwise, install them by following our setup guide:");
192+
builder.AppendLine("https://docs.improbable.io/unity/alpha/content/get-started/set-up");
193+
194+
EditorApplication.delayCall += () =>
195+
{
196+
EditorUtility.DisplayDialog("GDK dependencies check failed", builder.ToString(), "OK");
197+
};
198+
199+
return false;
200+
}
153201
}
154202
}

workers/unity/Packages/com.improbable.gdk.tools/DownloadCoreSdk.cs

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ internal static DownloadResult TryDownload()
116116
/// </summary>
117117
private static DownloadResult Download()
118118
{
119-
if (!CheckDependencies())
119+
if (!Common.CheckDependencies())
120120
{
121121
return DownloadResult.Error;
122122
}
@@ -149,46 +149,6 @@ private static DownloadResult Download()
149149
return exitCode == 0 ? DownloadResult.Success : DownloadResult.Error;
150150
}
151151

152-
private static bool CheckDependencies()
153-
{
154-
var hasDotnet = !string.IsNullOrEmpty(Common.DotNetBinary);
155-
var hasSpatial = !string.IsNullOrEmpty(Common.SpatialBinary);
156-
157-
if (hasDotnet && hasSpatial)
158-
{
159-
return true;
160-
}
161-
162-
var builder = new StringBuilder();
163-
164-
builder.AppendLine(
165-
"The SpatialOS GDK for Unity requires 'dotnet' and 'spatial' on your PATH to run its tooling.");
166-
builder.AppendLine();
167-
168-
if (!hasDotnet)
169-
{
170-
builder.AppendLine("Could not find 'dotnet' on your PATH.");
171-
}
172-
173-
if (!hasSpatial)
174-
{
175-
builder.AppendLine("Could not find 'spatial' on your PATH.");
176-
}
177-
178-
builder.AppendLine();
179-
builder.AppendLine("If these exist on your PATH, restart Unity and Unity Hub.");
180-
builder.AppendLine();
181-
builder.AppendLine("Otherwise, install them by following our setup guide:");
182-
builder.AppendLine("https://docs.improbable.io/unity/alpha/content/get-started/set-up");
183-
184-
EditorApplication.delayCall += () =>
185-
{
186-
EditorUtility.DisplayDialog("GDK dependencies check failed", builder.ToString(), "OK");
187-
};
188-
189-
return false;
190-
}
191-
192152
private static string[] ConstructArguments()
193153
{
194154
var toolsConfig = GdkToolsConfiguration.GetOrCreateInstance();

workers/unity/Packages/com.improbable.gdk.tools/RedirectedProcess.cs

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.IO;
45
using System.Text;
6+
using System.Threading.Tasks;
57
using Improbable.Gdk.Tools.MiniJSON;
68
using UnityEngine;
79
using Debug = UnityEngine.Debug;
810

911
namespace Improbable.Gdk.Tools
1012
{
13+
public class RedirectedProcessResult
14+
{
15+
public int ExitCode;
16+
public List<string> Stdout;
17+
public List<string> Stderr;
18+
}
19+
1120
/// <summary>
1221
/// Runs a windowless process with its stdout/stderr redirected to the Unity console as a single debug print at the
1322
/// end.
@@ -28,7 +37,7 @@ public static int Run(string command, params string[] arguments)
2837
/// <summary>
2938
/// Runs the redirected process and waits for it to return.
3039
/// </summary>
31-
/// <param name="workingDirectory">The directory to run the filename from.</param>
40+
/// <param name="workingDirectory">The directory to run the command from.</param>
3241
/// <param name="command">The filename to run.</param>
3342
/// <param name="arguments">Parameters that will be passed to the command.</param>
3443
/// <returns>The exit code.</returns>
@@ -95,6 +104,89 @@ void OnReceived(object sender, DataReceivedEventArgs args)
95104
}
96105
}
97106

107+
/// <summary>
108+
/// Runs the redirected process and returns a task which can be waited on.
109+
/// </summary>
110+
/// <param name="workingDirectory">The directory to run the command from.</param>
111+
/// <param name="command">The filename to run.</param>
112+
/// <param name="arguments">Parameters that will be passed to the command.</param>
113+
/// <param name="redirectStdout">Redirect standard output to Debug.Log</param>
114+
/// <param name="redirectStderr">Redirect standard error to Debug.LogError</param>
115+
/// <returns>A task which would return the exit code and output.</returns>
116+
public static async Task<RedirectedProcessResult> RunInAsync(string workingDirectory, string command, string[] arguments, bool redirectStdout = false, bool redirectStderr = false)
117+
{
118+
var info = new ProcessStartInfo(command, string.Join(" ", arguments))
119+
{
120+
CreateNoWindow = true,
121+
RedirectStandardError = true,
122+
RedirectStandardOutput = true,
123+
UseShellExecute = false,
124+
WorkingDirectory = workingDirectory
125+
};
126+
127+
return await Task.Run(() =>
128+
{
129+
using (var process = Process.Start(info))
130+
{
131+
if (process == null)
132+
{
133+
throw new Exception(
134+
$"Failed to run {info.FileName} {info.Arguments}\nIs the .NET Core SDK installed?");
135+
}
136+
137+
process.EnableRaisingEvents = true;
138+
139+
var processStandardOutput = new List<string>();
140+
var processStandardError = new List<string>();
141+
142+
void OnStandardOutput(object sender, DataReceivedEventArgs args)
143+
{
144+
if (!string.IsNullOrEmpty(args.Data))
145+
{
146+
if (redirectStdout)
147+
{
148+
Debug.Log(args.Data);
149+
}
150+
lock (processStandardOutput)
151+
{
152+
processStandardOutput.Add(args.Data);
153+
}
154+
}
155+
}
156+
157+
void OnStandardError(object sender, DataReceivedEventArgs args)
158+
{
159+
if (!string.IsNullOrEmpty(args.Data))
160+
{
161+
if (redirectStderr)
162+
{
163+
Debug.LogError(args.Data);
164+
}
165+
lock (processStandardOutput)
166+
{
167+
processStandardError.Add(args.Data);
168+
}
169+
}
170+
}
171+
172+
process.OutputDataReceived += OnStandardOutput;
173+
process.ErrorDataReceived += OnStandardError;
174+
175+
process.BeginOutputReadLine();
176+
process.BeginErrorReadLine();
177+
178+
process.WaitForExit();
179+
180+
return new RedirectedProcessResult
181+
{
182+
ExitCode = process.ExitCode,
183+
Stdout = processStandardOutput,
184+
Stderr = processStandardError
185+
};
186+
}
187+
});
188+
}
189+
98190
private static string ProcessSpatialOutput(string argsData)
99191
{
100192
if (!argsData.StartsWith("{") || !argsData.EndsWith("}"))
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
13.5.0
1+
13.5.1-b8033-b6bcd-WORKER-SNAPSHOT

0 commit comments

Comments
 (0)