refactor csproj and add os helper class and some refactors ...

This commit is contained in:
2026-04-14 16:34:52 +03:30
parent ddc0b4d0f4
commit 7014cd1200
5 changed files with 224 additions and 13 deletions
+38 -2
View File
@@ -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);
}
/// <summary>
/// Converts Byte Array to String Value ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static string FromBytes(this byte[] source)
{
//
var result = Encoding.UTF8.GetString(source, 0, source.Length);
return result;
}
/// <summary>
/// Check object is Null ...
/// </summary>
@@ -624,7 +637,7 @@ namespace xCommons.Extensions
{
return !source.IsNull() && source.Count() > 0;
}
/// <summary>
/// Select Async ...
/// </summary>
@@ -640,6 +653,29 @@ namespace xCommons.Extensions
return await Task.WhenAll(source.Select(async s => await method(s)));
}
/// <summary>
/// Converts an Enumerable to Async Enumerable ...
/// </summary>
/// <param name="source"></param>
/// <param name="cancellationToken"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static async IAsyncEnumerable<T> ToAsyncEnumerable<T>(
this IEnumerable<T> 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
}
}
/// <summary>
/// Update a Collection item ...
/// </summary>
@@ -1147,7 +1183,7 @@ namespace xCommons.Extensions
//
return result;
}
/// <summary>
/// Converts a List String Representation to GUid List ...
/// </summary>
-3
View File
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace xCommons.Helpers {
public partial class XDateHelper {
-2
View File
@@ -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 {
+178
View File
@@ -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
{
/// <summary>
/// Several OS Type ...
/// </summary>
public enum XOSType
{
[StringValue("Unknown")]
XUnknown,
[StringValue("Mac")]
XMacOs,
[StringValue("Linux")]
XLinux,
[StringValue("Windows")]
XWindows,
[StringValue("FreeBSD")]
XFreeBSD,
}
/// <summary>
/// OS Command Execution Result ...
/// </summary>
public class XOSCMDResult
{
/// <summary>
/// Execution Result ...
/// </summary>
/// <value></value>
public string Result { get; set; }
/// <summary>
/// Execution Error ...
/// </summary>
/// <value></value>
public string Error { get; set; }
}
/// <summary>
/// a Helper Class which Provides OS Command Executions ...
/// </summary>
public static partial class XOsHelper
{
/// <summary>
/// Retrieve OS Type ...
/// </summary>
/// <returns></returns>
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;
}
/// <summary>
/// Retrieve OS Description ...
/// </summary>
/// <returns></returns>
public static string GetOSDescription()
{
//
var result = RuntimeInformation.OSDescription;
return result;
}
/// <summary>
/// Execute an Specified Command ...
/// </summary>
/// <param name="cmd">Specified Command</param>
/// <param name="args">Command Arguments</param>
/// <returns></returns>
public static async Task<XOSCMDResult> 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<XOSCMDResult>(() =>
{
//
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;
}
}
}
+8 -6
View File
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<!-- Runtime Definition -->
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>xDashboard.xCommons</PackageId>
<Version>1.0.0</Version>
<LangVersion>8.0</LangVersion>
<Authors>Hadi Khazaee Asl</Authors>
<Company>SaherElm IT Center</Company>
<PackageId>xDashboard.xCommons</PackageId>
<TargetFramework>netstandard2.0</TargetFramework>
<Description>
provide all commonly used actions/extensions and classes which required to xDashboard project.
</Description>
@@ -20,9 +20,12 @@
<None Include="../../Resources/Images/favicon.png" Link="icon.png" Pack="true" PackagePath="\icon.png" />
</ItemGroup>
<!-- Local Modules -->
<ItemGroup>
</ItemGroup>
<!-- Local Dependencies -->
<ItemGroup>
<!-- <PackageReference Include="xDashboard.xExceptions" Version="1.0.0" /> -->
<ProjectReference Include="../xExceptions/xExceptions.csproj" />
</ItemGroup>
@@ -49,8 +52,8 @@
<!-- Swagger -->
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.0.2" />
</ItemGroup>
<!-- For XML Documentation Support -->
@@ -59,5 +62,4 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
</Project>