-
-
Notifications
You must be signed in to change notification settings - Fork 893
Expand file tree
/
Copy pathImageExtensionsSaveGenerator.cs
More file actions
165 lines (145 loc) · 7.94 KB
/
ImageExtensionsSaveGenerator.cs
File metadata and controls
165 lines (145 loc) · 7.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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;
[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);
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.
/// </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);
}
}