-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
RecipeHarvester.cs
61 lines (53 loc) · 2.36 KB
/
RecipeHarvester.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
59
60
61
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OrchardCore.Environment.Extensions;
using OrchardCore.Modules;
using OrchardCore.Recipes.Models;
namespace OrchardCore.Recipes.Services
{
public class RecipeHarvester : IRecipeHarvester
{
private readonly IRecipeReader _recipeReader;
private readonly IExtensionManager _extensionManager;
private readonly IHostEnvironment _hostingEnvironment;
private readonly ILogger _logger;
public RecipeHarvester(
IRecipeReader recipeReader,
IExtensionManager extensionManager,
IHostEnvironment hostingEnvironment,
ILogger<RecipeHarvester> logger)
{
_recipeReader = recipeReader;
_extensionManager = extensionManager;
_hostingEnvironment = hostingEnvironment;
_logger = logger;
}
public virtual Task<IEnumerable<RecipeDescriptor>> HarvestRecipesAsync()
{
return _extensionManager.GetExtensions().InvokeAsync(HarvestRecipes, _logger);
}
private Task<IEnumerable<RecipeDescriptor>> HarvestRecipes(IExtensionInfo extension)
{
var folderSubPath = PathExtensions.Combine(extension.SubPath, "Recipes");
return HarvestRecipesAsync(folderSubPath);
}
/// <summary>
/// Returns a list of recipes for a content path.
/// </summary>
/// <param name="path">A path string relative to the content root of the application.</param>
/// <returns>The list of <see cref="RecipeDescriptor"/> instances.</returns>
protected Task<IEnumerable<RecipeDescriptor>> HarvestRecipesAsync(string path)
{
var recipeDescriptors = new List<RecipeDescriptor>();
var recipeFiles = _hostingEnvironment.ContentRootFileProvider.GetDirectoryContents(path)
.Where(x => !x.IsDirectory && x.Name.EndsWith(".recipe.json", StringComparison.Ordinal));
recipeDescriptors.AddRange(recipeFiles.Select(recipeFile => _recipeReader.GetRecipeDescriptor(path, recipeFile, _hostingEnvironment.ContentRootFileProvider).Result));
return Task.FromResult<IEnumerable<RecipeDescriptor>>(recipeDescriptors);
}
}
}