Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce DeploymentSourceBase to simplify DeploymentSource #16842

Merged
merged 14 commits into from
Oct 9, 2024
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)
public override async Task ProcessDeploymentStepAsync(DeploymentPlanResult result)
{
var adminMenuStep = step as AdminMenuDeploymentStep;

if (adminMenuStep == 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.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)
public override async Task ProcessDeploymentStepAsync(DeploymentPlanResult result)
{
if (step is not ContentDefinitionDeploymentStep contentDefinitionStep)
{
return;
}

var contentTypeDefinitionRecord = await _contentDefinitionStore.LoadContentDefinitionAsync();

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

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

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

namespace OrchardCore.ContentTypes.Deployment;

public class DeleteContentDefinitionDeploymentSource : IDeploymentSource
public class DeleteContentDefinitionDeploymentSource
: DeploymentSourceBase<DeleteContentDefinitionDeploymentStep>
{
public Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
public override async Task ProcessDeploymentStepAsync(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(DeploymentStep.ContentTypes),
["ContentParts"] = JArray.FromObject(DeploymentStep.ContentParts),
});

return Task.CompletedTask;
await Task.CompletedTask;
hishamco marked this conversation as resolved.
Show resolved Hide resolved
}
}
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)
public override async Task ProcessDeploymentStepAsync(DeploymentPlanResult result)
{
if (step is not ReplaceContentDefinitionDeploymentStep replaceContentDefinitionStep)
{
return;
}

var contentTypeDefinitionRecord = await _contentDefinitionStore.LoadContentDefinitionAsync();

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

var contentParts = replaceContentDefinitionStep.IncludeAll
var contentParts = DeploymentStep.IncludeAll
? contentTypeDefinitionRecord.ContentPartDefinitionRecords
: contentTypeDefinitionRecord.ContentPartDefinitionRecords
.Where(x => replaceContentDefinitionStep.ContentParts.Contains(x.Name));
.Where(x => DeploymentStep.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)
public override async Task ProcessDeploymentStepAsync(DeploymentPlanResult result)
{
var contentItemDeploymentStep = step as ContentItemDeploymentStep;

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

var contentItem = await _contentManager.GetAsync(contentItemDeploymentStep.ContentItemId);
var contentItem = await _contentManager.GetAsync(DeploymentStep.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)
public override async Task ProcessDeploymentStepAsync(DeploymentPlanResult result)
{
var allContentStep = step as AllContentDeploymentStep;

if (allContentStep == null)
{
return;
}

var data = new JsonArray();
result.Steps.Add(new JsonObject
{
Expand All @@ -38,7 +32,7 @@ public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlan
// 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 (DeploymentStep.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 @@ -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)
public override async Task ProcessDeploymentStepAsync(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(DeploymentStep.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 (DeploymentStep.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)
public override async Task ProcessDeploymentStepAsync(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)
public override async Task ProcessDeploymentStepAsync(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 = DeploymentStep.IncludeAll
? (await _customSettingsService.GetAllSettingsTypesAsync()).ToArray()
: (await _customSettingsService.GetSettingsTypesAsync(customSettingsStep.SettingsTypeNames)).ToArray();
: (await _customSettingsService.GetSettingsTypesAsync(DeploymentStep.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)
public override async Task ProcessDeploymentStepAsync(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 = DeploymentStep.IncludeAll
? (await _deploymentPlanService.GetAllDeploymentPlansAsync()).ToArray()
: (await _deploymentPlanService.GetDeploymentPlansAsync(deploymentPlanStep.DeploymentPlanNames)).ToArray();
: (await _deploymentPlanService.GetDeploymentPlansAsync(DeploymentStep.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));
}
public override async Task ProcessDeploymentStepAsync(DeploymentPlanResult result)
=> await result.FileBuilder.SetFileAsync(DeploymentStep.FileName, Encoding.UTF8.GetBytes(DeploymentStep.FileContent));
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@

namespace OrchardCore.Deployment.Steps;

public class JsonRecipeDeploymentSource : IDeploymentSource
public class JsonRecipeDeploymentSource
: DeploymentSourceBase<JsonRecipeDeploymentStep>
{
public Task ProcessDeploymentStepAsync(DeploymentStep deploymentStep, DeploymentPlanResult result)
public override async Task ProcessDeploymentStepAsync(DeploymentPlanResult result)
{
if (deploymentStep is not JsonRecipeDeploymentStep jsonRecipeStep)
{
return Task.CompletedTask;
}
result.Steps.Add(JObject.Parse(DeploymentStep.Json));

result.Steps.Add(JObject.Parse(jsonRecipeStep.Json));

return Task.CompletedTask;
await Task.CompletedTask;
hishamco marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading