Skip to content

Commit

Permalink
Parallélisation de la génération de fichiers
Browse files Browse the repository at this point in the history
  • Loading branch information
JabX committed May 10, 2024
1 parent e4823d9 commit a6c10f6
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 76 deletions.
8 changes: 4 additions & 4 deletions TopModel.Core/ModelStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ public void TryApplyUpdates()

referenceErrors.AddRange(GetGlobalErrors());

foreach (var modelWatcher in _modelWatchers)
Parallel.ForEach(_modelWatchers, modelWatcher =>
{
modelWatcher.OnErrors(affectedFiles
.Select(file => (file, errors: referenceErrors.Where(e => e.File == file && !_config.NoWarn.Contains(e.ModelErrorType))))
.ToDictionary(i => i.file, i => i.errors));
}
});

foreach (var error in referenceErrors.Where(e => e.IsError))
{
Expand All @@ -219,10 +219,10 @@ public void TryApplyUpdates()
_logger.LogInformation("Modèle chargé avec succès.");
}

foreach (var modelWatcher in _modelWatchers)
Parallel.ForEach(_modelWatchers, modelWatcher =>
{
modelWatcher.OnFilesChanged(sortedFiles, _storeConfig);
}
});

var generatedFiles = _modelWatchers.Where(m => m.GeneratedFiles != null).SelectMany(m => m.GeneratedFiles!);
if (generatedFiles.Any() && !DisableLockfile)
Expand Down
19 changes: 7 additions & 12 deletions TopModel.Generator.Core/ClassGeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,12 @@ protected virtual bool FilterClass(Class classe)

protected override void HandleFiles(IEnumerable<ModelFile> files)
{
foreach (var file in files)
{
foreach (var classe in file.Classes.Where(FilterClass))
{
foreach (var (tag, fileName) in Config.Tags.Intersect(classe.Tags)
.Select(tag => (tag, fileName: GetFileName(classe, tag)))
.DistinctBy(t => t.fileName))
{
HandleClass(fileName, classe, tag);
}
}
}
Parallel.ForEach(files, file =>
Parallel.ForEach(file.Classes.Where(FilterClass), classe =>
Parallel.ForEach(
Config.Tags.Intersect(classe.Tags)
.Select(tag => (tag, fileName: GetFileName(classe, tag)))
.DistinctBy(t => t.fileName),
l => HandleClass(l.fileName, classe, l.tag))));
}
}
19 changes: 11 additions & 8 deletions TopModel.Generator.Core/ClassGroupGeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ public ClassGroupGeneratorBase(ILogger<ClassGroupGeneratorBase<T>> logger)

protected override void HandleFiles(IEnumerable<ModelFile> files)
{
foreach (var file in Classes
.SelectMany(classe => Config.Tags.Intersect(classe.Tags)
.SelectMany(tag => GetFileNames(classe, tag)
.Select(f => (key: (f.FileType, f.FileName), tag, classe))))
.GroupBy(f => f.key))
{
HandleFile(file.Key.FileType, file.Key.FileName, file.First().tag, file.Select(f => f.classe).Distinct());
}
Parallel.ForEach(
Classes
.SelectMany(classe => Config.Tags.Intersect(classe.Tags)
.SelectMany(tag => GetFileNames(classe, tag)
.Select(f => (key: (f.FileType, f.FileName), tag, classe))))
.GroupBy(f => f.key),
file => HandleFile(
file.Key.FileType,
file.Key.FileName,
file.First().tag,
file.Select(f => f.classe).Distinct()));
}
}
34 changes: 18 additions & 16 deletions TopModel.Generator.Core/EndpointsGeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,24 @@ protected virtual bool FilterTag(string tag)

protected override void HandleFiles(IEnumerable<ModelFile> files)
{
foreach (var file in Files.Values
.SelectMany(file => Config.Tags.Intersect(file.AllTags.Where(FilterTag))
.Select(tag => (tag, file, filePath: GetFilePath(file, tag))))
.GroupBy(file => file.filePath))
{
var endpoints = file
.SelectMany(f => f.file.Endpoints)
.Where(e => e.Tags.Intersect(file.Select(f => f.tag)).Any())
.Distinct()
.OrderBy(e => e.Name, StringComparer.Ordinal)
.ToList();

if (endpoints.Any())
Parallel.ForEach(
Files.Values
.SelectMany(file => Config.Tags.Intersect(file.AllTags.Where(FilterTag))
.Select(tag => (tag, file, filePath: GetFilePath(file, tag))))
.GroupBy(file => file.filePath),
file =>
{
HandleFile(file.Key, file.First().file.Options.Endpoints.FileName, file.First().tag, endpoints);
}
}
var endpoints = file
.SelectMany(f => f.file.Endpoints)
.Where(e => e.Tags.Intersect(file.Select(f => f.tag)).Any())
.Distinct()
.OrderBy(e => e.Name, StringComparer.Ordinal)
.ToList();

if (endpoints.Any())
{
HandleFile(file.Key, file.First().file.Options.Endpoints.FileName, file.First().tag, endpoints);
}
});
}
}
4 changes: 2 additions & 2 deletions TopModel.Generator.Core/MapperGeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ protected override void HandleFiles(IEnumerable<ModelFile> files)
.ToArray(),
Tags: f.Select(m => m.Tag)));

foreach (var fileName in fromMappers.Keys.Concat(toMappers.Keys).Distinct())
Parallel.ForEach(fromMappers.Keys.Concat(toMappers.Keys).Distinct(), fileName =>
{
var (fileFromMappers, fromTags) = fromMappers.ContainsKey(fileName) ? fromMappers[fileName] : (Array.Empty<(Class, FromMapper)>(), Array.Empty<string>());
var (fileToMappers, toTags) = toMappers.ContainsKey(fileName) ? toMappers[fileName] : (Mappers: Array.Empty<(Class, ClassMappings)>(), Tags: Array.Empty<string>());
HandleFile(fileName, fromTags.Concat(toTags).First(), fileFromMappers, fileToMappers);
}
});
}

protected virtual bool IsPersistent(Class classe)
Expand Down
73 changes: 39 additions & 34 deletions TopModel.Generator.Core/TranslationGeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,42 +50,47 @@ protected override void HandleFiles(IEnumerable<ModelFile> files)
{
var modules = new List<(string MainFilePath, string ModuleFilePath, string ModuleName)>();

foreach (var resources in Classes
.SelectMany(classe => Config.Tags.Intersect(classe.Tags)
.SelectMany(tag => classe.Properties.OfType<IFieldProperty>()
.SelectMany(p => GetResourceFileNames(p, tag)
.Select(f => (key: (MainFilePath: GetMainResourceFilePath(tag, f.Lang), ModuleFilePath: f.FilePath, f.Lang), p)))))
.GroupBy(f => f.key))
{
var properties = resources.Select(r => r.p.ResourceProperty).Distinct();
HandleResourceFile(resources.Key.ModuleFilePath, resources.Key.Lang, properties);

if (resources.Key.MainFilePath != null)
Parallel.ForEach(
Classes
.SelectMany(classe => Config.Tags.Intersect(classe.Tags)
.SelectMany(tag => classe.Properties.OfType<IFieldProperty>()
.SelectMany(p => GetResourceFileNames(p, tag)
.Select(f => (key: (MainFilePath: GetMainResourceFilePath(tag, f.Lang), ModuleFilePath: f.FilePath, f.Lang), p)))))
.GroupBy(f => f.key),
resources =>
{
modules.Add((resources.Key.MainFilePath, resources.Key.ModuleFilePath, properties.First().Parent.Namespace.RootModule));
}
}

foreach (var resources in Classes
.SelectMany(classe => Config.Tags.Intersect(classe.Tags)
.SelectMany(tag => classe.Properties.OfType<IFieldProperty>()
.SelectMany(p => GetCommentResourceFileNames(p, tag)
.Select(f => (key: (MainFilePath: GetMainResourceFilePath(tag, f.Lang), ModuleFilePath: f.FilePath, f.Lang), p)))))
.GroupBy(f => f.key))
{
var properties = resources.Select(r => r.p.CommentResourceProperty).Distinct();
HandleCommentResourceFile(resources.Key.ModuleFilePath, resources.Key.Lang, properties);

if (resources.Key.MainFilePath != null)
var properties = resources.Select(r => r.p.ResourceProperty).Distinct();
HandleResourceFile(resources.Key.ModuleFilePath, resources.Key.Lang, properties);

if (resources.Key.MainFilePath != null)
{
modules.Add((resources.Key.MainFilePath, resources.Key.ModuleFilePath, properties.First().Parent.Namespace.RootModule));
}
});

Parallel.ForEach(
Classes
.SelectMany(classe => Config.Tags.Intersect(classe.Tags)
.SelectMany(tag => classe.Properties.OfType<IFieldProperty>()
.SelectMany(p => GetCommentResourceFileNames(p, tag)
.Select(f => (key: (MainFilePath: GetMainResourceFilePath(tag, f.Lang), ModuleFilePath: f.FilePath, f.Lang), p)))))
.GroupBy(f => f.key),
resources =>
{
modules.Add((resources.Key.MainFilePath, resources.Key.ModuleFilePath, $"{properties.First().Parent.Namespace.RootModule}Comments"));
}
}

foreach (var g in modules.GroupBy(m => m.MainFilePath))
{
HandleMainResourceFile(g.Key, g.Select(l => (l.ModuleFilePath, l.ModuleName)).OrderBy(m => m.ModuleFilePath));
}
var properties = resources.Select(r => r.p.CommentResourceProperty).Distinct();
HandleCommentResourceFile(resources.Key.ModuleFilePath, resources.Key.Lang, properties);

if (resources.Key.MainFilePath != null)
{
modules.Add((resources.Key.MainFilePath, resources.Key.ModuleFilePath, $"{properties.First().Parent.Namespace.RootModule}Comments"));
}
});

Parallel.ForEach(
modules.GroupBy(m => m.MainFilePath),
g => HandleMainResourceFile(
g.Key,
g.Select(l => (l.ModuleFilePath, l.ModuleName)).OrderBy(m => m.ModuleFilePath)));
}

protected virtual void HandleMainResourceFile(string mainFilePath, IEnumerable<(string ModuleFilePath, string ModuleName)> modules)
Expand Down

0 comments on commit a6c10f6

Please sign in to comment.