Skip to content

Commit cfeec9d

Browse files
Lanning, MarkLanning, Mark
authored andcommitted
updated to support tracked string localizer, started building mongo cache wrapper,
1 parent 5559742 commit cfeec9d

File tree

25 files changed

+1099
-518
lines changed

25 files changed

+1099
-518
lines changed

Base/src/ThingsLibrary.Base/DataType/ActionResponse/ActionResponseObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace ThingsLibrary.DataType
3535
public class ActionResponse<TEntity> : ActionResponse
3636
{
3737
[JsonPropertyName("data")]
38-
public TEntity? Data { get; set; }
38+
public TEntity Data { get; set; } = default;
3939

4040
public ActionResponse()
4141
{

Base/src/ThingsLibrary.Base/DataType/Extensions/Assembly.cs

Lines changed: 255 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@
66
// ================================================================================
77

88
using System.Reflection;
9+
using System.Runtime.Versioning;
910
using ThingsLibrary.DataType.Extensions;
1011

11-
namespace ThingsLibrary.Base.DataType.Extensions
12+
namespace ThingsLibrary.DataType.Extensions
1213
{
1314
public static class AssemblyExtensions
1415
{
16+
/// <summary>
17+
/// Agent String based on Assembly Product Name/Version and OS Version
18+
/// </summary>
19+
/// <param name="assembly">Assembly</param>
20+
/// <returns></returns>
21+
/// <exception cref="ArgumentNullException"></exception>
1522
public static string AgentString(this Assembly assembly)
1623
{
1724
ArgumentNullException.ThrowIfNull(assembly);
@@ -22,5 +29,252 @@ public static string AgentString(this Assembly assembly)
2229

2330
return $"{name}/{version.ToDotString()} ({Metrics.MachineMetrics.OsVersion()})";
2431
}
32+
33+
/// <summary>
34+
/// Get the ID from the Guid Attribute property of the assembly is available.. Guid.Empty it not found
35+
/// </summary>
36+
/// <param name="assembly">Assembly</param>
37+
/// <returns>Guid of assembly, Guid.Empty is not found</returns>
38+
public static Guid GetId(this Assembly assembly)
39+
{
40+
// Requires an AssemblyInfo.cs with a [assembly: Guid("11111111-1111-1111-1111-111111111111")] style attribute
41+
42+
var guid = assembly.GetCustomAttribute<System.Runtime.InteropServices.GuidAttribute>();
43+
if (guid == null) { return Guid.Empty; }
44+
45+
return Guid.Parse(guid.Value);
46+
}
47+
48+
/// <summary>
49+
/// Name (same as Title)
50+
/// </summary>
51+
/// <param name="assembly">Assembly</param>
52+
/// <returns></returns>
53+
public static string Name(this Assembly assembly) => Title(assembly);
54+
55+
/// <summary>
56+
/// Title
57+
/// </summary>
58+
/// <param name="assembly">Assembly</param>
59+
/// <returns></returns>
60+
public static string Title(this Assembly assembly)
61+
{
62+
ArgumentNullException.ThrowIfNull(assembly);
63+
64+
return assembly.GetCustomAttribute<AssemblyTitleAttribute>()?.Title ?? string.Empty;
65+
}
66+
67+
/// <summary>
68+
/// Namespace
69+
/// </summary>
70+
/// <param name="assembly">Assembly</param>
71+
/// <returns></returns>
72+
public static string Namespace(this Assembly assembly)
73+
{
74+
ArgumentNullException.ThrowIfNull(assembly);
75+
76+
return assembly.GetName()?.Name ?? string.Empty;
77+
}
78+
79+
/// <summary>
80+
/// Version as a string
81+
/// </summary>
82+
/// <param name="assembly">Assembly</param>
83+
/// <returns></returns>
84+
public static string Version(this Assembly assembly)
85+
{
86+
ArgumentNullException.ThrowIfNull(assembly);
87+
88+
return assembly.GetCustomAttribute<AssemblyFileVersionAttribute>()?.Version ?? string.Empty;
89+
}
90+
91+
/// <summary>
92+
/// File Version as a Version data type
93+
/// </summary>
94+
/// <param name="assembly">Assembly</param>
95+
/// <returns></returns>
96+
public static Version FileVersion(this Assembly assembly)
97+
{
98+
ArgumentNullException.ThrowIfNull(assembly);
99+
return assembly.GetName()?.Version ?? new System.Version();
100+
}
101+
102+
/// <summary>
103+
/// File version as a string
104+
/// </summary>
105+
/// <param name="assembly">Assembly</param>
106+
/// <returns></returns>
107+
public static string FileVersionStr(this Assembly assembly)
108+
{
109+
var version = assembly.FileVersion();
110+
if (version == null) { return "0"; }
111+
112+
return version.ToDotString();
113+
}
114+
115+
/// <summary>
116+
/// File Version as a long data type
117+
/// </summary>
118+
/// <param name="assembly">Assembly</param>
119+
/// <returns></returns>
120+
public static long FileVersionLong(this Assembly assembly) => assembly.FileVersion().ToLong();
121+
122+
/// <summary>
123+
/// Product Name
124+
/// </summary>
125+
/// <param name="assembly">Assembly</param>
126+
/// <returns></returns>
127+
public static string ProductName(this Assembly assembly)
128+
{
129+
ArgumentNullException.ThrowIfNull(assembly);
130+
return assembly.GetCustomAttribute<AssemblyProductAttribute>()?.Product ?? string.Empty;
131+
}
132+
133+
public static string Company(this Assembly assembly)
134+
{
135+
ArgumentNullException.ThrowIfNull(assembly);
136+
return assembly.GetCustomAttribute<AssemblyCompanyAttribute>()?.Company ?? string.Empty;
137+
}
138+
139+
/// <summary>
140+
/// Retrieves the description text specified in the assembly's <see cref="AssemblyDescriptionAttribute"/>.
141+
/// </summary>
142+
/// <remarks>This method returns the value of the <see cref="AssemblyDescriptionAttribute"/>
143+
/// applied to the specified assembly. If the attribute is not present, an empty string is returned.</remarks>
144+
/// <param name="assembly">The assembly from which to obtain the description. Cannot be <see langword="null"/>.</param>
145+
/// <returns>A string containing the description of the assembly, or an empty string if no description is defined.</returns>
146+
public static string Description(this Assembly assembly)
147+
{
148+
ArgumentNullException.ThrowIfNull(assembly);
149+
return assembly.GetCustomAttribute<AssemblyDescriptionAttribute>()?.Description ?? string.Empty;
150+
}
151+
152+
153+
/// <summary>
154+
/// Copyright
155+
/// </summary>
156+
/// <param name="assembly">Assembly</param>
157+
/// <returns></returns>
158+
public static string Copyright(this Assembly assembly)
159+
{
160+
ArgumentNullException.ThrowIfNull(assembly);
161+
return assembly.GetCustomAttribute<AssemblyCopyrightAttribute>()?.Copyright ?? string.Empty;
162+
}
163+
164+
/// <summary>
165+
/// DotNet Framework Version
166+
/// </summary>
167+
/// <param name="assembly">Assembly</param>
168+
/// <returns></returns>
169+
public static string NetFrameworkVersion(this Assembly assembly)
170+
{
171+
ArgumentNullException.ThrowIfNull(assembly);
172+
return assembly.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName ?? string.Empty;
173+
}
174+
175+
/// <summary>
176+
/// Assembly creation date
177+
/// </summary>
178+
/// <param name="assembly">Assembly</param>
179+
/// <returns></returns>
180+
public static DateTime CreatedOn(this Assembly assembly)
181+
{
182+
ArgumentNullException.ThrowIfNull(assembly);
183+
return File.GetCreationTimeUtc(assembly.Location);
184+
}
185+
186+
/// <summary>
187+
/// Assembly last written / updated date
188+
/// </summary>
189+
/// <param name="assembly"></param>
190+
/// <returns></returns>
191+
public static DateTime UpdatedOn(this Assembly assembly)
192+
{
193+
ArgumentNullException.ThrowIfNull(assembly);
194+
return File.GetLastWriteTimeUtc(assembly.Location);
195+
}
196+
197+
/// <summary>
198+
/// Gets the file system path of the specified assembly.
199+
/// </summary>
200+
/// <remarks>This method returns the value of the <see cref="Assembly.Location"/> property. If the
201+
/// assembly was loaded from a byte array rather than a file, the returned path will be an empty
202+
/// string.</remarks>
203+
/// <param name="assembly">The assembly for which to retrieve the file system path. Cannot be null.</param>
204+
/// <returns>The full path to the file that contains the assembly. Returns an empty string if the assembly was loaded
205+
/// from a byte array.</returns>
206+
public static string Path(this Assembly assembly)
207+
{
208+
ArgumentNullException.ThrowIfNull(assembly);
209+
return assembly.Location;
210+
}
211+
212+
/// <summary>
213+
/// Directory of the executing assembly
214+
/// </summary>
215+
/// <param name="assembly">Assembly</param>
216+
/// <returns></returns>
217+
public static string DirectoryPath(this Assembly assembly)
218+
{
219+
ArgumentNullException.ThrowIfNull(assembly);
220+
return System.IO.Path.GetDirectoryName(assembly.Location) ?? string.Empty;
221+
}
222+
223+
/// <summary>
224+
/// Application Data Path
225+
/// </summary>
226+
/// <param name="assembly"></param>
227+
/// <returns></returns>
228+
public static string AppDataPath(this Assembly assembly)
229+
{
230+
ArgumentNullException.ThrowIfNull(assembly);
231+
var appDataPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), assembly.ProductName());
232+
233+
if (!Directory.Exists(appDataPath))
234+
{
235+
Directory.CreateDirectory(appDataPath);
236+
}
237+
238+
return appDataPath;
239+
}
240+
241+
/// <summary>
242+
/// Temp directory path (temp path + product name)
243+
/// </summary>
244+
/// <param name="assembly">Assembly</param>
245+
/// <returns></returns>
246+
public static string TempDirectoryPath(this Assembly assembly)
247+
{
248+
ArgumentNullException.ThrowIfNull(assembly);
249+
250+
var tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), assembly.ProductName());
251+
if (!Directory.Exists(tempPath))
252+
{
253+
Directory.CreateDirectory(tempPath);
254+
}
255+
return tempPath;
256+
}
257+
258+
/// <summary>
259+
/// Dependency Versions
260+
/// </summary>
261+
/// <param name="assembly">Assembly</param>
262+
/// <returns></returns>
263+
public static IDictionary<string, string> DependencyVersions(this Assembly assembly)
264+
{
265+
ArgumentNullException.ThrowIfNull(assembly);
266+
267+
var references = assembly.GetReferencedAssemblies();
268+
269+
var dict = new Dictionary<string, string>();
270+
foreach (var reference in references)
271+
{
272+
if (string.IsNullOrEmpty(reference.Name)) { continue; } // todo? not sure what should happen here.
273+
274+
dict[reference.Name] = reference.Version?.ToString() ?? string.Empty;
275+
}
276+
return dict;
277+
}
278+
25279
}
26280
}

Base/src/ThingsLibrary.Base/DataType/Extensions/Stream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// </copyright>
66
// ================================================================================
77

8-
namespace ThingsLibrary.Base.DataType.Extensions
8+
namespace ThingsLibrary.DataType.Extensions
99
{
1010
public static class StreamExtensions
1111
{
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// ================================================================================
2+
// <copyright file="EpochDateTimeConverter.cs" company="Starlight Software Co">
3+
// Copyright (c) Starlight Software Co. All rights reserved.
4+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
5+
// </copyright>
6+
// ================================================================================
7+
8+
namespace ThingsLibrary.DataType.Json.Converters
9+
{
10+
public class EpochDateTimeConverter : JsonConverter<DateTime>
11+
{
12+
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
13+
{
14+
if (reader.TokenType != JsonTokenType.Number)
15+
{
16+
throw new JsonException("Expected number for epoch time.");
17+
}
18+
19+
long epochValue = reader.GetInt64();
20+
21+
// Detect if epoch is in seconds or milliseconds
22+
if (epochValue > 9999999999) // > year 2286 in seconds
23+
{
24+
return DateTimeOffset.FromUnixTimeMilliseconds(epochValue).UtcDateTime;
25+
}
26+
else
27+
{
28+
return DateTimeOffset.FromUnixTimeSeconds(epochValue).UtcDateTime;
29+
}
30+
}
31+
32+
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
33+
{
34+
// Write as seconds since epoch
35+
long epochSeconds = new DateTimeOffset(value).ToUnixTimeSeconds();
36+
writer.WriteNumberValue(epochSeconds);
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)