Skip to content

Commit

Permalink
feat: #144 check dependency versions when running codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
ascott18 committed Sep 12, 2024
1 parent 9946063 commit c23736a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Outputs the Project Information needed for CodeGeneration to a file.
<WriteLinesToFile Encoding="Unicode" File="$(OutputFile)" Lines="{" Overwrite="true"/>

<WriteLinesToFile Encoding="Unicode" File="$(OutputFile)" Lines="DependenciesDesignTime: ["/>
<WriteLinesToFile Encoding="Unicode" File="$(OutputFile)" Lines="@(_DependenciesDesignTime->'{Path: %22%(Identity)%22, Name: %22%(Name)%22, Type: %22%(Type)%22, Target: %22%(Target)%22, Version: %22%(Version)%22, Resolved:%22%(Resolved)%22, Dependencies:%22%(Dependencies)%22 }, ')" />
<WriteLinesToFile Encoding="Unicode" File="$(OutputFile)" Lines="@(_PackageDependenciesDesignTime->'{Path: %22%(Identity)%22, Name: %22%(Name)%22, Type: %22%(Type)%22, Target: %22%(Target)%22, Version: %22%(Version)%22, Resolved:%22%(Resolved)%22, Dependencies:%22%(Dependencies)%22 }, ')" />
<WriteLinesToFile Encoding="Unicode" File="$(OutputFile)" Lines="],"/>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using IntelliTect.Coalesce.CodeGeneration.Generation;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;

#nullable enable

namespace IntelliTect.Coalesce.CodeGeneration.Analysis
{
internal class NpmDependencyAnalayzer(GenerationContext context, ILogger<NpmDependencyAnalayzer> logger)
{
public async Task<string?> GetNpmPackageVersion(string packageName)
{
var packageLockPath = Path.Combine(context.WebProject.ProjectPath, "package-lock.json");
var fileInfo = new FileInfo(packageLockPath);
if (!fileInfo.Exists)
{
logger.LogDebug("Unable to determine installed coalesce-vue version: package-lock.json not found.");
return null;
}

try
{
var content = await JsonSerializer.DeserializeAsync<JsonElement>(fileInfo.OpenRead());
var version = content
.GetProperty("packages")
.GetProperty("node_modules/" + packageName)
.GetProperty("version")
.GetString();

return version;
}
catch (Exception ex)
{
logger.LogDebug(ex, "Unable to determine installed coalesce-vue version");
return null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
using IntelliTect.Coalesce.CodeGeneration.Analysis;
using IntelliTect.Coalesce.CodeGeneration.Analysis.Base;
using IntelliTect.Coalesce.CodeGeneration.Analysis.MsBuild;
using IntelliTect.Coalesce.CodeGeneration.Analysis.Roslyn;
using IntelliTect.Coalesce.CodeGeneration.Configuration;
using IntelliTect.Coalesce.CodeGeneration.Templating.Resolution;
using IntelliTect.Coalesce.CodeGeneration.Utilities;
using IntelliTect.Coalesce.TypeDefinition;
using IntelliTect.Coalesce.Validation;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace IntelliTect.Coalesce.CodeGeneration.Generation
Expand All @@ -42,6 +38,7 @@ public GenerationExecutor(CoalesceConfiguration config, LogLevel logLevel)
services.AddSingleton<GeneratorServices>();
services.AddSingleton<CompositeGeneratorServices>();
services.AddSingleton<GenerationContext>();
services.AddSingleton<NpmDependencyAnalayzer>();
services.AddTransient<IProjectContextFactory, RoslynProjectContextFactory>();
ServiceProvider = services.BuildServiceProvider();
}
Expand Down Expand Up @@ -93,6 +90,7 @@ public async Task GenerateAsync(Type rootGenerator)
await LoadProjects(Logger, genContext);

var locator = genContext.DataProject.TypeLocator as RoslynTypeLocator;
var npmPackageVersionTask = ServiceProvider.GetRequiredService<NpmDependencyAnalayzer>().GetNpmPackageVersion("coalesce-vue");

Logger.LogInformation("Gathering Types");
var types = locator.GetAllTypes();
Expand Down Expand Up @@ -155,6 +153,23 @@ public async Task GenerateAsync(Type rootGenerator)

Logger.LogInformation("Generation Complete");


var generatorVersion = GetCodeGenVersion();
var npmPackageVersion = await npmPackageVersionTask;
if (npmPackageVersion is not null && npmPackageVersion != generatorVersion)
{
Logger.LogWarning($"Running Coalesce CLI {generatorVersion}, but NPM package `coalesce-vue` version is {npmPackageVersion}");
}
}

private string GetCodeGenVersion()
{
var generatorAssembly = Assembly.GetEntryAssembly();
return FileVersionInfo
.GetVersionInfo(generatorAssembly.Location)
.ProductVersion
// SourceLink will append the commit hash to the version, using '+' as a delimiter.
.Split('+').First();
}

private async Task LoadProjects(ILogger<GenerationExecutor> logger, GenerationContext genContext)
Expand Down Expand Up @@ -196,12 +211,7 @@ async Task<ProjectContext> TryLoadProject(ProjectConfiguration config)
.GroupBy(r => new { r.Name, r.Version }).Select(g => g.First())
.ToList();

var generationVersion = FileVersionInfo
.GetVersionInfo(Assembly.GetExecutingAssembly().Location)
.ProductVersion
// SourceLink will append the commit hash to the version, using '+' as a delimiter.
.Split('+').First();

var generationVersion = GetCodeGenVersion();
foreach (var coalescePkg in coalescePackages)
{
if (coalescePkg.Version != generationVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,16 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
Console.Write($"{ApplicationTimer.Stopwatch.ElapsedMilliseconds / 1000d:0.000}");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write("] ");
Console.ResetColor();
if (logLevel == LogLevel.Information)
{
Console.ResetColor();
}
else
{
Console.ForegroundColor = colorLevelMap[logLevel];
}
Console.WriteLine(formatter(state, exception));
Console.ResetColor();
Console.Out.Flush();
}
}
Expand Down

0 comments on commit c23736a

Please sign in to comment.