This commit is contained in:
2023-10-15 12:13:48 -07:00
parent cd5ab223c9
commit 37b7ad2a1f
102 changed files with 631 additions and 975 deletions

View File

@ -1,10 +0,0 @@
namespace View_by_Distance.Set.Created.Date.Models.Stateless;
public static class SerilogExtensionMethods
{
internal static void Warn(this Serilog.ILogger log, string messageTemplate) => log.Warning(messageTemplate);
internal static void Info(this Serilog.ILogger log, string messageTemplate) => log.Information(messageTemplate);
}

View File

@ -1,6 +1,8 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Phares.Shared;
using Serilog;
using System.Diagnostics;
using System.Reflection;
using View_by_Distance.Set.Created.Date.Models;
@ -11,9 +13,8 @@ namespace View_by_Distance.Set.Created.Date;
public class Program
{
public static void Secondary(List<string> args)
public static void Secondary(ILogger<Program> logger, List<string> args)
{
LoggerConfiguration loggerConfiguration = new();
Assembly assembly = Assembly.GetExecutingAssembly();
bool debuggerWasAttachedAtLineZero = Debugger.IsAttached || assembly.Location.Contains(@"\bin\Debug");
IsEnvironment isEnvironment = new(processesCount: null, nullASPNetCoreEnvironmentIsDevelopment: debuggerWasAttachedAtLineZero, nullASPNetCoreEnvironmentIsProduction: !debuggerWasAttachedAtLineZero);
@ -30,9 +31,6 @@ public class Program
throw new Exception("Working directory name must have a value!");
string workingDirectory = IWorkingDirectory.GetWorkingDirectory(assembly.GetName().Name, appSettings.WorkingDirectoryName);
Environment.SetEnvironmentVariable(nameof(workingDirectory), workingDirectory);
_ = ConfigurationLoggerConfigurationExtensions.Configuration(loggerConfiguration.ReadFrom, configurationRoot);
Log.Logger = loggerConfiguration.CreateLogger();
ILogger log = Log.ForContext<Program>();
int silentIndex = args.IndexOf("s");
if (silentIndex > -1)
args.RemoveAt(silentIndex);
@ -41,31 +39,28 @@ public class Program
if (args is null)
throw new Exception("args is null!");
Shared.Models.Console console = new();
_ = new SetCreatedDate(args, isEnvironment, configurationRoot, appSettings, workingDirectory, silentIndex > -1, console);
_ = new SetCreatedDate(args, logger, isEnvironment, configurationRoot, appSettings, workingDirectory, silentIndex > -1, console);
}
catch (Exception ex)
{
log.Fatal(string.Concat(ex.Message, Environment.NewLine, ex.StackTrace));
}
finally
{
Log.CloseAndFlush();
logger?.LogError(ex, "Error!");
}
if (silentIndex > -1)
log.Debug("Done. Bye");
logger?.LogDebug("Done. Bye");
else
{
log.Debug("Done. Press 'Enter' to end");
logger?.LogDebug("Done. Press 'Enter' to end");
_ = Console.ReadLine();
}
}
public static void Main(string[] args)
{
ILogger<Program>? logger = Host.CreateDefaultBuilder(args).Build().Services.GetRequiredService<ILogger<Program>>();
if (args is not null)
Secondary(args.ToList());
Secondary(logger, args.ToList());
else
Secondary(new List<string>());
Secondary(logger, new List<string>());
}
}

View File

@ -39,10 +39,6 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog" Version="2.12.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Metadata\Metadata.csproj" />

View File

@ -1,6 +1,6 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Phares.Shared;
using Serilog;
using ShellProgressBar;
using System.Text;
using View_by_Distance.Metadata.Models;
@ -23,7 +23,7 @@ public class SetCreatedDate
private readonly IReadOnlyDictionary<string, string[]> _FileGroups;
private readonly Property.Models.Configuration _PropertyConfiguration;
public SetCreatedDate(List<string> args, IsEnvironment isEnvironment, IConfigurationRoot configurationRoot, AppSettings appSettings, string workingDirectory, bool isSilent, IConsole console)
public SetCreatedDate(List<string> args, ILogger<Program> logger, IsEnvironment isEnvironment, IConfigurationRoot configurationRoot, AppSettings appSettings, string workingDirectory, bool isSilent, IConsole console)
{
if (isSilent)
{ }
@ -33,16 +33,15 @@ public class SetCreatedDate
_IsEnvironment = isEnvironment;
_WorkingDirectory = workingDirectory;
_ConfigurationRoot = configurationRoot;
ILogger? log = Log.ForContext<SetCreatedDate>();
Property.Models.Configuration propertyConfiguration = Property.Models.Binder.Configuration.Get(isEnvironment, configurationRoot);
string[] directories = new string[] { propertyConfiguration.ResultContent };
_FileGroups = Shared.Models.Stateless.Methods.IPath.GetKeyValuePairs(propertyConfiguration, appSettings.CopyTo, directories);
Configuration configuration = Models.Binder.Configuration.Get(isEnvironment, configurationRoot, propertyConfiguration);
_PropertyConfiguration = propertyConfiguration;
_Configuration = configuration;
log.Information(propertyConfiguration.RootDirectory);
logger?.LogInformation(propertyConfiguration.RootDirectory);
Verify();
List<string> lines = SetCreatedDateFilesInDirectories(log);
List<string> lines = SetCreatedDateFilesInDirectories(logger);
File.WriteAllLines($"D:/Tmp/Phares/{DateTime.Now.Ticks}.tsv", lines);
if (lines.Count == 0)
_ = Shared.Models.Stateless.Methods.IPath.DeleteEmptyDirectories(propertyConfiguration.RootDirectory);
@ -139,7 +138,7 @@ public class SetCreatedDate
return results;
}
private List<string> SetCreatedDateFilesInDirectories(ILogger log)
private List<string> SetCreatedDateFilesInDirectories(ILogger<Program>? logger)
{
List<string> results = new();
ProgressBar progressBar;
@ -154,23 +153,23 @@ public class SetCreatedDate
progressBar = new(count, message, options);
List<Record> toDoCollection = GetToDoCollection(progressBar, filesCollection);
progressBar.Dispose();
log.Information($"Ready to set created date {toDoCollection.Count} file(s)?");
logger?.LogInformation($"Ready to set created date {toDoCollection.Count} file(s)?");
for (int y = 0; y < int.MaxValue; y++)
{
log.Information("Press \"Y\" key to set created date file(s), \"N\" key to log file(s) or close console to not set created date files");
logger?.LogInformation("Press \"Y\" key to set created date file(s), \"N\" key to log file(s) or close console to not set created date files");
consoleKey = System.Console.ReadKey().Key;
if (consoleKey is ConsoleKey.Y or ConsoleKey.N)
break;
}
log.Information(". . .");
logger?.LogInformation(". . .");
if (consoleKey is null || consoleKey.Value != ConsoleKey.Y)
log.Information("Nothing set!");
logger?.LogInformation("Nothing set!");
else
{
progressBar = new(count, message, options);
results.AddRange(SetCreatedDateForeach(progressBar, toDoCollection));
progressBar.Dispose();
log.Information("Done setting created date");
logger?.LogInformation("Done setting created date");
}
return results;
}