From 1277d21a0d89a861989acec92e6ebbe666db73c6 Mon Sep 17 00:00:00 2001 From: Andre Rodrigues Date: Wed, 6 Apr 2011 15:56:40 -0700 Subject: [PATCH] #17585: Adding recipe commands. --HG-- branch : 1.x --- .../Commands/RecipesCommands.cs | 77 +++++++++++++++++++ .../Orchard.Recipes/Orchard.Recipes.csproj | 1 + 2 files changed, 78 insertions(+) create mode 100644 src/Orchard.Web/Modules/Orchard.Recipes/Commands/RecipesCommands.cs diff --git a/src/Orchard.Web/Modules/Orchard.Recipes/Commands/RecipesCommands.cs b/src/Orchard.Web/Modules/Orchard.Recipes/Commands/RecipesCommands.cs new file mode 100644 index 000000000..343c9962f --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Recipes/Commands/RecipesCommands.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Orchard.Commands; +using Orchard.Environment.Extensions; +using Orchard.Environment.Extensions.Models; +using Orchard.Recipes.Models; +using Orchard.Recipes.Services; + +namespace Orchard.Recipes.Commands { + public class RecipesCommands : DefaultOrchardCommandHandler { + private readonly IRecipeHarvester _recipeHarvester; + private readonly IRecipeManager _recipeManager; + private readonly IExtensionManager _extensionManager; + + public RecipesCommands(IRecipeHarvester recipeHarvester, IRecipeManager recipeManager, IExtensionManager extensionManager) { + _recipeHarvester = recipeHarvester; + _recipeManager = recipeManager; + _extensionManager = extensionManager; + } + + [CommandHelp("recipes harvest \r\n\t" + "Display list of available recipes for an extension")] + [CommandName("recipes harvest")] + public void HarvestRecipes(string extensionId) { + ExtensionDescriptor extensionDescriptor = _extensionManager.GetExtension(extensionId); + if (extensionDescriptor == null) { + throw new OrchardException(T("Could not discover recipes because module '{0}' was not found.", extensionId)); + } + + IEnumerable recipes = _recipeHarvester.HarvestRecipes(extensionId); + if (recipes == null) { + throw new OrchardException(T("No recipes found for extension {0}.", extensionId)); + } + + Context.Output.WriteLine(T("List of available recipes")); + Context.Output.WriteLine(T("--------------------------")); + Context.Output.WriteLine(); + + foreach (Recipe recipe in recipes) { + Context.Output.WriteLine(T("Recipe: {0}", recipe.Name)); + Context.Output.WriteLine(T(" Version: {0}", recipe.Version)); + Context.Output.WriteLine(T(" Tags: {0}", recipe.Tags)); + Context.Output.WriteLine(T(" Description: {0}", recipe.Description)); + Context.Output.WriteLine(T(" Author: {0}", recipe.Author)); + Context.Output.WriteLine(T(" Website: {0}", recipe.WebSite)); + } + } + + [CommandHelp("recipes execute \r\n\t" + "Executes a recipe from a module")] + [CommandName("recipes execute")] + public void ExecuteRecipe(string extensionId, string recipeName) { + ExtensionDescriptor extensionDescriptor = _extensionManager.GetExtension(extensionId); + if (extensionDescriptor == null) { + throw new OrchardException(T("Could not discover recipes because module '{0}' was not found.", extensionId)); + } + + IEnumerable recipes = _recipeHarvester.HarvestRecipes(extensionId); + if (recipes == null) { + throw new OrchardException(T("No recipes found for extension {0}.", extensionId)); + } + + Recipe recipe = recipes.FirstOrDefault(r => r.Name.Equals(recipeName, StringComparison.OrdinalIgnoreCase)); + if (recipe == null) { + throw new OrchardException(T("Invalid recipe name {0}.", recipeName)); + } + + try { + _recipeManager.Execute(recipe); + + Context.Output.WriteLine(T("Recipe scheduled for execution successfully.").Text); + } + catch { + Context.Output.WriteLine(T("Recipe failed to execute.").Text); + } + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Recipes/Orchard.Recipes.csproj b/src/Orchard.Web/Modules/Orchard.Recipes/Orchard.Recipes.csproj index d93306612..21af2b12a 100644 --- a/src/Orchard.Web/Modules/Orchard.Recipes/Orchard.Recipes.csproj +++ b/src/Orchard.Web/Modules/Orchard.Recipes/Orchard.Recipes.csproj @@ -51,6 +51,7 @@ +