You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The per-format SaveAsXxx and GetXxxMetadata extensions come from two T4 templates (ImageExtensions.Save.tt and ImageMetadataExtensions.tt, with _Formats.ttinclude), with about 1,780 lines of output checked in. #2237 tried a source generator for the save template in 2022, and the concerns raised there were fair: a generator that only emits fixed text is T4 with extra steps, and a generator referenced as a project in the repo has poor tooling.
I'd like to suggest an approach that avoids both. Disclosure: I wrote the generator it uses, Zomp.MethodCloning.Variants.
The Bmp methods are written by hand as ordinary C# with their documentation. [CloneVariants("Bmp", "Ani", "Cur", "Gif", ...)] on the class writes every other format from them at build time, swapping the word in names, types and docs. The generator reads the code rather than emitting a template.
It comes from a NuGet package (PrivateAssets="all"), not a project in this repo, so nothing has to be built before IntelliSense works, and the shipped package gains no dependency.
The package is new, but the part that does the heavy lifting isn't: qualifying every name, rebuilding the containing types and usings, and naming the files come from Zomp.SyncMethodGenerator, which has shipped that code since 2022 (about 126k downloads). Only the word-swapping layer on top is new.
The public API is unchanged: the same 205 members as the template output, nullability included. The Release build is clean, and the per-format ImageExtensionsTest and metadata tests pass.
7 files, +106/-1,956. Adding a format means adding its name to the attribute.
What it doesn't do: it can't replace the pixel-format templates, which specialize one class per pixel type, and it isn't the attribute-on-the-encoder design suggested in #2237. It also compiles one internal type, Zomp.MethodCloning.CloneVariantsAttribute, into the assembly.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
The per-format
SaveAsXxxandGetXxxMetadataextensions come from two T4 templates (ImageExtensions.Save.ttandImageMetadataExtensions.tt, with_Formats.ttinclude), with about 1,780 lines of output checked in. #2237 tried a source generator for the save template in 2022, and the concerns raised there were fair: a generator that only emits fixed text is T4 with extra steps, and a generator referenced as a project in the repo has poor tooling.I'd like to suggest an approach that avoids both. Disclosure: I wrote the generator it uses, Zomp.MethodCloning.Variants.
[CloneVariants("Bmp", "Ani", "Cur", "Gif", ...)]on the class writes every other format from them at build time, swapping the word in names, types and docs. The generator reads the code rather than emitting a template.PrivateAssets="all"), not a project in this repo, so nothing has to be built before IntelliSense works, and the shipped package gains no dependency.ImageExtensionsTestand metadata tests pass.What it doesn't do: it can't replace the pixel-format templates, which specialize one class per pixel type, and it isn't the attribute-on-the-encoder design suggested in #2237. It also compiles one internal type,
Zomp.MethodCloning.CloneVariantsAttribute, into the assembly.The change: main...virzak:ImageSharp:zomp/method-cloning
Would you be open to a PR?
All reactions