mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Alias step builder and executer
This commit is contained in:
@@ -110,6 +110,8 @@
|
||||
<Compile Include="Implementation\Updater\AliasHolderUpdaterTask.cs" />
|
||||
<Compile Include="Implementation\Utils.cs" />
|
||||
<Compile Include="Migrations.cs" />
|
||||
<Compile Include="Recipes\Builders\AliasStep.cs" />
|
||||
<Compile Include="Recipes\Executors\AliasStep.cs" />
|
||||
<Compile Include="Records\ActionRecord.cs" />
|
||||
<Compile Include="Records\AliasRecord.cs" />
|
||||
<Compile Include="Routes.cs" />
|
||||
@@ -123,20 +125,20 @@
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target> -->
|
||||
<Target Name="AfterBuild" DependsOnTargets="AfterBuildCompiler">
|
||||
<PropertyGroup>
|
||||
<AreasManifestDir>$(ProjectDir)\..\Manifests</AreasManifestDir>
|
||||
</PropertyGroup>
|
||||
<!-- If this is an area child project, uncomment the following line:
|
||||
<CreateAreaManifest AreaName="$(AssemblyName)" AreaType="Child" AreaPath="$(ProjectDir)" ManifestPath="$(AreasManifestDir)" ContentFiles="@(Content)" />
|
||||
<!-- If this is an area child project, uncomment the following line:
|
||||
<CreateAreaManifest AreaName="$(AssemblyName)" AreaType="Child" AreaPath="$(ProjectDir)" ManifestPath="$(AreasManifestDir)" ContentFiles="@(Content)" />
|
||||
-->
|
||||
<!-- If this is an area parent project, uncomment the following lines:
|
||||
<CreateAreaManifest AreaName="$(AssemblyName)" AreaType="Parent" AreaPath="$(ProjectDir)" ManifestPath="$(AreasManifestDir)" ContentFiles="@(Content)" />
|
||||
<CopyAreaManifests ManifestPath="$(AreasManifestDir)" CrossCopy="false" RenameViews="true" />
|
||||
<!-- If this is an area parent project, uncomment the following lines:
|
||||
<CreateAreaManifest AreaName="$(AssemblyName)" AreaType="Parent" AreaPath="$(ProjectDir)" ManifestPath="$(AreasManifestDir)" ContentFiles="@(Content)" />
|
||||
<CopyAreaManifests ManifestPath="$(AreasManifestDir)" CrossCopy="false" RenameViews="true" />
|
||||
-->
|
||||
</Target>
|
||||
<Target Name="AfterBuildCompiler" Condition="'$(MvcBuildViews)'=='true'">
|
||||
|
@@ -0,0 +1,129 @@
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
using Orchard.Data;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Recipes.Services;
|
||||
using Orchard.Alias.Records;
|
||||
using Orchard.Alias.Implementation.Holder;
|
||||
|
||||
namespace Orchard.Roles.Recipes.Builders {
|
||||
public class RolesStep : RecipeBuilderStep {
|
||||
private readonly IRepository<AliasRecord> _aliasRecordepository;
|
||||
private readonly IAliasHolder _aliasHolder;
|
||||
|
||||
public RolesStep(IRepository<AliasRecord> aliasRecordRepository, IAliasHolder aliasHolder)
|
||||
{
|
||||
_aliasRecordepository = aliasRecordRepository;
|
||||
_aliasHolder = aliasHolder;
|
||||
}
|
||||
|
||||
public override string Name {
|
||||
get { return "Alias"; }
|
||||
}
|
||||
|
||||
public override LocalizedString DisplayName {
|
||||
get { return T("Alias"); }
|
||||
}
|
||||
|
||||
public override LocalizedString Description {
|
||||
get { return T("Exports the aliases."); }
|
||||
}
|
||||
|
||||
public override void Build(BuildContext context) {
|
||||
//var aliases = _aliasRecordepository.Table.ToList();
|
||||
|
||||
var aliases = _aliasHolder.GetMaps().SelectMany(m => m.GetAliases()).ToList();
|
||||
|
||||
if (!aliases.Any())
|
||||
return;
|
||||
|
||||
var root = new XElement("Aliases");
|
||||
context.RecipeDocument.Element("Orchard").Add(root);
|
||||
|
||||
foreach (var alias in aliases.OrderBy(x => x.Path))
|
||||
{
|
||||
var aliasElement = new XElement("Alias", new XAttribute("Path", alias.Path));
|
||||
//root.Add(
|
||||
// new XElement("Alias",
|
||||
// new XAttribute("Path", alias.Path)));
|
||||
var routeValuesElement = new XElement("RouteValues");
|
||||
foreach (var routeValue in alias.RouteValues)
|
||||
{
|
||||
routeValuesElement.Add(new XElement("Add", new XAttribute("Key", routeValue.Key), new XAttribute("Value", routeValue.Value)));
|
||||
}
|
||||
|
||||
aliasElement.Add(routeValuesElement);
|
||||
root.Add(aliasElement);
|
||||
}
|
||||
|
||||
//add a collection of all the alias paths in this site so that the importing site can remove aliases that don't exist remotely
|
||||
var pathsElement = new XElement("Paths");
|
||||
foreach (var aliasInfo in aliases)
|
||||
{
|
||||
pathsElement.Add(new XElement("Add", new XAttribute("Path", aliasInfo.Path)));
|
||||
}
|
||||
|
||||
root.Add(pathsElement);
|
||||
|
||||
//var rootElement = context.Document.Descendants("Orchard").FirstOrDefault();
|
||||
|
||||
//if (rootElement == null)
|
||||
//{
|
||||
// var ex = new OrchardException(T("Could not export this site's Aliases because the document passed via the Export Context did not contain a node called 'Orchard'. The document was malformed."));
|
||||
// Logger.Error(ex, ex.Message);
|
||||
// throw ex;
|
||||
//}
|
||||
|
||||
//rootElement.Add(xmlElement);
|
||||
}
|
||||
|
||||
//public void Exporting(ExportContext context)
|
||||
//{
|
||||
// if (!context.ExportOptions.CustomSteps.Contains("Aliases")) { return; }
|
||||
|
||||
// var xmlElement = new XElement("Aliases");
|
||||
|
||||
// var autoroutePaths = _contentManager.Query<AutoroutePart>().List().Select(p => p.Path);
|
||||
// var allAliasInfos = _aliasHolder.GetMaps().SelectMany(m => m.GetAliases()).ToList();
|
||||
|
||||
// //we need to remove any aliases that are autoroutes because the remote conent id may not sync up with the local content id. the autoroutes will be imported as part of the content import
|
||||
// var aliasInfosToExport = allAliasInfos.Where(ai => !autoroutePaths.Contains(ai.Path));
|
||||
|
||||
// foreach (var aliasInfo in aliasInfosToExport)
|
||||
// {
|
||||
// var aliasElement = new XElement("Alias", new XAttribute("Path", aliasInfo.Path));
|
||||
|
||||
// var routeValuesElement = new XElement("RouteValues");
|
||||
// foreach (var routeValue in aliasInfo.RouteValues)
|
||||
// {
|
||||
// routeValuesElement.Add(new XElement("Add", new XAttribute("Key", routeValue.Key), new XAttribute("Value", routeValue.Value)));
|
||||
// }
|
||||
|
||||
// aliasElement.Add(routeValuesElement);
|
||||
// xmlElement.Add(aliasElement);
|
||||
// }
|
||||
|
||||
// //add a collection of all the alias paths in this site so that the importing site can remove aliases that don't exist remotely
|
||||
// var pathsElement = new XElement("Paths");
|
||||
// foreach (var aliasInfo in allAliasInfos)
|
||||
// {
|
||||
// pathsElement.Add(new XElement("Add", new XAttribute("Path", aliasInfo.Path)));
|
||||
// }
|
||||
|
||||
// xmlElement.Add(pathsElement);
|
||||
|
||||
// var rootElement = context.Document.Descendants("Orchard").FirstOrDefault();
|
||||
|
||||
// if (rootElement == null)
|
||||
// {
|
||||
// var ex = new OrchardException(T("Could not export this site's Aliases because the document passed via the Export Context did not contain a node called 'Orchard'. The document was malformed."));
|
||||
// Logger.Error(ex, ex.Message);
|
||||
// throw ex;
|
||||
// }
|
||||
|
||||
// rootElement.Add(xmlElement);
|
||||
//}
|
||||
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,132 @@
|
||||
using System;
|
||||
using System.Web;
|
||||
using System.Web.Routing;
|
||||
using System.Linq;
|
||||
using Orchard.Alias;
|
||||
using Orchard.Logging;
|
||||
using Orchard.Recipes.Models;
|
||||
using Orchard.Recipes.Services;
|
||||
|
||||
|
||||
namespace Orchard.Alias.Recipes.Executors {
|
||||
public class AliasStep : RecipeExecutionStep {
|
||||
private readonly IAliasService _aliasService;
|
||||
|
||||
public AliasStep(IAliasService aliasService)
|
||||
{
|
||||
_aliasService = aliasService;
|
||||
}
|
||||
|
||||
public override string Name {
|
||||
get { return "Alias"; }
|
||||
}
|
||||
|
||||
public override void Execute(RecipeExecutionContext context) {
|
||||
//var installedPermissions = _roleService.GetInstalledPermissions().SelectMany(p => p.Value).ToList();
|
||||
|
||||
foreach (var aliasElement in context.RecipeStep.Step.Elements()) {
|
||||
var aliasPath = aliasElement.Attribute("Path").Value;
|
||||
|
||||
Logger.Information("Processing alias '{0}'.", aliasPath);
|
||||
|
||||
//var path = aliasElement.Attribute("Path").Value;
|
||||
var rvd = new RouteValueDictionary();
|
||||
|
||||
var routeValuesElement = aliasElement.Descendants("RouteValues").FirstOrDefault();
|
||||
|
||||
if (routeValuesElement != null)
|
||||
{
|
||||
foreach (var routeValue in routeValuesElement.Descendants("Add"))
|
||||
{
|
||||
rvd.Add(routeValue.Attribute("Key").Value, routeValue.Attribute("Value").Value);
|
||||
}
|
||||
}
|
||||
|
||||
_aliasService.Set(aliasPath, rvd, "Custom");
|
||||
|
||||
try {
|
||||
_aliasService.Set(aliasPath, rvd, "Custom");
|
||||
//var role = _roleService.GetRoleByName(roleName);
|
||||
//if (role == null) {
|
||||
// _roleService.CreateRole(roleName);
|
||||
// role = _roleService.GetRoleByName(roleName);
|
||||
}
|
||||
|
||||
// var permissions = roleElement.Attribute("Permissions").Value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
// // Only import permissions for currenlty installed modules.
|
||||
// var permissionsValid = permissions.Where(permission => installedPermissions.Any(x => x.Name == permission)).ToList();
|
||||
|
||||
// // Union to keep existing permissions.
|
||||
// _roleService.UpdateRole(role.Id, role.Name, permissionsValid.Union(role.RolesPermissions.Select(p => p.Permission.Name)));
|
||||
//}
|
||||
catch (Exception ex) {
|
||||
Logger.Error(ex, "Error while processing alias '{0}'.", aliasPath);
|
||||
throw;
|
||||
}
|
||||
|
||||
////remove all local pathys that are not present in the remote export
|
||||
//var allRemotePaths = recipeContext.RecipeStep.Step.XPathSelectElements("Paths/Add").Select(e => e.Attribute("Path").Value);
|
||||
//var allLocalPaths = _aliasService.List().Select(t => t.Item1).ToList();
|
||||
|
||||
//foreach (var path in allLocalPaths.Where(p => !allRemotePaths.Contains(p)))
|
||||
//{
|
||||
// _aliasService.Delete(path);
|
||||
//}
|
||||
|
||||
|
||||
//recipeContext.Executed = true;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
<Aliases>
|
||||
<Alias Path="Profile/Edit" Area="Custom.Profile">
|
||||
<RouteValues>
|
||||
<Add Key="area" Value="Custom.Profile" />
|
||||
<Add Key="controller" Value="Profile" />
|
||||
<Add Key="action" Value="Edit" />
|
||||
</RouteValues>
|
||||
</Alias>
|
||||
*/
|
||||
//Enable any features that are in the list, disable features that aren't in the list
|
||||
//public override void Execute(RecipeExecutionContext context)
|
||||
//{
|
||||
// if (!String.Equals(context.RecipeStep.Name, "Aliases", StringComparison.OrdinalIgnoreCase))
|
||||
// {
|
||||
// return;
|
||||
// }
|
||||
|
||||
// var aliasElements = context.RecipeStep.Step.Descendants("Alias");
|
||||
|
||||
// foreach (var aliasElement in aliasElements)
|
||||
// {
|
||||
// var path = aliasElement.Attribute("Path").Value;
|
||||
// var rvd = new RouteValueDictionary();
|
||||
|
||||
// var routeValuesElement = aliasElement.Descendants("RouteValues").FirstOrDefault();
|
||||
|
||||
// if (routeValuesElement != null)
|
||||
// {
|
||||
// foreach (var routeValue in routeValuesElement.Descendants("Add"))
|
||||
// {
|
||||
// rvd.Add(routeValue.Attribute("Key").Value, routeValue.Attribute("Value").Value);
|
||||
// }
|
||||
// }
|
||||
|
||||
// _aliasService.Set(path, rvd, "Custom");
|
||||
// }
|
||||
|
||||
// //remove all local pathys that are not present in the remote export
|
||||
// var allRemotePaths = recipeContext.RecipeStep.Step.XPathSelectElements("Paths/Add").Select(e => e.Attribute("Path").Value);
|
||||
// var allLocalPaths = _aliasService.List().Select(t => t.Item1).ToList();
|
||||
|
||||
// foreach (var path in allLocalPaths.Where(p => !allRemotePaths.Contains(p)))
|
||||
// {
|
||||
// _aliasService.Delete(path);
|
||||
// }
|
||||
|
||||
|
||||
// recipeContext.Executed = true;
|
||||
//}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user