From 56c73420b8e32a8a604f0214b9543ec4c2799b33 Mon Sep 17 00:00:00 2001 From: Jesus Fernandez Date: Sat, 13 Jul 2024 18:24:33 +0300 Subject: [PATCH] Sample: Tasks (#12) --- PowerAutomate.Desktop.sln | 10 ++++ .../Modules.AssemblyResolution.Actions.csproj | 2 +- samples/Modules.Tasks.Actions/ErrorCodes.cs | 10 ++++ .../Modules.Tasks.Actions.csproj | 16 ++++++ .../Modules.Tasks.Actions/TaskDelayAction.cs | 36 ++++++++++++++ samples/Modules.Tasks.Actions/TaskObject.cs | 49 +++++++++++++++++++ .../TasksWhenAllAction.cs | 33 +++++++++++++ .../TasksWhenAnyAction.cs | 33 +++++++++++++ 8 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 samples/Modules.Tasks.Actions/ErrorCodes.cs create mode 100644 samples/Modules.Tasks.Actions/Modules.Tasks.Actions.csproj create mode 100644 samples/Modules.Tasks.Actions/TaskDelayAction.cs create mode 100644 samples/Modules.Tasks.Actions/TaskObject.cs create mode 100644 samples/Modules.Tasks.Actions/TasksWhenAllAction.cs create mode 100644 samples/Modules.Tasks.Actions/TasksWhenAnyAction.cs diff --git a/PowerAutomate.Desktop.sln b/PowerAutomate.Desktop.sln index cc5af5c..d4e9bb8 100644 --- a/PowerAutomate.Desktop.sln +++ b/PowerAutomate.Desktop.sln @@ -45,6 +45,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AssemblyResolution", "Assem EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Modules.AssemblyResolution.Actions", "samples\Modules.AssemblyResolution.Actions\Modules.AssemblyResolution.Actions.csproj", "{146D4CFB-A54A-4E9F-8473-FC0D73F7BEFD}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Modules.Tasks.Actions", "samples\Modules.Tasks.Actions\Modules.Tasks.Actions.csproj", "{801F4464-7843-4252-9476-E68830781126}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tasks", "Tasks", "{33E32C24-4A59-4DE1-B9C5-57CF051AD355}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -95,6 +99,10 @@ Global {146D4CFB-A54A-4E9F-8473-FC0D73F7BEFD}.Debug|Any CPU.Build.0 = Debug|Any CPU {146D4CFB-A54A-4E9F-8473-FC0D73F7BEFD}.Release|Any CPU.ActiveCfg = Release|Any CPU {146D4CFB-A54A-4E9F-8473-FC0D73F7BEFD}.Release|Any CPU.Build.0 = Release|Any CPU + {801F4464-7843-4252-9476-E68830781126}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {801F4464-7843-4252-9476-E68830781126}.Debug|Any CPU.Build.0 = Debug|Any CPU + {801F4464-7843-4252-9476-E68830781126}.Release|Any CPU.ActiveCfg = Release|Any CPU + {801F4464-7843-4252-9476-E68830781126}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -118,6 +126,8 @@ Global {1D4838E3-22A7-4D65-91CF-2395778EE92F} = {A5E3C936-7C1F-4719-9EAA-37F4AE439EF7} {8EC670BB-BC22-44B4-8228-8F053188C908} = {64A2FE14-920C-4A2F-8F4A-28B77CC84BA4} {146D4CFB-A54A-4E9F-8473-FC0D73F7BEFD} = {8EC670BB-BC22-44B4-8228-8F053188C908} + {801F4464-7843-4252-9476-E68830781126} = {33E32C24-4A59-4DE1-B9C5-57CF051AD355} + {33E32C24-4A59-4DE1-B9C5-57CF051AD355} = {64A2FE14-920C-4A2F-8F4A-28B77CC84BA4} EndGlobalSection GlobalSection(SharedMSBuildProjectFiles) = preSolution Modules.Actions.Shared\Modules.Actions.Shared.projitems*{7e19e2f1-6de7-4a0e-aed8-7cea38b166af}*SharedItemsImports = 13 diff --git a/samples/Modules.AssemblyResolution.Actions/Modules.AssemblyResolution.Actions.csproj b/samples/Modules.AssemblyResolution.Actions/Modules.AssemblyResolution.Actions.csproj index 09afbc9..cded1e0 100644 --- a/samples/Modules.AssemblyResolution.Actions/Modules.AssemblyResolution.Actions.csproj +++ b/samples/Modules.AssemblyResolution.Actions/Modules.AssemblyResolution.Actions.csproj @@ -1,7 +1,7 @@  - GettingStarted + AssemblyResolution net472 latest enable diff --git a/samples/Modules.Tasks.Actions/ErrorCodes.cs b/samples/Modules.Tasks.Actions/ErrorCodes.cs new file mode 100644 index 0000000..dd380f3 --- /dev/null +++ b/samples/Modules.Tasks.Actions/ErrorCodes.cs @@ -0,0 +1,10 @@ +// -------------------------------------------------------------- +// Copyright (c) Jesus Fernandez. All Rights Reserved. +// -------------------------------------------------------------- + +namespace PowerAutomate.Desktop.Modules.Tasks.Actions; + +internal static class ErrorCodes +{ + public const string Unknown = "UnknownError"; +} \ No newline at end of file diff --git a/samples/Modules.Tasks.Actions/Modules.Tasks.Actions.csproj b/samples/Modules.Tasks.Actions/Modules.Tasks.Actions.csproj new file mode 100644 index 0000000..2e586f4 --- /dev/null +++ b/samples/Modules.Tasks.Actions/Modules.Tasks.Actions.csproj @@ -0,0 +1,16 @@ + + + + Tasks + net472 + latest + enable + true + + + + + + + + diff --git a/samples/Modules.Tasks.Actions/TaskDelayAction.cs b/samples/Modules.Tasks.Actions/TaskDelayAction.cs new file mode 100644 index 0000000..a1ef77f --- /dev/null +++ b/samples/Modules.Tasks.Actions/TaskDelayAction.cs @@ -0,0 +1,36 @@ +// -------------------------------------------------------------- +// Copyright (c) Jesus Fernandez. All Rights Reserved. +// -------------------------------------------------------------- + +using System; +using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK; +using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK.Attributes; + +namespace PowerAutomate.Desktop.Modules.Tasks.Actions; + +[Action(Id = "Delay")] +[Throws(ErrorCodes.Unknown)] +public class TaskDelayAction : ActionBase +{ + [InputArgument(Order = 2)] + public int DelayInMilliseconds { get; set; } + + [InputArgument(Order = 1)] + public string Name { get; set; } = null!; + + [OutputArgument(Order = 0)] + public TaskObject Task { get; set; } = null!; + + public override void Execute(ActionContext context) + { + try + { + var task = System.Threading.Tasks.Task.Delay(DelayInMilliseconds); + Task = new TaskObject(Name, task); + } + catch (Exception ex) + { + throw new ActionException(ErrorCodes.Unknown, ex.Message, ex); + } + } +} \ No newline at end of file diff --git a/samples/Modules.Tasks.Actions/TaskObject.cs b/samples/Modules.Tasks.Actions/TaskObject.cs new file mode 100644 index 0000000..a66aad1 --- /dev/null +++ b/samples/Modules.Tasks.Actions/TaskObject.cs @@ -0,0 +1,49 @@ +// -------------------------------------------------------------- +// Copyright (c) Jesus Fernandez. All Rights Reserved. +// -------------------------------------------------------------- + +using System; +using System.Threading.Tasks; +using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK.Attributes; +using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK.Enums; +using Newtonsoft.Json; + +namespace PowerAutomate.Desktop.Modules.Tasks.Actions; + +[JsonObject(MemberSerialization.OptOut)] +[Type(DefaultPropertyVisibility = Visibility.Visible)] +public class TaskObject : IComparable, IComparable +{ + public string Name { get; private set; } = null!; + internal Task Task { get; private set; } = null!; + + [JsonConstructor] + public TaskObject() + { + } + + internal TaskObject(string name, Task task) + { + Name = name ?? throw new ArgumentNullException(nameof(name)); + Task = task ?? throw new ArgumentNullException(nameof(task)); + } + + public int CompareTo(object? obj) + { + if (ReferenceEquals(null, obj)) return 1; + if (ReferenceEquals(this, obj)) return 0; + return obj is TaskObject other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TaskObject)}"); + } + + public int CompareTo(TaskObject? other) + { + if (ReferenceEquals(this, other)) return 0; + if (ReferenceEquals(null, other)) return 1; + return string.Compare(Name, other.Name, StringComparison.InvariantCultureIgnoreCase); + } + + public override string ToString() + { + return Name; + } +} \ No newline at end of file diff --git a/samples/Modules.Tasks.Actions/TasksWhenAllAction.cs b/samples/Modules.Tasks.Actions/TasksWhenAllAction.cs new file mode 100644 index 0000000..639406a --- /dev/null +++ b/samples/Modules.Tasks.Actions/TasksWhenAllAction.cs @@ -0,0 +1,33 @@ +// -------------------------------------------------------------- +// Copyright (c) Jesus Fernandez. All Rights Reserved. +// -------------------------------------------------------------- + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK; +using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK.Attributes; + +namespace PowerAutomate.Desktop.Modules.Tasks.Actions; + +[Action(Id = "WhenAll")] +[Throws(ErrorCodes.Unknown)] +public class TasksWhenAllAction : ActionBase +{ + [InputArgument] + public List Tasks { get; set; } = null!; + + public override void Execute(ActionContext context) + { + try + { + var tasks = Tasks.Select(task => task.Task).ToList(); + Task.WhenAll(tasks).GetAwaiter().GetResult(); + } + catch (Exception ex) + { + throw new ActionException(ErrorCodes.Unknown, ex.Message, ex); + } + } +} \ No newline at end of file diff --git a/samples/Modules.Tasks.Actions/TasksWhenAnyAction.cs b/samples/Modules.Tasks.Actions/TasksWhenAnyAction.cs new file mode 100644 index 0000000..4447d40 --- /dev/null +++ b/samples/Modules.Tasks.Actions/TasksWhenAnyAction.cs @@ -0,0 +1,33 @@ +// -------------------------------------------------------------- +// Copyright (c) Jesus Fernandez. All Rights Reserved. +// -------------------------------------------------------------- + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK; +using Microsoft.PowerPlatform.PowerAutomate.Desktop.Actions.SDK.Attributes; + +namespace PowerAutomate.Desktop.Modules.Tasks.Actions; + +[Action(Id = "WhenAny")] +[Throws(ErrorCodes.Unknown)] +public class TasksWhenAnyAction : ActionBase +{ + [InputArgument] + public List Tasks { get; set; } = null!; + + public override void Execute(ActionContext context) + { + try + { + var tasks = Tasks.Select(task => task.Task).ToList(); + Task.WhenAny(tasks).GetAwaiter().GetResult(); + } + catch (Exception ex) + { + throw new ActionException(ErrorCodes.Unknown, ex.Message, ex); + } + } +} \ No newline at end of file