AOT Builds

This commit is contained in:
2023-10-22 11:25:08 -07:00
commit f7573e95e4
56 changed files with 4270 additions and 0 deletions

47
Rename/AA.Rename.csproj Normal file
View File

@ -0,0 +1,47 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<TargetFramework>net8.0</TargetFramework>
<UserSecretsId>fa0fa59b-afe4-4960-9afc-18fcbc7fb41b</UserSecretsId>
</PropertyGroup>
<PropertyGroup>
<PackageId>Phares.View.by.Distance.Rename</PackageId>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>8.0.101.1</Version>
<Authors>Mike Phares</Authors>
<Company>Phares</Company>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
<PropertyGroup>
<IsWindows Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'">true</IsWindows>
<IsOSX Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))' == 'true'">true</IsOSX>
<IsLinux Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' == 'true'">true</IsLinux>
</PropertyGroup>
<PropertyGroup Condition="'$(IsWindows)'=='true'">
<DefineConstants>Windows</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(IsOSX)'=='true'">
<DefineConstants>OSX</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(IsLinux)'=='true'">
<DefineConstants>Linux</DefineConstants>
</PropertyGroup>
<ItemGroup Condition="'$(RuntimeIdentifier)' == 'browser-wasm'">
<SupportedPlatform Include="browser" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="runtime.win-x64.Microsoft.DotNet.ILCompiler" Version="8.0.0-rc.2.23479.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="ShellProgressBar" Version="5.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Metadata\AA.Metadata.csproj" />
<ProjectReference Include="..\Shared\AA.Shared.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,26 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace View_by_Distance.Rename.Models;
public record AppSettings(string Company,
string DefaultUnknownDirectoryName,
bool ForceIdName,
int MaxDegreeOfParallelism,
int MaxMinutesDelta,
bool RenameUndo)
{
public override string ToString()
{
string result = JsonSerializer.Serialize(this, AppSettingsSourceGenerationContext.Default.AppSettings);
return result;
}
}
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(AppSettings))]
internal partial class AppSettingsSourceGenerationContext : JsonSerializerContext
{
}

View File

@ -0,0 +1,2 @@
[*.cs]
csharp_preserve_single_line_statements = true

View File

@ -0,0 +1,72 @@
using Microsoft.Extensions.Configuration;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace View_by_Distance.Rename.Models.Binder;
public class AppSettings
{
public string? Company { get; set; }
public string? DefaultUnknownDirectoryName { get; set; }
public bool? ForceIdName { get; set; }
public int? MaxDegreeOfParallelism { get; set; }
public int? MaxMinutesDelta { get; set; }
public bool? RenameUndo { get; set; }
public override string ToString()
{
string result = JsonSerializer.Serialize(this, BinderAppSettingsSourceGenerationContext.Default.AppSettings);
return result;
}
private static Models.AppSettings Get(AppSettings? appSettings)
{
Models.AppSettings result;
if (appSettings?.Company is null) throw new NullReferenceException(nameof(appSettings.Company));
if (appSettings?.DefaultUnknownDirectoryName is null) throw new NullReferenceException(nameof(appSettings.DefaultUnknownDirectoryName));
if (appSettings?.ForceIdName is null) throw new NullReferenceException(nameof(appSettings.ForceIdName));
if (appSettings?.MaxDegreeOfParallelism is null) throw new NullReferenceException(nameof(appSettings.MaxDegreeOfParallelism));
if (appSettings?.MaxMinutesDelta is null) throw new NullReferenceException(nameof(appSettings.MaxMinutesDelta));
if (appSettings?.RenameUndo is null) throw new NullReferenceException(nameof(appSettings.RenameUndo));
result = new(
appSettings.Company,
appSettings.DefaultUnknownDirectoryName,
appSettings.ForceIdName.Value,
appSettings.MaxDegreeOfParallelism.Value,
appSettings.MaxMinutesDelta.Value,
appSettings.RenameUndo.Value
);
return result;
}
public static Models.AppSettings Get(IConfigurationRoot configurationRoot)
{
Models.AppSettings result;
#pragma warning disable IL3050, IL2026
AppSettings? appSettings = configurationRoot.Get<AppSettings>();
#pragma warning restore IL3050, IL2026
if (appSettings?.Company is null)
{
foreach (IConfigurationProvider configurationProvider in configurationRoot.Providers)
{
if (configurationProvider is not Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider jsonConfigurationProvider)
continue;
if (jsonConfigurationProvider.Source.FileProvider is not Microsoft.Extensions.FileProviders.PhysicalFileProvider physicalFileProvider)
continue;
if (!physicalFileProvider.Root.Contains("UserSecrets"))
continue;
throw new NotSupportedException(physicalFileProvider.Root);
}
}
result = Get(appSettings);
return result;
}
}
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(AppSettings))]
internal partial class BinderAppSettingsSourceGenerationContext : JsonSerializerContext
{
}

View File

@ -0,0 +1,62 @@
using Microsoft.Extensions.Configuration;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace View_by_Distance.Rename.Models.Binder;
public class Configuration
{
public bool? ForceMetadataLastWriteTimeToCreationTime { get; set; }
public string[]? IgnoreExtensions { get; set; }
public string? PersonBirthdayFormat { get; set; }
public bool? PropertiesChangedForMetadata { get; set; }
public override string ToString()
{
string result = JsonSerializer.Serialize(this, BinderConfigurationSourceGenerationContext.Default.Configuration);
return result;
}
private static Models.Configuration Get(Configuration? configuration, Metadata.Models.MetadataConfiguration metadataConfiguration)
{
Models.Configuration result;
if (configuration is null) throw new NullReferenceException(nameof(configuration));
if (configuration.ForceMetadataLastWriteTimeToCreationTime is null) throw new NullReferenceException(nameof(configuration.ForceMetadataLastWriteTimeToCreationTime));
if (configuration.IgnoreExtensions is null) throw new NullReferenceException(nameof(configuration.IgnoreExtensions));
if (configuration.PersonBirthdayFormat is null) throw new NullReferenceException(nameof(configuration.PersonBirthdayFormat));
if (configuration.PropertiesChangedForMetadata is null) throw new NullReferenceException(nameof(configuration.PropertiesChangedForMetadata));
result = new(metadataConfiguration,
configuration.ForceMetadataLastWriteTimeToCreationTime.Value,
configuration.IgnoreExtensions,
configuration.PersonBirthdayFormat,
configuration.PropertiesChangedForMetadata.Value);
return result;
}
public static Models.Configuration Get(IConfigurationRoot configurationRoot, Metadata.Models.MetadataConfiguration metadataConfiguration)
{
Models.Configuration result;
#if Linux
string environmentName = "Linux";
#elif OSX
string environmentName = "OSX";
#elif Windows
string environmentName = "Windows";
#endif
string section = string.Concat(environmentName, ":", nameof(Configuration));
IConfigurationSection configurationSection = configurationRoot.GetSection(section);
#pragma warning disable IL3050, IL2026
Configuration? configuration = configurationSection.Get<Configuration>();
#pragma warning restore IL3050, IL2026
result = Get(configuration, metadataConfiguration);
return result;
}
}
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Configuration))]
internal partial class BinderConfigurationSourceGenerationContext : JsonSerializerContext
{
}

View File

@ -0,0 +1,26 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace View_by_Distance.Rename.Models;
public record Configuration(Metadata.Models.MetadataConfiguration MetadataConfiguration,
bool ForceMetadataLastWriteTimeToCreationTime,
string[] IgnoreExtensions,
string PersonBirthdayFormat,
bool PropertiesChangedForMetadata)
{
public override string ToString()
{
string result = JsonSerializer.Serialize(this, ConfigurationSourceGenerationContext.Default.Configuration);
return result;
}
}
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(Configuration))]
internal partial class ConfigurationSourceGenerationContext : JsonSerializerContext
{
}

57
Rename/Program.cs Normal file
View File

@ -0,0 +1,57 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using View_by_Distance.Rename.Models;
namespace View_by_Distance.Rename;
public class Program
{
public static void Secondary(ILogger<Program> logger, List<string> args)
{
IConfigurationBuilder configurationBuilder = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddUserSecrets<Program>();
IConfigurationRoot configurationRoot = configurationBuilder.Build();
AppSettings appSettings = Models.Binder.AppSettings.Get(configurationRoot);
if (appSettings.MaxDegreeOfParallelism > Environment.ProcessorCount)
throw new Exception("MaxDegreeOfParallelism must be =< Environment.ProcessorCount!");
if (string.IsNullOrEmpty(appSettings.Company))
throw new Exception("Company must have a value!");
int silentIndex = args.IndexOf("s");
if (silentIndex > -1)
args.RemoveAt(silentIndex);
try
{
if (args is null)
throw new Exception("args is null!");
Shared.Models.Console console = new();
_ = new Rename(args, logger, configurationRoot, appSettings, silentIndex > -1, console);
}
catch (Exception ex)
{
logger?.LogError(ex, "Error!");
}
if (silentIndex > -1)
logger?.LogDebug("Done. Bye");
else
{
logger?.LogDebug("Done. Press 'Enter' to end");
_ = Console.ReadLine();
}
}
public static void Main(string[] args)
{
#pragma warning disable IL3050
ILogger<Program>? logger = Host.CreateDefaultBuilder(args).Build().Services.GetRequiredService<ILogger<Program>>();
#pragma warning restore IL3050
if (args is not null)
Secondary(logger, args.ToList());
else
Secondary(logger, []);
}
}

71
Rename/Rename.cs Normal file
View File

@ -0,0 +1,71 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using ShellProgressBar;
using View_by_Distance.Metadata.Models;
using View_by_Distance.Rename.Models;
using View_by_Distance.Shared.Models;
using View_by_Distance.Shared.Models.Stateless.Methods;
namespace View_by_Distance.Rename;
public class Rename
{
private readonly AppSettings _AppSettings;
private readonly Configuration _Configuration;
private readonly IConfigurationRoot _ConfigurationRoot;
private readonly MetadataConfiguration _MetadataConfiguration;
public Rename(List<string> args, ILogger<Program>? logger, IConfigurationRoot configurationRoot, AppSettings appSettings, bool isSilent, IConsole console)
{
if (isSilent)
{ }
if (args is null)
throw new NullReferenceException(nameof(args));
if (console is null)
throw new NullReferenceException(nameof(console));
_AppSettings = appSettings;
_ConfigurationRoot = configurationRoot;
MetadataConfiguration metadataConfiguration = Metadata.Models.Binder.Configuration.Get(configurationRoot);
Configuration configuration = Models.Binder.Configuration.Get(configurationRoot, metadataConfiguration);
_MetadataConfiguration = metadataConfiguration;
_Configuration = configuration;
logger?.LogInformation("{RootDirectory}", metadataConfiguration.RootDirectory);
MetadataConfiguration.Verify(metadataConfiguration, requireExist: false);
Verify();
List<string> linesB = RenameFilesInDirectories(logger);
if (linesB.Count != 0)
{
File.WriteAllLines($"D:/Tmp/Phares/{DateTime.Now.Ticks}.tsv", linesB);
_ = IPath.DeleteEmptyDirectories(metadataConfiguration.RootDirectory);
}
}
private void Verify()
{
if (_AppSettings is null)
throw new NullReferenceException(nameof(_AppSettings));
if (_Configuration is null)
throw new NullReferenceException(nameof(_Configuration));
if (_ConfigurationRoot is null)
throw new NullReferenceException(nameof(_ConfigurationRoot));
if (_MetadataConfiguration is null)
throw new NullReferenceException(nameof(_MetadataConfiguration));
}
private List<string> RenameFilesInDirectories(ILogger? logger)
{
List<string> old = [];
List<ExifDirectory> exifDirectories = [];
string rootDirectoryFullPath = Path.GetFullPath(_MetadataConfiguration.RootDirectory);
ParallelOptions parallelOptions = new() { MaxDegreeOfParallelism = _AppSettings.MaxDegreeOfParallelism };
IEnumerable<string> files = Directory.EnumerateFiles(rootDirectoryFullPath, "*", SearchOption.AllDirectories);
A_Metadata metadata = new(_MetadataConfiguration, _Configuration.ForceMetadataLastWriteTimeToCreationTime, _Configuration.PropertiesChangedForMetadata);
ProgressBar progressBar = new(123000, "EnumerateFiles load", new ProgressBarOptions() { ProgressCharacter = '─', ProgressBarOnBottom = true, DisableBottomPercentage = true });
files.AsParallel().ForAll(A_Metadata.GetResultCollection(metadata, exifDirectories, () => progressBar.Tick()));
if (progressBar.CurrentTick != exifDirectories.Count)
throw new NotSupportedException();
return old;
}
}