Skip to content

Commit

Permalink
Update dui3/alpha with changes from dev (#3483)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanRynne authored Jun 7, 2024
2 parents f57d285 + c92da2d commit 6e6d810
Show file tree
Hide file tree
Showing 19 changed files with 524 additions and 88 deletions.
26 changes: 8 additions & 18 deletions Automate/Speckle.Automate.Sdk/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,29 +97,17 @@ public static async Task<int> Main(string[] args, Func<AutomationContext, Task>
public static async Task<int> Main<TInput>(string[] args, Func<AutomationContext, TInput, Task> automateFunction)
where TInput : struct
{
int returnCode = 0; // This is the CLI return code, defaults to 0 (Success), change to 1 to flag a failed run.

Argument<string> pathArg = new(name: "Input Path", description: "A file path to retrieve function inputs");
RootCommand rootCommand = new();

rootCommand.AddArgument(pathArg);
rootCommand.SetHandler(
async (inputPath) =>
async inputPath =>
{
FunctionRunData<TInput>? data = FunctionRunDataParser.FromPath<TInput>(inputPath);

AutomationContext context = await RunFunction(
automateFunction,
data.AutomationRunData,
data.SpeckleToken,
data.FunctionInputs
)
.ConfigureAwait(false);
FunctionRunData<TInput> data = FunctionRunDataParser.FromPath<TInput>(inputPath);

if (context.RunStatus != AutomationStatusMapping.Get(AutomationStatus.Succeeded))
{
returnCode = 1; // Flag run as failed.
}
await RunFunction(automateFunction, data.AutomationRunData, data.SpeckleToken, data.FunctionInputs)
.ConfigureAwait(false);
},
pathArg
);
Expand All @@ -135,7 +123,7 @@ public static async Task<int> Main<TInput>(string[] args, Func<AutomationContext
JSchemaGenerator generator = new() { ContractResolver = new CamelCasePropertyNamesContractResolver() };
generator.GenerationProviders.Add(new SpeckleSecretProvider());
JSchema schema = generator.Generate(typeof(TInput));
schema.ToString(global::Newtonsoft.Json.Schema.SchemaVersion.Draft2019_09);
schema.ToString(SchemaVersion.Draft2019_09);
File.WriteAllText(schemaFilePath, schema.ToString());
},
schemaFilePathArg
Expand All @@ -144,7 +132,9 @@ public static async Task<int> Main<TInput>(string[] args, Func<AutomationContext

await rootCommand.InvokeAsync(args).ConfigureAwait(false);

return returnCode;
// if we've gotten this far, the execution should technically be completed as expected
// thus exiting with 0 is the semantically correct thing to do
return 0;
}
}

Expand Down
79 changes: 79 additions & 0 deletions Automate/Speckle.Automate.Sdk/Test/TestAutomateEnvironment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using Speckle.Core.Logging;
using System.Text.Json;

namespace Speckle.Automate.Sdk.Test;

public class TestAppSettings
{
public string? SpeckleToken { get; set; }
public string? SpeckleServerUrl { get; set; }
public string? SpeckleProjectId { get; set; }
public string? SpeckleAutomationId { get; set; }
}

public static class TestAutomateEnvironment
{
public static TestAppSettings? AppSettings { get; private set; }

private static string GetEnvironmentVariable(string environmentVariableName)
{
var value = TryGetEnvironmentVariable(environmentVariableName);

if (value is null)
{
throw new SpeckleException($"Cannot run tests without a {environmentVariableName} environment variable");
}

return value;
}

private static string? TryGetEnvironmentVariable(string environmentVariableName)
{
return Environment.GetEnvironmentVariable(environmentVariableName);
}

private static TestAppSettings? GetAppSettings()
{
if (AppSettings != null)
{
return AppSettings;
}

var path = "./appsettings.json";
var json = File.ReadAllText(path);

var appSettings = JsonSerializer.Deserialize<TestAppSettings>(json);

AppSettings = appSettings;

return AppSettings;
}

public static string GetSpeckleToken()
{
return GetAppSettings()?.SpeckleToken ?? GetEnvironmentVariable("SPECKLE_TOKEN");
}

public static Uri GetSpeckleServerUrl()
{
var urlString =
GetAppSettings()?.SpeckleServerUrl ?? TryGetEnvironmentVariable("SPECKLE_SERVER_URL") ?? "http://127.0.0.1:3000";

return new Uri(urlString);
}

public static string GetSpeckleProjectId()
{
return GetAppSettings()?.SpeckleProjectId ?? GetEnvironmentVariable("SPECKLE_PROJECT_ID");
}

public static string GetSpeckleAutomationId()
{
return GetAppSettings()?.SpeckleAutomationId ?? GetEnvironmentVariable("SPECKLE_AUTOMATION_ID");
}

public static void Clear()
{
AppSettings = null;
}
}
63 changes: 63 additions & 0 deletions Automate/Speckle.Automate.Sdk/Test/TestAutomateUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using GraphQL;
using Speckle.Automate.Sdk.Schema;
using Speckle.Automate.Sdk.Schema.Triggers;
using Speckle.Core.Api;

namespace Speckle.Automate.Sdk.Test;

public static class TestAutomateUtils
{
public static async Task<AutomationRunData> CreateTestRun(Client speckleClient)
{
GraphQLRequest query =
new(
query: """
mutation Mutation($projectId: ID!, $automationId: ID!) {
projectMutations {
automationMutations(projectId: $projectId) {
createTestAutomationRun(automationId: $automationId) {
automationRunId
functionRunId
triggers {
payload {
modelId
versionId
}
triggerType
}
}
}
}
}
""",
variables: new
{
automationId = TestAutomateEnvironment.GetSpeckleAutomationId(),
projectId = TestAutomateEnvironment.GetSpeckleProjectId()
}
);

dynamic res = await speckleClient.ExecuteGraphQLRequest<object>(query).ConfigureAwait(false);

var runData = res["projectMutations"]["automationMutations"]["createTestAutomationRun"];
var triggerData = runData["triggers"][0]["payload"];

string modelId = triggerData["modelId"];
string versionId = triggerData["versionId"];

var data = new AutomationRunData()
{
ProjectId = TestAutomateEnvironment.GetSpeckleProjectId(),
SpeckleServerUrl = TestAutomateEnvironment.GetSpeckleServerUrl().ToString(),
AutomationId = TestAutomateEnvironment.GetSpeckleAutomationId(),
AutomationRunId = runData["automationRunId"],
FunctionRunId = runData["functionRunId"],
Triggers = new List<AutomationRunTriggerBase>()
{
new VersionCreationTrigger(modelId: modelId, versionId: versionId)
}
};

return data;
}
}
3 changes: 2 additions & 1 deletion ConnectorArchicad/ConnectorArchicad/ConnectorBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ public override async Task<string> SendStream(StreamState state, ProgressViewMod
return await Speckle.Core.Api.Helpers.Send(
IdentifyStream(state),
commitObject,
state.CommitMessage,
state.CommitMessage
?? $"Sent {progress.Report.ReportObjects.Count} objects from {HostApplications.Archicad.Name + " " + archicadVersion}.",
HostApplications.Archicad.Name
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
using Microsoft.CSharp.RuntimeBinder;
using Rhino;
using Rhino.Display;
using Rhino.Geometry;
using Speckle.Core.Kits;
Expand All @@ -27,17 +26,9 @@ public static class Utilities
/// Gets the appropriate Grasshopper App name depending on the Version of Rhino currently running.
/// </summary>
/// <remarks>If running in Rhino >7, Rhino7 will be used as fallback.</remarks>
/// <returns><see cref="VersionedHostApplications.Grasshopper7"/> when Rhino 7 is running, <see cref="VersionedHostApplications.Grasshopper6"/> otherwise.</returns>
public static string GetVersionedAppName()
{
var version = HostApplications.Grasshopper.GetVersion(HostAppVersion.v6);
if (RhinoApp.Version.Major >= 7)
{
version = HostApplications.Grasshopper.GetVersion(HostAppVersion.v7);
}

return version;
}
/// <returns><see cref="VersionedHostApplications.Grasshopper7"/> when Rhino 7 is running, <see cref="VersionedHostApplications.Grasshopper8"/> when Rhino 8 is running, <see cref="VersionedHostApplications.Grasshopper6"/> otherwise.</returns>
[Obsolete("Use Loader.GetGrasshopperHostAppVersion instead")]
public static string GetVersionedAppName() => Loader.GetGrasshopperHostAppVersion();

public static ISpeckleConverter GetDefaultConverter()
{
Expand Down
20 changes: 13 additions & 7 deletions ConnectorGrasshopper/ConnectorGrasshopperShared/Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public override GH_LoadingInstruction PriorityLoad()
Setup.Init(GetRhinoHostAppVersion(), HostApplications.Rhino.Slug, logConfig);

Instances.CanvasCreated += OnCanvasCreated;
#if RHINO7

#if RHINO7_OR_GREATER
if (Instances.RunningHeadless)
{
// If GH is running headless, we listen for document added/removed events.
Expand Down Expand Up @@ -492,15 +493,15 @@ public static void KeepOpenOnDropdownCheck(ToolStripMenuItem ctl)

public static void DisposeHeadlessDoc()
{
#if RHINO7
#if RHINO7_OR_GREATER
_headlessDoc?.Dispose();
#endif
_headlessDoc = null;
}

public static void SetupHeadlessDoc()
{
#if RHINO7
#if RHINO7_OR_GREATER
// var templatePath = Path.Combine(Helpers.UserApplicationDataPath, "Speckle", "Templates",
// SpeckleGHSettings.HeadlessTemplateFilename);
// Console.WriteLine($"Setting up doc. Looking for '{templatePath}'");
Expand All @@ -510,7 +511,8 @@ public static void SetupHeadlessDoc()

_headlessDoc = RhinoDoc.CreateHeadless(null);
Console.WriteLine(
$"Headless run with doc '{_headlessDoc.Name ?? "Untitled"}'\n with template: '{_headlessDoc.TemplateFileUsed ?? "No template"}'\n with units: {_headlessDoc.ModelUnitSystem}");
$"Speckle - Backup headless doc is ready: '{_headlessDoc.Name ?? "Untitled"}'\n with template: '{_headlessDoc.TemplateFileUsed ?? "No template"}'\n with units: {_headlessDoc.ModelUnitSystem}");
Console.WriteLine("Speckle - To modify the units in a headless run, you can override the 'RhinoDoc.ActiveDoc' in the '.gh' file using a c#/python script.");
#endif
}

Expand All @@ -521,14 +523,18 @@ public static void SetupHeadlessDoc()
/// <returns></returns>
public static RhinoDoc GetCurrentDocument()
{
#if RHINO7
if (Instances.RunningHeadless && RhinoDoc.ActiveDoc == null)
#if RHINO7_OR_GREATER
if (Instances.RunningHeadless && RhinoDoc.ActiveDoc == null && _headlessDoc != null)
{
// Running headless, with no ActiveDoc override and _headlessDoc was correctly initialised.
// Only time the _headlessDoc is not set is upon document opening, where the components will
// check for this as their normal initialisation routine, but the document will be refreshed on every solution run.
Console.WriteLine(
$"Fetching headless doc '{_headlessDoc.Name ?? "Untitled"}'\n with template: '{_headlessDoc.TemplateFileUsed ?? "No template"}'");
$"Speckle - Fetching headless doc '{_headlessDoc?.Name ?? "Untitled"}'\n with template: '{_headlessDoc.TemplateFileUsed ?? "No template"}'");
Console.WriteLine(" Model units:" + _headlessDoc.ModelUnitSystem);
return _headlessDoc;
}

return RhinoDoc.ActiveDoc;
#else
return RhinoDoc.ActiveDoc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public override void AddedToDocument(GH_Document document)
break;
}
(Params.Output[0] as SpeckleBaseParam).UseSchemaTag = UseSchemaTag;
#if RHINO7
#if RHINO7_OR_GREATER
if (!Instances.RunningHeadless)
{
(Params.Output[0] as SpeckleBaseParam).ExpirePreview(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public override void AddedToDocument(GH_Document document)
}

((SpeckleBaseParam)Params.Output[0]).UseSchemaTag = UseSchemaTag;
#if RHINO7
#if RHINO7_OR_GREATER
if (!Instances.RunningHeadless)
{
(Params.Output[0] as SpeckleBaseParam).ExpirePreview(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ protected override LoadReturnCode OnLoad(ref string errorMessage)

var hostAppName = HostApplications.Rhino.Slug;
var hostAppVersion = Utils.GetRhinoHostAppVersion();
SpeckleLog.Initialize(HostApplications.Rhino.Slug, Utils.GetRhinoHostAppVersion());
SpeckleLog.Initialize(HostApplications.Rhino.Slug, Utils.GetRhinoHostAppVersion(), logConfig);
SpeckleLog.Logger.Information(
"Loading Speckle Plugin for host app {hostAppName} version {hostAppVersion}",
hostAppName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ private static bool IsPreviewIgnore(Base @object)
|| @object.speckle_type.Contains("View")
|| @object.speckle_type.Contains("Level")
|| @object.speckle_type.Contains("GridLine")
|| @object.speckle_type.Contains("Collection");
|| @object.speckle_type.Contains("Collection")
|| @object.speckle_type.Contains("PolygonElement")
|| @object.speckle_type.Contains("GisFeature");
}

public override async Task<StreamState> PreviewReceive(StreamState state, ProgressViewModel progress)
Expand Down
Loading

0 comments on commit 6e6d810

Please sign in to comment.