-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Six Labors Split License. | ||
|
||
using System.Text; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
namespace ImageSharp.Generators; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: namespace should be SixLabors.ImageSharp.Generators to match assembly name |
||
|
||
[Generator(LanguageNames.CSharp)] | ||
internal class ImageExtensionsSaveGenerator : IIncrementalGenerator | ||
{ | ||
private const string FileHeader = @"// Copyright (c) Six Labors. | ||
// Licensed under the Six Labors Split License. | ||
|
||
// <auto-generated />"; | ||
|
||
private static readonly string[] ImageFormats = | ||
{ | ||
"Bmp", | ||
"Gif", | ||
"Jpeg", | ||
"Pbm", | ||
"Png", | ||
"Tga", | ||
"Webp", | ||
"Tiff" | ||
}; | ||
|
||
public void Initialize(IncrementalGeneratorInitializationContext context) => context.RegisterPostInitializationOutput(GenerateExtensionsMethods); | ||
|
||
private static void GenerateExtensionsMethods(IncrementalGeneratorPostInitializationContext ctx) | ||
{ | ||
StringBuilder stringBuilder = new(FileHeader); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: consider using StringBuilder capacity constructor to avoid reallocations |
||
stringBuilder.AppendLine(); | ||
stringBuilder.AppendLine("using SixLabors.ImageSharp.Advanced;"); | ||
foreach (string format in ImageFormats) | ||
{ | ||
stringBuilder.Append("using SixLabors.ImageSharp.Formats.").Append(format).AppendLine(";"); | ||
} | ||
|
||
stringBuilder.AppendLine(); | ||
stringBuilder.AppendLine("namespace SixLabors.ImageSharp;"); | ||
stringBuilder.AppendLine(); | ||
stringBuilder.AppendLine(@"/// <summary> | ||
/// Extension methods for the <see cref=""Image""/> type. | ||
/// </summary> | ||
public static partial class ImageExtensions | ||
{"); | ||
|
||
ctx.CancellationToken.ThrowIfCancellationRequested(); | ||
|
||
foreach (string format in ImageFormats) | ||
{ | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. logic: incorrect documentation - method saves to path, not stream |
||
/// </summary> | ||
/// <param name=""source"">The image this method extends.</param> | ||
/// <param name=""path"">The file path to save the image to.</param> | ||
/// <exception cref=""System.ArgumentNullException"">Thrown if the path is null.</exception> | ||
public static void SaveAs{format}(this Image source, string path) => SaveAs{format}(source, path, null); | ||
|
||
/// <summary> | ||
/// Saves the image to the given stream with the {format} format. | ||
/// </summary> | ||
/// <param name=""source"">The image this method extends.</param> | ||
/// <param name=""path"">The file path to save the image to.</param> | ||
/// <exception cref=""System.ArgumentNullException"">Thrown if the path is null.</exception> | ||
/// <returns>A <see cref=""Task""/> representing the asynchronous operation.</returns> | ||
public static Task SaveAs{format}Async(this Image source, string path) => SaveAs{format}Async(source, path, null); | ||
|
||
/// <summary> | ||
/// Saves the image to the given stream with the {format} format. | ||
/// </summary> | ||
/// <param name=""source"">The image this method extends.</param> | ||
/// <param name=""path"">The file path to save the image to.</param> | ||
/// <param name=""cancellationToken"">The token to monitor for cancellation requests.</param> | ||
/// <exception cref=""System.ArgumentNullException"">Thrown if the path is null.</exception> | ||
/// <returns>A <see cref=""Task""/> representing the asynchronous operation.</returns> | ||
public static Task SaveAs{format}Async(this Image source, string path, CancellationToken cancellationToken) | ||
=> SaveAs{format}Async(source, path, null, cancellationToken); | ||
|
||
/// <summary> | ||
/// Saves the image to the given stream with the {format} format. | ||
/// </summary> | ||
/// <param name=""source"">The image this method extends.</param> | ||
/// <param name=""path"">The file path to save the image to.</param> | ||
/// <param name=""encoder"">The encoder to save the image with.</param> | ||
/// <exception cref=""System.ArgumentNullException"">Thrown if the path is null.</exception> | ||
public static void SaveAs{format}(this Image source, string path, {format}Encoder encoder) => | ||
source.Save( | ||
path, | ||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder({format}Format.Instance)); | ||
|
||
/// <summary> | ||
/// Saves the image to the given stream with the {format} format. | ||
/// </summary> | ||
/// <param name=""source"">The image this method extends.</param> | ||
/// <param name=""path"">The file path to save the image to.</param> | ||
/// <param name=""encoder"">The encoder to save the image with.</param> | ||
/// <param name=""cancellationToken"">The token to monitor for cancellation requests.</param> | ||
/// <exception cref=""System.ArgumentNullException"">Thrown if the path is null.</exception> | ||
/// <returns>A <see cref=""Task""/> representing the asynchronous operation.</returns> | ||
public static Task SaveAs{format}Async(this Image source, string path, {format}Encoder encoder, CancellationToken cancellationToken = default) => | ||
source.SaveAsync( | ||
path, | ||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder({format}Format.Instance), | ||
cancellationToken); | ||
|
||
/// <summary> | ||
/// Saves the image to the given stream with the {format} format. | ||
/// </summary> | ||
/// <param name=""source"">The image this method extends.</param> | ||
/// <param name=""stream"">The stream to save the image to.</param> | ||
/// <exception cref=""System.ArgumentNullException"">Thrown if the stream is null.</exception> | ||
public static void SaveAs{format}(this Image source, Stream stream) | ||
=> SaveAs{format}(source, stream, null); | ||
|
||
/// <summary> | ||
/// Saves the image to the given stream with the {format} format. | ||
/// </summary> | ||
/// <param name=""source"">The image this method extends.</param> | ||
/// <param name=""stream"">The stream to save the image to.</param> | ||
/// <param name=""cancellationToken"">The token to monitor for cancellation requests.</param> | ||
/// <exception cref=""System.ArgumentNullException"">Thrown if the stream is null.</exception> | ||
/// <returns>A <see cref=""Task""/> representing the asynchronous operation.</returns> | ||
public static Task SaveAs{format}Async(this Image source, Stream stream, CancellationToken cancellationToken = default) | ||
=> SaveAs{format}Async(source, stream, null, cancellationToken); | ||
|
||
/// <summary> | ||
/// Saves the image to the given stream with the {format} format. | ||
/// </summary> | ||
/// <param name=""source"">The image this method extends.</param> | ||
/// <param name=""stream"">The stream to save the image to.</param> | ||
/// <param name=""encoder"">The encoder to save the image with.</param> | ||
/// <exception cref=""System.ArgumentNullException"">Thrown if the stream is null.</exception> | ||
public static void SaveAs{format}(this Image source, Stream stream, {format}Encoder encoder) | ||
=> source.Save( | ||
stream, | ||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder({format}Format.Instance)); | ||
|
||
/// <summary> | ||
/// Saves the image to the given stream with the {format} format. | ||
/// </summary> | ||
/// <param name=""source"">The image this method extends.</param> | ||
/// <param name=""stream"">The stream to save the image to.</param> | ||
/// <param name=""encoder"">The encoder to save the image with.</param> | ||
/// <param name=""cancellationToken"">The token to monitor for cancellation requests.</param> | ||
/// <exception cref=""System.ArgumentNullException"">Thrown if the stream is null.</exception> | ||
/// <returns>A <see cref=""Task""/> representing the asynchronous operation.</returns> | ||
public static Task SaveAs{format}Async(this Image source, Stream stream, {format}Encoder encoder, CancellationToken cancellationToken = default) => | ||
source.SaveAsync( | ||
stream, | ||
encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder({format}Format.Instance), | ||
cancellationToken);"; | ||
stringBuilder.AppendLine(methods); | ||
} | ||
|
||
stringBuilder.Append('}'); | ||
|
||
SourceText sourceText = SourceText.From(stringBuilder.ToString(), Encoding.UTF8); | ||
ctx.AddSource("ImageExtensions.Save.g.cs", sourceText); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<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 commentThe 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 |
||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<!-- Keep up to date with .NET version of ImageSharp --> | ||
<LangVersion>10</LangVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Remove="SixLabors" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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.