Skip to content

Commit

Permalink
[modgen] Build des générateurs custom si sources ou dll modifiées (av…
Browse files Browse the repository at this point in the history
…ec hash dans le lockfile)
  • Loading branch information
JabX committed Sep 24, 2024
1 parent b12b5cd commit 67f27db
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 12 deletions.
69 changes: 59 additions & 10 deletions TopModel.Generator/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.CommandLine;
using System.Diagnostics;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
Expand Down Expand Up @@ -230,6 +231,50 @@ void HandleFile(FileInfo file)

Console.WriteLine();

foreach (var cg in config.CustomGenerators)
{
string GetCgHash()
{
return GetHash(
GetCustomAssemblies(cg, fullName).Select(f => f.FullName)
.Concat(Directory.EnumerateFiles(
Path.GetFullPath(cg, new FileInfo(fullName).DirectoryName!),
"*.cs",
SearchOption.AllDirectories)
.Where(f => !f.Contains($"{Path.DirectorySeparatorChar}obj{Path.DirectorySeparatorChar}")))
.ToList(),
cg) ?? string.Empty;
}

var customHash = GetCgHash();
if (!(topModelLock.Custom?.TryGetValue(cg, out var customLockHash) ?? false) || customHash != customLockHash)
{
logger.LogInformation($"Build de '{cg}' en cours...");
var build = Process.Start(new ProcessStartInfo
{
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "dotnet.exe",
Arguments = "build -v q",
WorkingDirectory = cg,
RedirectStandardOutput = true,
StandardOutputEncoding = Encoding.UTF8
});
await build!.WaitForExitAsync();

if (build.ExitCode != 0)
{
logger.LogError($"Erreur lors du build de '{cg}'");
logger.LogError((await build.StandardOutput.ReadToEndAsync()).Trim());
return 1;
}

logger.LogInformation($"Build de '{cg}' terminé.");
topModelLock.Custom ??= [];
topModelLock.Custom[cg] = GetCgHash();
}
}

var generators = new List<Type>();
var deps = new List<ModgenDependency>();

Expand Down Expand Up @@ -306,10 +351,7 @@ void HandleFile(FileInfo file)

if (returnCode == 0)
{
var assemblies = new DirectoryInfo(Path.Combine(Path.GetFullPath(cg, new FileInfo(fullName).DirectoryName!), "bin"))
.GetFiles($"*.dll", SearchOption.AllDirectories)
.Where(a => a.FullName.Contains(framework) && !modgenAssemblies.Contains(a.Name))
.DistinctBy(a => a.Name)
var assemblies = GetCustomAssemblies(cg, fullName)
.Select(f => Assembly.LoadFrom(f.FullName))
.ToList();

Expand Down Expand Up @@ -632,15 +674,22 @@ void HandleFile(FileInfo file)

return returnCode;

IEnumerable<FileInfo> GetCustomAssemblies(string cg, string fullName)
{
return new DirectoryInfo(Path.Combine(Path.GetFullPath(cg, new FileInfo(fullName).DirectoryName!), "bin"))
.GetFiles($"*.dll", SearchOption.AllDirectories)
.Where(a => a.FullName.Contains(framework) && !modgenAssemblies.Contains(a.Name))
.DistinctBy(a => a.Name);
}

static string? GetFolderHash(string path)
{
var md5 = MD5.Create();
if (!Directory.Exists(path))
{
return null;
}
return GetHash(Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories).OrderBy(p => p).ToList(), path);
}

var files = Directory.GetFiles(path, "*", SearchOption.AllDirectories).OrderBy(p => p).ToList();
static string? GetHash(IList<string> files, string path)
{
var md5 = MD5.Create();
foreach (var file in files)
{
var relativePath = file[(path.Length + 1)..];
Expand Down
14 changes: 12 additions & 2 deletions TopModel.Utils/LoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,22 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
var split2 = message.Split('/');
if (split2.Length > 1)
{
Console.ForegroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write($"{string.Join('/', split2[0..^1])}/");
Console.ForegroundColor = ConsoleColor.Blue;
}

Console.WriteLine(split2[^1]);
var split3 = split2[^1].Split('\'');
if (split3.Length == 2)
{
Console.Write(split3[0]);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine($"'{split3[1]}");
}
else
{
Console.WriteLine(split2[^1]);
}
}
else
{
Expand Down
1 change: 1 addition & 0 deletions TopModel.Utils/TopModelLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public TopModelLock(ILogger logger, string modelRoot, string lockFileName)
Version = lf.Version;
GeneratedFiles = lf.GeneratedFiles;
Modules = lf.Modules;
Custom = lf.Custom;
}
catch
{
Expand Down
2 changes: 2 additions & 0 deletions TopModel.Utils/TopModelLockFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ public class TopModelLockFile

public Dictionary<string, TopModelLockModule> Modules { get; set; } = [];

public Dictionary<string, string>? Custom { get; set; }

public List<string> GeneratedFiles { get; set; } = [];
}

0 comments on commit 67f27db

Please sign in to comment.