diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6800233..53b31d9 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -45,7 +45,12 @@ uptool: $tmp1 = $asmver.split('.')[2] $tmp2 = $asmver.split('.')[3] $suffix = "$tmp1.$tmp2" - cd Misc + cd Core + dotnet pack --version-suffix "$suffix" -c Release -o . + $tmp = $(Get-Item $(Resolve-Path *.nupkg).Path).Name + echo $tmp + dotnet nuget push $tmp -s https://api.nuget.org/v3/index.json -k $NUGET + cd ..\Misc dotnet pack --version-suffix "$suffix" -c Release -o . $tmp = $(Get-Item $(Resolve-Path *.nupkg).Path).Name echo $tmp diff --git a/CC-Functions.sln b/CC-Functions.sln index 3b833f4..8c6947a 100644 --- a/CC-Functions.sln +++ b/CC-Functions.sln @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commandline", "Commandline\ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CLITest", "CLITest\CLITest.csproj", "{3FAB0713-3021-4C6A-BF4A-ABBD542634A6}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core", "Core\Core.csproj", "{780EC190-E223-46DE-B6FB-1CF56ABDF34E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {3FAB0713-3021-4C6A-BF4A-ABBD542634A6}.Debug|Any CPU.Build.0 = Debug|Any CPU {3FAB0713-3021-4C6A-BF4A-ABBD542634A6}.Release|Any CPU.ActiveCfg = Release|Any CPU {3FAB0713-3021-4C6A-BF4A-ABBD542634A6}.Release|Any CPU.Build.0 = Release|Any CPU + {780EC190-E223-46DE-B6FB-1CF56ABDF34E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {780EC190-E223-46DE-B6FB-1CF56ABDF34E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {780EC190-E223-46DE-B6FB-1CF56ABDF34E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {780EC190-E223-46DE-B6FB-1CF56ABDF34E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/CLITest/Program.cs b/CLITest/Program.cs index 798a0fd..45fb482 100644 --- a/CLITest/Program.cs +++ b/CLITest/Program.cs @@ -1,6 +1,7 @@ using System; using System.Drawing; using System.Threading; +using CC_Functions.Commandline; using CC_Functions.Commandline.TUI; namespace CLITest diff --git a/Commandline/Commandline.csproj b/Commandline/Commandline.csproj index 49e237f..d2e50fc 100644 --- a/Commandline/Commandline.csproj +++ b/Commandline/Commandline.csproj @@ -9,11 +9,11 @@ false CC-Functions.Commandline CC-Functions.Commandline - CC24 + JFronny Random pieces of code used across my projects. CLI/TUI extensions Copyright 2020 - https://github.com/JFronny/CC-Functions - https://github.com/JFronny/CC-Functions.git + https://gitlab.com/JFronny/CC-Functions + https://gitlab.com/JFronny/CC-Functions.git git 1.1.* 1.0.0.0 @@ -31,7 +31,6 @@ - + - diff --git a/Commandline/TUI/DiffDraw.cs b/Commandline/DiffDraw.cs similarity index 98% rename from Commandline/TUI/DiffDraw.cs rename to Commandline/DiffDraw.cs index bd5fa54..c1a968b 100644 --- a/Commandline/TUI/DiffDraw.cs +++ b/Commandline/DiffDraw.cs @@ -1,8 +1,9 @@ using System; using System.Drawing; -using CC_Functions.Misc; +using CC_Functions.Commandline.TUI; +using CC_Functions.Core; -namespace CC_Functions.Commandline.TUI +namespace CC_Functions.Commandline { /// /// Provides differential drawing of a char[,] Do not use in combination with System.Console diff --git a/Commandline/TUI/Button.cs b/Commandline/TUI/Button.cs index 2603a9b..a50e9a2 100644 --- a/Commandline/TUI/Button.cs +++ b/Commandline/TUI/Button.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using System.Drawing; using System.Linq; -using CC_Functions.Misc; +using CC_Functions.Core; namespace CC_Functions.Commandline.TUI { diff --git a/Commandline/TUI/CenteredScreen.cs b/Commandline/TUI/CenteredScreen.cs index 2d6c9f4..90e556b 100644 --- a/Commandline/TUI/CenteredScreen.cs +++ b/Commandline/TUI/CenteredScreen.cs @@ -1,6 +1,6 @@ using System; using System.Drawing; -using CC_Functions.Misc; +using CC_Functions.Core; namespace CC_Functions.Commandline.TUI { diff --git a/Commandline/TUI/CheckBox.cs b/Commandline/TUI/CheckBox.cs index dd1fcef..e8bff9d 100644 --- a/Commandline/TUI/CheckBox.cs +++ b/Commandline/TUI/CheckBox.cs @@ -1,6 +1,6 @@ using System; using System.Drawing; -using CC_Functions.Misc; +using CC_Functions.Core; namespace CC_Functions.Commandline.TUI { diff --git a/Commandline/TUI/Label.cs b/Commandline/TUI/Label.cs index 2f441ba..bf0bef5 100644 --- a/Commandline/TUI/Label.cs +++ b/Commandline/TUI/Label.cs @@ -1,5 +1,5 @@ using System.Drawing; -using CC_Functions.Misc; +using CC_Functions.Core; namespace CC_Functions.Commandline.TUI { diff --git a/Commandline/TUI/Panel.cs b/Commandline/TUI/Panel.cs index e298fc5..7a599a6 100644 --- a/Commandline/TUI/Panel.cs +++ b/Commandline/TUI/Panel.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; using System.Linq; -using CC_Functions.Misc; +using CC_Functions.Core; namespace CC_Functions.Commandline.TUI { diff --git a/Commandline/TUI/Slider.cs b/Commandline/TUI/Slider.cs index 7dcdbe8..f8e34b3 100644 --- a/Commandline/TUI/Slider.cs +++ b/Commandline/TUI/Slider.cs @@ -1,5 +1,5 @@ using System; -using CC_Functions.Misc; +using CC_Functions.Core; namespace CC_Functions.Commandline.TUI { diff --git a/Commandline/TUI/TextBox.cs b/Commandline/TUI/TextBox.cs index 50544cb..ab67c83 100644 --- a/Commandline/TUI/TextBox.cs +++ b/Commandline/TUI/TextBox.cs @@ -2,7 +2,7 @@ using System; using System.Collections.Generic; using System.Drawing; using System.Linq; -using CC_Functions.Misc; +using CC_Functions.Core; namespace CC_Functions.Commandline.TUI { diff --git a/Misc/ArrayFormatter.cs b/Core/ArrayFormatter.cs similarity index 99% rename from Misc/ArrayFormatter.cs rename to Core/ArrayFormatter.cs index 931ea3e..266a615 100644 --- a/Misc/ArrayFormatter.cs +++ b/Core/ArrayFormatter.cs @@ -2,7 +2,7 @@ using System.Drawing; using System.Linq; -namespace CC_Functions.Misc +namespace CC_Functions.Core { /// /// Contains extension functions to work with 1D and 2D arrays diff --git a/Core/Core.csproj b/Core/Core.csproj new file mode 100644 index 0000000..ad08b0b --- /dev/null +++ b/Core/Core.csproj @@ -0,0 +1,29 @@ + + + + Library + CC_Functions.Core + CC_Functions.Core + 8 + false + CC-Functions.Core + CC-Functions.Core + JFronny + Random pieces of code without external dependencies. Used in other CC_Functions packages + Copyright 2020 + https://gitlab.com/JFronny/CC-Functions + https://gitlab.com/JFronny/CC-Functions.git + git + 1.1.* + 1.0.0.0 + 0.0 + 1.1.$(VersionSuffix) + netstandard2.0 + + + bin\Debug\Core.xml + + + bin\Release\Core.xml + + diff --git a/Core/GenericExtensions.cs b/Core/GenericExtensions.cs new file mode 100644 index 0000000..7e1ddd0 --- /dev/null +++ b/Core/GenericExtensions.cs @@ -0,0 +1,244 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.IO; +using System.IO.Compression; +using System.Linq; +using System.Net; + +namespace CC_Functions.Core +{ + /// + /// Extension methods for various types + /// + public static class GenericExtensions + { + /// + /// Gets an element from the dictionary or adds the default + /// + /// The dictionary to get from + /// The key to check + /// The default value to place + /// The key type + /// The value type + /// The element at the key + public static TValue Get(this IDictionary dict, TKey key, TValue def = default) + { + if (!dict.ContainsKey(key)) + dict[key] = def; + return dict[key]; + } + + /// + /// Sets an element and returns it + /// + /// The dictionary to set in + /// The key to set at + /// The value to place + /// The key type + /// The value type + /// The value that was placed + public static TValue Set(this IDictionary dict, TKey key, TValue val = default) + { + dict[key] = val; + return dict[key]; + } + + /// + /// Tries to cast an object + /// + /// The object to try to parse + /// The parsed object (if successful) or the default (usually null) + /// The type to cast to + /// Whether the cast was successful + public static bool TryCast(this object o, out T parsed) + { + try + { + parsed = (T) o; + return true; + } + catch + { + parsed = default; + return false; + } + } + + /// + /// Runs a function that transforms an object in-line + /// + /// The object to run on + /// The function to run + /// The input type + /// The output type + /// + public static TOut SelectO(this TIn self, Func func) => func.Invoke(self); + + /// + /// Runs a function under a condition in-line (equal to if) + /// + /// The condition to check + /// The function to run + public static void RunIf(bool condition, Action func) + { + if (condition) + func(); + } + + /// + /// Parses a string to a value of an enum + /// + /// The string to parse + /// The enum type (MUST be an enum) + /// The element + public static TEnum ParseToEnum(string value) => (TEnum) Enum.Parse(typeof(TEnum), + Enum.GetNames(typeof(TEnum)).First(s => s.ToLower() == value.ToLower())); + + /// + /// Parses a string to a nullable bool (defaults to null if parse fails) + /// + /// The st string to parse + /// The output nullable bool + public static bool? ParseBool(string value) => + bool.TryParse(value, out bool tmp) ? (bool?) tmp : null; + + /// + /// AND operation for nullable bools (uses True) + /// + /// First bool to check + /// Second bool to check + /// The operation result + public static bool And(this bool? left, bool? right) => left.True() && right.True(); + + /// + /// OR operation for nullable bools (uses True) + /// + /// First bool to check + /// Second bool to check + /// The operation result + public static bool Or(this bool? left, bool? right) => left.True() || right.True(); + + /// + /// XOR operation for nullable bools (uses True) + /// + /// First bool to check + /// Second bool to check + /// The operation result + public static bool Xor(this bool? left, bool? right) => left.Or(right) && !left.And(right); + + /// + /// Whether the nullable bool is true (null->false) + /// + /// Value to check + /// Whether it is true + public static bool True(this bool? self) => self == true; + + /// + /// Whether the nullable bool is false (null->false) + /// + /// Value to check + /// Whether it is false + public static bool False(this bool? self) => self == false; + + /// + /// Whether the nullable bool is null + /// + /// Value to check + /// Whether it is null + public static bool Null(this bool? self) => self == null; + + /// + /// Removes an element from a dictionary by its index (not key) + /// + /// The dictionary to remove from + /// The index of the value + /// The key type + /// The value type + public static void RemoveAt(this Dictionary dict, int index) => + dict.Remove(dict.Keys.ToArray()[index]); + + /// + /// "Unshorten" (follow) an URL + /// + /// The URL to unshorten + /// The unshortened URL + public static Uri Unshorten(this Uri self) + { + HttpWebRequest req = (HttpWebRequest) WebRequest.Create(self); + req.AllowAutoRedirect = true; + req.MaximumAutomaticRedirections = 100; + WebResponse resp = req.GetResponse(); + return resp.ResponseUri; + } + + /// + /// Rounds a RectangleF to a Rectangle instead of flooring + /// + /// The RectangleF to round + /// The rounded Rectangle + public static Rectangle Round(this RectangleF self) => Rectangle.Round(self); + + /// + /// Ceilings a RectangleF to a Rectangle instead of flooring + /// + /// The RectangleF to ceil (?) + /// The ceiled (?) Rectangle + public static Rectangle Ceiling(this RectangleF self) => Rectangle.Ceiling(self); + + /// + /// Pings an URL to check for availability + /// + /// The URL to check + /// Whether the service is online + public static bool Ping(this Uri self) + { + try + { + HttpWebRequest request = (HttpWebRequest) WebRequest.Create(self); + request.Timeout = 3000; + request.AllowAutoRedirect = true; + using WebResponse response = request.GetResponse(); + return true; + } + catch + { + return false; + } + } + + /// + /// Gets the size of a dictionary + /// + /// The dictionary to check + /// The size of the dictionary + public static long GetSize(this DirectoryInfo directory) => IO.GetDirectorySize(directory.FullName); + + /// + /// Adds a directory to an archive recursively + /// + /// The archive to add to + /// The directory to add + /// The name of the directory in-archive + /// Extensions for files to ignore + /// Paths to exclude from adding + /// The new entry + public static ZipArchiveEntry AddDirectory(this ZipArchive archive, string folderPath, string entryName, + string[] ignoredExtensions, string[] ignoredPaths) + { + entryName = entryName.TrimEnd('/'); + ZipArchiveEntry result = archive.CreateEntry($"{entryName}/"); + string[] files = Directory.GetFiles(folderPath); + foreach (string t in files) + if (!ignoredExtensions.Contains(Path.GetExtension(t)) && + !ignoredPaths.Any(s => IO.CheckPathEqual(s, t))) + archive.CreateEntryFromFile(t, $"{entryName}/{Path.GetFileName(t)}"); + string[] dirs = Directory.GetDirectories(folderPath); + foreach (string t in dirs) + if (!ignoredPaths.Any(s => IO.CheckPathEqual(s, t))) + archive.AddDirectory(t, $"{entryName}/{Path.GetFileName(t)}", ignoredExtensions, + ignoredPaths); + return result; + } + } +} \ No newline at end of file diff --git a/Misc/IO.cs b/Core/IO.cs similarity index 97% rename from Misc/IO.cs rename to Core/IO.cs index 5bf4b0f..09fae1b 100644 --- a/Misc/IO.cs +++ b/Core/IO.cs @@ -1,7 +1,7 @@ using System; using System.IO; -namespace CC_Functions.Misc +namespace CC_Functions.Core { /// /// IO functions diff --git a/Misc/SpecialChars.cs b/Core/SpecialChars.cs similarity index 99% rename from Misc/SpecialChars.cs rename to Core/SpecialChars.cs index cfd6390..eca932c 100644 --- a/Misc/SpecialChars.cs +++ b/Core/SpecialChars.cs @@ -1,4 +1,4 @@ -namespace CC_Functions.Misc +namespace CC_Functions.Core { /// /// Characters for use in CC-Functions.CommandLine diff --git a/Misc/GenericExtensions.cs b/Misc/GenericExtensions.cs index 022eed6..34fe67f 100644 --- a/Misc/GenericExtensions.cs +++ b/Misc/GenericExtensions.cs @@ -1,10 +1,9 @@ using System; -using System.Collections.Generic; -using System.Drawing; using System.IO; using System.IO.Compression; using System.Linq; using System.Net; +using CC_Functions.Core; namespace CC_Functions.Misc { @@ -13,234 +12,6 @@ namespace CC_Functions.Misc /// public static class GenericExtensions { - /// - /// Gets an element from the dictionary or adds the default - /// - /// The dictionary to get from - /// The key to check - /// The default value to place - /// The key type - /// The value type - /// The element at the key - public static TValue Get(this IDictionary dict, TKey key, TValue def = default) - { - if (!dict.ContainsKey(key)) - dict[key] = def; - return dict[key]; - } - - /// - /// Sets an element and returns it - /// - /// The dictionary to set in - /// The key to set at - /// The value to place - /// The key type - /// The value type - /// The value that was placed - public static TValue Set(this IDictionary dict, TKey key, TValue val = default) - { - dict[key] = val; - return dict[key]; - } - - /// - /// Tries to cast an object - /// - /// The object to try to parse - /// The parsed object (if successful) or the default (usually null) - /// The type to cast to - /// Whether the cast was successful - public static bool TryCast(this object o, out T parsed) - { - try - { - parsed = (T) o; - return true; - } - catch - { - parsed = default; - return false; - } - } - - /// - /// Runs a function that transforms an object in-line - /// - /// The object to run on - /// The function to run - /// The input type - /// The output type - /// - public static TOut SelectO(this TIn self, Func func) => func.Invoke(self); - - /// - /// Runs a function under a condition in-line (equal to if) - /// - /// The condition to check - /// The function to run - public static void RunIf(bool condition, Action func) - { - if (condition) - func(); - } - - /// - /// Parses a string to a value of an enum - /// - /// The string to parse - /// The enum type (MUST be an enum) - /// The element - public static TEnum ParseToEnum(string value) => (TEnum) Enum.Parse(typeof(TEnum), - Enum.GetNames(typeof(TEnum)).First(s => s.ToLower() == value.ToLower())); - - /// - /// Parses a string to a nullable bool (defaults to null if parse fails) - /// - /// The st string to parse - /// The output nullable bool - public static bool? ParseBool(string value) => - bool.TryParse(value, out bool tmp) ? (bool?) tmp : null; - - /// - /// AND operation for nullable bools (uses True) - /// - /// First bool to check - /// Second bool to check - /// The operation result - public static bool And(this bool? left, bool? right) => left.True() && right.True(); - - /// - /// OR operation for nullable bools (uses True) - /// - /// First bool to check - /// Second bool to check - /// The operation result - public static bool Or(this bool? left, bool? right) => left.True() || right.True(); - - /// - /// XOR operation for nullable bools (uses True) - /// - /// First bool to check - /// Second bool to check - /// The operation result - public static bool Xor(this bool? left, bool? right) => left.Or(right) && !left.And(right); - - /// - /// Whether the nullable bool is true (null->false) - /// - /// Value to check - /// Whether it is true - public static bool True(this bool? self) => self == true; - - /// - /// Whether the nullable bool is false (null->false) - /// - /// Value to check - /// Whether it is false - public static bool False(this bool? self) => self == false; - - /// - /// Whether the nullable bool is null - /// - /// Value to check - /// Whether it is null - public static bool Null(this bool? self) => self == null; - - /// - /// Removes an element from a dictionary by its index (not key) - /// - /// The dictionary to remove from - /// The index of the value - /// The key type - /// The value type - public static void RemoveAt(this Dictionary dict, int index) => - dict.Remove(dict.Keys.ToArray()[index]); - - /// - /// Gets the size of a dictionary - /// - /// The dictionary to check - /// The size of the dictionary - public static long GetSize(this DirectoryInfo directory) => IO.GetDirectorySize(directory.FullName); - - /// - /// Adds a directory to an archive recursively - /// - /// The archive to add to - /// The directory to add - /// The name of the directory in-archive - /// Extensions for files to ignore - /// Paths to exclude from adding - /// The new entry - public static ZipArchiveEntry AddDirectory(this ZipArchive archive, string folderPath, string entryName, - string[] ignoredExtensions, string[] ignoredPaths) - { - entryName = entryName.TrimEnd('/'); - ZipArchiveEntry result = archive.CreateEntry($"{entryName}/"); - string[] files = Directory.GetFiles(folderPath); - foreach (string t in files) - if (!ignoredExtensions.Contains(Path.GetExtension(t)) && - !ignoredPaths.Any(s => IO.CheckPathEqual(s, t))) - archive.CreateEntryFromFile(t, $"{entryName}/{Path.GetFileName(t)}"); - string[] dirs = Directory.GetDirectories(folderPath); - foreach (string t in dirs) - if (!ignoredPaths.Any(s => IO.CheckPathEqual(s, t))) - archive.AddDirectory(t, $"{entryName}/{Path.GetFileName(t)}", ignoredExtensions, - ignoredPaths); - return result; - } - - /// - /// "Unshorten" (follow) an URL - /// - /// The URL to unshorten - /// The unshortened URL - public static Uri Unshorten(this Uri self) - { - HttpWebRequest req = (HttpWebRequest) WebRequest.Create(self); - req.AllowAutoRedirect = true; - req.MaximumAutomaticRedirections = 100; - WebResponse resp = req.GetResponse(); - return resp.ResponseUri; - } - - /// - /// Pings an URL to check for availability - /// - /// The URL to check - /// Whether the service is online - public static bool Ping(this Uri self) - { - try - { - HttpWebRequest request = (HttpWebRequest) WebRequest.Create(self); - request.Timeout = 3000; - request.AllowAutoRedirect = true; - using WebResponse response = request.GetResponse(); - return true; - } - catch - { - return false; - } - } - - /// - /// Rounds a RectangleF to a Rectangle instead of flooring - /// - /// The RectangleF to round - /// The rounded Rectangle - public static Rectangle Round(this RectangleF self) => Rectangle.Round(self); - - /// - /// Ceilings a RectangleF to a Rectangle instead of flooring - /// - /// The RectangleF to ceil (?) - /// The ceiled (?) Rectangle - public static Rectangle Ceiling(this RectangleF self) => Rectangle.Ceiling(self); - /// /// Extension method for Crypto's Encrypt /// diff --git a/Misc/Misc.csproj b/Misc/Misc.csproj index b97cb0c..737c94c 100644 --- a/Misc/Misc.csproj +++ b/Misc/Misc.csproj @@ -7,11 +7,11 @@ false CC-Functions.Misc CC-Functions.Misc - CC24 + JFronny Random pieces of code used across my projects. I do NOT recommend using this in your own project! Copyright 2020 - https://github.com/JFronny/CC-Functions - https://github.com/JFronny/CC-Functions.git + https://gitlab.com/JFronny/CC-Functions + https://gitlab.com/JFronny/CC-Functions.git git 1.1.* 1.0.0.0 @@ -36,4 +36,7 @@ - \ No newline at end of file + + + + diff --git a/W32/W32.csproj b/W32/W32.csproj index 4ebb052..4bded92 100644 --- a/W32/W32.csproj +++ b/W32/W32.csproj @@ -7,11 +7,11 @@ true net461;netcoreapp3.1 false - CC24 + JFronny W32 Additions for CC-Functions Copyright 2020 - https://github.com/JFronny/CC-Functions - https://github.com/JFronny/CC-Functions.git + https://gitlab.com/JFronny/CC-Functions + https://gitlab.com/JFronny/CC-Functions.git git 1.1.* 1.0.0.0