From aa98c20af3650a493d28dc258d45efb47928b3b8 Mon Sep 17 00:00:00 2001 From: calebjenkins Date: Sun, 10 Nov 2024 16:41:57 -0600 Subject: [PATCH] version bump - added IntExtensions and expanded IFileIO --- .editorconfig | 4 + Calebs.Extensions.sln | 1 + README.md | 172 +++++++++--------- src/ExtensionTests/ExtensionTests.csproj | 10 +- src/ExtensionTests/FileIOTests.cs | 21 ++- src/ExtensionTests/IntExtensionTests.cs | 46 +++++ src/ExtensionTests/StringExtensionTests.cs | 7 +- src/ExtensionTests7/ExtensionTests7.csproj | 10 +- src/Extensions/Console/ConsoleExtensions.cs | 82 +++++++-- src/Extensions/Console/IConsole.cs | 34 ++++ src/Extensions/Extensions.csproj | 6 +- src/Extensions/StringExtensions.cs | 101 +++++++--- src/Extensions/SystemIO/IFileIO.cs | 22 ++- .../ExtensionNugetTests7.csproj | 10 +- 14 files changed, 368 insertions(+), 158 deletions(-) create mode 100644 .editorconfig create mode 100644 src/ExtensionTests/IntExtensionTests.cs create mode 100644 src/Extensions/Console/IConsole.cs diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..aab4418 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,4 @@ +[*.cs] + +# CS8632: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. +dotnet_diagnostic.CS8632.severity = none diff --git a/Calebs.Extensions.sln b/Calebs.Extensions.sln index 752acfc..4c2d9bc 100644 --- a/Calebs.Extensions.sln +++ b/Calebs.Extensions.sln @@ -11,6 +11,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExtensionTests", "src\Exten EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{4316AA9F-ECF9-489D-B86B-E643AD24C6F1}" ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig .gitignore = .gitignore LICENSE = LICENSE README.md = README.md diff --git a/README.md b/README.md index 80579fb..4381224 100644 --- a/README.md +++ b/README.md @@ -1,46 +1,48 @@ -[![.github/workflows/dev-ci.yml](https://github.com/calebjenkins/Calebs.Extensions/actions/workflows/dev-ci.yml/badge.svg?branch=develop)](https://github.com/calebjenkins/Calebs.Extensions/actions/workflows/dev-ci.yml) -[![.github/workflows/main-publish.yml](https://github.com/calebjenkins/Calebs.Extensions/actions/workflows/main-publish.yml/badge.svg?branch=main)](https://github.com/calebjenkins/Calebs.Extensions/actions/workflows/main-publish.yml) -[![NuGet](https://img.shields.io/nuget/dt/calebs.extensions.svg)](https://www.nuget.org/packages/calebs.extensions) -[![NuGet](https://img.shields.io/nuget/vpre/calebs.extensions.svg)](https://www.nuget.org/packages/calebs.extensions) -[![.github/workflows/ci.yml](https://github.com/calebjenkins/Calebs.Extensions/actions/workflows/ci.yml/badge.svg)](https://github.com/calebjenkins/Calebs.Extensions/actions/workflows/ci.yml) - -![Extensions Logo](https://raw.githubusercontent.com/calebjenkins/Calebs.Extensions/develop/Assets/logo.png) -# Calebs.Extensions -Useful extension methods and attributes for working with enums, strings and lists. The majority of these extensions were born out of working with various models while building micro services. - - -### Installing Calebs.Extensions - -You should install [Extensions with NuGet](https://www.nuget.org/packages/Calebs.Extensions): - - Install-Package Calebs.Extensions - -Or via the .NET Core command line interface: - - dotnet add package Calebs.Extensions - -Either command, from Package Manager Console or .NET Core CLI, will download and install Calebs.Extensions and all required dependencies. - -### .NET 7 -These extensions target .NET 6 and .NET 7. With .NET 7 the `Calebs.Extensions.Validators` include `EnumStringValidator`. The ability for Attributes to support was added with .NET 7. - -# Helpers - not really extensions - -## EnumStringValidator -Used for string properties in models that are supposed to conform to an enum value. The philosophy of my micro-services have been to be liberal in what you accept and conservative in what you send. - -`Let's propose a scenario:` - you are recieving a message (model) that represents an `account` with a field `accountType`. Now, in this scenarios `accountType` could be `Standard, Silver or Gold` values. The easy way to restrict this is with en enum. The problem is - that if the incoming message doesn't exactly have one of those values (say `"AccountStatus":"Gold-Status"` is passed in instead of `"AccountStatus":"Gold"`), and if you are leveraging `Microsoft Web API` with model binding - then it is likely that model binding will fail and you will return a `400 - Bad Request` by default. This is the correct response, but you might want to log what was actually sent, or add additional context like an error message stating what field or fields were incorrect and what values are supported for that field. This makes for a much more developer friendly API. - -So instead of having your model directly bind to an enum - and throw a Bad Request exception - you can accept a `string` in that field, and use the `Calebs.Extensions.Validators.EnumStringValidatorAttribute` to perform model validation and then decide how to handle the errors. - -## SystemIO - -### IFileIO -A collection of thin shims for common File IO opperations. Helpful when you want to mock out the File IO opperations for testing. -The interface `IFileIO` - every method is so slim that they each have a default implementaion. The default implementation `FileIO` jump implements the Interface, but doesn't need to implement any of the methods. -To use this helper - register `IFileIO` is your `DI` with `FileIO` as the implmentation. For unit tests use something like `nSubstitute` to mock out and intercept interactions through `IFileIO` methods. - +[![.github/workflows/dev-ci.yml](https://github.com/calebjenkins/Calebs.Extensions/actions/workflows/dev-ci.yml/badge.svg?branch=develop)](https://github.com/calebjenkins/Calebs.Extensions/actions/workflows/dev-ci.yml) +[![.github/workflows/main-publish.yml](https://github.com/calebjenkins/Calebs.Extensions/actions/workflows/main-publish.yml/badge.svg?branch=main)](https://github.com/calebjenkins/Calebs.Extensions/actions/workflows/main-publish.yml) +[![NuGet](https://img.shields.io/nuget/dt/calebs.extensions.svg)](https://www.nuget.org/packages/calebs.extensions) +[![NuGet](https://img.shields.io/nuget/vpre/calebs.extensions.svg)](https://www.nuget.org/packages/calebs.extensions) +[![.github/workflows/ci.yml](https://github.com/calebjenkins/Calebs.Extensions/actions/workflows/ci.yml/badge.svg)](https://github.com/calebjenkins/Calebs.Extensions/actions/workflows/ci.yml) + +![Extensions Logo](https://raw.githubusercontent.com/calebjenkins/Calebs.Extensions/develop/Assets/logo.png) +# Calebs.Extensions +Useful extension methods and attributes for working with enums, strings and lists. The majority of these extensions were born out of working with various models while building micro services. + + +### Installing Calebs.Extensions + +You should install [Extensions with NuGet](https://www.nuget.org/packages/Calebs.Extensions): + + Install-Package Calebs.Extensions + +Or via the .NET Core command line interface: + + dotnet add package Calebs.Extensions + +Either command, from Package Manager Console or .NET Core CLI, will download and install Calebs.Extensions and all required dependencies. + +### .NET 7 +These extensions target .NET 6 and .NET 7. With .NET 7 the `Calebs.Extensions.Validators` include `EnumStringValidator`. The ability for Attributes to support was added with .NET 7. + +# Helpers - not really extensions + +## EnumStringValidator +Used for string properties in models that are supposed to conform to an enum value. The philosophy of my micro-services have been to be liberal in what you accept and conservative in what you send. + +`Let's propose a scenario:` - you are recieving a message (model) that represents an `account` with a field `accountType`. Now, in this scenarios `accountType` could be `Standard, Silver or Gold` values. The easy way to restrict this is with en enum. The problem is - that if the incoming message doesn't exactly have one of those values (say `"AccountStatus":"Gold-Status"` is passed in instead of `"AccountStatus":"Gold"`), and if you are leveraging `Microsoft Web API` with model binding - then it is likely that model binding will fail and you will return a `400 - Bad Request` by default. This is the correct response, but you might want to log what was actually sent, or add additional context like an error message stating what field or fields were incorrect and what values are supported for that field. This makes for a much more developer friendly API. + +So instead of having your model directly bind to an enum - and throw a Bad Request exception - you can accept a `string` in that field, and use the `Calebs.Extensions.Validators.EnumStringValidatorAttribute` to perform model validation and then decide how to handle the errors. + +## SystemIO + +### IFileIO +A collection of thin shims for common File IO opperations. Helpful when you want to mock out the File IO opperations for testing. +The interface `IFileIO` - every method is so slim that they each have a default implementaion. The default implementation `FileIO` jump implements the Interface, but doesn't need to implement any of the methods. +To use this helper - register `IFileIO` is your `DI` with `FileIO` as the implmentation. For unit tests use something like `nSubstitute` to mock out and intercept interactions through `IFileIO` methods. + - GetFiles(path, filter) // returns a string [] +- GetFileInfo(path) +- GetFileAttributes(path) - DirectoryExists(path) - GetDirectoryName(path) - ReadAllText (path) @@ -48,52 +50,56 @@ To use this helper - register `IFileIO` is your `DI` with `FileIO` as the implme - WriteAllLines(path, lines) - DeleteFile(path) - CreateDirectory (path) -- DeleteDirectory (path) - -# Extension Methods - -## EnumExtensions -- ToList - -- ToList(Type) -- Description (enum) -- Description(Enum) -- Parse +- DeleteDirectory (path) + +# Extension Methods + +## EnumExtensions +- ToList - +- ToList(Type) +- Description (enum) +- Description(Enum) +- Parse - Parse - - -## StringExtensions -- IsNotNullOrEmpty -- IsNullOrEmpty + +## IntExtensions +- RandomText // For integers between 1 and 1000 -- returns a random string with that length + + +## StringExtensions +- IsNotNullOrEmpty +- IsNullOrEmpty - Compare -- string?.ValueOrEmpty() - -## ObjectExtensions -- ToSafeString() - -## ListExtensions -- ToDelimitedList -- ToUpper +- string?.ValueOrEmpty() + +## ObjectExtensions +- ToSafeString() + +## ListExtensions +- ToDelimitedList +- ToUpper - AddRange - `IList.AddRange(IList)` -- AddUnlessBlank IList.AddUnlessBlank(string) - -## JsonExtensions -For both of these extension methods I'm using the `Newtonsoft.Json` library. I'm planning on migrating to `System.Text.Json` as soon as it is viable. Right now, Newtonsoft is easeir to serialize enums to thier ToString() value rather than index, and to deserialize the same. For example, by default - an enum is serialized to the index of the value. So an enum with (High, Med, Low) values would otherwise be serialiezed to a 2, instead of to "Med". Serializing to "Med" is my prefered behavior. I will continue to evailuate `System.Text.Json` against these unit tests and most likely migrate at some point. -- ToJson -- FromJson - -## Versioning -This package follow semantic versioning as much as possible. - -# Contributions -Please submit PR's to the `develop` branch. -Merges to `deveoper` automtically run all unit tests and publish a nuget package with the postfix `-ci-build_number` -Merges to `main` publish to nuget as a major release. - -# Change Log -- 1.1.0 - added IList.AddRange extension method +- AddUnlessBlank IList.AddUnlessBlank(string) + +## JsonExtensions +For both of these extension methods I'm using the `Newtonsoft.Json` library. I'm planning on migrating to `System.Text.Json` as soon as it is viable. Right now, Newtonsoft is easeir to serialize enums to thier ToString() value rather than index, and to deserialize the same. For example, by default - an enum is serialized to the index of the value. So an enum with (High, Med, Low) values would otherwise be serialiezed to a 2, instead of to "Med". Serializing to "Med" is my prefered behavior. I will continue to evailuate `System.Text.Json` against these unit tests and most likely migrate at some point. +- ToJson +- FromJson + +## Versioning +This package follow semantic versioning as much as possible. + +# Contributions +Please submit PR's to the `develop` branch. +Merges to `deveoper` automtically run all unit tests and publish a nuget package with the postfix `-ci-build_number` +Merges to `main` publish to nuget as a major release. + +# Change Log +- 1.1.0 - added IList.AddRange extension method - 1.2.0 - never published - only preview - 1.3.0 - added `IFileIO` - an interface + implementation for making common filesystem opperations easier to test - 1.3.1 - suppressed some test warnings and updated the GH workflows -- 1.4.0 - added `CreatedDiretory` and `DeleteDirectory` to `IFileIO` +- 1.4.0 - added `CreatedDiretory` and `DeleteDirectory` to `IFileIO` - 1.5.0 - Added package logo and `ToSafeString` for `ObjectExtensions` - 1.6.0 - Added AddUnlessBlank to list extensions +- 1.7.0 - Added `IntExtensions` and expanded `IFileIO` diff --git a/src/ExtensionTests/ExtensionTests.csproj b/src/ExtensionTests/ExtensionTests.csproj index d0d92c2..056c1c5 100644 --- a/src/ExtensionTests/ExtensionTests.csproj +++ b/src/ExtensionTests/ExtensionTests.csproj @@ -9,11 +9,11 @@ - - - - - + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/ExtensionTests/FileIOTests.cs b/src/ExtensionTests/FileIOTests.cs index 2e4cf80..363e197 100644 --- a/src/ExtensionTests/FileIOTests.cs +++ b/src/ExtensionTests/FileIOTests.cs @@ -13,13 +13,14 @@ public class FileIOTests public void DirectoryExists_ShouldBeTrue() { var exists = _files.DirectoryExists("./"); - exists.Should().BeTrue(); + exists.Should().BeTrue(); } [Fact] public void DirectoryDoesNotExists_ShouldBeFalse() { - var exists = _files.DirectoryExists("./blah/"); + var rndPath = $"./{6.RandomText()}"; + var exists = _files.DirectoryExists(rndPath); exists.Should().BeFalse(); } @@ -37,6 +38,20 @@ public void GetFilesCount_FilterToOneFile() results.Count().Should().Be(1); } + [Fact] + public void GetFileInfo() + { + var results = _files.GetFileInfo("./Calebs.Extensions.dll"); + results.CreationTime.Ticks.Should().BeLessThan(DateTime.Now.Ticks); + } + + [Fact] + public void GetAttributes() + { + var results = _files.GetFileAttributes("./Calebs.Extensions.dll"); + results.Description().Count().Should().BeGreaterThan(0); + } + [Fact] public void GetDirectoryName() { @@ -64,7 +79,7 @@ public void FilesExists_DoesExist() [Fact] public void FilesExists_DoesNotExist() { - var result = _files.FileExists("./blah"); + var result = _files.FileExists("./blah.txt"); result.Should().BeFalse(); } diff --git a/src/ExtensionTests/IntExtensionTests.cs b/src/ExtensionTests/IntExtensionTests.cs new file mode 100644 index 0000000..ba74198 --- /dev/null +++ b/src/ExtensionTests/IntExtensionTests.cs @@ -0,0 +1,46 @@ + +namespace ExtensionTests; + +using Calebs.Extensions; + +public class IntExtensionTests +{ + [Theory] + [InlineData(1)] + [InlineData(5)] + [InlineData(10)] + [InlineData(32)] + public void RandomTextUnder32(int value) + { + value.RandomText().Length.Should().Be(value); + } + + [Theory] + [InlineData(33)] + [InlineData(100)] + [InlineData(1000)] + public void RandomTextOver32_Below1000_LongRunning(int value) + { + value.RandomText().Length.Should().Be(value); + } + + [Theory] + [InlineData (0)] + [InlineData(-1)] + [InlineData(-10)] + [InlineData (1001)] + [InlineData(5000)] + public void RandomTextBelowZero_or_Over1000_Should_Thrown_Exception(int value) + { + try + { + var _ = value.RandomText(); + } + catch (Exception ex) + { + ex.Should().BeOfType(); + } + } + + +} \ No newline at end of file diff --git a/src/ExtensionTests/StringExtensionTests.cs b/src/ExtensionTests/StringExtensionTests.cs index 383bc43..6fe8f92 100644 --- a/src/ExtensionTests/StringExtensionTests.cs +++ b/src/ExtensionTests/StringExtensionTests.cs @@ -8,9 +8,7 @@ public class StringExtensionTests [Fact] public void ShouldReturnEmptyStringOnNull() { -#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. string? value = null; -#pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. string v2 = value.ValueOrEmpty(); v2.Should().Be(String.Empty); @@ -19,11 +17,10 @@ public void ShouldReturnEmptyStringOnNull() [Fact] public void ShouldReturnValueIfNotNull() { -#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. + string? value = "hello"; -#pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. string v2 = value.ValueOrEmpty(); v2.Should().Be(value); } -} \ No newline at end of file +} diff --git a/src/ExtensionTests7/ExtensionTests7.csproj b/src/ExtensionTests7/ExtensionTests7.csproj index f47f65c..f286dc9 100644 --- a/src/ExtensionTests7/ExtensionTests7.csproj +++ b/src/ExtensionTests7/ExtensionTests7.csproj @@ -19,14 +19,14 @@ - - - - + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/src/Extensions/Console/ConsoleExtensions.cs b/src/Extensions/Console/ConsoleExtensions.cs index 8f2f5e8..ed2c1d3 100644 --- a/src/Extensions/Console/ConsoleExtensions.cs +++ b/src/Extensions/Console/ConsoleExtensions.cs @@ -1,20 +1,78 @@  -namespace Calebs.Extensions.Console +using System; + +namespace Calebs.Extensions.Console; + +public static class ConsoleExtensions { - public static class ConsoleExtensions + public static IConsole Imp => new ConsoleImp(); + public static void WriteLine (this ConsoleColor color, string text) + { + color.Write(text); + Imp.WriteLine(""); + } + public static void WriteLine(this ConsoleColor color, Exception ex) + { + color.WriteLine(ex.ToString()); + } + + public static void Write (this ConsoleColor color, string text) { - public static void WriteLine (this System.ConsoleColor color, string text) + color.Do(() => { - color.Write(text); - System.Console.WriteLine(); - } + Imp.Write(text); + }); + } + + public static void Write(this ConsoleColor color, Exception ex) + { + color.Write(ex.ToString()); + } + + public static void WriteLable(this ConsoleColor color, string text, int Margin = 2) + { + WriteLable(color, text, Margin, color); + } + public static void WriteLable(this ConsoleColor color, string text, int Margin, ConsoleColor BorderColor) + { + var labelLength = text.Length + (Margin * 2) + 2; // length + margin + margin + 2 - the last 2 are for the border characters + var margin = " ".Times(Margin); + + Imp.WriteLine(""); + Imp.Write(" "); // spacer before label + BorderColor.WriteTimes("-", labelLength); + Imp.WriteLine(""); + + BorderColor.Write(" |" + margin); + color.Write(text); + BorderColor.WriteLine(margin + "|"); + + Imp.Write(" "); + BorderColor.WriteTimes("-", labelLength); + Imp.WriteLine(""); + } + + public static void WriteTimes(this ConsoleColor color, string text, int Times = 1) + { + if(Times < 0) { throw new ArgumentOutOfRangeException(nameof(Times), "Times cannot be a negative number"); } - public static void Write (this System.ConsoleColor color, string text) + color.Do(() => { - var currentColor = System.Console.ForegroundColor; - System.Console.ForegroundColor = color; - System.Console.Write(text); - System.Console.ForegroundColor = currentColor; - } + for(var i =0; i < Times; i++) + { + Imp.Write(text); + } + }); + + } + + public static void Do(this ConsoleColor color, Action act) + { + var currentColor = Imp.ForegroundColor; + Imp.ForegroundColor = color; + + act.Invoke(); + + Imp.ForegroundColor = currentColor; } } diff --git a/src/Extensions/Console/IConsole.cs b/src/Extensions/Console/IConsole.cs new file mode 100644 index 0000000..737448b --- /dev/null +++ b/src/Extensions/Console/IConsole.cs @@ -0,0 +1,34 @@ + +using System; + +namespace Calebs.Extensions.Console; + +public class ConsoleImp : IConsole { } + +public interface IConsole +{ + void WriteLine(string text) => System.Console.WriteLine(text); + void Write(string text) => System.Console.Write(text); + void WriteLine(Exception ex) => System.Console.WriteLine(ex); + void Error(string text) => ConsoleColor.Red.WriteLine(text); + void Error(Exception ex) => ConsoleColor.Red.WriteLine(ex); + ConsoleColor ForegroundColor + { + get => System.Console.ForegroundColor; + set => System.Console.ForegroundColor = value; + } + ConsoleColor BackgroundColor + { + get => System.Console.BackgroundColor; + set => System.Console.BackgroundColor = value; + } + + string ReadLine() => System.Console.ReadLine(); + string ReadLine(string text) => _readline(text); + + private string _readline(string text) + { + this.Write(text); + return this.ReadLine(); + } +} diff --git a/src/Extensions/Extensions.csproj b/src/Extensions/Extensions.csproj index 4d26ced..0df6693 100644 --- a/src/Extensions/Extensions.csproj +++ b/src/Extensions/Extensions.csproj @@ -6,11 +6,11 @@ - net6.0;net7.0 + net6.0;net7.0;net8.0 Debug;Release;NET7 Calebs.Extensions Calebs.Extensions - 1.6.0 + 1.7.0 true Caleb Jenkins Caleb Jenkins @@ -30,7 +30,7 @@ - + True diff --git a/src/Extensions/StringExtensions.cs b/src/Extensions/StringExtensions.cs index 1981f59..fcba16c 100644 --- a/src/Extensions/StringExtensions.cs +++ b/src/Extensions/StringExtensions.cs @@ -1,27 +1,28 @@ -using System; - -namespace Calebs.Extensions; - -public static class StringExtensions -{ - public static bool IsNotNullOrEmpty(this string value) - { - return !String.IsNullOrEmpty(value); - } - - public static bool IsNullOrEmpty(this string value) - { - return String.IsNullOrEmpty(value); - } - - public static bool Compare(this string string1, string string2) - { - var returnValue = false; - if (string1.IsNotNullOrEmpty()) - { - returnValue = string1.Equals(string2, StringComparison.CurrentCultureIgnoreCase); - } - return returnValue; +using System; +using System.Text; + +namespace Calebs.Extensions; + +public static class StringExtensions +{ + public static bool IsNotNullOrEmpty(this string value) + { + return !String.IsNullOrEmpty(value); + } + + public static bool IsNullOrEmpty(this string value) + { + return String.IsNullOrEmpty(value); + } + + public static bool Compare(this string string1, string string2) + { + var returnValue = false; + if (string1.IsNotNullOrEmpty()) + { + returnValue = string1.Equals(string2, StringComparison.CurrentCultureIgnoreCase); + } + return returnValue; } #pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. @@ -34,6 +35,52 @@ public static string ValueOrEmpty(this string? value) } return value; - } -} - + } + + public static string Times(this string text, int times) + { + if (times < 0) { throw new ArgumentOutOfRangeException(nameof(times), "Times cannot be a negative number"); } + + var sb = new StringBuilder(times); + for (var i = 0; i < times; i++) + { + sb.Append(text); + } + return sb.ToString(); + } +} + +public static class IntExtensions +{ + public static string RandomText(this int length) + { + // Note: we use a GUID to be lazy - there are MUCH more performant ways to do this! + + if (length <0) throw new ArgumentOutOfRangeException($"{nameof(length)} must be a positive number"); + + if (length > 1000) throw new ArgumentOutOfRangeException($"{nameof(length)} must be below 1000 - this is too high!"); + + // shortcut - just do it! + if (length < 33) + return Guid.NewGuid().ToString("N").Substring(0, length); + + + // Figure out randomness for larger numbers. + // TODO: this is a cringy way to do this. Move to better perf at some point! + int times = 1; + if(length > 32) + { + times = (length / 32) + 1; + } + + var returnValue = new StringBuilder(times * 32); + for(int i = 0; i < times; i++) + { + returnValue.Append(Guid.NewGuid().ToString("N")); + } + + var tmpTxt = returnValue.ToString(); + return tmpTxt.Substring(0, length); + } +} + diff --git a/src/Extensions/SystemIO/IFileIO.cs b/src/Extensions/SystemIO/IFileIO.cs index 63383e4..74aac7c 100644 --- a/src/Extensions/SystemIO/IFileIO.cs +++ b/src/Extensions/SystemIO/IFileIO.cs @@ -1,6 +1,6 @@ using System; using System.Collections.Generic; -using System.IO; +using io = System.IO; namespace Calebs.Extensions.SystemIO; @@ -10,13 +10,15 @@ namespace Calebs.Extensions.SystemIO; /// public interface IFileIO { - bool FileExists(string path) => File.Exists(path); - void DeleteFile(string path) => File.Delete(path); - string ReadAllText (string path) => File.ReadAllText(path); - void WriteAllLines(string path, IEnumerable lines) => File.WriteAllLines(path, lines); - string[] GetFiles(string path, string filter = "") => Directory.GetFiles(path, filter); - bool DirectoryExists(string path) => Directory.Exists(path); - string GetDirectoryName(string path) => Path.GetDirectoryName(path); - void CreateDirectory(string path) => Directory.CreateDirectory(path); - void DeleteDirectory (string path) => Directory.Delete(path); + bool FileExists(string path) => io.File.Exists(path); + void DeleteFile(string path) => io.File.Delete(path); + string ReadAllText (string path) => io.File.ReadAllText(path); + void WriteAllLines(string path, IEnumerable lines) => io.File.WriteAllLines(path, lines); + string[] GetFiles(string path, string filter = "") => io.Directory.GetFiles(path, filter); + io.FileAttributes GetFileAttributes(string path) => io.File.GetAttributes(path); + io.FileInfo GetFileInfo (string path) => new io.FileInfo(path); + bool DirectoryExists(string path) => io.Directory.Exists(path); + string GetDirectoryName(string path) => io.Path.GetDirectoryName(path); + void CreateDirectory(string path) => io.Directory.CreateDirectory(path); + void DeleteDirectory (string path) => io.Directory.Delete(path); } diff --git a/src/ExtensionsNugetTests7/ExtensionNugetTests7/ExtensionNugetTests7.csproj b/src/ExtensionsNugetTests7/ExtensionNugetTests7/ExtensionNugetTests7.csproj index e24e456..900301f 100644 --- a/src/ExtensionsNugetTests7/ExtensionNugetTests7/ExtensionNugetTests7.csproj +++ b/src/ExtensionsNugetTests7/ExtensionNugetTests7/ExtensionNugetTests7.csproj @@ -23,14 +23,14 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - - + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive