Skip to content

Commit

Permalink
Introduce DeploymentSourceBase to simplify DeploymentSource (#16842)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Mike Alhayek <[email protected]>
  • Loading branch information
hishamco and MikeAlhayek authored Oct 9, 2024
1 parent 5770ac8 commit 28de8c7
Show file tree
Hide file tree
Showing 51 changed files with 246 additions and 450 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

namespace OrchardCore.AdminMenu.Deployment;

public class AdminMenuDeploymentSource : IDeploymentSource
public class AdminMenuDeploymentSource
: DeploymentSourceBase<AdminMenuDeploymentStep>
{
private readonly IAdminMenuService _adminMenuService;
private readonly JsonSerializerOptions _serializationOptions;
Expand All @@ -19,15 +20,8 @@ public AdminMenuDeploymentSource(IAdminMenuService adminMenuService,
_serializationOptions = serializationOptions.Value.SerializerOptions;
}

public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
protected override async Task ProcessAsync(AdminMenuDeploymentStep step, DeploymentPlanResult result)
{
var adminMenuStep = step as AdminMenuDeploymentStep;

if (adminMenuStep == null)
{
return;
}

var data = new JsonArray();
result.Steps.Add(new JsonObject
{
Expand All @@ -40,7 +34,5 @@ public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlan
var objectData = JObject.FromObject(adminMenu, _serializationOptions);
data.Add(objectData);
}

return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace OrchardCore.ContentTypes.Deployment;

public class ContentDefinitionDeploymentSource : IDeploymentSource
public class ContentDefinitionDeploymentSource
: DeploymentSourceBase<ContentDefinitionDeploymentStep>
{
private readonly IContentDefinitionStore _contentDefinitionStore;

Expand All @@ -13,24 +14,19 @@ public ContentDefinitionDeploymentSource(IContentDefinitionStore contentDefiniti
_contentDefinitionStore = contentDefinitionStore;
}

public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
protected override async Task ProcessAsync(ContentDefinitionDeploymentStep step, DeploymentPlanResult result)
{
if (step is not ContentDefinitionDeploymentStep contentDefinitionStep)
{
return;
}

var contentTypeDefinitionRecord = await _contentDefinitionStore.LoadContentDefinitionAsync();

var contentTypes = contentDefinitionStep.IncludeAll
var contentTypes = step.IncludeAll
? contentTypeDefinitionRecord.ContentTypeDefinitionRecords
: contentTypeDefinitionRecord.ContentTypeDefinitionRecords
.Where(x => contentDefinitionStep.ContentTypes.Contains(x.Name));
.Where(x => step.ContentTypes.Contains(x.Name));

var contentParts = contentDefinitionStep.IncludeAll
var contentParts = step.IncludeAll
? contentTypeDefinitionRecord.ContentPartDefinitionRecords
: contentTypeDefinitionRecord.ContentPartDefinitionRecords
.Where(x => contentDefinitionStep.ContentParts.Contains(x.Name));
.Where(x => step.ContentParts.Contains(x.Name));

result.Steps.Add(new JsonObject
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@

namespace OrchardCore.ContentTypes.Deployment;

public class DeleteContentDefinitionDeploymentSource : IDeploymentSource
public class DeleteContentDefinitionDeploymentSource
: DeploymentSourceBase<DeleteContentDefinitionDeploymentStep>
{
public Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
protected override Task ProcessAsync(DeleteContentDefinitionDeploymentStep step, DeploymentPlanResult result)
{
if (step is not DeleteContentDefinitionDeploymentStep deleteContentDefinitionStep)
{
return Task.CompletedTask;
}

result.Steps.Add(new JsonObject
{
["name"] = "DeleteContentDefinition",
["ContentTypes"] = JArray.FromObject(deleteContentDefinitionStep.ContentTypes),
["ContentParts"] = JArray.FromObject(deleteContentDefinitionStep.ContentParts),
["ContentTypes"] = JArray.FromObject(step.ContentTypes),
["ContentParts"] = JArray.FromObject(step.ContentParts),
});

return Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace OrchardCore.ContentTypes.Deployment;

public class ReplaceContentDefinitionDeploymentSource : IDeploymentSource
public class ReplaceContentDefinitionDeploymentSource
: DeploymentSourceBase<ReplaceContentDefinitionDeploymentStep>
{
private readonly IContentDefinitionStore _contentDefinitionStore;

Expand All @@ -13,24 +14,19 @@ public ReplaceContentDefinitionDeploymentSource(IContentDefinitionStore contentD
_contentDefinitionStore = contentDefinitionStore;
}

public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
protected override async Task ProcessAsync(ReplaceContentDefinitionDeploymentStep step, DeploymentPlanResult result)
{
if (step is not ReplaceContentDefinitionDeploymentStep replaceContentDefinitionStep)
{
return;
}

var contentTypeDefinitionRecord = await _contentDefinitionStore.LoadContentDefinitionAsync();

var contentTypes = replaceContentDefinitionStep.IncludeAll
var contentTypes = step.IncludeAll
? contentTypeDefinitionRecord.ContentTypeDefinitionRecords
: contentTypeDefinitionRecord.ContentTypeDefinitionRecords
.Where(x => replaceContentDefinitionStep.ContentTypes.Contains(x.Name));
.Where(x => step.ContentTypes.Contains(x.Name));

var contentParts = replaceContentDefinitionStep.IncludeAll
var contentParts = step.IncludeAll
? contentTypeDefinitionRecord.ContentPartDefinitionRecords
: contentTypeDefinitionRecord.ContentPartDefinitionRecords
.Where(x => replaceContentDefinitionStep.ContentParts.Contains(x.Name));
.Where(x => step.ContentParts.Contains(x.Name));

result.Steps.Add(new JsonObject
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace OrchardCore.Contents.Deployment.AddToDeploymentPlan;

public class ContentItemDeploymentSource : IDeploymentSource
public class ContentItemDeploymentSource
: DeploymentSourceBase<ContentItemDeploymentStep>
{
private readonly IContentManager _contentManager;

Expand All @@ -13,16 +14,14 @@ public ContentItemDeploymentSource(IContentManager contentManager)
_contentManager = contentManager;
}

public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
protected override async Task ProcessAsync(ContentItemDeploymentStep step, DeploymentPlanResult result)
{
var contentItemDeploymentStep = step as ContentItemDeploymentStep;

if (contentItemDeploymentStep == null || contentItemDeploymentStep.ContentItemId == null)
if (step.ContentItemId == null)
{
return;
}

var contentItem = await _contentManager.GetAsync(contentItemDeploymentStep.ContentItemId);
var contentItem = await _contentManager.GetAsync(step.ContentItemId);

if (contentItem == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

namespace OrchardCore.Contents.Deployment;

public class AllContentDeploymentSource : IDeploymentSource
public class AllContentDeploymentSource
: DeploymentSourceBase<AllContentDeploymentStep>
{
private readonly ISession _session;

Expand All @@ -15,15 +16,8 @@ public AllContentDeploymentSource(ISession session)
_session = session;
}

public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
protected override async Task ProcessAsync(AllContentDeploymentStep step, DeploymentPlanResult result)
{
var allContentStep = step as AllContentDeploymentStep;

if (allContentStep == null)
{
return;
}

var data = new JsonArray();
result.Steps.Add(new JsonObject
{
Expand All @@ -35,10 +29,10 @@ public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlan
{
var objectData = JObject.FromObject(contentItem);

// Don't serialize the Id as it could be interpreted as an updated object when added back to YesSql
// Don't serialize the Id as it could be interpreted as an updated object when added back to YesSql.
objectData.Remove(nameof(ContentItem.Id));

if (allContentStep.ExportAsSetupRecipe)
if (step.ExportAsSetupRecipe)
{
objectData[nameof(ContentItem.Owner)] = "[js: parameters('AdminUserId')]";
objectData[nameof(ContentItem.Author)] = "[js: parameters('AdminUsername')]";
Expand All @@ -51,7 +45,5 @@ public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlan

data.Add(objectData);
}

return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

namespace OrchardCore.Contents.Deployment;

public class ContentDeploymentSource : IDeploymentSource
public class ContentDeploymentSource
: DeploymentSourceBase<ContentDeploymentStep>
{
private readonly ISession _session;

Expand All @@ -16,27 +17,19 @@ public ContentDeploymentSource(ISession session)
_session = session;
}

public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
protected override async Task ProcessAsync(ContentDeploymentStep step, DeploymentPlanResult result)
{
// TODO: Batch and create separate content files in the result.

var contentStep = step as ContentDeploymentStep;

if (contentStep == null)
{
return;
}

var data = new JsonArray();

foreach (var contentItem in await _session.Query<ContentItem, ContentItemIndex>(x => x.Published && x.ContentType.IsIn(contentStep.ContentTypes)).ListAsync())
foreach (var contentItem in await _session.Query<ContentItem, ContentItemIndex>(x => x.Published && x.ContentType.IsIn(step.ContentTypes)).ListAsync())
{
var objectData = JObject.FromObject(contentItem);

// Don't serialize the Id as it could be interpreted as an updated object when added back to YesSql.
objectData.Remove(nameof(ContentItem.Id));

if (contentStep.ExportAsSetupRecipe)
if (step.ExportAsSetupRecipe)
{
objectData[nameof(ContentItem.Owner)] = "[js: parameters('AdminUserId')]";
objectData[nameof(ContentItem.Author)] = "[js: parameters('AdminUsername')]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

namespace OrchardCore.Contents.Deployment.ExportContentToDeploymentTarget;

public class ExportContentToDeploymentTargetDeploymentSource : IDeploymentSource
public class ExportContentToDeploymentTargetDeploymentSource
: DeploymentSourceBase<ExportContentToDeploymentTargetDeploymentStep>
{
private readonly IContentManager _contentManager;
private readonly ISession _session;
Expand All @@ -24,15 +25,8 @@ public ExportContentToDeploymentTargetDeploymentSource(
_updateModelAccessor = updateModelAccessor;
}

public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
protected override async Task ProcessAsync(ExportContentToDeploymentTargetDeploymentStep step, DeploymentPlanResult result)
{
var exportContentToDeploymentTargetContentDeploymentStep = step as ExportContentToDeploymentTargetDeploymentStep;

if (exportContentToDeploymentTargetContentDeploymentStep == null)
{
return;
}

var data = new JsonArray();
result.Steps.Add(new JsonObject
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace OrchardCore.CustomSettings.Deployment;

public class CustomSettingsDeploymentSource : IDeploymentSource
public class CustomSettingsDeploymentSource
: DeploymentSourceBase<CustomSettingsDeploymentStep>
{
private readonly CustomSettingsService _customSettingsService;

Expand All @@ -13,22 +14,16 @@ public CustomSettingsDeploymentSource(CustomSettingsService customSettingsServic
_customSettingsService = customSettingsService;
}

public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
protected override async Task ProcessAsync(CustomSettingsDeploymentStep step, DeploymentPlanResult result)
{
var customSettingsStep = step as CustomSettingsDeploymentStep;
if (customSettingsStep == null)
{
return;
}

var settingsList = new List<KeyValuePair<string, JsonNode>>
{
new("name", "custom-settings"),
};

var settingsTypes = customSettingsStep.IncludeAll
var settingsTypes = step.IncludeAll
? (await _customSettingsService.GetAllSettingsTypesAsync()).ToArray()
: (await _customSettingsService.GetSettingsTypesAsync(customSettingsStep.SettingsTypeNames)).ToArray();
: (await _customSettingsService.GetSettingsTypesAsync(step.SettingsTypeNames)).ToArray();

foreach (var settingsType in settingsTypes)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

namespace OrchardCore.Deployment.Deployment;

public class DeploymentPlanDeploymentSource : IDeploymentSource
public class DeploymentPlanDeploymentSource
: DeploymentSourceBase<DeploymentPlanDeploymentStep>
{
private readonly IDeploymentPlanService _deploymentPlanService;
private readonly IEnumerable<IDeploymentStepFactory> _deploymentStepFactories;
Expand All @@ -21,23 +22,18 @@ public DeploymentPlanDeploymentSource(
_jsonSerializerOptions = jsonSerializerOptions.Value.SerializerOptions;
}

public async Task ProcessDeploymentStepAsync(DeploymentStep deploymentStep, DeploymentPlanResult result)
protected override async Task ProcessAsync(DeploymentPlanDeploymentStep step, DeploymentPlanResult result)
{
if (deploymentStep is not DeploymentPlanDeploymentStep deploymentPlanStep)
{
return;
}

if (!await _deploymentPlanService.DoesUserHavePermissionsAsync())
{
return;
}

var deploymentStepFactories = _deploymentStepFactories.ToDictionary(f => f.Name);

var deploymentPlans = deploymentPlanStep.IncludeAll
var deploymentPlans = step.IncludeAll
? (await _deploymentPlanService.GetAllDeploymentPlansAsync()).ToArray()
: (await _deploymentPlanService.GetDeploymentPlansAsync(deploymentPlanStep.DeploymentPlanNames)).ToArray();
: (await _deploymentPlanService.GetDeploymentPlansAsync(step.DeploymentPlanNames)).ToArray();

var plans = (from plan in deploymentPlans
select new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@

namespace OrchardCore.Deployment.Steps;

public class CustomFileDeploymentSource : IDeploymentSource
public class CustomFileDeploymentSource
: DeploymentSourceBase<CustomFileDeploymentStep>
{
public Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
{
if (step is not CustomFileDeploymentStep customFile)
{
return Task.CompletedTask;
}

return result.FileBuilder.SetFileAsync(customFile.FileName, Encoding.UTF8.GetBytes(customFile.FileContent));
}
protected override Task ProcessAsync(CustomFileDeploymentStep step, DeploymentPlanResult result)
=> result.FileBuilder.SetFileAsync(step.FileName, Encoding.UTF8.GetBytes(step.FileContent));
}
Loading

0 comments on commit 28de8c7

Please sign in to comment.