forked from CommunityToolkit/Aspire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlProjectBuilderExtensions.cs
More file actions
271 lines (240 loc) · 15 KB
/
SqlProjectBuilderExtensions.cs
File metadata and controls
271 lines (240 loc) · 15 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using Aspire.Hosting.ApplicationModel;
using Microsoft.Build.Locator;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects;
using Microsoft.SqlServer.Dac;
namespace Aspire.Hosting;
/// <summary>
/// Provides extension methods for adding SQL Server Database Projects to the application.
/// </summary>
public static class SqlProjectBuilderExtensions
{
/// <summary>
/// Static constructor to ensure that MSBuild assemblies are properly loaded.
/// </summary>
static SqlProjectBuilderExtensions()
{
if (!MSBuildLocator.IsRegistered)
{
MSBuildLocator.RegisterDefaults();
}
}
/// <summary>
/// Adds a SQL Server Database Project resource to the application based on a referenced MSBuild.Sdk.SqlProj project.
/// </summary>
/// <typeparam name="TProject">Type that represents the project that produces the .dacpac file.</typeparam>
/// <param name="builder">An <see cref="IDistributedApplicationBuilder"/> instance to add the SQL Server Database project to.</param>
/// <param name="name">Name of the resource.</param>
/// <returns>An <see cref="IResourceBuilder{T}"/> that can be used to further customize the resource.</returns>
public static IResourceBuilder<SqlProjectResource> AddSqlProject<TProject>(this IDistributedApplicationBuilder builder, [ResourceName] string name)
where TProject : IProjectMetadata, new()
{
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
ArgumentNullException.ThrowIfNull(name, nameof(name));
return builder.AddSqlProject(name)
.WithAnnotation(new TProject());
}
/// <summary>
/// Adds a SQL Server Database Project resource to the application.
/// </summary>
/// <param name="builder">An <see cref="IDistributedApplicationBuilder"/> instance to add the SQL Server Database project to.</param>
/// <param name="name">Name of the resource.</param>
/// <returns>An <see cref="IResourceBuilder{T}"/> that can be used to further customize the resource.</returns>
public static IResourceBuilder<SqlProjectResource> AddSqlProject(this IDistributedApplicationBuilder builder, [ResourceName] string name)
{
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
ArgumentNullException.ThrowIfNull(name, nameof(name));
var resource = new SqlProjectResource(name);
return builder.AddResource(resource)
.WithInitialState(new CustomResourceSnapshot
{
Properties = [],
ResourceType = "SqlProject",
State = new ResourceStateSnapshot("Pending", KnownResourceStateStyles.Info)
})
.ExcludeFromManifest();
}
/// <summary>
/// Adds a SQL Server Database Project resource to the application based on a referenced NuGet package.
/// </summary>
/// <typeparam name="TPackage">Type that represents the NuGet package that contains the .dacpac file.</typeparam>
/// <param name="builder">An <see cref="IDistributedApplicationBuilder"/> instance to add the SQL Server Database project to.</param>
/// <param name="name">Name of the resource.</param>
/// <returns>Am <see cref="IResourceBuilder{T}"/> that can be used to further customize the resource.</returns>
public static IResourceBuilder<SqlPackageResource<TPackage>> AddSqlPackage<TPackage>(this IDistributedApplicationBuilder builder, [ResourceName] string name)
where TPackage : IPackageMetadata, new()
{
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
ArgumentNullException.ThrowIfNull(name, nameof(name));
var resource = new SqlPackageResource<TPackage>(name);
return builder.AddResource(resource)
.WithAnnotation(new TPackage())
.WithInitialState(new CustomResourceSnapshot
{
Properties = [],
ResourceType = "SqlPackage",
State = new ResourceStateSnapshot("Pending", KnownResourceStateStyles.Info)
})
.ExcludeFromManifest();
}
/// <summary>
/// Specifies the path to the .dacpac file.
/// </summary>
/// <param name="builder">An <see cref="IResourceBuilder{T}"/> representing the SQL Server Database project.</param>
/// <param name="dacpacPath">Path to the .dacpac file.</param>
/// <returns>An <see cref="IResourceBuilder{T}"/> that can be used to further customize the resource.</returns>
public static IResourceBuilder<SqlProjectResource> WithDacpac(this IResourceBuilder<SqlProjectResource> builder, string dacpacPath)
=> InternalWithDacpac(builder, dacpacPath);
/// <summary>
/// Specifies the path to the .dacpac file.
/// </summary>
/// <param name="builder">An <see cref="IResourceBuilder{T}"/> representing the SQL Server Database project.</param>
/// <param name="dacpacPath">Path to the .dacpac file.</param>
/// <returns>An <see cref="IResourceBuilder{T}"/> that can be used to further customize the resource.</returns>
public static IResourceBuilder<SqlPackageResource<TPackage>> WithDacpac<TPackage>(this IResourceBuilder<SqlPackageResource<TPackage>> builder, string dacpacPath)
where TPackage : IPackageMetadata => InternalWithDacpac(builder, dacpacPath);
internal static IResourceBuilder<TResource> InternalWithDacpac<TResource>(this IResourceBuilder<TResource> builder, string dacpacPath)
where TResource : IResourceWithDacpac
{
return builder.WithAnnotation(new DacpacMetadataAnnotation(dacpacPath));
}
/// <summary>
/// Adds a delegate annotation for configuring dacpac deployment options to the <see cref="SqlProjectResource"/>.
/// </summary>
/// <param name="builder">An <see cref="IResourceBuilder{T}"/> representing the SQL Server Database project.</param>
/// <param name="configureDeploymentOptions">The delegate for configuring dacpac deployment options</param>
/// <returns>An <see cref="IResourceBuilder{T}"/> that can be used to further customize the resource.</returns>
public static IResourceBuilder<SqlProjectResource> WithConfigureDacDeployOptions(this IResourceBuilder<SqlProjectResource> builder, Action<DacDeployOptions> configureDeploymentOptions)
=> InternalWithConfigureDacDeployOptions(builder, configureDeploymentOptions);
/// <summary>
/// Adds a delegate annotation for configuring dacpac deployment options to the <see cref="SqlProjectResource"/>.
/// </summary>
/// <param name="builder">An <see cref="IResourceBuilder{T}"/> representing the SQL Server Database project.</param>
/// <param name="configureDeploymentOptions">The delegate for configuring dacpac deployment options</param>
/// <returns>An <see cref="IResourceBuilder{T}"/> that can be used to further customize the resource.</returns>
public static IResourceBuilder<SqlPackageResource<TPackage>> WithConfigureDacDeployOptions<TPackage>(this IResourceBuilder<SqlPackageResource<TPackage>> builder, Action<DacDeployOptions> configureDeploymentOptions)
where TPackage : IPackageMetadata => InternalWithConfigureDacDeployOptions(builder, configureDeploymentOptions);
internal static IResourceBuilder<TResource> InternalWithConfigureDacDeployOptions<TResource>(this IResourceBuilder<TResource> builder, Action<DacDeployOptions> configureDeploymentOptions)
where TResource : IResourceWithDacpac
{
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
ArgumentNullException.ThrowIfNull(configureDeploymentOptions);
return builder
.WithAnnotation(new ConfigureDacDeployOptionsAnnotation(configureDeploymentOptions));
}
/// <summary>
/// Publishes the SQL Server Database project to the target <see cref="SqlServerDatabaseResource"/>.
/// </summary>
/// <param name="builder">An <see cref="IResourceBuilder{T}"/> representing the SQL Server Database project to publish.</param>
/// <param name="target">An <see cref="IResourceBuilder{T}"/> representing the target <see cref="SqlServerDatabaseResource"/> to publish the SQL Server Database project to.</param>
/// <returns>An <see cref="IResourceBuilder{T}"/> that can be used to further customize the resource.</returns>
public static IResourceBuilder<SqlProjectResource> WithReference(
this IResourceBuilder<SqlProjectResource> builder, IResourceBuilder<SqlServerDatabaseResource> target) => InternalWithReference(builder, target, target.Resource.DatabaseName);
/// <summary>
/// Publishes the SQL Server Database project to the target <see cref="IResourceWithConnectionString"/>.
/// </summary>
/// <param name="builder">An <see cref="IResourceBuilder{T}"/> representing the SQL Server Database project to publish.</param>
/// <param name="target">An <see cref="IResourceBuilder{T}"/> representing the target <see cref="IResourceWithConnectionString"/> to publish the SQL Server Database project to.</param>
/// <returns>An <see cref="IResourceBuilder{T}"/> that can be used to further customize the resource.</returns>
public static IResourceBuilder<SqlProjectResource> WithReference(
this IResourceBuilder<SqlProjectResource> builder, IResourceBuilder<IResourceWithConnectionString> target) => InternalWithReference(builder, target);
/// <summary>
/// Publishes the SQL Server Database project to the target <see cref="SqlServerDatabaseResource"/>.
/// </summary>
/// <param name="builder">An <see cref="IResourceBuilder{T}"/> representing the SQL Server Database project to publish.</param>
/// <param name="target">An <see cref="IResourceBuilder{T}"/> representing the target <see cref="SqlServerDatabaseResource"/> to publish the SQL Server Database project to.</param>
/// <returns>An <see cref="IResourceBuilder{T}"/> that can be used to further customize the resource.</returns>
public static IResourceBuilder<SqlPackageResource<TPackage>> WithReference<TPackage>(
this IResourceBuilder<SqlPackageResource<TPackage>> builder, IResourceBuilder<SqlServerDatabaseResource> target)
where TPackage : IPackageMetadata
{
return InternalWithReference(builder, target, target.Resource.DatabaseName);
}
/// <summary>
/// Publishes the SQL Server Database project to the target <see cref="IResourceWithConnectionString"/>.
/// </summary>
/// <param name="builder">An <see cref="IResourceBuilder{T}"/> representing the SQL Server Database project to publish.</param>
/// <param name="target">An <see cref="IResourceBuilder{T}"/> representing the target <see cref="IResourceWithConnectionString"/> to publish the SQL Server Database project to.</param>
/// <returns>An <see cref="IResourceBuilder{T}"/> that can be used to further customize the resource.</returns>
public static IResourceBuilder<SqlPackageResource<TPackage>> WithReference<TPackage>(
this IResourceBuilder<SqlPackageResource<TPackage>> builder, IResourceBuilder<IResourceWithConnectionString> target)
where TPackage : IPackageMetadata
{
return InternalWithReference(builder, target);
}
internal static IResourceBuilder<TResource> InternalWithReference<TResource>(this IResourceBuilder<TResource> builder, IResourceBuilder<IResourceWithConnectionString> target, string? targetDatabaseName = null)
where TResource : IResourceWithDacpac
{
builder.ApplicationBuilder.Services.TryAddSingleton<IDacpacDeployer, DacpacDeployer>();
builder.ApplicationBuilder.Services.TryAddSingleton<SqlProjectPublishService>();
builder.WithParentRelationship(target.Resource);
if (target.Resource is SqlServerDatabaseResource)
{
builder.ApplicationBuilder.Eventing.Subscribe<ResourceReadyEvent>(target.Resource, async (resourceReady, ct) =>
{
if (builder.Resource.HasAnnotationOfType<ExplicitStartupAnnotation>())
{
await MarkNotStarted(builder, resourceReady.Services);
}
else
{
await RunPublish(builder, target, targetDatabaseName, resourceReady.Services, ct);
}
});
}
else
{
builder.ApplicationBuilder.Eventing.Subscribe<AfterResourcesCreatedEvent>(async (@event, ct) =>
{
if (builder.Resource.HasAnnotationOfType<ExplicitStartupAnnotation>())
{
await MarkNotStarted(builder, @event.Services);
}
else
{
await RunPublish(builder, target, targetDatabaseName, @event.Services, ct);
}
});
}
builder.WaitFor(target);
var commandOptions = new CommandOptions
{
IconName = "ArrowReset",
IconVariant = IconVariant.Filled,
IsHighlighted = true,
Description = "Deploy the SQL Server Database Project to the target database.",
UpdateState = (context) =>
{
if (context.ResourceSnapshot?.State?.Text is string stateText && (stateText == KnownResourceStates.Finished || stateText == KnownResourceStates.NotStarted))
{
return ResourceCommandState.Enabled;
}
else
{
return ResourceCommandState.Disabled;
}
},
};
builder.WithCommand("deploy", "Deploy", async (context) =>
{
var service = context.ServiceProvider.GetRequiredService<SqlProjectPublishService>();
await service.PublishSqlProject(builder.Resource, target.Resource, targetDatabaseName, context.CancellationToken);
return new ExecuteCommandResult { Success = true };
}, commandOptions);
return builder;
}
private static async Task MarkNotStarted<TResource>(IResourceBuilder<TResource> builder, IServiceProvider serviceProvider)
where TResource : IResourceWithDacpac
{
var resourceNotificationService = serviceProvider.GetRequiredService<ResourceNotificationService>();
await resourceNotificationService.PublishUpdateAsync(builder.Resource,
state => state with { State = new ResourceStateSnapshot(KnownResourceStates.NotStarted, KnownResourceStateStyles.Info) });
}
private static async Task RunPublish<TResource>(IResourceBuilder<TResource> builder, IResourceBuilder<IResourceWithConnectionString> target, string? targetDatabaseName, IServiceProvider serviceProvider, CancellationToken ct)
where TResource : IResourceWithDacpac
{
var service = serviceProvider.GetRequiredService<SqlProjectPublishService>();
await service.PublishSqlProject(builder.Resource, target.Resource, targetDatabaseName, ct);
}
}