Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

lizard-boy
Copy link

@lizard-boy lizard-boy commented Nov 1, 2024

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules 👮.
  • I have provided test coverage for my change (where applicable)

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.

  • Added new src/ImageSharp.Generators project targeting multiple frameworks (netstandard2.0, net6.0, net7.0)
  • Implemented ImageExtensionsSaveGenerator.cs to generate image saving extension methods at compile time
  • Removed src/ImageSharp/Formats/ImageExtensions.Save.tt T4 template and its generated code
  • Modified ImageSharp.csproj to reference the new generator project as an analyzer
  • Added generator project to solution under 'src' folder with GUID {4743B273-473A-4D20-BE25-EC3D0B38E396}

Copy link

@greptile-apps greptile-apps bot left a 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}"
Copy link

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;
Copy link

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.
Copy link

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);
Copy link

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>
Copy link

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" />
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants