-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathExtensions.cs
38 lines (30 loc) · 1.54 KB
/
Extensions.cs
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
internal static class Extensions
{
public static IResourceBuilder<ExecutableResource>? AddTodoDbMigration(this IDistributedApplicationBuilder builder)
{
IResourceBuilder<ExecutableResource>? migrateOperation = default;
if (builder.ExecutionContext.IsRunMode)
{
var projectDirectory = Path.GetDirectoryName(new Projects.Todo_Api().ProjectPath)!;
var dbDirectory = Path.Combine(projectDirectory, ".db");
if (!Directory.Exists(dbDirectory))
{
Directory.CreateDirectory(dbDirectory);
migrateOperation = builder.AddEfMigration<Projects.Todo_Api>("todo-db-migration");
}
}
return migrateOperation;
}
public static IResourceBuilder<ExecutableResource> AddEfMigration<TProject>(this IDistributedApplicationBuilder builder, string name)
where TProject : IProjectMetadata, new()
{
var projectDirectory = Path.GetDirectoryName(new TProject().ProjectPath)!;
// Install the EF tool
var install = builder.AddExecutable("install-ef", "dotnet", projectDirectory, "tool", "install", "--global", "dotnet-ef");
// TODO: Support passing a connection string
return builder.AddExecutable(name, "dotnet", projectDirectory, "ef", "database", "update", "--no-build")
.WaitForCompletion(install);
}
public static string GetProjectDirectory(this IResourceBuilder<ProjectResource> project) =>
Path.GetDirectoryName(project.Resource.GetProjectMetadata().ProjectPath)!;
}