Bladeren bron

Factoring out script functionality to be global for posters

Kenric Nugteren 1 jaar geleden
bovenliggende
commit
7155970dd3

+ 7 - 0
InABox.Core/Postable/PostResult.cs

@@ -45,6 +45,13 @@ namespace InABox.Core
             posts.Add(post);
         }
 
+        public void AddFragment<TPostableFragment>(TPostableFragment fragment, PostedStatus status)
+            where TPostableFragment : IPostableFragment<TPostable>, IPostable
+        {
+            fragment.Post();
+            fragment.PostedStatus = status;
+        }
+
         public void AddFragment(IPostableFragment<TPostable> fragment)
         {
             var type = fragment.GetType();

+ 12 - 0
InABox.Core/Utils/Result.cs

@@ -21,6 +21,18 @@ namespace InABox.Core
             return new OkResult<T>(value);
         }
 
+        public static Result<E> Flatten<E>(this Result<Result<E>, E> result)
+        {
+            if(result.Get(out var inner, out var error))
+            {
+                return inner;
+            }
+            else
+            {
+                return Result.Error(error);
+            }
+        }
+
         public static Result<T, E> Flatten<T, E>(this Result<Result<T, E>, E> result)
         {
             if(result.Get(out var inner, out var error))

+ 2 - 29
InABox.Poster.Custom/CustomPosterEngine.cs

@@ -1,4 +1,5 @@
 using InABox.Core;
+using InABox.Poster.Shared;
 using InABox.Scripting;
 using System;
 using System.Collections.Generic;
@@ -8,37 +9,9 @@ using System.Threading.Tasks;
 
 namespace InABox.Poster.Custom;
 
-public class CustomPosterEngine<TPostable> : PosterEngine<TPostable, ICustomPoster<TPostable>, CustomPosterSettings>
+public class CustomPosterEngine<TPostable> : BasePosterEngine<TPostable, ICustomPoster<TPostable>, CustomPosterSettings>
     where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
 {
-    private ScriptDocument? _script;
-    private bool _hasCheckedScript;
-
-    private ScriptDocument? GetScriptDocument()
-    {
-        if (_hasCheckedScript)
-        {
-            return _script;
-        }
-
-        var settings = GetSettings();
-        if (settings.ScriptEnabled && !string.IsNullOrWhiteSpace(settings.Script))
-        {
-            var document = new ScriptDocument(settings.Script);
-            if (!document.Compile())
-            {
-                throw new Exception("Script failed to compile!");
-            }
-            _script = document;
-        }
-        else
-        {
-            _script = null;
-        }
-
-        _hasCheckedScript = true;
-        return _script;
-    }
 
     public override bool BeforePost(IDataModel<TPostable> model)
     {

+ 1 - 0
InABox.Poster.Custom/InABox.Poster.Custom.csproj

@@ -8,6 +8,7 @@
 
   <ItemGroup>
     <ProjectReference Include="..\InABox.Core\InABox.Core.csproj" />
+    <ProjectReference Include="..\InABox.Poster.Shared\InABox.Poster.Shared.csproj" />
     <ProjectReference Include="..\inabox.scripting\InABox.Scripting.csproj" />
   </ItemGroup>
 

+ 4 - 1
InABox.Poster.MYOB/IMYOBPoster.cs

@@ -1,5 +1,6 @@
 using InABox.Core;
 using InABox.Core.Postable;
+using InABox.Scripting;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -13,7 +14,9 @@ public interface IMYOBPoster<TPostable, TSettings> : IPoster<TPostable, TSetting
     where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
     where TSettings : MYOBPosterSettings
 {
-    public MYOBConnectionData ConnectionData { get; set; }
+    ScriptDocument? Script { get; set; }
+
+    MYOBConnectionData ConnectionData { get; set; }
 
     bool BeforePost(IDataModel<TPostable> model);
 

+ 1 - 0
InABox.Poster.MYOB/InABox.Poster.MYOB.csproj

@@ -19,6 +19,7 @@
 
   <ItemGroup>
     <ProjectReference Include="..\InABox.Core\InABox.Core.csproj" />
+    <ProjectReference Include="..\InABox.Poster.Shared\InABox.Poster.Shared.csproj" />
     <ProjectReference Include="..\inabox.wpf\InABox.Wpf.csproj" />
   </ItemGroup>
 

+ 9 - 1
InABox.Poster.MYOB/MYOBPosterEngine.cs

@@ -1,6 +1,7 @@
 using InABox.Core;
 using InABox.Core.Postable;
 using InABox.DynamicGrid;
+using InABox.Poster.Shared;
 using Microsoft.Web.WebView2.Wpf;
 using MYOB.AccountRight.SDK;
 using MYOB.AccountRight.SDK.Contracts;
@@ -133,7 +134,7 @@ public static partial class MYOBPosterEngine
 }
 
 public abstract class MYOBPosterEngine<TPostable, TSettings> :
-    PosterEngine<TPostable, IMYOBPoster<TPostable, TSettings>, TSettings>,
+    BasePosterEngine<TPostable, IMYOBPoster<TPostable, TSettings>, TSettings>,
     IGlobalSettingsPosterEngine<IMYOBPoster<TPostable, TSettings>, MYOBGlobalPosterSettings>
 
     where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
@@ -145,6 +146,13 @@ public abstract class MYOBPosterEngine<TPostable, TSettings> :
     private void SaveGlobalSettings(MYOBGlobalPosterSettings settings) =>
         (this as IGlobalSettingsPosterEngine<IMYOBPoster<TPostable, TSettings>, MYOBGlobalPosterSettings>).SaveGlobalSettings(settings);
 
+    protected override IMYOBPoster<TPostable, TSettings> CreatePoster()
+    {
+        var poster = base.CreatePoster();
+        poster.Script = GetScriptDocument();
+        return poster;
+    }
+
     public override bool BeforePost(IDataModel<TPostable> model)
     {
         return Poster.BeforePost(model);

+ 28 - 0
InABox.Poster.MYOB/MYOBPosterUtils.cs

@@ -1,5 +1,6 @@
 using InABox.Core;
 using InABox.Core.Postable;
+using InABox.Poster.Shared;
 using MYOB.AccountRight.SDK;
 using MYOB.AccountRight.SDK.Contracts.Version2;
 using MYOB.AccountRight.SDK.Services;
@@ -196,4 +197,31 @@ public static class MYOBPosterUtils
     {
         return GetDefaultTaxCode(poster.ConnectionData, poster.GlobalSettings);
     }
+
+    public static Result<Exception> WrapScript<TPostable, TSettings>(
+        this IMYOBPoster<TPostable, TSettings> poster,
+        string methodname,
+        params object?[] parameters
+    )
+        where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
+        where TSettings : MYOBPosterSettings
+    {
+        if (poster.Script is null) return Result.Ok();
+
+        try
+        {
+            if(poster.Script.Execute(methodname: methodname, parameters: parameters))
+            {
+                return Result.Ok();
+            }
+            else
+            {
+                throw new PostCancelledException();
+            }
+        }
+        catch(Exception e)
+        {
+            return Result.Error(e);
+        }
+    }
 }

+ 44 - 0
InABox.Poster.Shared/BasePosterEngine.cs

@@ -0,0 +1,44 @@
+using InABox.Core;
+using InABox.Scripting;
+
+namespace InABox.Poster.Shared;
+
+public abstract class BasePosterEngine<TPostable, TPoster, TSettings> : PosterEngine<TPostable, TPoster, TSettings>
+    where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
+    where TPoster : class, IPoster<TPostable, TSettings>
+    where TSettings : PosterSettings, new()
+{
+    private ScriptDocument? _script;
+    private bool _hasCheckedScript;
+
+    protected ScriptDocument? GetScriptDocument()
+    {
+        if (_hasCheckedScript)
+        {
+            return _script;
+        }
+
+        var settings = GetSettings();
+        _script = GetScriptDocument(settings);
+
+        _hasCheckedScript = true;
+        return _script;
+    }
+
+    public static ScriptDocument? GetScriptDocument(TSettings settings)
+    {
+        if (settings.ScriptEnabled && !string.IsNullOrWhiteSpace(settings.Script))
+        {
+            var document = new ScriptDocument(settings.Script);
+            if (!document.Compile())
+            {
+                throw new Exception("Script failed to compile!");
+            }
+            return document;
+        }
+        else
+        {
+            return null;
+        }
+    }
+}

+ 14 - 0
InABox.Poster.Shared/InABox.Poster.Shared.csproj

@@ -0,0 +1,14 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFramework>net8.0</TargetFramework>
+    <ImplicitUsings>enable</ImplicitUsings>
+    <Nullable>enable</Nullable>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\InABox.Core\InABox.Core.csproj" />
+    <ProjectReference Include="..\inabox.scripting\InABox.Scripting.csproj" />
+  </ItemGroup>
+
+</Project>

+ 1 - 0
InABox.Poster.Timberline/InABox.Poster.Timberline.csproj

@@ -12,6 +12,7 @@
 
   <ItemGroup>
     <ProjectReference Include="..\InABox.Core\InABox.Core.csproj" />
+    <ProjectReference Include="..\InABox.Poster.Shared\InABox.Poster.Shared.csproj" />
     <ProjectReference Include="..\inabox.scripting\InABox.Scripting.csproj" />
   </ItemGroup>
 

+ 2 - 31
InABox.Poster.Timberline/TimberlinePosterEngine.cs

@@ -1,5 +1,6 @@
 using InABox.Core;
 using InABox.Core.Postable;
+using InABox.Poster.Shared;
 using InABox.Scripting;
 using System.Collections.Generic;
 using System.Formats.Asn1;
@@ -10,14 +11,10 @@ using System.Threading.Tasks;
 
 namespace InABox.Poster.Timberline;
 
-public class TimberlinePosterEngine<TPostable, TSettings> : PosterEngine<TPostable, ITimberlinePoster<TPostable, TSettings>, TSettings>
+public class TimberlinePosterEngine<TPostable, TSettings> : BasePosterEngine<TPostable, ITimberlinePoster<TPostable, TSettings>, TSettings>
     where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
     where TSettings : TimberlinePosterSettings<TPostable>, new()
 {
-
-    private ScriptDocument? _script;
-    private bool _hasCheckedScript;
-
     protected override ITimberlinePoster<TPostable, TSettings> CreatePoster()
     {
         var poster = base.CreatePoster();
@@ -25,32 +22,6 @@ public class TimberlinePosterEngine<TPostable, TSettings> : PosterEngine<TPostab
         return poster;
     }
 
-    private ScriptDocument? GetScriptDocument()
-    {
-        if (_hasCheckedScript)
-        {
-            return _script;
-        }
-
-        var settings = GetSettings();
-        if (settings.ScriptEnabled && !string.IsNullOrWhiteSpace(settings.Script))
-        {
-            var document = new ScriptDocument(settings.Script);
-            if (!document.Compile())
-            {
-                throw new Exception("Script failed to compile!");
-            }
-            _script = document;
-        }
-        else
-        {
-            _script = null;
-        }
-
-        _hasCheckedScript = true;
-        return _script;
-    }
-
     public override bool BeforePost(IDataModel<TPostable> model)
     {
         return Poster.BeforePost(model);

+ 3 - 3
inabox.scripting/ScriptDocument.cs

@@ -245,7 +245,7 @@ public class ScriptDocument : INotifyPropertyChanged
         }
     }
 
-    public bool Execute(string classname = "Module", string methodname = "Execute", object[]? parameters = null, bool defaultResult = false)
+    public bool Execute(string classname = "Module", string methodname = "Execute", object?[]? parameters = null, bool defaultResult = false)
     {
         var result = defaultResult;
 
@@ -263,11 +263,11 @@ public class ScriptDocument : INotifyPropertyChanged
 
             if (method.ReturnType == typeof(bool))
             {
-                result = (bool)(method.Invoke(obj, parameters ?? Array.Empty<object>()) ?? false);
+                result = (bool)(method.Invoke(obj, parameters ?? []) ?? false);
             }
             else
             {
-                method.Invoke(obj, parameters ?? Array.Empty<object>());
+                method.Invoke(obj, parameters ?? []);
                 result = true;
             }