Skip to content

Commit

Permalink
Initialisation lock file dans modgen au lieu de core
Browse files Browse the repository at this point in the history
  • Loading branch information
JabX committed Sep 2, 2024
1 parent 1f0f74d commit 5c53e79
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 95 deletions.
25 changes: 5 additions & 20 deletions TopModel.Core/ModelStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public class ModelStore
private readonly HashSet<string> _pendingUpdates = [];

private readonly object _puLock = new();
private readonly TopModelLock _topModelLock = new();

private readonly TranslationStore _translationStore;

private LoggingScope? _storeConfig;
private TopModelLock? _topModelLock;

public ModelStore(IMemoryCache fsCache, ModelFileLoader modelFileLoader, ILogger<ModelStore> logger, ModelConfig config, IEnumerable<IModelWatcher> modelWatchers, TranslationStore translationStore)
{
Expand All @@ -34,20 +34,6 @@ public ModelStore(IMemoryCache fsCache, ModelFileLoader modelFileLoader, ILogger
_modelFileLoader = modelFileLoader;
_translationStore = translationStore;
_modelWatchers = modelWatchers.Where(mw => !mw.Disabled);

var lockFile = new FileInfo(Path.Combine(_config.ModelRoot, _config.LockFileName));
if (lockFile.Exists)
{
try
{
using var file = lockFile.OpenText();
_topModelLock = new FileChecker().Deserialize<TopModelLock>(file);
}
catch
{
logger.LogError($"Erreur à la lecture du fichier {_config.LockFileName}. Merci de rétablir la version générée automatiquement.");
}
}
}

public event Action<bool>? OnResolve;
Expand Down Expand Up @@ -97,14 +83,13 @@ public Dictionary<string, Class> GetReferencedClasses(ModelFile modelFile)
.ToDictionary(c => c.Key, c => c.First());
}

public IDisposable? LoadFromConfig(bool watch = false, LoggingScope? storeConfig = null)
public IDisposable? LoadFromConfig(bool watch = false, TopModelLock? topModelLock = null, LoggingScope? storeConfig = null)
{
_storeConfig = storeConfig;
_topModelLock = topModelLock;

using var scope = _logger.BeginScope(_storeConfig!);

_topModelLock.Init(_logger);

var watchers = _modelWatchers.Select(mw => mw.FullName.Split("@")).GroupBy(split => split[0]).Select(grp => $"{grp.Key}@{{{string.Join(",", grp.Select(split => split[1]))}}}");
_logger.LogInformation($"Watchers enregistrés : \n - {string.Join("\n - ", watchers.OrderBy(x => x))}");

Expand Down Expand Up @@ -225,9 +210,9 @@ public void TryApplyUpdates()
});

var generatedFiles = _modelWatchers.Where(m => m.GeneratedFiles != null).SelectMany(m => m.GeneratedFiles!);
if (generatedFiles.Any() && !DisableLockfile)
if (generatedFiles.Any() && !DisableLockfile && _topModelLock != null)
{
_topModelLock.Update(_config.ModelRoot, _config.LockFileName, _logger, generatedFiles);
_topModelLock.Update(generatedFiles);
}

_logger.LogInformation($"Mise à jour terminée avec succès.");
Expand Down
22 changes: 11 additions & 11 deletions TopModel.Core/ServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace TopModel.Core;

public static class ServiceExtensions
{
public static IServiceCollection AddModelStore(this IServiceCollection services, FileChecker fileChecker, ModelConfig? config = null, string? rootDir = null)
public static IServiceCollection AddModelStore(this IServiceCollection services, FileChecker fileChecker, ModelConfig config)
{
services
.AddMemoryCache()
Expand All @@ -21,17 +21,17 @@ public static IServiceCollection AddModelStore(this IServiceCollection services,
.AddSingleton<ModelFileLoader>()
.AddSingleton<ModelConfig>()
.AddSingleton<TranslationStore>()
.AddSingleton<ModelStore>();

if (config != null && rootDir != null)
{
config.ModelRoot ??= string.Empty;
ModelUtils.TrimSlashes(config, c => c.ModelRoot);
ModelUtils.CombinePath(rootDir, config, c => c.ModelRoot);
ModelUtils.CombinePath(rootDir, config.I18n, c => c.RootPath);
services.AddSingleton(config);
}
.AddSingleton<ModelStore>()
.AddSingleton(config);

return services;
}

public static void FixConfig(this ModelConfig config, string rootDir)
{
config.ModelRoot ??= string.Empty;
ModelUtils.TrimSlashes(config, c => c.ModelRoot);
ModelUtils.CombinePath(rootDir, config, c => c.ModelRoot);
ModelUtils.CombinePath(rootDir, config.I18n, c => c.RootPath);
}
}
1 change: 0 additions & 1 deletion TopModel.Core/TopModel.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
<PackageReference Include="NJsonSchema" Version="11.0.2" />
<PackageReference Include="OneOf" Version="3.0.271" />
<PackageReference Include="YamlDotNet" Version="16.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
44 changes: 22 additions & 22 deletions TopModel.Generator.Jpa/JpaModelPropertyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,6 @@ public void WriteCompositePrimaryKeyClass(JavaWriter fw, Class classe, string ta
fw.WriteLine(1, "}");
}

private string GetterToCompareCompositePkPk(IProperty pk)
{
if (pk is AssociationProperty ap)
{
return $".get{ap.Property.NamePascal}()";
}
else if (pk is AliasProperty al && al.Property is AssociationProperty asp)
{
return $".get{asp.Property.NamePascal}()";
}

return string.Empty;
}

public void WriteCompositionProperty(JavaWriter fw, CompositionProperty property, string tag)
{
fw.WriteDocEnd(1);
Expand Down Expand Up @@ -209,6 +195,20 @@ private static void WriteValidationAnnotations(JavaWriter fw, string javaOrJakar
fw.AddImport($"{javaOrJakarta}.validation.constraints.NotNull");
}

private string GetterToCompareCompositePkPk(IProperty pk)
{
if (pk is AssociationProperty ap)
{
return $".get{ap.Property.NamePascal}()";
}
else if (pk is AliasProperty al && al.Property is AssociationProperty asp)
{
return $".get{asp.Property.NamePascal}()";
}

return string.Empty;
}

private bool ShouldWriteColumnAnnotation(Class classe, IProperty property)
{
return (classe.IsPersistent || _config.UseJdbc) && (property.Domain is null || !_config.GetImplementation(property.Domain)!.Annotations
Expand Down Expand Up @@ -275,14 +275,6 @@ private void WriteAliasProperty(JavaWriter fw, Class classe, AliasProperty prope
fw.WriteLine(1, $"private {_config.GetType(property, useClassForAssociation: useClassForAssociation)} {(isAssociationNotPersistent && !shouldWriteAssociation ? property.NameCamel : property.NameByClassCamel)}{suffix};");
}

private void WriteConvertAnnotation(JavaWriter fw, CompositionProperty property, int indentLevel, string tag)
{
var javaOrJakarta = _config.PersistenceMode.ToString().ToLower();
fw.AddImport($"{javaOrJakarta}.persistence.Convert");
fw.AddImport(_config.CompositionConverterCanonicalName.Replace("{class}", property.Class.Name).Replace("{package}", _config.GetPackageName(property.Class, tag)));
fw.WriteLine(indentLevel, $"@Convert(converter = {_config.CompositionConverterSimpleName.Replace("{class}", property.Class.Name)}.class)");
}

private void WriteAssociationAnnotations(JavaWriter fw, Class classe, AssociationProperty property, int indentLevel)
{
switch (property.Type)
Expand Down Expand Up @@ -429,6 +421,14 @@ private void WriteColumnAnnotation(JavaWriter fw, IProperty property, int indent
fw.WriteLine(indentLevel, column);
}

private void WriteConvertAnnotation(JavaWriter fw, CompositionProperty property, int indentLevel, string tag)
{
var javaOrJakarta = _config.PersistenceMode.ToString().ToLower();
fw.AddImport($"{javaOrJakarta}.persistence.Convert");
fw.AddImport(_config.CompositionConverterCanonicalName.Replace("{class}", property.Class.Name).Replace("{package}", _config.GetPackageName(property.Class, tag)));
fw.WriteLine(indentLevel, $"@Convert(converter = {_config.CompositionConverterSimpleName.Replace("{class}", property.Class.Name)}.class)");
}

private void WriteDomainAnnotations(JavaWriter fw, IProperty property, string tag, int indentLevel)
{
foreach (var annotation in _config.GetDomainAnnotations(property, tag))
Expand Down
15 changes: 10 additions & 5 deletions TopModel.Generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ void HandleFile(FileInfo file)
{
var (config, fullName, dn) = configs[i];

config.FixConfig(dn);

var storeConfig = new LoggingScope(i + 1, colors[i % colors.Length]);
var logger = loggerProvider.CreateLogger("TopModel.Generator");
using var scope = logger.BeginScope(storeConfig);
var topModelLock = new TopModelLock(logger, config.ModelRoot, config.LockFileName);

Console.WriteLine();

var generators = baseGenerators
Expand Down Expand Up @@ -248,7 +255,7 @@ void HandleFile(FileInfo file)
.AddTransient(typeof(ILogger<>), typeof(Logger<>))
.AddTransient<ILoggerFactory, LoggerFactory>()
.AddSingleton<ILoggerProvider>(loggerProvider)
.AddModelStore(fileChecker, config, dn);
.AddModelStore(fileChecker, config);

var hasError = false;

Expand Down Expand Up @@ -278,9 +285,7 @@ void HandleFile(FileInfo file)

var instance = Activator.CreateInstance(generator);
instance!.GetType().GetMethod("Register")!
.Invoke(
instance,
new object[] { services, genConfig, number });
.Invoke(instance, [services, genConfig, number]);
}
catch (ModelException me)
{
Expand Down Expand Up @@ -310,7 +315,7 @@ void HandleFile(FileInfo file)
hasErrors[k] = hasError;
};

var watcher = modelStore.LoadFromConfig(watchMode, new(i + 1, colors[i % colors.Length]));
var watcher = modelStore.LoadFromConfig(watchMode, topModelLock, storeConfig);
if (watcher != null)
{
disposables.Add(watcher);
Expand Down
3 changes: 2 additions & 1 deletion TopModel.LanguageServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
var configFile = new FileInfo(args.Length > 0 ? args[0] : "topmodel.config");
var config = fileChecker.Deserialize<ModelConfig>(configFile.OpenText().ReadToEnd());
var dn = configFile.DirectoryName;
config.FixConfig(dn!);

services
.AddModelStore(fileChecker, config, dn)
.AddModelStore(fileChecker, config)
.AddSingleton<IModelWatcher, ModelWatcher>()
.AddSingleton<ModelFileCache>();
})
Expand Down
26 changes: 5 additions & 21 deletions TopModel.ModelGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
var configs = new List<(ModelGeneratorConfig Config, string FullPath, string DirectoryName)>();
var serializer = new Serializer(new() { NamingConvention = new CamelCaseNamingConvention() });

var fileOption = new Option<IEnumerable<FileInfo>>(new[] { "-f", "--file" }, "Chemin vers un fichier de config.");
var watchOption = new Option<bool>(new[] { "-w", "--watch" }, "Lance le générateur en mode 'watch'");
var checkOption = new Option<bool>(new[] { "-c", "--check" }, "Vérifie que le modèle généré est conforme aux sources.");
var fileOption = new Option<IEnumerable<FileInfo>>(["-f", "--file"], "Chemin vers un fichier de config.");
var watchOption = new Option<bool>(["-w", "--watch"], "Lance le générateur en mode 'watch'");
var checkOption = new Option<bool>(["-c", "--check"], "Vérifie que le modèle généré est conforme aux sources.");
command.AddOption(fileOption);
command.AddOption(watchOption);
command.AddOption(checkOption);
Expand Down Expand Up @@ -210,31 +210,15 @@ async Task StartGeneration(string filePath, string directoryName, int i)

mainLogger.LogInformation($"Générateurs enregistrés :\n {string.Join("\n ", generators.Select(g => $"- {g.Name}@{{{g.Number}}}"))}");

TopModelLock tmdLock = new();
var lockFile = new FileInfo(Path.Combine(config.ModelRoot, config.LockFileName));
if (lockFile.Exists)
{
try
{
using var file = lockFile.OpenText();
tmdLock = serializer.Deserialize<TopModelLock>(file)!;
}
catch
{
mainLogger.LogError($"Erreur à la lecture du fichier {config.LockFileName}. Merci de rétablir la version générée automatiquement.");
}
}

tmdLock.Init(mainLogger);

var tmdLock = new TopModelLock(mainLogger, config.ModelRoot, config.LockFileName);
var generatedFiles = new List<string>();

foreach (var generator in generators)
{
generatedFiles.AddRange(await generator.Generate(loggingScope));
}

tmdLock.Update(config.ModelRoot, config.LockFileName, mainLogger, generatedFiles);
tmdLock.Update(generatedFiles);

mainLogger.LogInformation("Mise à jour terminée avec succès.");
}
Expand Down
3 changes: 2 additions & 1 deletion TopModel.UI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ public static void Main(string[] args)
var configFile = new FileInfo(args[0]);
var config = fileChecker.Deserialize<ModelConfig>(configFile.OpenText().ReadToEnd());
var dn = configFile.DirectoryName;
config.FixConfig(dn!);

services
.AddModelStore(fileChecker, config, dn)
.AddModelStore(fileChecker, config)
.AddHostedService<ModelWatcherService>()
.AddSingleton<ModelFileProvider>()
.AddSingleton<IModelWatcher>(p => p.GetRequiredService<ModelFileProvider>());
Expand Down
1 change: 1 addition & 0 deletions TopModel.Utils/TopModel.Utils.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="YamlDotNet" Version="16.0.0" />
</ItemGroup>

</Project>
54 changes: 41 additions & 13 deletions TopModel.Utils/TopModelLock.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
using System.Reflection;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Extensions.Logging;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

namespace TopModel.Utils;

public class TopModelLock
public class TopModelLock : TopModelLockFile
{
#nullable disable
public string Version { get; set; }
#nullable enable
private readonly IDeserializer _deserializer = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();

public List<string> GeneratedFiles { get; set; } = new();
private readonly string _lockFileName;
private readonly ILogger _logger;
private readonly string _modelRoot;

public void Init(ILogger logger)
[SetsRequiredMembers]
public TopModelLock(ILogger logger, string modelRoot, string lockFileName)
{
_lockFileName = lockFileName;
_logger = logger;
_modelRoot = modelRoot;

var lockFile = new FileInfo(Path.Combine(modelRoot, lockFileName));

if (lockFile.Exists)
{
try
{
using var file = lockFile.OpenText();
var lf = _deserializer.Deserialize<TopModelLockFile>(file);
Version = lf.Version;
GeneratedFiles = lf.GeneratedFiles;
}
catch
{
_logger.LogError($"Erreur à la lecture du fichier {lockFileName}. Merci de rétablir la version générée automatiquement.");
throw;
}
}

var assembly = Assembly.GetEntryAssembly()!.GetName()!;
var version = $"{assembly.Version!.Major}.{assembly.Version!.Minor}.{assembly.Version!.Build}";

Expand All @@ -25,12 +53,12 @@ public void Init(ILogger logger)
Version = version;
}

public void Update(string modelRoot, string lockFileName, ILogger logger, IEnumerable<string> generatedFiles)
public void Update(IEnumerable<string> generatedFiles)
{
GeneratedFiles ??= new();
GeneratedFiles ??= [];

var generatedFilesList = generatedFiles
.Select(f => f.ToRelative(modelRoot))
.Select(f => f.ToRelative(_modelRoot))
.Distinct()
.OrderBy(f => f)
.ToList();
Expand All @@ -39,17 +67,17 @@ public void Update(string modelRoot, string lockFileName, ILogger logger, IEnume
var filesToPrune = GeneratedFiles
.Select(f => f.Replace("\\", "/"))
.Where(f => !generatedFilesList.Select(gf => isWindows ? gf.ToLowerInvariant() : gf).Contains(isWindows ? f.ToLowerInvariant() : f))
.Select(f => Path.Combine(modelRoot, f));
.Select(f => Path.Combine(_modelRoot, f));

Parallel.ForEach(filesToPrune.Where(File.Exists), fileToPrune =>
{
File.Delete(fileToPrune);
logger.LogInformation($"Supprimé: {fileToPrune.ToRelative()}");
_logger.LogInformation($"Supprimé: {fileToPrune.ToRelative()}");
});

GeneratedFiles = generatedFilesList;

using var fw = new FileWriter(Path.Combine(modelRoot, lockFileName), logger)
using var fw = new FileWriter(Path.Combine(_modelRoot, _lockFileName), _logger)
{
StartCommentToken = "#"
};
Expand Down
Loading

0 comments on commit 5c53e79

Please sign in to comment.