Skip to content

Commit

Permalink
CLI fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Apr 15, 2021
1 parent 427fac8 commit f08c0f9
Show file tree
Hide file tree
Showing 20 changed files with 531 additions and 580 deletions.
3 changes: 3 additions & 0 deletions cli/Squidex.CLI/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

# IDE0063: Use simple 'using' statement
csharp_prefer_simple_using_statement = true:none

# IDE0090: Use 'new(...)'
dotnet_diagnostic.IDE0090.severity = none
2 changes: 2 additions & 0 deletions cli/Squidex.CLI/Squidex.CLI/Commands/App_Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using FluentValidation;
using FluentValidation.Attributes;

#pragma warning disable RECS0014 // If all fields, properties and methods members are static, the class can be made static.

namespace Squidex.CLI.Commands
{
public sealed partial class App
Expand Down
11 changes: 7 additions & 4 deletions cli/Squidex.CLI/Squidex.CLI/Commands/App_Sync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task Out(OutArguments arguments)
[Command(Name = "in", Description = "Imports the app from a folder")]
public async Task In(InArguments arguments)
{
var session = configuration.StartSession();
var session = configuration.StartSession(arguments.Emulate);

await synchronizer.ImportAsync(arguments.Folder, arguments.ToOptions(), session);

Expand Down Expand Up @@ -95,12 +95,15 @@ public sealed class InArguments : IArgumentModel
[Option(ShortName = "t", LongName = "targets", Description = "The targets to sync, e.g. schemas, workflows, app, rules.")]
public string[] Targets { get; set; }

[Option(LongName = "nodelete", Description = "Use this flag to prevent deletions.")]
public bool NoDeletion { get; set; }
[Option(LongName = "delete", Description = "Use this flag to also delete entities.")]
public bool Delete { 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 { NoDeletion = NoDeletion, Targets = Targets };
return new SyncOptions { Delete = Delete, Targets = Targets };
}

public sealed class Validator : AbstractValidator<InArguments>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private async Task SynchronizeClientsAsync(AppModel model, SyncOptions options,
{
var current = await session.Apps.GetClientsAsync(session.App);

if (!options.NoDeletion)
if (options.Delete)
{
foreach (var client in current.Items)
{
Expand Down Expand Up @@ -180,7 +180,7 @@ private async Task SynchronizeLanguagesAsync(AppModel model, SyncOptions options
{
var current = await session.Apps.GetLanguagesAsync(session.App);

if (!options.NoDeletion)
if (options.Delete)
{
foreach (var language in current.Items)
{
Expand Down Expand Up @@ -235,7 +235,7 @@ private async Task SynchronizeRolesAsync(AppModel model, SyncOptions options, IS
{
var current = await session.Apps.GetRolesAsync(session.App);

if (!options.NoDeletion)
if (options.Delete)
{
foreach (var role in current.Items)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public async Task ImportAsync(DirectoryInfo directoryInfo, JsonHelper jsonHelper
GetFiles(directoryInfo)
.Select(x => (x, jsonHelper.Read<AssetsModel>(x, log)));

var tree = new FolderTree(session);

foreach (var (_, model) in models)
{
if (model?.Assets?.Count > 0)
Expand All @@ -102,7 +104,9 @@ public async Task ImportAsync(DirectoryInfo directoryInfo, JsonHelper jsonHelper

foreach (var asset in model.Assets)
{
request.Jobs.Add(asset.ToMoveJob());
var parentId = await tree.GetIdAsync(asset.FolderPath);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public static FileInfo GetBlobFile(this DirectoryInfo directoryInfo, string id)
return new FileInfo(Path.Combine(directoryInfo.FullName, $"assets/files/{id}.blob"));
}

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

Expand All @@ -36,8 +36,6 @@ public static BulkUpdateAssetsJobDto ToAnnotateJob(this AssetModel model)
Type = BulkUpdateAssetType.Annotate,
FileName = model.FileName,
IsProtected = model.IsProtected,
ParentId = null,
ParentPath = null,
Metadata = model.Metadata,
Slug = model.Slug,
Tags = model.Tags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ public async Task<string> GetIdAsync(string path)

foreach (var name in names)
{
if (current.Children.TryGetValue(name, out current))
if (current.Children.TryGetValue(name, out var child))
{
current = child;
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public sealed class UploadPipeline

public UploadPipeline(ISession session, ILogger log, DirectoryInfo directoryInfo)
{
var tree = new FolderTree(session);

pipeline = new ActionBlock<AssetModel>(async asset =>
{
var process = $"Uploading {asset.Id}";
Expand All @@ -32,7 +34,7 @@ public UploadPipeline(ISession session, ILogger log, DirectoryInfo directoryInfo
{
var file = new FileParameter(stream, asset.FileName, asset.MimeType);
await session.Assets.PostUpsertAssetAsync(session.App, asset.Id, null, file);
await session.Assets.PostUpsertAssetAsync(session.App, asset.Id, null, true, file);
}
log.ProcessCompleted(process);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Squidex.ClientLibrary;
using Squidex.ClientLibrary.Management;

namespace Squidex.CLI.Commands.Implementation.Sync.Rules
Expand Down Expand Up @@ -40,16 +41,10 @@ public async Task ExportAsync(DirectoryInfo directoryInfo, JsonHelper jsonHelper
{
var current = await session.Rules.GetRulesAsync();

var schemas = await session.Schemas.GetSchemasAsync(session.App);
var schemaMap = schemas.Items.ToDictionary(x => x.Id, x => x.Name);
await MapSchemaIdsToNamesAsync(session, current);

await current.Items.OrderBy(x => x.Created).Foreach(async (rule, i) =>
{
if (rule.Trigger is ContentChangedRuleTriggerDto contentTrigger)
{
MapSchemas(contentTrigger, schemaMap);
}
var ruleName = rule.Name;
if (string.IsNullOrWhiteSpace(ruleName))
Expand Down Expand Up @@ -87,7 +82,7 @@ public async Task ImportAsync(DirectoryInfo directoryInfo, JsonHelper jsonHelper

var rulesByName = current.Items.ToDictionary(x => x.Name);

if (!options.NoDeletion)
if (options.Delete)
{
foreach (var (name, rule) in rulesByName.ToList())
{
Expand All @@ -103,6 +98,8 @@ await log.DoSafeAsync($"Rule '{name}' deleting", async () =>
}
}

await MapSchemaNamesToIdsAsync(session, models);

foreach (var newRule in models)
{
if (rulesByName.ContainsKey(newRule.Name))
Expand All @@ -125,9 +122,6 @@ await log.DoSafeAsync($"Rule '{newRule.Name}' creating", async () =>
});
}

var schemas = await session.Schemas.GetSchemasAsync(session.App);
var schemaMap = schemas.Items.ToDictionary(x => x.Name, x => x.Id);

foreach (var newRule in models)
{
var rule = rulesByName.GetValueOrDefault(newRule.Name);
Expand All @@ -137,11 +131,6 @@ await log.DoSafeAsync($"Rule '{newRule.Name}' creating", async () =>
return;
}

if (newRule.Trigger is ContentChangedRuleTriggerDto contentTrigger)
{
MapSchemas(contentTrigger, schemaMap);
}

await log.DoVersionedAsync($"Rule '{newRule.Name}' updating", rule.Version, async () =>
{
var request = newRule.ToUpdate();
Expand Down Expand Up @@ -175,6 +164,36 @@ await log.DoVersionedAsync($"Rule '{newRule.Name}' disabling", rule.Version, asy
}
}

private async Task MapSchemaIdsToNamesAsync(ISession session, ExtendableRules current)
{
var schemas = await session.Schemas.GetSchemasAsync(session.App);

var map = schemas.Items.ToDictionary(x => x.Id, x => x.Name);

foreach (var rule in current.Items)
{
if (rule.Trigger is ContentChangedRuleTriggerDto contentTrigger)
{
MapSchemas(contentTrigger, map);
}
}
}

private async Task MapSchemaNamesToIdsAsync(ISession session, List<RuleModel> models)
{
var schemas = await session.Schemas.GetSchemasAsync(session.App);

var map = schemas.Items.ToDictionary(x => x.Name, x => x.Id);

foreach (var newRule in models)
{
if (newRule.Trigger is ContentChangedRuleTriggerDto contentTrigger)
{
MapSchemas(contentTrigger, map);
}
}
}

private void MapSchemas(ContentChangedRuleTriggerDto dto, Dictionary<string, string> schemaMap)
{
foreach (var schema in dto.Schemas)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public async Task ImportAsync(DirectoryInfo directoryInfo, JsonHelper jsonHelper

var schemasByName = current.Items.ToDictionary(x => x.Name);

if (!options.NoDeletion)
if (options.Delete)
{
foreach (var name in current.Items.Select(x => x.Name))
{
Expand Down Expand Up @@ -124,7 +124,7 @@ await log.DoSafeAsync($"Schema {model.Name} creating", async () =>

var version = schemasByName[model.Name].Version;

if (options.NoDeletion)
if (!options.Delete)
{
model.Schema.NoFieldDeletion = true;
model.Schema.NoFieldRecreation = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public sealed class SyncOptions

public string[] Languages { get; set; }

public bool NoDeletion { get; set; }
public bool Delete { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public async Task ExportAsync(string path, SyncOptions options, ISession session
var directoryInfo = Directory.CreateDirectory(path);

var selectedSynchronizers = GetSynchronizers(options.Targets);
var selectedCount = selectedSynchronizers.Count;

WriteSummary(directoryInfo, selectedSynchronizers);

Expand All @@ -49,14 +50,14 @@ await selectedSynchronizers.Foreach(async (synchronizer, step) =>
{
log.WriteLine();
log.WriteLine("--------------------------------------------------------");
log.WriteLine("* STEP {0} of {1}: Exporting {2} started", step, synchronizers.Count(), synchronizer.Name);
log.WriteLine("* STEP {0} of {1}: Exporting {2} started", step, selectedCount, synchronizer.Name);
log.WriteLine();
await synchronizer.CleanupAsync(directoryInfo);
await synchronizer.ExportAsync(directoryInfo, jsonHelper, options, session);
log.WriteLine();
log.WriteLine("* STEP {0} of {1}: Exporting {2} completed", step, synchronizers.Count(), synchronizer.Name);
log.WriteLine("* STEP {0} of {1}: Exporting {2} completed", step, selectedSynchronizers.Count, synchronizer.Name);
log.WriteLine("--------------------------------------------------------");
});
}
Expand All @@ -66,6 +67,7 @@ public async Task ImportAsync(string path, SyncOptions options, ISession session
var directoryInfo = new DirectoryInfo(path);

var selectedSynchronizers = GetSynchronizers(options.Targets);
var selectedCount = selectedSynchronizers.Count;

WriteSummary(directoryInfo, selectedSynchronizers);

Expand All @@ -75,13 +77,13 @@ await selectedSynchronizers.Foreach(async (synchronizer, step) =>
{
log.WriteLine();
log.WriteLine("--------------------------------------------------------");
log.WriteLine("* STEP {0} of {1}: Importing {2} started", step, synchronizers.Count(), synchronizer.Name);
log.WriteLine("* STEP {0} of {1}: Importing {2} started", step, selectedCount, synchronizer.Name);
log.WriteLine();
await synchronizer.ImportAsync(directoryInfo, jsonHelper, options, session);
log.WriteLine();
log.WriteLine("* STEP {0} of {1}: Importing {2} completed", step, synchronizers.Count(), synchronizer.Name);
log.WriteLine("* STEP {0} of {1}: Importing {2} completed", step, selectedCount, synchronizer.Name);
log.WriteLine("--------------------------------------------------------");
});
}
Expand All @@ -92,9 +94,11 @@ private void WriteSummary(DirectoryInfo directoryInfo, List<ISynchronizer> selec
log.WriteLine();
log.WriteLine("Executing the following steps");

var selectedCount = selectedSynchronizers.Count;

selectedSynchronizers.Foreach((synchronizer, step) =>
{
log.WriteLine("* STEP {0} of {1}: {2}", step, synchronizers.Count(), synchronizer.Name);
log.WriteLine("* STEP {0} of {1}: {2}", step, selectedCount, synchronizer.Name);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public async Task ImportAsync(DirectoryInfo directoryInfo, JsonHelper jsonHelper

var workflowsByName = current.Items.ToDictionary(x => x.Name);

if (options.NoDeletion)
if (options.Delete)
{
foreach (var (name, workflow) in workflowsByName.ToList())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static string Text(int maxCharacters, bool html)

if (sb.Length > 0)
{
nextWord.Append(" ");
nextWord.Append(' ');
}

nextWord.Append(word);
Expand Down Expand Up @@ -115,7 +115,7 @@ void Append(string value)

while (sb.Length < maxCharacters)
{
sb.Append(".");
sb.Append('.');
}

return sb.ToString();
Expand Down
Loading

0 comments on commit f08c0f9

Please sign in to comment.