From 45a8522469ff34470a1d9e09818750fc6577420d Mon Sep 17 00:00:00 2001 From: Suha Can Date: Mon, 21 Feb 2011 18:06:56 -0800 Subject: [PATCH] Cleanup and refactor module/theme recipe step handler code. --HG-- branch : recipe --- .../Sample1/Recipes/cms.recipe.xml | 2 - .../Recipes/Services/RecipeManagerTests.cs | 2 +- .../RecipeHandlers/ModuleRecipeHandler.cs | 81 ++++++++--------- .../RecipeHandlers/ThemeRecipeHandler.cs | 86 +++++++++---------- 4 files changed, 76 insertions(+), 95 deletions(-) diff --git a/src/Orchard.Tests.Modules/Recipes/Services/FoldersData/Sample1/Recipes/cms.recipe.xml b/src/Orchard.Tests.Modules/Recipes/Services/FoldersData/Sample1/Recipes/cms.recipe.xml index b1047c222..711fbd549 100644 --- a/src/Orchard.Tests.Modules/Recipes/Services/FoldersData/Sample1/Recipes/cms.recipe.xml +++ b/src/Orchard.Tests.Modules/Recipes/Services/FoldersData/Sample1/Recipes/cms.recipe.xml @@ -10,7 +10,6 @@ - @@ -38,7 +37,6 @@ - diff --git a/src/Orchard.Tests.Modules/Recipes/Services/RecipeManagerTests.cs b/src/Orchard.Tests.Modules/Recipes/Services/RecipeManagerTests.cs index d06a95ed3..0b4e5a01e 100644 --- a/src/Orchard.Tests.Modules/Recipes/Services/RecipeManagerTests.cs +++ b/src/Orchard.Tests.Modules/Recipes/Services/RecipeManagerTests.cs @@ -125,7 +125,7 @@ namespace Orchard.Tests.Modules.Recipes.Services { var sampleRecipe = recipes[0]; var recipeSteps = (List) sampleRecipe.RecipeSteps; - Assert.That(recipeSteps.Count, Is.EqualTo(11)); + Assert.That(recipeSteps.Count, Is.EqualTo(9)); } [Test] diff --git a/src/Orchard.Web/Modules/Orchard.Recipes/RecipeHandlers/ModuleRecipeHandler.cs b/src/Orchard.Web/Modules/Orchard.Recipes/RecipeHandlers/ModuleRecipeHandler.cs index b0b7d10d3..2de85a889 100644 --- a/src/Orchard.Web/Modules/Orchard.Recipes/RecipeHandlers/ModuleRecipeHandler.cs +++ b/src/Orchard.Web/Modules/Orchard.Recipes/RecipeHandlers/ModuleRecipeHandler.cs @@ -38,22 +38,18 @@ namespace Orchard.Recipes.RecipeHandlers { public Localizer T { get; set; } ILogger Logger { get; set; } - // - // - // install modules from url or feed. + // + // install modules from feed. public void ExecuteRecipeStep(RecipeContext recipeContext) { if (!String.Equals(recipeContext.RecipeStep.Name, "Module", StringComparison.OrdinalIgnoreCase)) { return; } bool replace; - string source = null, name = null, version = null, repository = null; + string name = null, version = null, repository = null; foreach (var attribute in recipeContext.RecipeStep.Step.Attributes()) { - if (String.Equals(attribute.Name.LocalName, "src", StringComparison.OrdinalIgnoreCase)) { - source = attribute.Value; - } - else if (String.Equals(attribute.Name.LocalName, "replace", StringComparison.OrdinalIgnoreCase)) { + if (String.Equals(attribute.Name.LocalName, "replace", StringComparison.OrdinalIgnoreCase)) { replace = Boolean.Parse(attribute.Value); } else if (String.Equals(attribute.Name.LocalName, "name", StringComparison.OrdinalIgnoreCase)) { @@ -70,48 +66,43 @@ namespace Orchard.Recipes.RecipeHandlers { } } - if (source != null) { + if (name == null) { + throw new InvalidOperationException("Name is required in a Module declaration in a recipe file."); } - else { - if (name == null) { - throw new InvalidOperationException("Either name or source is required in a Module declaration in a recipe file."); - } - // download and install module from the orchard feed or a custom feed if repository is specified. - bool enforceVersion = version != null; - bool installed = false; - PackagingSource packagingSource = _packagingSourceManager.GetSources().FirstOrDefault(); - if (repository != null) { - enforceVersion = false; - packagingSource = new PackagingSource {FeedTitle = repository, FeedUrl = repository}; - } - foreach (var packagingEntry in _packagingSourceManager.GetExtensionList(packagingSource)) { - if (String.Equals(packagingEntry.Title, name, StringComparison.OrdinalIgnoreCase)) { - if (enforceVersion && !String.Equals(packagingEntry.Version, version, StringComparison.OrdinalIgnoreCase)) { - continue; - } - // use for replace. - bool moduleExists = false; - foreach (var extension in _extensionManager.AvailableExtensions() - .Where(extension => - DefaultExtensionTypes.IsModule(extension.ExtensionType) && - String.Equals(packagingEntry.Title, extension.Name, StringComparison.OrdinalIgnoreCase))) { - moduleExists = true; - } - _packageManager.Install(packagingEntry.PackageId, packagingEntry.Version, packagingSource.FeedUrl, HostingEnvironment.MapPath("~/")); - _moduleService.EnableFeatures(new[] { packagingEntry.Title }, true); - _dataMigrationManager.Update(packagingEntry.Title); - - installed = true; - break; + // download and install module from the orchard feed or a custom feed if repository is specified. + bool enforceVersion = version != null; + bool installed = false; + PackagingSource packagingSource = _packagingSourceManager.GetSources().FirstOrDefault(); + if (repository != null) { + enforceVersion = false; + packagingSource = new PackagingSource {FeedTitle = repository, FeedUrl = repository}; + } + foreach (var packagingEntry in _packagingSourceManager.GetExtensionList(packagingSource)) { + if (String.Equals(packagingEntry.Title, name, StringComparison.OrdinalIgnoreCase)) { + if (enforceVersion && !String.Equals(packagingEntry.Version, version, StringComparison.OrdinalIgnoreCase)) { + continue; } + // use for replace. + bool moduleExists = false; + foreach (var extension in _extensionManager.AvailableExtensions() + .Where(extension => + DefaultExtensionTypes.IsModule(extension.ExtensionType) && + String.Equals(packagingEntry.Title, extension.Name, StringComparison.OrdinalIgnoreCase))) { + moduleExists = true; + } + _packageManager.Install(packagingEntry.PackageId, packagingEntry.Version, packagingSource.FeedUrl, HostingEnvironment.MapPath("~/")); + _moduleService.EnableFeatures(new[] { packagingEntry.Title }, true); + _dataMigrationManager.Update(packagingEntry.Title); + installed = true; + break; } - - if (!installed) { - throw new InvalidOperationException(string.Format("Module {0} was not found in the specified location.", name)); - } - } + if (!installed) { + throw new InvalidOperationException(string.Format("Module {0} was not found in the specified location.", name)); + } + + recipeContext.Executed = true; } } diff --git a/src/Orchard.Web/Modules/Orchard.Recipes/RecipeHandlers/ThemeRecipeHandler.cs b/src/Orchard.Web/Modules/Orchard.Recipes/RecipeHandlers/ThemeRecipeHandler.cs index eda69557d..e6fa2a462 100644 --- a/src/Orchard.Web/Modules/Orchard.Recipes/RecipeHandlers/ThemeRecipeHandler.cs +++ b/src/Orchard.Web/Modules/Orchard.Recipes/RecipeHandlers/ThemeRecipeHandler.cs @@ -37,22 +37,18 @@ namespace Orchard.Recipes.RecipeHandlers { public Localizer T { get; set; } ILogger Logger { get; set; } - // - // install themes from url or feed. + // + // install themes from feed. public void ExecuteRecipeStep(RecipeContext recipeContext) { if (!String.Equals(recipeContext.RecipeStep.Name, "Theme", StringComparison.OrdinalIgnoreCase)) { return; } bool replace, enable = false, current = false; - string source = null, name = null, version = null, repository = null; + string name = null, version = null, repository = null; foreach (var attribute in recipeContext.RecipeStep.Step.Attributes()) { - if (String.Equals(attribute.Name.LocalName, "src", StringComparison.OrdinalIgnoreCase)) { - source = attribute.Value; - } - else if (String.Equals(attribute.Name.LocalName, "replace", StringComparison.OrdinalIgnoreCase)) { + if (String.Equals(attribute.Name.LocalName, "replace", StringComparison.OrdinalIgnoreCase)) { replace = Boolean.Parse(attribute.Value); } else if (String.Equals(attribute.Name.LocalName, "enable", StringComparison.OrdinalIgnoreCase)) { @@ -75,50 +71,46 @@ namespace Orchard.Recipes.RecipeHandlers { } } - if (source != null) { + if (name == null) { + throw new InvalidOperationException("Name is required in a Theme declaration in a recipe file."); } - else { - if (name == null) { - throw new InvalidOperationException("Either name or source is required in a Theme declaration in a recipe file."); - } - // download and install theme from the orchard feed or a custom feed if repository is specified. - bool enforceVersion = version != null; - bool installed = false; - PackagingSource packagingSource = _packagingSourceManager.GetSources().FirstOrDefault(); - if (repository != null) { - enforceVersion = false; - packagingSource = new PackagingSource { FeedTitle = repository, FeedUrl = repository }; - } - foreach (var packagingEntry in _packagingSourceManager.GetExtensionList(packagingSource)) { - if (String.Equals(packagingEntry.Title, name, StringComparison.OrdinalIgnoreCase)) { - if (enforceVersion && !String.Equals(packagingEntry.Version, version, StringComparison.OrdinalIgnoreCase)) { - continue; - } - // use for replace. - bool themeExists = false; - foreach (var extension in _extensionManager.AvailableExtensions() - .Where(extension => - DefaultExtensionTypes.IsTheme(extension.ExtensionType) && - String.Equals(packagingEntry.Title, extension.Name, StringComparison.OrdinalIgnoreCase))) { - themeExists = true; - } - _packageManager.Install(packagingEntry.PackageId, packagingEntry.Version, packagingSource.FeedUrl, HostingEnvironment.MapPath("~/")); - if (enable) { - _themeService.EnableThemeFeatures(packagingEntry.Title); - } - if (current) { - _siteThemeService.SetSiteTheme(packagingEntry.Title); - } - - installed = true; - break; + // download and install theme from the orchard feed or a custom feed if repository is specified. + bool enforceVersion = version != null; + bool installed = false; + PackagingSource packagingSource = _packagingSourceManager.GetSources().FirstOrDefault(); + if (repository != null) { + enforceVersion = false; + packagingSource = new PackagingSource { FeedTitle = repository, FeedUrl = repository }; + } + foreach (var packagingEntry in _packagingSourceManager.GetExtensionList(packagingSource)) { + if (String.Equals(packagingEntry.Title, name, StringComparison.OrdinalIgnoreCase)) { + if (enforceVersion && !String.Equals(packagingEntry.Version, version, StringComparison.OrdinalIgnoreCase)) { + continue; + } + // use for replace. + bool themeExists = false; + foreach (var extension in _extensionManager.AvailableExtensions() + .Where(extension => + DefaultExtensionTypes.IsTheme(extension.ExtensionType) && + String.Equals(packagingEntry.Title, extension.Name, StringComparison.OrdinalIgnoreCase))) { + themeExists = true; + } + _packageManager.Install(packagingEntry.PackageId, packagingEntry.Version, packagingSource.FeedUrl, HostingEnvironment.MapPath("~/")); + if (current) { + _themeService.EnableThemeFeatures(packagingEntry.Title); + _siteThemeService.SetSiteTheme(packagingEntry.Title); + } + else if (enable) { + _themeService.EnableThemeFeatures(packagingEntry.Title); } - } - if (!installed) { - throw new InvalidOperationException(string.Format("Theme {0} was not found in the specified location.", name)); + installed = true; + break; } + } + if (!installed) { + throw new InvalidOperationException(string.Format("Theme {0} was not found in the specified location.", name)); } recipeContext.Executed = true;