Skip to content

Commit

Permalink
Do not update current client and some refactorings.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Nov 17, 2021
1 parent 5c7ed75 commit 3d22696
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 84 deletions.
2 changes: 1 addition & 1 deletion cli/Squidex.CLI/Squidex.CLI/Commands/App_OpenLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public async Task Authors(ImportArguments arguments)
{
var session = configuration.StartSession(arguments.App);

using (var stream = new FileStream(arguments.File, FileMode.Open))
await using (var stream = new FileStream(arguments.File, FileMode.Open))
{
var importer = new AuthorImporter(session);

Expand Down
15 changes: 14 additions & 1 deletion cli/Squidex.CLI/Squidex.CLI/Commands/App_Sync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,31 @@ public sealed class InArguments : AppArguments
[Option(ShortName = "t", LongName = "targets", Description = "The targets to sync, e.g. schemas, workflows, app, rules.")]
public string[] Targets { get; set; }

[Option(ShortName = "l", LongName = "language", Description = "The content language to synchronize.")]
public string[] Languages { get; set; }

[Option(LongName = "delete", Description = "Use this flag to also delete entities.")]
public bool Delete { get; set; }

[Option(LongName = "recreate", Description = "Use this flag to also recreate entities.")]
public bool Recreate { get; set; }

[Option(LongName = "update-current-client", Description = "Also update the client that is used during the sync process.")]
public bool UpdateCurrentClient { get; set; }

[Option(LongName = "emulate", Description = "Use this flag to not make any updates and to emulate the changes.")]
public bool Emulate { get; set; }

public SyncOptions ToOptions()
{
return new SyncOptions { Delete = Delete, Recreate = Recreate, Targets = Targets };
return new SyncOptions
{
Delete = Delete,
Recreate = Recreate,
Languages = Languages,
Targets = Targets,
UpdateCurrentClient = UpdateCurrentClient
};
}

public sealed class Validator : AbstractValidator<InArguments>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public IFile GetFile(FilePath path)
{
var relativePath = GetRelativePath(path);

return new EmbeddedFile(assembly, path.Elements.Last(), relativePath, path.ToString());
return new EmbeddedFile(assembly, path.Elements[^1], relativePath, path.ToString());
}

public IEnumerable<IFile> GetFiles(FilePath path, string extension)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,27 @@ public async Task ImportAsync(Stream stream)

var json = JObject.Parse(x.Json);

if (json.TryGetValue("name", out var name))
if (json.TryGetValue("name", StringComparison.Ordinal, out var name))
{
data.Name = GetString(name);
}

if (json.TryGetValue("birth_date", out var birthdate))
if (json.TryGetValue("birth_date", StringComparison.Ordinal, out var birthdate))
{
data.Birthdate = GetString(birthdate);
}

if (json.TryGetValue("bio", out var bio))
if (json.TryGetValue("bio", StringComparison.Ordinal, out var bio))
{
data.Bio = GetString(bio);
}

if (json.TryGetValue("personal_name", out var personalName))
if (json.TryGetValue("personal_name", StringComparison.Ordinal, out var personalName))
{
data.PersonalName = GetString(personalName);
}

if (json.TryGetValue("wikipedia", out var wikipedia))
if (json.TryGetValue("wikipedia", StringComparison.Ordinal, out var wikipedia))
{
data.Wikipedia = GetString(wikipedia);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ public sealed class AppRoleModel
{
[Required]
public List<string> Permissions { get; set; }

public Dictionary<string, object> Properties { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Squidex.CLI.Commands.Implementation.FileSystem;
Expand Down Expand Up @@ -44,11 +45,7 @@ await log.DoSafeAsync("Exporting clients", async () =>

foreach (var client in clients.Items)
{
model.Clients[client.Name] = new AppClientModel
{
Name = client.Name,
Role = client.Role
};
model.Clients[client.Name] = client.ToModel();
}
});

Expand All @@ -60,12 +57,7 @@ await log.DoSafeAsync("Exporting languages", async () =>

foreach (var language in languages.Items)
{
model.Languages[language.Iso2Code] = new UpdateLanguageDto
{
Fallback = language.Fallback,
IsMaster = language.IsMaster,
IsOptional = language.IsOptional
};
model.Languages[language.Iso2Code] = language.ToModel();
}
});

Expand All @@ -77,10 +69,7 @@ await log.DoSafeAsync("Exporting Roles", async () =>

foreach (var role in roles.Items)
{
model.Roles[role.Name] = new AppRoleModel
{
Permissions = role.Permissions
};
model.Roles[role.Name] = role.ToModel();
}
});

Expand Down Expand Up @@ -128,7 +117,7 @@ private async Task SynchronizeClientsAsync(AppModel model, SyncOptions options,
{
var generatedClientId = $"{session.App}:{client.Id}";

if (model.Clients.ContainsKey(client.Id) || session.ClientId.Equals(generatedClientId))
if (model.Clients.ContainsKey(client.Id) || session.ClientId.Equals(generatedClientId, StringComparison.Ordinal))
{
continue;
}
Expand Down Expand Up @@ -157,18 +146,23 @@ await log.DoSafeAsync($"Client '{clientId}' creating", async () =>
});
}

foreach (var (clientId, value) in model.Clients)
foreach (var (clientId, client) in model.Clients)
{
var existing = current.Items.Find(x => x.Id == clientId);

if (existing == null || value.JsonEquals(existing))
if (existing == null || client.JsonEquals(existing))
{
continue;
}

if (!options.UpdateCurrentClient && session.ClientId.Equals(clientId, StringComparison.Ordinal))
{
continue;
}

await log.DoSafeAsync($"Client '{clientId}' updating", async () =>
{
var request = new UpdateClientDto { Role = value.Role };
var request = client.ToUpdate();

await session.Apps.PutClientAsync(session.App, clientId, request);
});
Expand Down Expand Up @@ -212,20 +206,18 @@ await log.DoSafeAsync($"Language '{isoCode}' creating", async () =>
});
}

foreach (var (isoCode, value) in model.Languages)
foreach (var (isoCode, language) in model.Languages)
{
var existing = current.Items.Find(x => x.Iso2Code == isoCode);

if (existing == null || value.JsonEquals(existing))
if (existing == null || language.JsonEquals(existing))
{
continue;
}

await log.DoSafeAsync($"Language '{isoCode}' updating", async () =>
{
var request = value;

await session.Apps.PutLanguageAsync(session.App, isoCode, request);
await session.Apps.PutLanguageAsync(session.App, isoCode, language);
});
}
}
Expand Down Expand Up @@ -270,18 +262,18 @@ await log.DoSafeAsync($"Role '{roleName}' creating", async () =>
});
}

foreach (var (roleName, value) in model.Roles)
foreach (var (roleName, role) in model.Roles)
{
var existing = current.Items.Find(x => x.Name == roleName);

if (existing == null || value.JsonEquals(existing))
if (existing == null || role.JsonEquals(existing))
{
continue;
}

await log.DoSafeAsync($"Role '{roleName}' updating", async () =>
{
var request = new UpdateRoleDto { Permissions = value.Permissions };
var request = role.ToUpdate();

await session.Apps.PutRoleAsync(session.App, roleName, request);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using Squidex.ClientLibrary.Management;

namespace Squidex.CLI.Commands.Implementation.Sync.App
{
public static class Extensions
{
public static AppRoleModel ToModel(this RoleDto role)
{
return new AppRoleModel { Permissions = role.Permissions, Properties = role.Properties };
}

public static UpdateRoleDto ToUpdate(this AppRoleModel model)
{
return new UpdateRoleDto { Permissions = model.Permissions, Properties = model.Properties };
}

public static AppClientModel ToModel(this ClientDto client)
{
return new AppClientModel { Name = client.Name, Role = client.Role };
}

public static UpdateClientDto ToUpdate(this AppClientModel model)
{
return new UpdateClientDto { Name = model.Name, Role = model.Role };
}

public static UpdateLanguageDto ToModel(this AppLanguageDto language)
{
return new UpdateLanguageDto
{
Fallback = language.Fallback,
IsMaster = language.IsMaster,
IsOptional = language.IsOptional
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public async Task ImportAsync(ISyncService sync, SyncOptions options, ISession s
{
var parentId = await tree.GetIdAsync(asset.FolderPath);

request.Jobs.Add(asset.ToMoveJob(parentId));
request.Jobs.Add(asset.ToAnnotateJob());
request.Jobs.Add(asset.ToMove(parentId));
request.Jobs.Add(asset.ToAnnotate());
}

var assetIndex = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,31 @@ public static string GetFileHash(this IFile file, string fileName)
}
}

public static BulkUpdateAssetsJobDto ToMoveJob(this AssetModel model, string parentId)
public static BulkUpdateAssetsJobDto ToMove(this AssetModel model, string parentId)
{
return new BulkUpdateAssetsJobDto
{
Id = model.Id,
Type = BulkUpdateAssetType.Move,
ParentId = parentId
};
var bulkJob = model.ToJob(BulkUpdateAssetType.Move);

bulkJob.ParentId = parentId;

return bulkJob;
}

public static BulkUpdateAssetsJobDto ToAnnotateJob(this AssetModel model)
public static BulkUpdateAssetsJobDto ToAnnotate(this AssetModel model)
{
return new BulkUpdateAssetsJobDto
{
Id = model.Id,
Type = BulkUpdateAssetType.Annotate,
FileName = model.FileName,
ParentId = null,
Permanent = false,
IsProtected = model.IsProtected,
Metadata = model.Metadata,
Slug = model.Slug,
Tags = model.Tags
};
var bulkJob = model.ToJob(BulkUpdateAssetType.Annotate);

bulkJob.FileName = model.FileName;
bulkJob.Metadata = model.Metadata;
bulkJob.IsProtected = model.IsProtected;
bulkJob.Slug = model.Slug;
bulkJob.Tags = model.Tags;

return bulkJob;
}

private static BulkUpdateAssetsJobDto ToJob(this AssetModel model, BulkUpdateAssetType type)
{
return new BulkUpdateAssetsJobDto { Id = model.Id, Type = type };
}

public static async Task<AssetModel> ToModelAsync(this AssetDto asset, FolderTree folders)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public async Task ImportAsync(ISyncService sync, SyncOptions options, ISession s
DoNotScript = true,
DoNotValidate = false,
DoNotValidateWorkflow = true,
Jobs = model.Contents.Select(x => x.ToJob(schemas)).ToList()
Jobs = model.Contents.Select(x => x.ToUpsert(schemas)).ToList()
};

var contentIdAssigned = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Squidex.CLI.Commands.Implementation.Sync.Contents
{
public static class Extensions
{
public static BulkUpdateJob ToJob(this ContentModel model, SchemasDto schemas)
public static BulkUpdateJob ToUpsert(this ContentModel model, SchemasDto schemas)
{
var id = model.Id;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using System;
using Squidex.ClientLibrary;

namespace Squidex.CLI.Commands.Implementation.Sync.Rules
{
public static class Extensions
{
public static UpdateExtendableRuleDto ToUpdate(this RuleModel model)
{
return new UpdateExtendableRuleDto { Action = model.Action, Trigger = model.Trigger, Name = model.Name };
}

public static CreateExtendableRuleDto ToCreate(this RuleModel model)
{
return new CreateExtendableRuleDto { Action = model.Action, Trigger = model.Trigger };
}
}
}
Loading

0 comments on commit 3d22696

Please sign in to comment.