diff --git a/Extensions/CommonExtensions.cs b/Extensions/CommonExtensions.cs index 22d9153..ed5d881 100644 --- a/Extensions/CommonExtensions.cs +++ b/Extensions/CommonExtensions.cs @@ -9,6 +9,7 @@ using System.Runtime.Serialization; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; +using System.Threading; using System.Threading.Tasks; using System.Web; using System.Xml; @@ -582,6 +583,18 @@ namespace xCommons.Extensions return Encoding.UTF8.GetBytes(source); } + /// + /// Converts Byte Array to String Value ... + /// + /// + /// + public static string FromBytes(this byte[] source) + { + // + var result = Encoding.UTF8.GetString(source, 0, source.Length); + return result; + } + /// /// Check object is Null ... /// @@ -624,7 +637,7 @@ namespace xCommons.Extensions { return !source.IsNull() && source.Count() > 0; } - + /// /// Select Async ... /// @@ -640,6 +653,29 @@ namespace xCommons.Extensions return await Task.WhenAll(source.Select(async s => await method(s))); } + /// + /// Converts an Enumerable to Async Enumerable ... + /// + /// + /// + /// + /// + public static async IAsyncEnumerable ToAsyncEnumerable( + this IEnumerable source, + [System.Runtime.CompilerServices.EnumeratorCancellation] + CancellationToken cancellationToken = default + ) + { + // + foreach (var item in source) + { + // + cancellationToken.ThrowIfCancellationRequested(); + yield return item; + await Task.Yield(); // keeps method truly async + } + } + /// /// Update a Collection item ... /// @@ -1147,7 +1183,7 @@ namespace xCommons.Extensions // return result; } - + /// /// Converts a List String Representation to GUid List ... /// diff --git a/Helpers/XDateHelper.cs b/Helpers/XDateHelper.cs index 6307551..b6b2bf1 100644 --- a/Helpers/XDateHelper.cs +++ b/Helpers/XDateHelper.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace xCommons.Helpers { public partial class XDateHelper { diff --git a/Helpers/XNetworkHelper.cs b/Helpers/XNetworkHelper.cs index 7daeff0..31680f7 100644 --- a/Helpers/XNetworkHelper.cs +++ b/Helpers/XNetworkHelper.cs @@ -1,10 +1,8 @@ -using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; -using System.Threading.Tasks; using xCommons.Models; namespace xCommons.Helpers { diff --git a/Helpers/XOsHelper.cs b/Helpers/XOsHelper.cs new file mode 100644 index 0000000..e3aef8f --- /dev/null +++ b/Helpers/XOsHelper.cs @@ -0,0 +1,178 @@ +using System; +using System.Diagnostics; +using System.Runtime.InteropServices; +using System.Threading.Tasks; +using xCommons.Extensions; +using xExceptions.Attributes; +using xExceptions.Constants; + +namespace xCommons.Helpers +{ + /// + /// Several OS Type ... + /// + public enum XOSType + { + [StringValue("Unknown")] + XUnknown, + + [StringValue("Mac")] + XMacOs, + + [StringValue("Linux")] + XLinux, + + [StringValue("Windows")] + XWindows, + + [StringValue("FreeBSD")] + XFreeBSD, + } + + /// + /// OS Command Execution Result ... + /// + public class XOSCMDResult + { + /// + /// Execution Result ... + /// + /// + public string Result { get; set; } + + /// + /// Execution Error ... + /// + /// + public string Error { get; set; } + } + + /// + /// a Helper Class which Provides OS Command Executions ... + /// + public static partial class XOsHelper + { + /// + /// Retrieve OS Type ... + /// + /// + public static XOSType GetOSType() + { + // + XOSType result = XOSType.XUnknown; + + // + if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + result = XOSType.XMacOs; + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + result = XOSType.XLinux; + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + result = XOSType.XWindows; + } + // else if (RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD)) + // { + // result = XOSType.XFreeBSD; + // } + + // + return result; + } + + /// + /// Retrieve OS Description ... + /// + /// + public static string GetOSDescription() + { + // + var result = RuntimeInformation.OSDescription; + return result; + } + + /// + /// Execute an Specified Command ... + /// + /// Specified Command + /// Command Arguments + /// + public static async Task Execute( + string cmd, + string args + ) + { + // + var result = new XOSCMDResult + { + Error = "Unknown", + }; + + // + try + { + // + // Validate CMD ... + var isValid = !cmd.IsNullOrEmpty(); + if (!isValid) + { + XException.InvalidArgs.Throw(); + } + + // + // Validate OS Type ... + XOSType osType = GetOSType(); + isValid = osType != XOSType.XUnknown; + if (!isValid) + { + XException.InvalidArgs.Throw(); + } + + // + return await Task.Run(() => + { + // + ProcessStartInfo psi = new ProcessStartInfo(); + + // + psi.FileName = cmd; + psi.Arguments = args; + psi.UseShellExecute = false; + psi.RedirectStandardError = true; + psi.RedirectStandardOutput = true; + + // + Process process = Process.Start(psi); + + // + // Read the output + string output = process.StandardOutput.ReadToEnd(); + string error = process.StandardError.ReadToEnd(); + + // + process.WaitForExit(); + + // + result = new XOSCMDResult + { + Error = error, + Result = output, + }; + + // + return result; + }); + } + catch (Exception ex) + { + result.Error = ex.Message; + } + + // + return result; + } + } +} \ No newline at end of file diff --git a/xCommons.csproj b/xCommons.csproj index f669239..b429f17 100644 --- a/xCommons.csproj +++ b/xCommons.csproj @@ -1,12 +1,12 @@ - - netstandard2.0 - xDashboard.xCommons 1.0.0 + 8.0 Hadi Khazaee Asl SaherElm IT Center + xDashboard.xCommons + netstandard2.0 provide all commonly used actions/extensions and classes which required to xDashboard project. @@ -20,9 +20,12 @@ + + + + - @@ -49,8 +52,8 @@ - + @@ -59,5 +62,4 @@ true $(NoWarn);1591 - \ No newline at end of file