-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
RecipeMigrator.cs
58 lines (51 loc) · 2.52 KB
/
RecipeMigrator.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OrchardCore.Data.Migration;
using OrchardCore.Environment.Extensions;
using OrchardCore.Modules;
namespace OrchardCore.Recipes.Services
{
public class RecipeMigrator : IRecipeMigrator
{
private readonly IRecipeReader _recipeReader;
private readonly IRecipeExecutor _recipeExecutor;
private readonly IHostEnvironment _hostingEnvironment;
private readonly ITypeFeatureProvider _typeFeatureProvider;
private readonly IEnumerable<IRecipeEnvironmentProvider> _environmentProviders;
private readonly ILogger _logger;
public RecipeMigrator(
IRecipeReader recipeReader,
IRecipeExecutor recipeExecutor,
IHostEnvironment hostingEnvironment,
ITypeFeatureProvider typeFeatureProvider,
IEnumerable<IRecipeEnvironmentProvider> environmentProviders,
ILogger<RecipeMigrator> logger)
{
_recipeReader = recipeReader;
_recipeExecutor = recipeExecutor;
_hostingEnvironment = hostingEnvironment;
_typeFeatureProvider = typeFeatureProvider;
_environmentProviders = environmentProviders;
_logger = logger;
}
public async Task<string> ExecuteAsync(string recipeFileName, IDataMigration migration)
{
var featureInfo = _typeFeatureProvider.GetFeatureForDependency(migration.GetType());
var recipeBasePath = Path.Combine(featureInfo.Extension.SubPath, "Migrations").Replace('\\', '/');
var recipeFilePath = Path.Combine(recipeBasePath, recipeFileName).Replace('\\', '/');
var recipeFileInfo = _hostingEnvironment.ContentRootFileProvider.GetFileInfo(recipeFilePath);
var recipeDescriptor = await _recipeReader.GetRecipeDescriptorAsync(recipeBasePath, recipeFileInfo, _hostingEnvironment.ContentRootFileProvider);
recipeDescriptor.RequireNewScope = false;
var environment = new Dictionary<string, object>();
await _environmentProviders.OrderBy(x => x.Order).InvokeAsync((provider, env) => provider.PopulateEnvironmentAsync(env), environment, _logger);
var executionId = Guid.NewGuid().ToString("n");
return await _recipeExecutor.ExecuteAsync(executionId, recipeDescriptor, environment, CancellationToken.None);
}
}
}