Skip to content

Commit fb084c7

Browse files
committed
Use calling assembly for determining build configuration
1 parent 6e92a52 commit fb084c7

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects/SqlProjectBuilderExtensions.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.Extensions.DependencyInjection.Extensions;
55
using CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects;
66
using Microsoft.SqlServer.Dac;
7+
using System.Reflection;
78

89
namespace Aspire.Hosting;
910

@@ -36,7 +37,7 @@ public static IResourceBuilder<SqlProjectResource> AddSqlProject<TProject>(this
3637
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
3738
ArgumentNullException.ThrowIfNull(name, nameof(name));
3839

39-
return builder.AddSqlProject(name)
40+
return builder.AddSqlProject(name, Assembly.GetCallingAssembly())
4041
.WithAnnotation(new TProject());
4142
}
4243

@@ -51,7 +52,22 @@ public static IResourceBuilder<SqlProjectResource> AddSqlProject(this IDistribut
5152
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
5253
ArgumentNullException.ThrowIfNull(name, nameof(name));
5354

54-
var resource = new SqlProjectResource(name);
55+
return builder.AddSqlProject(name, Assembly.GetCallingAssembly());
56+
}
57+
58+
/// <summary>
59+
/// Adds a SQL Server Database Project resource to the application.
60+
/// </summary>
61+
/// <param name="builder">An <see cref="IDistributedApplicationBuilder"/> instance to add the SQL Server Database project to.</param>
62+
/// <param name="name">Name of the resource.</param>
63+
/// <param name="appHostAssembly">The app host assembly to determine the build configuration</param>
64+
/// <returns>An <see cref="IResourceBuilder{T}"/> that can be used to further customize the resource.</returns>
65+
internal static IResourceBuilder<SqlProjectResource> AddSqlProject(this IDistributedApplicationBuilder builder, [ResourceName] string name, Assembly appHostAssembly)
66+
{
67+
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
68+
ArgumentNullException.ThrowIfNull(name, nameof(name));
69+
70+
var resource = new SqlProjectResource(name, appHostAssembly);
5571

5672
return builder.AddResource(resource)
5773
.WithInitialState(new CustomResourceSnapshot

src/CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects/SqlProjectResource.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.Build.Evaluation;
2+
using Microsoft.Extensions.Hosting;
23
using Microsoft.SqlServer.Dac;
34
using System.Reflection;
45

@@ -8,7 +9,8 @@ namespace Aspire.Hosting.ApplicationModel;
89
/// Represents a SQL Server Database project resource.
910
/// </summary>
1011
/// <param name="name">Name of the resource.</param>
11-
public sealed class SqlProjectResource(string name) : Resource(name), IResourceWithWaitSupport, IResourceWithDacpac
12+
/// <param name="appHostAssembly">The app host assembly to determine the build configuration</param>
13+
public sealed class SqlProjectResource(string name, Assembly appHostAssembly) : Resource(name), IResourceWithWaitSupport, IResourceWithDacpac
1214
{
1315
string IResourceWithDacpac.GetDacpacPath()
1416
{
@@ -17,7 +19,7 @@ string IResourceWithDacpac.GetDacpacPath()
1719
var projectPath = projectMetadata.ProjectPath;
1820
using var projectCollection = new ProjectCollection();
1921

20-
var attr = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyConfigurationAttribute>();
22+
var attr = appHostAssembly.GetCustomAttribute<AssemblyConfigurationAttribute>();
2123
if (attr is not null)
2224
projectCollection.SetGlobalProperty("Configuration", attr.Configuration);
2325

0 commit comments

Comments
 (0)