");
26 | help.AddExample(".");
27 | help.AddExample("-o=80 c:\\www");
28 | }
29 |
30 | protected override void ExecuteInternal()
31 | {
32 | base.ExecuteInternal();
33 |
34 | var directoryToServe = GetArgValueDirectory(0, "directoryToServe", useCurrentDirectoryAsDefault: true);
35 |
36 | var config = GetConfig();
37 | config.DirectoryToServe = directoryToServe;
38 | config.DirectoryToServeUrlPath = "/";
39 |
40 | log.Info("Serving file from " + directoryToServe);
41 | LoopUntilKey(config);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.Console/Commands/WebServerUtilityEncryptFile.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System.Linq;
16 | using EmbedIO;
17 | using MaxRunSoftware.Utilities.External;
18 |
19 | namespace MaxRunSoftware.Utilities.Console.Commands;
20 |
21 | public class WebServerUtilityEncryptFile : WebServerUtilityBase
22 | {
23 | public override HttpVerbs Verbs => HttpVerbs.Any;
24 |
25 | public override string HandleHtml()
26 | {
27 | var html = new HtmlWriter();
28 | html.Title = "Encrypt File";
29 | var files = Files;
30 | var password = GetParameterString("password").TrimOrNull();
31 | if (files.Count == 0 || password == null)
32 | {
33 | html.Form("?");
34 | html.P();
35 | html.InputPassword("password", "Password ");
36 | html.PEnd();
37 | html.P("Click on the 'Choose File' button to upload a file");
38 | html.InputFile("file");
39 | html.InputSubmit("Encrypt");
40 | html.FormEnd();
41 | }
42 | else
43 | {
44 | var file = files.First();
45 | var fileName = file.Key;
46 | var fileData = file.Value;
47 | var encryptedBytes = Encryption.Encrypt(Constant.ENCODING_UTF8.GetBytes(password), fileData);
48 | Context.SendFile(encryptedBytes, fileName);
49 | html.P("File encrypted");
50 | }
51 |
52 | return html.ToString();
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.Console/Commands/WebServerUtilityGenerateKeyPair.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System;
16 | using MaxRunSoftware.Utilities.External;
17 |
18 | namespace MaxRunSoftware.Utilities.Console.Commands;
19 |
20 | public class WebServerUtilityGenerateKeyPair : WebServerUtilityBase
21 | {
22 | public (bool success, string publicKey, string privateKey) Handle()
23 | {
24 | var lengthN = GetParameterInt("length");
25 | if (lengthN == null) return (false, null, null);
26 |
27 | var length = lengthN.Value;
28 |
29 | var keyPair = Encryption.GenerateKeyPair(length);
30 | return (true, keyPair.publicKey, keyPair.privateKey);
31 | }
32 |
33 | public override string HandleHtml()
34 | {
35 | try
36 | {
37 | var result = Handle();
38 |
39 | var html = @"
40 |
51 | ";
52 | if (result.success)
53 | {
54 | html += $@"
55 |
56 | Public Key
57 |
58 |
59 |
60 | Private Key
61 |
62 | ";
63 | }
64 |
65 | return External.WebServer.HtmlMessage("Asymmetric Key Pair", html.Replace("'", "\""));
66 | }
67 | catch (Exception e) { return External.WebServer.HtmlMessage(e.GetType().FullNameFormatted(), e.ToString()); }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.Console/Commands/WebServerUtilityGenerateRandomFile.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using MaxRunSoftware.Utilities.External;
16 |
17 | namespace MaxRunSoftware.Utilities.Console.Commands;
18 |
19 | public class WebServerUtilityGenerateRandomFile : WebServerUtilityBase
20 | {
21 | public string Handle()
22 | {
23 | var generateRandom = new WebServerUtilityGenerateRandom();
24 | var randomString = (string)generateRandom.Handle(Context);
25 | Context.SendFile(randomString, "random.txt");
26 | return "Generated Random File";
27 | }
28 |
29 | public override string HandleHtml() => Handle();
30 | }
31 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.Console/Commands/WindowsTaskSchedulerRemove.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using MaxRunSoftware.Utilities.External;
16 |
17 | namespace MaxRunSoftware.Utilities.Console.Commands;
18 |
19 | public class WindowsTaskSchedulerRemove : WindowsTaskSchedulerBase
20 | {
21 | protected override void CreateHelp(CommandHelpBuilder help)
22 | {
23 | base.CreateHelp(help);
24 | help.AddSummary("Deletes a task from the Windows Task Scheduler");
25 | help.AddValue("");
26 | help.AddExample(HelpExamplePrefix + " MyTask");
27 | }
28 |
29 | protected override void ExecuteInternal()
30 | {
31 | base.ExecuteInternal();
32 |
33 | var taskPath = GetArgValueTrimmed(0);
34 | if (taskPath == null) throw ArgsException.ValueNotSpecified(nameof(taskPath));
35 |
36 | using (var scheduler = GetTaskScheduler())
37 | {
38 | var t = scheduler.GetTask(taskPath);
39 | if (t == null) throw new ArgsException(nameof(taskPath), "Task does not exist " + taskPath);
40 |
41 | log.Debug("Deleting task " + t.GetPath());
42 | scheduler.TaskDelete(t);
43 | log.Info("Successfully deleted task " + t.GetPath());
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.Console/Commands/Xslt.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities.Console.Commands;
16 |
17 | public class Xslt : Command
18 | {
19 | protected override void CreateHelp(CommandHelpBuilder help)
20 | {
21 | help.AddSummary("Applies an XSLT transform to an XML file");
22 | // ReSharper disable once StringLiteralTypo
23 | help.AddExample("mytransform.xslt myxml.xml");
24 | help.AddValue(" ");
25 | }
26 |
27 | protected override void ExecuteInternal()
28 | {
29 | var xsltFile = GetArgValueTrimmed(0);
30 | xsltFile.CheckValueNotNull(nameof(xsltFile), log);
31 | var xsltContent = ReadFile(xsltFile);
32 |
33 | var xmlFile = GetArgValueTrimmed(1);
34 | xmlFile.CheckValueNotNull(nameof(xmlFile), log);
35 | var xmlFiles = ParseInputFiles(xmlFile.Yield());
36 | log.Debug(xmlFiles, nameof(xmlFiles));
37 |
38 | foreach (var xml in xmlFiles)
39 | {
40 | var xmlContent = ReadFile(xml);
41 | var data = XmlWriter.ApplyXslt(xsltContent, xmlContent);
42 | WriteFile(xml, data);
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.Console/Encoding.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities.Console;
16 |
17 | public enum FileEncoding
18 | {
19 | ASCII,
20 | BigEndianUnicode,
21 | Default,
22 | Unicode,
23 | UTF32,
24 | UTF8,
25 | UTF8BOM
26 | }
27 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.Console/Extensions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System.Collections.Generic;
16 |
17 | namespace MaxRunSoftware.Utilities.Console;
18 |
19 | public static class Extensions
20 | {
21 | public static void Debug(this ILogger log, IEnumerable enumerable, string name)
22 | {
23 | if (enumerable == null) return;
24 |
25 | var list = new List(enumerable);
26 | for (var i = 0; i < list.Count; i++) log.Debug(name + "[" + i + "]: " + list[i]);
27 | }
28 |
29 | public static void DebugParameter(this ILogger log, string parameterName, object parameterValue) => log.Debug(parameterName + ": " + parameterValue);
30 |
31 | public static string CheckValueNotNull(this string val, string valName, ILogger log)
32 | {
33 | log.DebugParameter(valName, val);
34 | if (val == null) throw ArgsException.ValueNotSpecified(valName);
35 |
36 | return val;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.Console/ICommand.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities.Console;
16 |
17 | public interface ICommand
18 | {
19 | string Name { get; }
20 | string[] Args { get; set; }
21 | void Execute();
22 | string HelpSummary { get; }
23 | string HelpDetails { get; }
24 | bool IsHidden { get; }
25 | bool SuppressBanner { get; }
26 | }
27 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.Console/Icon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maxrunsoftware/huc/35d4f91a2503005c5946fa805079a436534c6f04/MaxRunSoftware.Utilities.Console/Icon.ico
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.Console/MaxRunSoftware.Utilities.Console.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | 1.13.1
7 | Misc command line helper utilities
8 | MaxRunSoftware.Utilities.Console.Program
9 | Icon.ico
10 |
11 |
12 |
13 | 4
14 | 1701;1702;CA1416
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.Console/Version.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities.Console;
16 |
17 | public static class Version
18 | {
19 | public static string Value => Utilities.Version.Value;
20 | }
21 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.External/ActiveDirectory/ActiveDirectoryGroupType.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities.External;
16 |
17 | ///
18 | /// GroupType enumerates the type of group objects in Active Directory.
19 | ///
20 | public enum ActiveDirectoryGroupType
21 | {
22 | ///
23 | /// Specifies a group that can contain accounts from the domain and other global
24 | /// groups from the same domain. This type of group can be exported to a different
25 | /// domain.
26 | ///
27 | GlobalDistributionGroup = 2,
28 |
29 | ///
30 | /// Specifies a group that can contain accounts from any domain, other domain
31 | /// local groups from the same domain, global groups from any domain, and
32 | /// universal groups. This type of group should not be included in access-control
33 | /// lists of resources in other domains. This type of group is intended for use
34 | /// with the LDAP provider.
35 | ///
36 | LocalDistributionGroup = 4,
37 |
38 | ///
39 | /// Specifies a group that can contain accounts from any domain, global
40 | /// groups from any domain, and other universal groups. This type of group
41 | /// cannot contain domain local groups.
42 | ///
43 | UniversalDistributionGroup = 8,
44 |
45 | GlobalSecurityGroup = -2147483646,
46 |
47 | LocalSecurityGroup = -2147483644,
48 |
49 | BuiltInGroup = -2147483643,
50 |
51 | UniversalSecurityGroup = -2147483640
52 | }
53 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.External/Ftp/FtpClientFile.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities.External;
16 |
17 | public class FtpClientFile
18 | {
19 | public string Name { get; }
20 |
21 | public string FullName { get; }
22 |
23 | public FtpClientFileType Type { get; }
24 |
25 | public FtpClientFile(string name, string fullName, FtpClientFileType type)
26 | {
27 | Name = name;
28 | FullName = fullName;
29 | Type = type;
30 | }
31 |
32 | ///
33 | /// Checks to see if our FullName matches a pathOrPattern value.
34 | /// if FullName=/dir1/file1.txt pathOrPattern=/*/file?.txt isCaseSensitive=true IsMatch=true
35 | /// if FullName=/dir1/file1.txt pathOrPattern=/*/FILE?.TXT isCaseSensitive=true IsMatch=false
36 | ///
37 | ///
38 | ///
39 | ///
40 | public bool IsMatch(string pathOrPattern, bool isCaseSensitive)
41 | {
42 | pathOrPattern = pathOrPattern.CheckNotNullTrimmed(nameof(pathOrPattern));
43 | var source = pathOrPattern.StartsWith("/") ? FullName : Name;
44 | return source.EqualsWildcard(pathOrPattern, !isCaseSensitive);
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.External/Ftp/FtpClientFileType.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities.External;
16 |
17 | public enum FtpClientFileType
18 | {
19 | Unknown,
20 | Directory,
21 | File,
22 | Link
23 | }
24 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.External/Ftp/FtpClientFtpSEncryptionMode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities.External;
16 |
17 | public enum FtpClientFtpSEncryptionMode
18 | {
19 | Explicit,
20 | Implicit
21 | }
22 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.External/Ftp/IFtpClient.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System;
16 | using System.Collections.Generic;
17 |
18 | namespace MaxRunSoftware.Utilities.External;
19 |
20 | public interface IFtpClient : IDisposable
21 | {
22 | string ServerInfo { get; }
23 |
24 | string WorkingDirectory { get; }
25 |
26 | void GetFile(string remoteFile, string localFile);
27 |
28 | void PutFile(string remoteFile, string localFile);
29 |
30 | void DeleteFile(string remoteFile);
31 |
32 | IEnumerable ListFiles(string remotePath);
33 | }
34 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.External/GitHub.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System.Collections.Generic;
16 | using System.Threading.Tasks;
17 | using Octokit;
18 |
19 | namespace MaxRunSoftware.Utilities.External;
20 |
21 | public class GitHub
22 | {
23 | private readonly string username;
24 | private readonly string repositoryName;
25 |
26 | public GitHub(string username, string repositoryName)
27 | {
28 | this.username = username.CheckNotNullTrimmed(nameof(username));
29 | this.repositoryName = repositoryName.CheckNotNullTrimmed(nameof(repositoryName));
30 | }
31 |
32 | public IReadOnlyList Releases
33 | {
34 | get
35 | {
36 | // https://api.github.com/repos/maxrunsoftware/huc/releases
37 | var releasesTask = Task.Run(async () => await GetReleasesAsync());
38 | var releases = releasesTask.Result;
39 | return releases;
40 | }
41 | }
42 |
43 | private async Task> GetReleasesAsync()
44 | {
45 | var client = new GitHubClient(new ProductHeaderValue(username));
46 |
47 | var result = await client.Repository.Release.GetAll(username, repositoryName);
48 | return result;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.External/Ldap/LdapExtensions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System;
16 | using System.DirectoryServices;
17 | using System.DirectoryServices.AccountManagement;
18 | using System.Reflection;
19 | using System.Text;
20 |
21 | namespace MaxRunSoftware.Utilities.External;
22 |
23 | public static class LdapExtensions
24 | {
25 | private static readonly ILogger log = Logging.LogFactory.GetLogger(MethodBase.GetCurrentMethod()!.DeclaringType);
26 |
27 | private static string ToStringDebugOutput(object o)
28 | {
29 | var sb = new StringBuilder();
30 | foreach (var prop in ClassReaderWriter.GetProperties(o.GetType(), true, isInstance: true))
31 | {
32 | object val = null;
33 | try { val = prop.GetValue(o); }
34 | catch (Exception e) { log.Debug("Error retrieving property " + o.GetType().FullNameFormatted() + "." + prop.Name, e); }
35 |
36 | sb.AppendLine(" " + prop.Name + ": " + val.ToStringGuessFormat());
37 | }
38 |
39 | return sb.ToString();
40 | }
41 |
42 | public static string ToStringDebug(this DirectoryEntry entry) => ToStringDebugOutput(entry);
43 |
44 | public static string ToStringDebug(this DirectorySearcher searcher) => ToStringDebugOutput(searcher);
45 |
46 | public static string ToStringDebug(this PrincipalContext context) => ToStringDebugOutput(context);
47 | }
48 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.External/Logging.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities.External;
16 |
17 | internal class Logging
18 | {
19 | public static ILogFactory LogFactory => Utilities.LogFactory.LogFactoryImpl;
20 | }
21 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.External/MaxRunSoftware.Utilities.External.csproj.DotSettings:
--------------------------------------------------------------------------------
1 |
2 | True
3 | True
4 | True
5 | True
6 | True
7 | True
8 | True
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.External/VMware/VMwareDatacenter.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System.Collections.Generic;
16 | using Newtonsoft.Json.Linq;
17 |
18 | namespace MaxRunSoftware.Utilities.External;
19 |
20 | public class VMwareDatacenter : VMwareObject
21 | {
22 | public string Name { get; }
23 | public string Datacenter { get; }
24 | public string DatastoreFolder { get; }
25 | public string HostFolder { get; }
26 | public string NetworkFolder { get; }
27 | public string VMFolder { get; }
28 |
29 | public VMwareDatacenter(VMwareClient vmware, JToken obj)
30 | {
31 | Name = obj.ToString("name");
32 | Datacenter = obj.ToString("datacenter");
33 |
34 | obj = QueryValueObjectSafe(vmware, "/rest/vcenter/datacenter/" + Datacenter);
35 | if (obj != null)
36 | {
37 | DatastoreFolder = obj.ToString("datastore_folder");
38 | HostFolder = obj.ToString("host_folder");
39 | NetworkFolder = obj.ToString("network_folder");
40 | VMFolder = obj.ToString("vm_folder");
41 | }
42 | }
43 |
44 | public static IEnumerable Query(VMwareClient vmware)
45 | {
46 | foreach (var obj in vmware.GetValueArray("/rest/vcenter/datacenter")) yield return new VMwareDatacenter(vmware, obj);
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.External/VMware/VMwareDatastore.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System.Collections.Generic;
16 | using Newtonsoft.Json.Linq;
17 |
18 | // ReSharper disable RedundantCast
19 |
20 | namespace MaxRunSoftware.Utilities.External;
21 |
22 | public class VMwareDatastore : VMwareObject
23 | {
24 | public string Name { get; }
25 | public string Datastore { get; }
26 | public string Type { get; }
27 | public long? FreeSpace { get; }
28 | public long? Capacity { get; }
29 | public long? Used => FreeSpace == null || Capacity == null ? null : Capacity.Value - FreeSpace.Value;
30 | public byte? PercentFree => FreeSpace == null || Capacity == null ? null : (byte)((double)FreeSpace.Value / (double)Capacity.Value * (double)100);
31 | public byte? PercentUsed => PercentFree == null ? null : (byte)((double)100 - (double)PercentFree.Value);
32 |
33 | public bool? Accessible { get; }
34 | public bool? MultipleHostAccess { get; }
35 | public bool? ThinProvisioningSupported { get; }
36 |
37 | public VMwareDatastore(VMwareClient vmware, JToken obj)
38 | {
39 | Name = obj.ToString("name");
40 | Datastore = obj.ToString("datastore");
41 | Type = obj.ToString("type");
42 | FreeSpace = obj.ToLong("free_space");
43 | Capacity = obj.ToLong("capacity");
44 |
45 | obj = QueryValueObjectSafe(vmware, "/rest/vcenter/datastore/" + Datastore);
46 | if (obj != null)
47 | {
48 | Accessible = obj.ToBool("accessible");
49 | MultipleHostAccess = obj.ToBool("multiple_host_access");
50 | ThinProvisioningSupported = obj.ToBool("thin_provisioning_supported");
51 | }
52 | }
53 |
54 | public static IEnumerable Query(VMwareClient vmware)
55 | {
56 | foreach (var obj in vmware.GetValueArray("/rest/vcenter/datastore")) yield return new VMwareDatastore(vmware, obj);
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.External/VMware/VMwareFolder.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System.Collections.Generic;
16 | using Newtonsoft.Json.Linq;
17 |
18 | namespace MaxRunSoftware.Utilities.External;
19 |
20 | public class VMwareFolder : VMwareObject
21 | {
22 | public string Name { get; }
23 | public string Folder { get; }
24 | public string Type { get; }
25 |
26 | // ReSharper disable once UnusedParameter.Local
27 | public VMwareFolder(VMwareClient vmware, JToken obj)
28 | {
29 | Name = obj.ToString("name");
30 | Folder = obj.ToString("folder");
31 | Type = obj.ToString("type");
32 | }
33 |
34 | public static IEnumerable Query(VMwareClient vmware)
35 | {
36 | foreach (var obj in vmware.GetValueArray("/rest/vcenter/folder")) yield return new VMwareFolder(vmware, obj);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.External/VMware/VMwareHost.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System.Collections.Generic;
16 | using Newtonsoft.Json.Linq;
17 |
18 | namespace MaxRunSoftware.Utilities.External;
19 |
20 | public class VMwareHost : VMwareObject
21 | {
22 | public enum HostPowerState
23 | {
24 | Unknown,
25 | PoweredOn,
26 | PoweredOff,
27 | Standby
28 | }
29 |
30 | public enum HostConnectionState
31 | {
32 | Unknown,
33 | Connected,
34 | Disconnected,
35 | NotResponding
36 | }
37 |
38 | public string Name { get; }
39 | public string Host { get; }
40 | public HostConnectionState ConnectionState { get; }
41 | public HostPowerState PowerState { get; }
42 |
43 | // ReSharper disable once UnusedParameter.Local
44 | public VMwareHost(VMwareClient vmware, JToken obj)
45 | {
46 | Name = obj.ToString("name");
47 | Host = obj.ToString("host");
48 |
49 | var connectionState = obj.ToString("connection_state");
50 | if (connectionState == null) { ConnectionState = HostConnectionState.Unknown; }
51 | else if (connectionState.EqualsCaseInsensitive("CONNECTED")) { ConnectionState = HostConnectionState.Connected; }
52 | else if (connectionState.EqualsCaseInsensitive("DISCONNECTED")) { ConnectionState = HostConnectionState.Disconnected; }
53 | else if (connectionState.EqualsCaseInsensitive("NOT_RESPONDING")) ConnectionState = HostConnectionState.NotResponding;
54 |
55 | var powerState = obj.ToString("power_state");
56 | if (powerState == null) { PowerState = HostPowerState.Unknown; }
57 | else if (powerState.EqualsCaseInsensitive("POWERED_OFF")) { PowerState = HostPowerState.PoweredOff; }
58 | else if (powerState.EqualsCaseInsensitive("POWERED_ON")) { PowerState = HostPowerState.PoweredOn; }
59 | else if (powerState.EqualsCaseInsensitive("STANDBY")) PowerState = HostPowerState.Standby;
60 | }
61 |
62 | public static IEnumerable Query(VMwareClient vmware)
63 | {
64 | foreach (var obj in vmware.GetValueArray("/rest/vcenter/host")) yield return new VMwareHost(vmware, obj);
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.External/VMware/VMwareNetwork.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System.Collections.Generic;
16 | using Newtonsoft.Json.Linq;
17 |
18 | namespace MaxRunSoftware.Utilities.External;
19 |
20 | public class VMwareNetwork : VMwareObject
21 | {
22 | public string Name { get; }
23 | public string Network { get; }
24 | public string Type { get; }
25 |
26 | // ReSharper disable once UnusedParameter.Local
27 | public VMwareNetwork(VMwareClient vmware, JToken obj)
28 | {
29 | Name = obj.ToString("name");
30 | Network = obj.ToString("network");
31 | Type = obj.ToString("type");
32 | }
33 |
34 | public static IEnumerable Query(VMwareClient vmware)
35 | {
36 | foreach (var obj in vmware.GetValueArray("/rest/vcenter/network")) yield return new VMwareNetwork(vmware, obj);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.External/VMware/VMwareResourcePool.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System.Collections.Generic;
16 | using Newtonsoft.Json.Linq;
17 |
18 | namespace MaxRunSoftware.Utilities.External;
19 |
20 | public class VMwareResourcePool : VMwareObject
21 | {
22 | public string Name { get; }
23 | public string ResourcePool { get; }
24 |
25 | // ReSharper disable once UnusedParameter.Local
26 | public VMwareResourcePool(VMwareClient vmware, JToken obj)
27 | {
28 | Name = obj.ToString("name");
29 | ResourcePool = obj.ToString("resource_pool");
30 | }
31 |
32 | public static IEnumerable Query(VMwareClient vmware)
33 | {
34 | foreach (var obj in vmware.GetValueArray("/rest/vcenter/resource-pool")) yield return new VMwareResourcePool(vmware, obj);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.External/VMware/VMwareStoragePolicy.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System.Collections.Generic;
16 | using System.Linq;
17 | using Newtonsoft.Json.Linq;
18 |
19 | namespace MaxRunSoftware.Utilities.External;
20 |
21 | public class VMwareStoragePolicy : VMwareObject
22 | {
23 | public class Disk : VMwareObject
24 | {
25 | public string Key { get; }
26 | public string VmHome { get; }
27 | public IReadOnlyList Disks { get; }
28 |
29 | public Disk(JToken obj)
30 | {
31 | Key = obj.ToString("key");
32 | VmHome = obj.ToString("value", "vm_home");
33 | Disks = obj["value"]?["disks"].OrEmpty().Select(o => o?.ToString()).WhereNotNull().ToList();
34 | }
35 | }
36 |
37 | public string Name { get; }
38 | public string Description { get; }
39 | public string Policy { get; }
40 | public IReadOnlyList Disks { get; }
41 |
42 | public VMwareStoragePolicy(VMwareClient vmware, JToken obj)
43 | {
44 | Name = obj.ToString("name");
45 | Description = obj.ToString("description");
46 | Policy = obj.ToString("policy");
47 |
48 | Disks = vmware.GetValueArray($"/rest/vcenter/storage/policies/{Policy}/vm").OrEmpty().Select(o => new Disk(o)).ToList();
49 | }
50 |
51 | public static IEnumerable Query(VMwareClient vmware)
52 | {
53 | foreach (var obj in vmware.GetValueArray("/rest/vcenter/storage/policies")) yield return new VMwareStoragePolicy(vmware, obj);
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.External/Version.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities.External;
16 |
17 | public static class Version
18 | {
19 | public static string Value => Utilities.Version.Value;
20 | }
21 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.External/WebBrowser/WebBrowserType.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities.External;
16 |
17 | public enum WebBrowserType
18 | {
19 | Chrome,
20 | Edge,
21 | Firefox,
22 | InternetExplorer,
23 | Opera
24 | }
25 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.External/WindowsTaskScheduler/WindowsTaskSchedulerExtensions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System;
16 | using Microsoft.Win32.TaskScheduler;
17 |
18 | namespace MaxRunSoftware.Utilities.External;
19 |
20 | public static class WindowsTaskSchedulerExtensions
21 | {
22 | public static WindowsTaskSchedulerPath GetPath(this Task task) => new(task);
23 |
24 | public static WindowsTaskSchedulerPath GetPath(this TaskFolder folder) => new(folder);
25 |
26 | public static DaysOfTheWeek ToDaysOfTheWeek(this DayOfWeek dayOfWeek) =>
27 | dayOfWeek switch
28 | {
29 | DayOfWeek.Sunday => DaysOfTheWeek.Sunday,
30 | DayOfWeek.Monday => DaysOfTheWeek.Monday,
31 | DayOfWeek.Tuesday => DaysOfTheWeek.Tuesday,
32 | DayOfWeek.Wednesday => DaysOfTheWeek.Wednesday,
33 | DayOfWeek.Thursday => DaysOfTheWeek.Thursday,
34 | DayOfWeek.Friday => DaysOfTheWeek.Friday,
35 | DayOfWeek.Saturday => DaysOfTheWeek.Saturday,
36 | _ => throw new ArgumentOutOfRangeException(nameof(dayOfWeek), dayOfWeek, null)
37 | };
38 | }
39 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.Tests/AtomicBooleanTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | // ReSharper disable ConditionIsAlwaysTrueOrFalse
16 |
17 | namespace MaxRunSoftware.Utilities.Tests;
18 |
19 | public class AtomicBooleanTests
20 | {
21 | [Fact]
22 | public void EqualsWork()
23 | {
24 | var ab1 = new AtomicBoolean(true);
25 | var ab2 = new AtomicBoolean(false);
26 |
27 | var b1 = true;
28 | var b2 = false;
29 |
30 | Assert.True(ab1.Equals(b1));
31 | Assert.True(ab2.Equals(b2));
32 | Assert.True(b1.Equals(ab1));
33 | Assert.True(b2.Equals(ab2));
34 | Assert.True(ab1.Equals(new AtomicBoolean(true)));
35 |
36 | Assert.False(ab1.Equals(b2));
37 | Assert.False(ab2.Equals(b1));
38 | Assert.False(b1.Equals(ab2));
39 | Assert.False(b2.Equals(ab1));
40 | Assert.False(ab1.Equals(ab2));
41 | Assert.False(ab2.Equals(ab1));
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.Tests/BucketTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | // ReSharper disable RedundantAssignment
16 |
17 | namespace MaxRunSoftware.Utilities.Tests;
18 |
19 | public class BucketReadOnlyTests
20 | {
21 | [Fact]
22 | public void Testing()
23 | {
24 | var cgf = new CacheGenFunc();
25 |
26 | var bro = new BucketCache(cgf.GenVal);
27 | Assert.Equal(0, cgf.TimesCalled);
28 | Assert.Empty(bro.Keys);
29 |
30 | var val = bro["a"];
31 | Assert.Equal("Va", val);
32 | Assert.Equal(1, cgf.TimesCalled);
33 | Assert.Single(bro.Keys);
34 |
35 | val = bro["a"];
36 | Assert.Equal("Va", val);
37 | Assert.Equal(1, cgf.TimesCalled);
38 | Assert.Single(bro.Keys);
39 |
40 | val = bro["b"];
41 | Assert.Equal("Vb", val);
42 | Assert.Equal(2, cgf.TimesCalled);
43 | Assert.Equal(2, bro.Keys.Count());
44 |
45 | val = bro["a"];
46 | Assert.Equal("Va", val);
47 | Assert.Equal(2, cgf.TimesCalled);
48 | Assert.Equal(2, bro.Keys.Count());
49 |
50 | val = bro["n"];
51 | Assert.Null(val);
52 | Assert.Equal(3, cgf.TimesCalled);
53 | Assert.Equal(3, bro.Keys.Count());
54 |
55 | val = bro["n"];
56 | Assert.Null(val);
57 | Assert.Equal(3, cgf.TimesCalled);
58 | Assert.Equal(3, bro.Keys.Count());
59 |
60 | try
61 | {
62 | val = bro[null];
63 | Assert.True(false, "Expecting exception to be thrown");
64 | }
65 | catch (Exception) { Assert.True(true); }
66 | }
67 |
68 | private class CacheGenFunc
69 | {
70 | public int TimesCalled { get; private set; }
71 |
72 | public string GenVal(string key)
73 | {
74 | TimesCalled++;
75 | if (key == "n") return null;
76 |
77 | return "V" + key;
78 | }
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.Tests/Config.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | // ReSharper disable InconsistentNaming
16 |
17 | namespace MaxRunSoftware.Utilities.Tests;
18 |
19 | public static class Config
20 | {
21 | public static string Sql_MsSql_ConnectionString => "Server=172.16.46.3;Database=NorthWind;User Id=testuser;Password=testpass;TrustServerCertificate=True;";
22 |
23 | public static string Sql_MySql_ConnectionString => "Server=172.16.46.3;Database=NorthWind;User Id=testuser;Password=testpass;";
24 |
25 | //public static string Sql_Oracle_ConnectionString => "Data Source=172.16.46.9;User Id=system;Password=oracle;";
26 | public static string Sql_Oracle_ConnectionString => "Data Source=172.16.46.9:1521/orcl;User Id=testuser;Password=testpass;";
27 |
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.Tests/MaxRunSoftware.Utilities.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net6.0
5 | false
6 | 1.13.1
7 |
8 |
9 |
10 |
11 |
12 | runtime; build; native; contentfiles; analyzers; buildtransitive
13 | all
14 |
15 |
16 | runtime; build; native; contentfiles; analyzers; buildtransitive
17 | all
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.Tests/MaxRunSoftware.Utilities.Tests.csproj.DotSettings:
--------------------------------------------------------------------------------
1 |
2 | True
3 | True
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.Tests/Threading/ExecutablePoolTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities.Tests;
16 |
17 | public class ExecutablePoolTests
18 | {
19 | public class Executable : IExecutable
20 | {
21 | public int ExecutedCount { get; private set; }
22 |
23 | public void Execute()
24 | {
25 | Thread.Sleep(10);
26 | ExecutedCount++;
27 |
28 | }
29 | }
30 |
31 | public class OnComplete
32 | {
33 | public int CompletedCount { get; private set; }
34 | public void Complete(ExecutablePool pool) => CompletedCount++;
35 |
36 | }
37 |
38 | [Fact]
39 | public void AllTasksExecuted()
40 | {
41 | var list = new List();
42 | var executableCount = 1000;
43 | for (var i = 0; i < executableCount; i++) list.Add(new Executable());
44 |
45 | var oc = new OnComplete();
46 |
47 |
48 |
49 | var c = new ExecutablePoolConfig
50 | {
51 | Enumerator = list.GetEnumerator(),
52 | NumberOfThreads = 12,
53 | OnComplete = oc.Complete,
54 | };
55 |
56 | using (var ep = ExecutablePool.Execute(c))
57 | {
58 | while (!ep.GetState(false).IsComplete)
59 | {
60 | Thread.Sleep(100);
61 | }
62 |
63 | var state = ep.GetState(true);
64 | Assert.True(state.ExecutingItems.IsEmpty());
65 | Assert.True(state.ThreadsActive == 0);
66 | Assert.True(state.ThreadsInactive == 12);
67 | Assert.True(state.ThreadsTotal == 12);
68 | }
69 |
70 |
71 | Assert.True(list.All(o => o.ExecutedCount == 1));
72 | Assert.True(oc.CompletedCount == 1);
73 |
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.Tests/Threading/SingleUseTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities.Tests;
16 |
17 | public class SingleUseTests
18 | {
19 | [Fact]
20 | public void SingleThreaded()
21 | {
22 | var su = new SingleUse();
23 | Assert.False(su.IsUsed);
24 |
25 | Assert.True(su.TryUse());
26 | Assert.True(su.IsUsed);
27 | Assert.False(su.TryUse());
28 |
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities.Tests/Usings.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | global using Xunit;
16 | global using System;
17 | global using System.Collections.Generic;
18 | global using System.Linq;
19 | global using System.Threading;
20 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Buckets/BucketCache.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | ///
18 | /// Non-thread safe cache implementation using backing dictionary and value generation function
19 | ///
20 | /// Key
21 | /// Generated Value
22 | public class BucketCache : IBucketReadOnly
23 | {
24 | private readonly IDictionary dictionary;
25 | private readonly Func factory;
26 |
27 | public BucketCache(Func factory, IDictionary dictionary)
28 | {
29 | this.factory = factory.CheckNotNull(nameof(factory));
30 | this.dictionary = dictionary.CheckNotNull(nameof(dictionary));
31 | }
32 |
33 | public BucketCache(Func factory) : this(factory, new Dictionary()) { }
34 |
35 | public IEnumerable Keys => dictionary.Keys;
36 |
37 | public TValue this[TKey key]
38 | {
39 | get
40 | {
41 | if (!dictionary.TryGetValue(key, out var value)) dictionary[key] = value = factory(key);
42 |
43 | return value;
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Buckets/BucketCacheThreadSafe.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | ///
18 | /// Thread safe cache implementation using backing dictionary and value generation function.
19 | ///
20 | /// Key
21 | /// Generated Value
22 | public class BucketCacheThreadSafe : IBucketReadOnly
23 | {
24 | private readonly IDictionary dictionary;
25 | private readonly Func factory;
26 | private readonly object locker = new();
27 |
28 | public BucketCacheThreadSafe(Func factory, IDictionary dictionary)
29 | {
30 | this.factory = factory.CheckNotNull(nameof(factory));
31 | this.dictionary = dictionary.CheckNotNull(nameof(dictionary));
32 | }
33 |
34 | public BucketCacheThreadSafe(Func factory) : this(factory, new Dictionary()) { }
35 |
36 | public IEnumerable Keys
37 | {
38 | get
39 | {
40 | lock (locker) { return dictionary.Keys; }
41 | }
42 | }
43 |
44 | public TValue this[TKey key]
45 | {
46 | get
47 | {
48 | lock (locker)
49 | {
50 | if (!dictionary.TryGetValue(key, out var value))
51 | {
52 | value = factory(key);
53 | dictionary.Add(key, value);
54 | }
55 |
56 | return value;
57 | }
58 | }
59 | }
60 |
61 | public void Clear()
62 | {
63 | lock (locker) { dictionary.Clear(); }
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Buckets/BucketExtensions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | public static class BucketExtensions
18 | {
19 | public static IBucket AsBucket(this IDictionary dictionary) => new BucketDictionaryWrapper(dictionary);
20 |
21 | public static IEnumerable<(TKey key, TValue value)> GetItems(this IBucketReadOnly bucket) => bucket.Keys.Select(key => (key, bucket[key]));
22 |
23 | private sealed class BucketDictionaryWrapper : IBucket
24 | {
25 | private readonly IDictionary dictionary;
26 |
27 | public BucketDictionaryWrapper(IDictionary dictionary) { this.dictionary = dictionary; }
28 |
29 | public TValue this[TKey key]
30 | {
31 | get => dictionary[key];
32 | set => dictionary[key] = value;
33 | }
34 |
35 | TValue IBucketReadOnly.this[TKey key] => dictionary[key];
36 |
37 | public IEnumerable Keys => dictionary.Keys;
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Buckets/BucketStoreBase.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | public abstract class BucketStoreBase : IBucketStore
18 | {
19 | public IBucket this[string name] => new Bucket(this, name.CheckNotNullTrimmed(nameof(name)));
20 |
21 | protected virtual string CleanName(string name) => name.CheckNotNullTrimmed(nameof(name));
22 |
23 | protected virtual TKey CleanKey(TKey key) => key;
24 |
25 | protected virtual TValue CleanValue(TValue value) => value;
26 |
27 | protected abstract TValue GetValue(string bucketName, TKey bucketKey);
28 |
29 | protected abstract IEnumerable GetKeys(string bucketName);
30 |
31 | protected abstract void SetValue(string bucketName, TKey bucketKey, TValue bucketValue);
32 |
33 | private sealed class Bucket : IBucket
34 | {
35 | private readonly string bucketName;
36 | private readonly BucketStoreBase bucketStore;
37 |
38 | public Bucket(BucketStoreBase bucketStore, string bucketName)
39 | {
40 | this.bucketStore = bucketStore.CheckNotNull(nameof(bucketStore));
41 | this.bucketName = bucketName.CheckNotNullTrimmed(nameof(bucketName));
42 | }
43 |
44 | public IEnumerable Keys =>
45 | bucketStore.GetKeys(bucketStore.CleanName(bucketName)) ?? Enumerable.Empty();
46 |
47 | public TTValue this[TTKey key]
48 | {
49 | get => bucketStore.CleanValue(bucketStore.GetValue(bucketStore.CleanName(bucketName),
50 | bucketStore.CleanKey(key)));
51 | set => bucketStore.SetValue(bucketStore.CleanName(bucketName), bucketStore.CleanKey(key),
52 | bucketStore.CleanValue(value));
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Buckets/BucketStoreMemory.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | public class BucketStoreMemory : BucketStoreBase
18 | {
19 | private static readonly Func> dictionaryFactoryDefault = () => new Dictionary();
20 |
21 | private readonly Dictionary> buckets = new(StringComparer.OrdinalIgnoreCase);
22 | private readonly Func> dictionaryFactory;
23 | private readonly TValue nullValue;
24 |
25 | public BucketStoreMemory(TValue nullValue = default, Func> dictionaryFactory = null)
26 | {
27 | this.nullValue = nullValue;
28 | this.dictionaryFactory = dictionaryFactory ?? dictionaryFactoryDefault;
29 | }
30 |
31 | public IEnumerable Buckets
32 | {
33 | get
34 | {
35 | lock (buckets) { return buckets.Keys.ToList(); }
36 | }
37 | }
38 |
39 | protected override IEnumerable GetKeys(string bucketName)
40 | {
41 | IDictionary d;
42 | lock (buckets)
43 | {
44 | if (!buckets.TryGetValue(bucketName, out d)) return new TKey[] { };
45 | }
46 |
47 | lock (d) { return d.Keys.ToArray(); }
48 | }
49 |
50 | protected override TValue GetValue(string bucketName, TKey bucketKey)
51 | {
52 | IDictionary d;
53 | lock (buckets)
54 | {
55 | if (!buckets.TryGetValue(bucketName, out d)) return nullValue;
56 | }
57 |
58 | lock (d) { return d.TryGetValue(bucketKey, out var val) ? val : nullValue; }
59 | }
60 |
61 | protected override void SetValue(string bucketName, TKey bucketKey, TValue bucketValue)
62 | {
63 | IDictionary d;
64 | lock (buckets)
65 | {
66 | if (!buckets.TryGetValue(bucketName, out d))
67 | {
68 | if (bucketValue == null) return;
69 |
70 | buckets.Add(bucketName, d = dictionaryFactory());
71 | }
72 | }
73 |
74 | lock (d)
75 | {
76 | if (bucketValue == null) { d.Remove(bucketKey); }
77 | else { d[bucketKey] = bucketValue; }
78 | }
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Buckets/BucketStoreMemoryString.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | public class BucketStoreMemoryString : BucketStoreMemory
18 | {
19 | private static readonly Func> dictionaryFactoryDefault = () => new Dictionary(StringComparer.OrdinalIgnoreCase);
20 |
21 | public BucketStoreMemoryString() : base(dictionaryFactory: dictionaryFactoryDefault) { }
22 |
23 | protected override string CleanName(string name) => base.CleanName(name).CheckNotNullTrimmed(nameof(name));
24 |
25 | protected override string CleanKey(string key) => base.CleanKey(key).CheckNotNullTrimmed(nameof(key));
26 |
27 | protected override string CleanValue(string value) => base.CleanValue(value).TrimOrNull();
28 | }
29 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Buckets/IBucket.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | ///
18 | /// Read-Write Key+Value store
19 | ///
20 | /// Key type
21 | /// Value type
22 | public interface IBucket : IBucketReadOnly
23 | {
24 | ///
25 | /// Get or sets a value for a specific key
26 | ///
27 | /// Key
28 | /// Value
29 | new TValue this[TKey key] { get; set; }
30 | }
31 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Buckets/IBucketReadOnly.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | ///
18 | /// Readonly Key+Value store
19 | ///
20 | /// Key type
21 | /// Value type
22 | public interface IBucketReadOnly
23 | {
24 | ///
25 | /// Keys in bucket
26 | ///
27 | IEnumerable Keys { get; }
28 |
29 | ///
30 | /// Gets value for a specific key
31 | ///
32 | /// Key
33 | /// Value
34 | TValue this[TKey key] { get; }
35 | }
36 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Buckets/IBucketStore.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | ///
18 | /// Storage for multiple buckets based on a name
19 | ///
20 | /// Key type
21 | /// Value type
22 | public interface IBucketStore
23 | {
24 | IBucket this[string name] { get; }
25 | }
26 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Collections/ComparatorBase.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | ///
18 | /// Makes implementing a comparator easier
19 | ///
20 | /// Type this comparator compares
21 | public abstract class ComparatorBase : IComparer, IEqualityComparer, IComparer, IEqualityComparer
22 | {
23 | public abstract int Compare(T x, T y);
24 |
25 | public abstract int GetHashCode(T obj);
26 |
27 | public int Compare(object x, object y)
28 | {
29 | if (x == y) return 0;
30 |
31 | if (x == null) return -1;
32 |
33 | if (y == null) return 1;
34 |
35 | if (x is T sa)
36 | {
37 | if (y is T sb) { return Compare(sa, sb); }
38 | }
39 |
40 | if (x is IComparable ia) return ia.CompareTo(y);
41 |
42 | throw new ArgumentException("Argument_ImplementIComparable"); // TODO: Better error
43 | }
44 |
45 | public virtual bool Equals(T x, T y) => Compare(x, y) == 0;
46 |
47 | public new bool Equals(object x, object y)
48 | {
49 | if (x == y) return true;
50 |
51 | if (x == null || y == null) return false;
52 |
53 | if (x is T sa)
54 | {
55 | if (y is T sb) { return Equals(sa, sb); }
56 | }
57 |
58 | return x.Equals(y);
59 | }
60 |
61 | public int GetHashCode(object obj)
62 | {
63 | obj.CheckNotNull(nameof(obj));
64 |
65 | if (obj is T s) return GetHashCode(s);
66 |
67 | return obj.GetHashCode();
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Data/TableRow.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | public sealed class TableRow : IReadOnlyList, IBucketReadOnly, IBucketReadOnly
18 | {
19 | private readonly string[] data;
20 | public Table Table { get; }
21 | public int RowIndex { get; }
22 |
23 | internal TableRow(Table table, string[] data, int rowIndex)
24 | {
25 | Table = table;
26 | this.data = data;
27 | RowIndex = rowIndex;
28 | }
29 |
30 | public int GetNumberOfCharacters(int lengthOfNull) => data.GetNumberOfCharacters(lengthOfNull);
31 |
32 | #region IBucketReadOnly
33 |
34 | public IEnumerable Keys => Table.Columns.ColumnNames;
35 | IEnumerable IBucketReadOnly.Keys => Keys;
36 |
37 | public string this[string columnName] => this[Table.Columns[columnName].Index];
38 |
39 | #endregion IBucketReadOnly
40 |
41 | #region IBucketReadOnly
42 |
43 | IEnumerable IBucketReadOnly.Keys => Table.Columns;
44 | public string this[TableColumn column] => this[column.Index];
45 |
46 | #endregion IBucketReadOnly
47 |
48 | #region IReadOnlyList
49 |
50 | public int Count => Table.Columns.Count;
51 | public string this[int columnIndex] => data[columnIndex];
52 |
53 | public IEnumerator GetEnumerator() => ((IEnumerable)data).GetEnumerator();
54 |
55 | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
56 |
57 | #endregion IReadOnlyList
58 |
59 | public string[] ToArray() => data.Copy();
60 | }
61 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Data/XmlElement.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | public class XmlElement
18 | {
19 | public string Name { get; set; }
20 | public string Value { get; set; }
21 | public IDictionary Attributes { get; } = new Dictionary();
22 | public IList Children { get; } = new List();
23 |
24 | public IEnumerable ChildrenAll
25 | {
26 | get
27 | {
28 | foreach (var child in Children)
29 | {
30 | yield return child;
31 | foreach (var child2 in child.ChildrenAll) yield return child2;
32 | }
33 | }
34 | }
35 |
36 | public XmlElement Parent { get; set; }
37 |
38 | public string this[string attributeName] => Attributes.GetValueCaseInsensitive(attributeName);
39 | }
40 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Data/XmlReader.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System.Xml;
16 |
17 | namespace MaxRunSoftware.Utilities;
18 |
19 | public class XmlReader
20 | {
21 | public static XmlElement Read(string xml)
22 | {
23 | var document = new XmlDocument();
24 | document.LoadXml(xml);
25 | return Process(document);
26 | }
27 |
28 | private static XmlElement Process(XmlDocument document)
29 | {
30 | var elementRoot = document.DocumentElement;
31 | return ProcessElement(elementRoot, null);
32 | }
33 |
34 | private static XmlElement ProcessElement(System.Xml.XmlElement element, XmlElement parent)
35 | {
36 | var newElement = new XmlElement();
37 | newElement.Name = element.Name;
38 | newElement.Parent = parent;
39 | var attrs = element.Attributes;
40 | foreach (XmlAttribute attr in attrs) newElement.Attributes[attr.Name] = attr.Value;
41 |
42 | var values = new List();
43 | foreach (XmlNode child in element.ChildNodes)
44 | {
45 | if (child.NodeType.In(XmlNodeType.Element))
46 | {
47 | var childElement = (System.Xml.XmlElement)child;
48 | var newChild = ProcessElement(childElement, newElement);
49 | newElement.Children.Add(newChild);
50 | }
51 | else if (child.NodeType.In(XmlNodeType.Text))
52 | {
53 | var v = child.Value;
54 | if (v != null) values.Add(v);
55 | }
56 | }
57 |
58 | if (values.IsNotEmpty()) newElement.Value = values.ToStringDelimited("");
59 |
60 |
61 | return newElement;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Extensions/ExtensionsIO.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | public static class ExtensionsIO
18 | {
19 | public static string[] RemoveBase(this FileSystemInfo info, DirectoryInfo baseToRemove, bool caseSensitive = false)
20 | {
21 | var sourceParts = info.FullName.Split('/', '\\', Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar).Where(o => o.TrimOrNull() != null).ToArray();
22 | var baseParts = baseToRemove.FullName.Split('/', '\\', Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar).Where(o => o.TrimOrNull() != null).ToArray();
23 |
24 | var msgInvalidParent = $"{nameof(baseToRemove)} of {baseToRemove.FullName} is not a parent directory of {info.FullName}";
25 |
26 | if (baseParts.Length > sourceParts.Length) throw new ArgumentException(msgInvalidParent, nameof(baseToRemove));
27 |
28 | var list = new List();
29 | for (var i = 0; i < sourceParts.Length; i++)
30 | {
31 | if (i >= baseParts.Length) { list.Add(sourceParts[i]); }
32 | else
33 | {
34 | if (caseSensitive)
35 | {
36 | if (!string.Equals(sourceParts[i], baseParts[i])) throw new ArgumentException(msgInvalidParent, nameof(baseToRemove));
37 | }
38 | else
39 | {
40 | if (!string.Equals(sourceParts[i], baseParts[i], StringComparison.OrdinalIgnoreCase)) throw new ArgumentException(msgInvalidParent, nameof(baseToRemove));
41 | }
42 | }
43 | }
44 |
45 | return list.ToArray();
46 | }
47 |
48 | public static long GetLength(this FileInfo file)
49 | {
50 | if (file == null) return -1;
51 |
52 | // https://stackoverflow.com/a/26473940
53 | if (file.Attributes.HasFlag(FileAttributes.ReparsePoint)) // probably symbolic link
54 | // https://stackoverflow.com/a/57454136
55 | {
56 | using (Stream fs = Util.FileOpenRead(file.FullName)) { return fs.Length; }
57 | }
58 |
59 | return file.Length;
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Extensions/ExtensionsReflection.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System.Diagnostics;
16 |
17 | namespace MaxRunSoftware.Utilities;
18 |
19 | public static class ExtensionsReflection
20 | {
21 | public static string GetFileVersion(this Assembly assembly) => FileVersionInfo.GetVersionInfo(assembly.Location).FileVersion;
22 |
23 | public static string GetVersion(this Assembly assembly) => assembly.GetCustomAttribute()?.Version;
24 | }
25 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Logging/ILogAppender.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | public interface ILogAppender
18 | {
19 | LogLevel Level { get; }
20 |
21 | // ReSharper disable once UnusedParameter.Global
22 | void Log(object sender, LogEventArgs args);
23 | }
24 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Logging/ILogFactory.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | public interface ILogFactory : IDisposable
18 | {
19 | bool IsTraceEnabled { get; }
20 |
21 | bool IsDebugEnabled { get; }
22 |
23 | bool IsInfoEnabled { get; }
24 |
25 | bool IsWarnEnabled { get; }
26 |
27 | bool IsErrorEnabled { get; }
28 |
29 | bool IsCriticalEnabled { get; }
30 |
31 | //event EventHandler Logging;
32 |
33 | ILogger GetLogger();
34 |
35 | ILogger GetLogger(Type type);
36 |
37 | void AddAppender(ILogAppender appender);
38 | }
39 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Logging/ILogger.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | public interface ILogger
18 | {
19 | void Trace(string message);
20 |
21 | void Trace(Exception exception);
22 |
23 | void Trace(string message, Exception exception);
24 |
25 | void Debug(string message);
26 |
27 | void Debug(Exception exception);
28 |
29 | void Debug(string message, Exception exception);
30 |
31 | void Info(string message);
32 |
33 | void Info(Exception exception);
34 |
35 | void Info(string message, Exception exception);
36 |
37 | void Warn(string message);
38 |
39 | void Warn(Exception exception);
40 |
41 | void Warn(string message, Exception exception);
42 |
43 | void Error(string message);
44 |
45 | void Error(Exception exception);
46 |
47 | void Error(string message, Exception exception);
48 |
49 | void Critical(string message);
50 |
51 | void Critical(Exception exception);
52 |
53 | void Critical(string message, Exception exception);
54 | }
55 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Logging/LogLevel.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | public enum LogLevel : byte
18 | {
19 | Trace = 0,
20 | Debug = 1,
21 | Info = 2,
22 | Warn = 3,
23 | Error = 4,
24 | Critical = 5
25 | }
26 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/MaxRunSoftware.Utilities.csproj.DotSettings:
--------------------------------------------------------------------------------
1 |
2 | True
3 | True
4 | True
5 | True
6 | True
7 | True
8 | True
9 | True
10 | True
11 | True
12 | True
13 | True
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Reflection/TypeConverter.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | public delegate object TypeConverter(object inputObject, Type outputType);
18 |
19 | public static class TypeConverterExtensions
20 | {
21 | public static TypeConverter AsTypeConverter(this Converter converter) => (inputObject, _) => converter((TInput)inputObject);
22 |
23 | public static Converter AsConverter(this TypeConverter typeConverter) => inputObject => (TOutput)typeConverter(inputObject, typeof(TOutput));
24 | }
25 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Sql/SqlExtensions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | public static class SqlExtensions
18 | {
19 | public static IDataReader ExecuteReaderExceptionWrapped(this IDbCommand command, bool exceptionShowFullSql)
20 | {
21 | try { return command.ExecuteReader(); }
22 | catch (Exception e) { throw new SqlException(e, command, exceptionShowFullSql); }
23 | }
24 |
25 | public static object ExecuteScalarExceptionWrapped(this IDbCommand command, bool exceptionShowFullSql)
26 | {
27 | try { return command.ExecuteScalar(); }
28 | catch (Exception e) { throw new SqlException(e, command, exceptionShowFullSql); }
29 | }
30 |
31 | public static int ExecuteNonQueryExceptionWrapped(this IDbCommand command, bool exceptionShowFullSql)
32 | {
33 | try { return command.ExecuteNonQuery(); }
34 | catch (Exception e) { throw new SqlException(e, command, exceptionShowFullSql); }
35 | }
36 |
37 | public static List ExecuteQueryToList(this Sql sql, string sqlQuery, params SqlParameter[] parameters)
38 | {
39 | var list = new List();
40 | var table = ExecuteQueryToTable(sql, sqlQuery, parameters);
41 | if (table == null) return list;
42 | if (table.Columns.Count < 1) return list;
43 |
44 | foreach (var row in table) list.Add(row[0]);
45 |
46 | return list;
47 | }
48 |
49 | public static Table ExecuteQueryToTable(this Sql sql, string sqlQuery, params SqlParameter[] parameters) => ExecuteQueryToTables(sql, sqlQuery, parameters).FirstOrDefault();
50 |
51 | public static Table[] ExecuteQueryToTables(this Sql sql, string sqlQuery, params SqlParameter[] parameters) => Table.Create(sql.ExecuteQuery(sqlQuery, parameters));
52 | }
53 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Sql/SqlParameter.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | ///
18 | /// Parameter used for calling SQL stored procedure and functions
19 | ///
20 | public sealed class SqlParameter
21 | {
22 | ///
23 | /// Parameter name
24 | ///
25 | public string Name { get; }
26 |
27 | ///
28 | /// Parameter type
29 | ///
30 | public DbType Type { get; }
31 |
32 | ///
33 | /// Parameter value
34 | ///
35 | public object Value { get; }
36 |
37 | ///
38 | /// Constructs a new SqlParameter attempting to figure out the Type based on the value supplied
39 | ///
40 | /// Name
41 | /// Value
42 | public SqlParameter(string name, object value)
43 | {
44 | Name = name;
45 |
46 | if (value == null)
47 | {
48 | Type = DbType.String;
49 | Value = null;
50 | }
51 | else
52 | {
53 | if (Constant.TYPE_DBTYPE.TryGetValue(value.GetType(), out var dbType))
54 | {
55 | Type = dbType;
56 | Value = value;
57 | }
58 | else
59 | {
60 | Type = DbType.String;
61 | Value = value.ToStringGuessFormat();
62 | }
63 | }
64 | }
65 |
66 | ///
67 | /// Constructs a new SqlParameter
68 | ///
69 | /// Name
70 | /// Value
71 | ///
72 | public SqlParameter(string name, object value, DbType type)
73 | {
74 | Name = name;
75 | Value = value;
76 | Type = type;
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Sql/SqlTypeAttribute.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | [AttributeUsage(AttributeTargets.Field)]
18 | public class SqlTypeAttribute : Attribute
19 | {
20 | public DbType DbType { get; }
21 | public Type DotNetType { get; set; }
22 |
23 | public string SqlTypeNames { get; set; }
24 | public object ActualSqlType { get; set; }
25 |
26 | public SqlTypeAttribute(DbType dbType) { DbType = dbType; }
27 | }
28 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Threading/ConsumerProducerThread.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System.Collections.Concurrent;
16 |
17 | namespace MaxRunSoftware.Utilities;
18 |
19 | ///
20 | /// Thread that consumes an object and produces a new object
21 | ///
22 | /// Consumed Type
23 | /// Produced Type
24 | public class ConsumerProducerThread : ConsumerProducerThreadBase
25 | {
26 | private readonly Func func;
27 |
28 | public ConsumerProducerThread(BlockingCollection consumerQueue, BlockingCollection producerQueue, Func func) : base(consumerQueue, producerQueue) { this.func = func.CheckNotNull(nameof(func)); }
29 |
30 | protected override TProduce WorkConsumeProduce(TConsume item) => func(item);
31 | }
32 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Threading/ConsumerProducerThreadBase.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System.Collections.Concurrent;
16 | using System.Threading;
17 |
18 | namespace MaxRunSoftware.Utilities;
19 |
20 | public abstract class ConsumerProducerThreadBase : ConsumerThreadBase
21 | {
22 | // ReSharper disable once StaticMemberInGenericType
23 | private static readonly ILogger log = LogFactory.GetLogger(MethodBase.GetCurrentMethod()!.DeclaringType);
24 |
25 | private readonly BlockingCollection producerQueue;
26 | private readonly CancellationTokenSource cancellation = new();
27 |
28 | protected ConsumerProducerThreadBase(BlockingCollection consumerQueue, BlockingCollection producerQueue) : base(consumerQueue) { this.producerQueue = producerQueue.CheckNotNull(nameof(producerQueue)); }
29 |
30 | protected override void WorkConsume(TConsume item)
31 | {
32 | if (IsCancelled) return;
33 |
34 | try
35 | {
36 | var produceItem = WorkConsumeProduce(item);
37 | if (IsCancelled) return;
38 |
39 | producerQueue.Add(produceItem, cancellation.Token);
40 | }
41 | catch (OperationCanceledException) { Cancel(); }
42 | }
43 |
44 | protected override void CancelInternal()
45 | {
46 | try { cancellation.Cancel(); }
47 | catch (Exception e) { log.Warn("CancellationTokenSource.Cancel() request threw exception", e); }
48 |
49 | base.CancelInternal();
50 | }
51 |
52 | protected abstract TProduce WorkConsumeProduce(TConsume item);
53 | }
54 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Threading/ConsumerThread.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System.Collections.Concurrent;
16 |
17 | namespace MaxRunSoftware.Utilities;
18 |
19 | ///
20 | /// Thread for performing a specified action on a collection
21 | ///
22 | /// The Type of object to process
23 | public class ConsumerThread : ConsumerThreadBase
24 | {
25 | private readonly Action action;
26 |
27 | public ConsumerThread(BlockingCollection queue, Action action) : base(queue) { this.action = action.CheckNotNull(nameof(action)); }
28 |
29 | protected override void WorkConsume(T item) => action(item);
30 | }
31 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Threading/ConsumerThreadState.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | public enum ConsumerThreadState
18 | {
19 | Waiting,
20 | Working,
21 | Stopped
22 | }
23 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Threading/IntervalThread.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System.Threading;
16 |
17 | namespace MaxRunSoftware.Utilities;
18 |
19 | ///
20 | /// Simple polling thread that calls WorkInterval() periodically
21 | ///
22 | public abstract class IntervalThread : ThreadBase
23 | {
24 | private DateTime lastCheck;
25 | public TimeSpan SleepInterval { get; set; } = TimeSpan.FromSeconds(1);
26 | public TimeSpan SleepIntervalDelay { get; set; } = TimeSpan.FromMilliseconds(50);
27 |
28 | protected override void Work()
29 | {
30 | while (true)
31 | {
32 | if (IsDisposed) return;
33 |
34 | if (DateTime.UtcNow - lastCheck > SleepInterval)
35 | {
36 | WorkInterval();
37 | lastCheck = DateTime.UtcNow;
38 | }
39 |
40 | Thread.Sleep(SleepIntervalDelay);
41 | }
42 | }
43 |
44 | protected abstract void WorkInterval();
45 | }
46 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Threading/MutexLockTimeoutException.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System.Threading;
16 |
17 | namespace MaxRunSoftware.Utilities;
18 |
19 | public sealed class MutexLockTimeoutException : WaitHandleCannotBeOpenedException
20 | {
21 | public string MutexName { get; }
22 | public TimeSpan Timeout { get; }
23 |
24 | public MutexLockTimeoutException(string mutexName, TimeSpan timeout) : base("Failed to acquire mutex [" + mutexName + "] after waiting " + timeout.ToStringTotalSeconds(3) + "s")
25 | {
26 | MutexName = mutexName;
27 | Timeout = timeout;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Threading/SingleUse.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | public class SingleUse
18 | {
19 | private readonly AtomicBoolean boolean = false;
20 |
21 | public bool IsUsed => boolean;
22 |
23 | ///
24 | /// Attempts to 'use' this instance. If this is the first time using it, we will return
25 | /// true. Otherwise we return false if we have already been used.
26 | ///
27 | /// true if we have never used before, false if we have already been used
28 | public bool TryUse() => boolean.SetTrue();
29 | }
30 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Usings.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | global using System;
16 | global using System.Collections;
17 | global using System.Collections.Generic;
18 | global using System.Data;
19 | global using System.IO;
20 | global using System.Linq;
21 | global using System.Reflection;
22 | global using System.Text;
23 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Utils/UtilBaseConversion.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | public static partial class Util
18 | {
19 | #region Base16
20 |
21 | private static readonly uint[] lookupBase16 = Base16();
22 |
23 | private static uint[] Base16()
24 | {
25 | var result = new uint[256];
26 | for (var i = 0; i < 256; i++)
27 | {
28 | var s = i.ToString("X2");
29 | result[i] = s[0] + ((uint)s[1] << 16);
30 | }
31 |
32 | return result;
33 | }
34 |
35 | public static string Base16(byte[] bytes)
36 | {
37 | // https://stackoverflow.com/a/24343727/48700 https://stackoverflow.com/a/624379
38 |
39 | var lookup32 = lookupBase16;
40 | var result = new char[bytes.Length * 2];
41 | for (var i = 0; i < bytes.Length; i++)
42 | {
43 | var val = lookup32[bytes[i]];
44 | result[2 * i] = (char)val;
45 | result[2 * i + 1] = (char)(val >> 16);
46 | }
47 |
48 | return new string(result);
49 | }
50 |
51 | public static byte[] Base16(string base16String)
52 | {
53 | var numberChars = base16String.Length;
54 | var bytes = new byte[numberChars / 2];
55 | for (var i = 0; i < numberChars; i += 2) bytes[i / 2] = Convert.ToByte(base16String.Substring(i, 2), 16);
56 |
57 | return bytes;
58 | }
59 |
60 | #endregion Base16
61 |
62 | #region Base64
63 |
64 | public static string Base64(byte[] bytes) => Convert.ToBase64String(bytes);
65 |
66 | public static byte[] Base64(string base64String) => Convert.FromBase64String(base64String);
67 |
68 | #endregion Base64
69 | }
70 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Utils/UtilChangeType.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | using System.Net;
16 | using System.Net.Mail;
17 |
18 | namespace MaxRunSoftware.Utilities;
19 |
20 | public static partial class Util
21 | {
22 | public static TOutput ChangeType(TInput obj) => (TOutput)ChangeType(obj, typeof(TOutput));
23 |
24 | public static TOutput ChangeType(object obj) => (TOutput)ChangeType(obj, typeof(TOutput));
25 |
26 | public static object ChangeType(object obj, Type outputType)
27 | {
28 | if (obj == null || obj == DBNull.Value)
29 | {
30 | if (!outputType.IsValueType) return null;
31 |
32 | if (outputType.IsNullable()) return null;
33 |
34 | return Convert.ChangeType(obj, outputType); // Should throw exception
35 | }
36 |
37 | if (outputType.IsNullable(out var underlyingTypeOutput)) return ChangeType(obj, underlyingTypeOutput);
38 |
39 | var inputType = obj.GetType();
40 | if (inputType.IsNullable(out var underlyingTypeInput)) inputType = underlyingTypeInput;
41 |
42 | if (inputType == typeof(string))
43 | {
44 | var o = obj as string;
45 | if (outputType == typeof(bool)) return o.ToBool();
46 |
47 | if (outputType == typeof(DateTime)) return o.ToDateTime();
48 |
49 | if (outputType == typeof(Guid)) return o.ToGuid();
50 |
51 | if (outputType == typeof(MailAddress)) return o.ToMailAddress();
52 |
53 | if (outputType == typeof(Uri)) return o.ToUri();
54 |
55 | if (outputType == typeof(IPAddress)) return o.ToIPAddress();
56 |
57 | if (outputType.IsEnum) return outputType.GetEnumValue(o);
58 | }
59 |
60 | if (inputType.IsEnum) return ChangeType(obj.ToString(), outputType);
61 |
62 | if (outputType == typeof(string)) return obj.ToStringGuessFormat();
63 |
64 | return Convert.ChangeType(obj, outputType);
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Utils/UtilConsole.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | public static partial class Util
18 | {
19 | private readonly struct ConsoleColorChanger : IDisposable
20 | {
21 | private readonly ConsoleColor foreground;
22 | private readonly bool foregroundSwitched;
23 | private readonly ConsoleColor background;
24 | private readonly bool backgroundSwitched;
25 |
26 | public ConsoleColorChanger(ConsoleColor? foreground, ConsoleColor? background)
27 | {
28 | this.foreground = Console.ForegroundColor;
29 | this.background = Console.BackgroundColor;
30 |
31 | var foregroundSwitch = false;
32 | if (foreground != null)
33 | {
34 | if (this.foreground != foreground.Value)
35 | {
36 | foregroundSwitch = true;
37 | Console.ForegroundColor = foreground.Value;
38 | }
39 | }
40 |
41 | foregroundSwitched = foregroundSwitch;
42 |
43 | var backgroundSwitch = false;
44 | if (background != null)
45 | {
46 | if (this.background != background.Value)
47 | {
48 | backgroundSwitch = true;
49 | Console.BackgroundColor = background.Value;
50 | }
51 | }
52 |
53 | backgroundSwitched = backgroundSwitch;
54 | }
55 |
56 | public void Dispose()
57 | {
58 | if (foregroundSwitched) Console.ForegroundColor = foreground;
59 |
60 | if (backgroundSwitched) Console.BackgroundColor = background;
61 | }
62 | }
63 |
64 | ///
65 | /// Changes the console color, and then changes it back when it is disposed
66 | ///
67 | /// The foreground color
68 | /// The background color
69 | /// A disposable that will change the colors back once disposed
70 | public static IDisposable ChangeConsoleColor(ConsoleColor? foreground = null, ConsoleColor? background = null) => new ConsoleColorChanger(foreground, background);
71 | }
72 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Utils/UtilEnum.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | public static partial class Util
18 | {
19 | public static IReadOnlyList GetEnumValues() where TEnum : struct, Enum => (TEnum[])Enum.GetValues(typeof(TEnum));
20 |
21 | public static TEnum CombineEnumFlags(IEnumerable enums) where TEnum : struct, Enum => (TEnum)Enum.Parse(typeof(TEnum), string.Join(", ", enums.Select(o => o.ToString())));
22 | }
23 |
--------------------------------------------------------------------------------
/MaxRunSoftware.Utilities/Version.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2022 Max Run Software (dev@maxrunsoftware.com)
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | namespace MaxRunSoftware.Utilities;
16 |
17 | public static class Version
18 | {
19 | public static string Value => "1.13.1";
20 | }
21 |
--------------------------------------------------------------------------------
/test.bat:
--------------------------------------------------------------------------------
1 | SETLOCAL
2 | SET host=localhost
3 | SET username=testadmin
4 | SET password=mySecretAdmin1
5 |
6 | huc ActiveDirectoryAddUser -h=%host% -u=%username% -p=%password% -fn=Test1 -ln=Doe -dn="John1 Doe" -ea="jd1@aol.com" jd1
7 | ::huc ActiveDirectoryDisableUsers -h=%host% -u=%username% -p=%password% -l=5
8 | huc ActiveDirectoryDisableUser -h=%host% -u=%username% -p=%password% jd1
9 | huc ActiveDirectoryListObjectDetails -h=%host% -u=%username% -p=%password% Administrator
10 |
11 | huc ActiveDirectoryChangePassword -h=%host% -u=%username% -p=%password% jd1 myNew29Password
12 | huc ActiveDirectoryAddGroup -h=%host% -u=%username% -p=%password% TestGroup1
13 | huc ActiveDirectoryAddOU -h=%host% -u=%username% -p=%password% OU1
14 | huc ActiveDirectoryAddOU -h=%host% -u=%username% -p=%password% -pou=OU1 OU2
15 | huc ActiveDirectoryMoveGroup -h=%host% -u=%username% -p=%password% TestGroup1 OU1
16 | huc ActiveDirectoryMoveUser -h=%host% -u=%username% -p=%password% jd1 OU2
17 | huc ActiveDirectoryAddUserToGroup -h=%host% -u=%username% -p=%password% jd1 TestGroup1
18 |
19 | huc ActiveDirectoryListUsers -h=%host% -u=%username% -p=%password% j?1
20 | huc ActiveDirectoryListGroups -h=%host% -u=%username% -p=%password% Test*1
21 |
22 | huc ActiveDirectoryRemoveUserFromGroup -h=%host% -u=%username% -p=%password% jd1 TestGroup1
23 | huc ActiveDirectoryRemoveUser -h=%host% -u=%username% -p=%password% jd1
24 | huc ActiveDirectoryRemoveGroup -h=%host% -u=%username% -p=%password% TestGroup1
25 | huc ActiveDirectoryRemoveOU -h=%host% -u=%username% -p=%password% OU2
26 | huc ActiveDirectoryRemoveOU -h=%host% -u=%username% -p=%password% OU1
27 |
28 | huc WindowsTaskSchedulerAdd -h=%host% -u=%username% -p=%password% -tn=MyTask1 -tu=System -td="Some task description" -t1="DAILY 11:42" -t2="HOURLY 25" C:\Temp\SomeTestFile.bat
29 | huc WindowsTaskSchedulerAdd -h=%host% -u=%username% -p=%password% -tn=/MyTasks/MyTask2 -tu=System -td="Some task description 2" -t1="DAILY 9:18" -t2="HOURLY 12" C:\Temp\SomeTestFile.bat
30 |
31 | huc WindowsTaskSchedulerRemove -h=%host% -u=%username% -p=%password% MyTask1
32 | huc WindowsTaskSchedulerRemove -h=%host% -u=%username% -p=%password% /MyTasks/MyTask2
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/toolbox.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/maxrunsoftware/huc/35d4f91a2503005c5946fa805079a436534c6f04/toolbox.png
--------------------------------------------------------------------------------