From 062e08b1b228f3b1055f55c80e84dd2a7321140b Mon Sep 17 00:00:00 2001 From: Jedd Morgan <45512892+JR-Morgan@users.noreply.github.com> Date: Wed, 28 May 2025 23:37:47 +0100 Subject: [PATCH] First pass --- SpeckleAutomateDotnetExample.sln | 6 +++ .../AutomateFunction.cs | 17 ++++---- .../FunctionInputs.cs | 10 ++--- SpeckleAutomateDotnetExample/Program.cs | 15 +++++-- .../SpeckleAutomateDotnetExample.csproj | 7 ++-- TestAutomateFunction/AutomationContextTest.cs | 40 +++++++++++-------- TestAutomateFunction/ServiceRegistration.cs | 15 +++++++ .../TestAutomateFunction.csproj | 2 +- TestAutomateFunction/Usings.cs | 2 +- 9 files changed, 76 insertions(+), 38 deletions(-) create mode 100644 TestAutomateFunction/ServiceRegistration.cs diff --git a/SpeckleAutomateDotnetExample.sln b/SpeckleAutomateDotnetExample.sln index ac801c6..aca030a 100644 --- a/SpeckleAutomateDotnetExample.sln +++ b/SpeckleAutomateDotnetExample.sln @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpeckleAutomateDotnetExampl EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestAutomateFunction", "TestAutomateFunction\TestAutomateFunction.csproj", "{8107A920-A5E2-459C-9756-04B91FB63F3F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Speckle.Automate.Sdk", "..\speckle-sharp-sdk\Speckle.Automate.Sdk\Speckle.Automate.Sdk.csproj", "{8DEBC0DE-0893-4595-A268-42F90655D0F1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +23,10 @@ Global {8107A920-A5E2-459C-9756-04B91FB63F3F}.Debug|Any CPU.Build.0 = Debug|Any CPU {8107A920-A5E2-459C-9756-04B91FB63F3F}.Release|Any CPU.ActiveCfg = Release|Any CPU {8107A920-A5E2-459C-9756-04B91FB63F3F}.Release|Any CPU.Build.0 = Release|Any CPU + {8DEBC0DE-0893-4595-A268-42F90655D0F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8DEBC0DE-0893-4595-A268-42F90655D0F1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8DEBC0DE-0893-4595-A268-42F90655D0F1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8DEBC0DE-0893-4595-A268-42F90655D0F1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SpeckleAutomateDotnetExample/AutomateFunction.cs b/SpeckleAutomateDotnetExample/AutomateFunction.cs index 4b583e2..8e90c10 100644 --- a/SpeckleAutomateDotnetExample/AutomateFunction.cs +++ b/SpeckleAutomateDotnetExample/AutomateFunction.cs @@ -1,16 +1,14 @@ -using Objects; using Speckle.Automate.Sdk; -using Speckle.Core.Models.Extensions; +using Speckle.Sdk.Models.Extensions; -public static class AutomateFunction +public class AutomateFunction { - public static async Task Run( - AutomationContext automationContext, + public async Task Run( + IAutomationContext automationContext, FunctionInputs functionInputs ) { Console.WriteLine("Starting execution"); - _ = typeof(ObjectsKit).Assembly; // INFO: Force objects kit to initialize Console.WriteLine("Receiving version"); var commitObject = await automationContext.ReceiveVersion(); @@ -23,8 +21,11 @@ public static class AutomateFunction Console.WriteLine($"Counted {count} objects"); - if (count < functionInputs.SpeckleTypeTargetCount) { - automationContext.MarkRunFailed($"Counted {count} objects where {functionInputs.SpeckleTypeTargetCount} were expected"); + if (count < functionInputs.SpeckleTypeTargetCount) + { + automationContext.MarkRunFailed( + $"Counted {count} objects where {functionInputs.SpeckleTypeTargetCount} were expected" + ); return; } diff --git a/SpeckleAutomateDotnetExample/FunctionInputs.cs b/SpeckleAutomateDotnetExample/FunctionInputs.cs index 29b7b88..5140427 100644 --- a/SpeckleAutomateDotnetExample/FunctionInputs.cs +++ b/SpeckleAutomateDotnetExample/FunctionInputs.cs @@ -1,19 +1,19 @@ -using Speckle.Automate.Sdk.DataAnnotations; using System.ComponentModel; using System.ComponentModel.DataAnnotations; +using Speckle.Automate.Sdk.DataAnnotations; /// /// This class describes the user specified variables that the function wants to work with. /// /// This class is used to generate a JSON Schema to ensure that the user provided values /// are valid and match the required schema. -public struct FunctionInputs +public readonly struct FunctionInputs { /// /// The object type to count instances of in the given model version. /// [Required] - public string SpeckleTypeToCount; + public string SpeckleTypeToCount { get; init; } /// /// The total number of the specified type expected. @@ -21,12 +21,12 @@ public struct FunctionInputs [DefaultValue(10)] [Range(1, 100)] [Required] - public int SpeckleTypeTargetCount; + public int SpeckleTypeTargetCount { get; init; } /// /// An arbitrary example of using a secret input value. /// [Required] [Secret] - public string ExternalServiceKey; + public string ExternalServiceKey { get; init; } } diff --git a/SpeckleAutomateDotnetExample/Program.cs b/SpeckleAutomateDotnetExample/Program.cs index 0bac59a..84534cf 100644 --- a/SpeckleAutomateDotnetExample/Program.cs +++ b/SpeckleAutomateDotnetExample/Program.cs @@ -1,6 +1,13 @@ -using Speckle.Automate.Sdk; +using Microsoft.Extensions.DependencyInjection; +using Speckle.Automate.Sdk; + +var serviceCollection = new ServiceCollection(); +serviceCollection.AddAutomateSdk(); +serviceCollection.AddSingleton(); +await using var container = serviceCollection.BuildServiceProvider(); + +var runner = container.GetRequiredService(); +var function = container.GetRequiredService(); // WARNING do not delete this call, this is the actual execution of your function -return await AutomationRunner - .Main(args, AutomateFunction.Run) - .ConfigureAwait(false); +return await runner.Main(args, function.Run); diff --git a/SpeckleAutomateDotnetExample/SpeckleAutomateDotnetExample.csproj b/SpeckleAutomateDotnetExample/SpeckleAutomateDotnetExample.csproj index f1b4a10..427c771 100644 --- a/SpeckleAutomateDotnetExample/SpeckleAutomateDotnetExample.csproj +++ b/SpeckleAutomateDotnetExample/SpeckleAutomateDotnetExample.csproj @@ -2,14 +2,15 @@ Exe - net7.0 + net8.0 enable enable - - + + + diff --git a/TestAutomateFunction/AutomationContextTest.cs b/TestAutomateFunction/AutomationContextTest.cs index 692a691..9eb9853 100644 --- a/TestAutomateFunction/AutomationContextTest.cs +++ b/TestAutomateFunction/AutomationContextTest.cs @@ -1,27 +1,35 @@ -namespace TestAutomateFunction; - +using Microsoft.Extensions.DependencyInjection; using Speckle.Automate.Sdk; using Speckle.Automate.Sdk.Test; -using Speckle.Core.Api; -using Speckle.Core.Api.GraphQL.Models; -using Speckle.Core.Credentials; +using Speckle.Sdk.Api; +using Speckle.Sdk.Api.GraphQL.Models; +using Speckle.Sdk.Credentials; + +namespace TestAutomateFunction; [TestFixture] public sealed class AutomationContextTest : IDisposable { - - private Client client; - private Account account; + private IClient _client; + private Account _account; + private IAutomationRunner _runner; + private AutomateFunction _function; [OneTimeSetUp] public void Setup() { - account = new Account + var serviceProvider = ServiceRegistration.GetServiceProvider(); + _account = new Account { token = TestAutomateEnvironment.GetSpeckleToken(), - serverInfo = new ServerInfo { url = TestAutomateEnvironment.GetSpeckleServerUrl().ToString() } + serverInfo = new ServerInfo + { + url = TestAutomateEnvironment.GetSpeckleServerUrl().ToString() + } }; - client = new Client(account); + _client = serviceProvider.GetRequiredService().Create(_account); + _runner = serviceProvider.GetRequiredService(); + _function = serviceProvider.GetRequiredService(); } [Test] @@ -33,11 +41,11 @@ public sealed class AutomationContextTest : IDisposable SpeckleTypeTargetCount = 1 }; - var automationRunData = await TestAutomateUtils.CreateTestRun(client); - var automationContext = await AutomationRunner.RunFunction( - AutomateFunction.Run, + var automationRunData = await TestAutomateUtils.CreateTestRun(_client); + var automationContext = await _runner.RunFunction( + _function.Run, automationRunData, - account.token, + _account.token, inputs ); @@ -46,7 +54,7 @@ public sealed class AutomationContextTest : IDisposable public void Dispose() { - client.Dispose(); + _client.Dispose(); TestAutomateEnvironment.Clear(); } } diff --git a/TestAutomateFunction/ServiceRegistration.cs b/TestAutomateFunction/ServiceRegistration.cs new file mode 100644 index 0000000..5ba583f --- /dev/null +++ b/TestAutomateFunction/ServiceRegistration.cs @@ -0,0 +1,15 @@ +using Microsoft.Extensions.DependencyInjection; +using Speckle.Automate.Sdk; + +namespace TestAutomateFunction; + +public static class ServiceRegistration +{ + public static IServiceProvider GetServiceProvider() + { + var serviceCollection = new ServiceCollection(); + serviceCollection.AddAutomateSdk(); + serviceCollection.AddSingleton(); + return serviceCollection.BuildServiceProvider(); + } +} diff --git a/TestAutomateFunction/TestAutomateFunction.csproj b/TestAutomateFunction/TestAutomateFunction.csproj index 10b7954..9bbf298 100644 --- a/TestAutomateFunction/TestAutomateFunction.csproj +++ b/TestAutomateFunction/TestAutomateFunction.csproj @@ -1,7 +1,7 @@ - net7.0 + net8.0 enable enable diff --git a/TestAutomateFunction/Usings.cs b/TestAutomateFunction/Usings.cs index cefced4..3244567 100644 --- a/TestAutomateFunction/Usings.cs +++ b/TestAutomateFunction/Usings.cs @@ -1 +1 @@ -global using NUnit.Framework; \ No newline at end of file +global using NUnit.Framework;