-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace ImageExtensions.Save.tt with Source Generator #5
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
6 file(s) reviewed, 8 comment(s)
Edit PR Review Bot Settings | Greptile
@@ -647,6 +647,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tga", "Tga", "{5DFC394F-136 | |||
tests\Images\Input\Tga\targa_8bit_rle.tga = tests\Images\Input\Tga\targa_8bit_rle.tga | |||
EndProjectSection | |||
EndProject | |||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImageSharp.Generators", "src\ImageSharp.Generators\ImageSharp.Generators.csproj", "{4743B273-473A-4D20-BE25-EC3D0B38E396}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: Project is duplicated - appears at both line 650 and line 36. Remove one instance to avoid confusion.
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
namespace ImageSharp.Generators; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: namespace should be SixLabors.ImageSharp.Generators to match assembly name
{ | ||
string methods = $@" | ||
/// <summary> | ||
/// Saves the image to the given stream with the {format} format. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: incorrect documentation - method saves to path, not stream
|
||
private static void GenerateExtensionsMethods(IncrementalGeneratorPostInitializationContext ctx) | ||
{ | ||
StringBuilder stringBuilder = new(FileHeader); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: consider using StringBuilder capacity constructor to avoid reallocations
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0;net6.0;net7.0</TargetFrameworks> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: netstandard2.0 target may be unnecessary since ImageSharp.csproj only targets net6.0 and net7.0
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Microsoft.CodeAnalysis.CSharp 4.0.1 is outdated. Consider using a newer version for better C# language support
Prerequisites
Description
This PR removes the ImageExtensions.Save T4 template and replaces it with a Source Generator.
The generator (and all future generators) is placed in the
ImageSharp.Generators
project, since generators need to be in a separate project than the code they generate for.I have also added
<LangVersion>10</LangVersion>
to the generator, in order to use features like file-scoped namespaces and such. It is completely safe to use a language version as high as the project which consumes the generator.Everything still builds and it is still possible to navigate to source for the extension methods the generator replaces. Let me know what you think!
Greptile Summary
This PR replaces the T4 template-based code generation with a modern source generator approach for image saving functionality in ImageSharp, moving the generation logic to a dedicated ImageSharp.Generators project.
src/ImageSharp.Generators
project targeting multiple frameworks (netstandard2.0, net6.0, net7.0)ImageExtensionsSaveGenerator.cs
to generate image saving extension methods at compile timesrc/ImageSharp/Formats/ImageExtensions.Save.tt
T4 template and its generated codeImageSharp.csproj
to reference the new generator project as an analyzer