Skip to content

Commit

Permalink
Describe exports with sync describe
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Feb 28, 2022
1 parent 5ca5f15 commit 1c8547e
Show file tree
Hide file tree
Showing 16 changed files with 451 additions and 5 deletions.
32 changes: 32 additions & 0 deletions cli/Squidex.CLI/Squidex.CLI/Commands/App_Sync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public async Task Out(OutArguments arguments)

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

if (arguments.Describe)
{
await synchronizer.Describe(arguments.Folder, session);
}

log.WriteLine("> Synchronization completed.");
}

Expand All @@ -61,6 +66,16 @@ public async Task In(InArguments arguments)
log.WriteLine("> Synchronization completed.");
}

[Command("describe", Description = "Describe the synced folder.")]
public async Task Describe(DescribeArguments arguments)
{
var session = configuration.StartSession(arguments.App, false);

await synchronizer.Describe(arguments.Folder, session);

log.WriteLine("> Describing completed.");
}

[Command("targets", Description = "List all targets")]
public void Targets()
{
Expand Down Expand Up @@ -139,6 +154,9 @@ public sealed class OutArguments : AppArguments
[Option('t', "targets", Description = "The targets to sync, e.g. schemas, workflows, app, rules, contents.")]
public string[] Targets { get; set; }

[Option("describe", Description = "Create a README.md file.")]
public bool Describe { get; set; }

public SyncOptions ToOptions()
{
return new SyncOptions { Targets = Targets };
Expand All @@ -152,6 +170,20 @@ public Validator()
}
}
}

public sealed class DescribeArguments : AppArguments
{
[Operand("folder", Description = "The target folder to describe.")]
public string Folder { get; set; }

public sealed class Validator : AbstractValidator<DescribeArguments>
{
public Validator()
{
RuleFor(x => x.Folder).NotEmpty();
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,56 @@ await log.DoSafeAsync("Exporting Roles", async () =>
await sync.WriteWithSchema(new FilePath("app.json"), model, Ref);
}

public Task DescribeAsync(ISyncService sync, MarkdownWriter writer)
{
var appFile = sync.FileSystem.GetFile(new FilePath("app.json"));

if (!appFile.Exists)
{
return Task.CompletedTask;
}

var model = sync.Read<AppModel>(appFile, log);

if (model.Clients.Count > 0)
{
var rows = model.Clients.Select(x => new object[] { x.Key, x.Value.Name, x.Value.Role }).OrderBy(x => x[0]).ToArray();

writer.H3("Clients");
writer.Paragraph($"{rows.Length} client(s).");
writer.Table(new[] { "Name", "Label", "Role" }, rows);
}

if (model.Roles.Count > 0)
{
var rows = model.Roles.Select(x => new object[] { x.Key, x.Value.Permissions.Count }).OrderBy(x => x[0]).ToArray();

writer.H3("Roles");
writer.Paragraph($"{rows.Length} role(s).");
writer.Table(new[] { "Name", "Permissions" }, rows);
}

if (model.Contributors.Count > 0)
{
var rows = model.Contributors.Select(x => new object[] { x.Key, x.Value.Role }).OrderBy(x => x[0]).ToArray();

writer.H3("Contributors");
writer.Paragraph($"{rows.Length} contributor(s).");
writer.Table(new[] { "Id", "Role" }, rows);
}

if (model.Languages.Count > 0)
{
var rows = model.Languages.Select(x => new object[] { x.Key, x.Value.IsMaster == true ? "y" : "n" }).OrderBy(x => x[0]).ToArray();

writer.H3("Languages");
writer.Paragraph($"{rows.Length} language(s).");
writer.Table(new[] { "Code", "Master" }, rows);
}

return Task.CompletedTask;
}

public async Task ImportAsync(ISyncService sync, SyncOptions options, ISession session)
{
var appFile = sync.FileSystem.GetFile(new FilePath("app.json"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ await log.DoSafeAsync("Exporting folders", async () =>
await sync.WriteWithSchema(new FilePath("assetFolders/assetFolders.json"), model, Ref);
}

public Task DescribeAsync(ISyncService sync, MarkdownWriter writer)
{
var models =
GetFiles(sync.FileSystem)
.Select(x => sync.Read<AssetFoldersModel>(x, log))
.ToList();

writer.Paragraph($"{models.SelectMany(x => x.Paths).Distinct().Count()} asset folder(s).");

return Task.CompletedTask;
}

public async Task ImportAsync(ISyncService sync, SyncOptions options, ISession session)
{
var models =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ await session.Assets.GetAllAsync(session.App, async asset =>
}
}

public Task DescribeAsync(ISyncService sync, MarkdownWriter writer)
{
var models =
GetFiles(sync.FileSystem)
.Select(x => (x, sync.Read<AssetsModel>(x, log)));

writer.Paragraph($"{models.SelectMany(x => x.Item2.Assets).Count()} asset(s).");

return Task.CompletedTask;
}

public async Task ImportAsync(ISyncService sync, SyncOptions options, ISession session)
{
var models =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,28 @@ await client.GetAllAsync(async content =>
}
}

public Task DescribeAsync(ISyncService sync, MarkdownWriter writer)
{
var models =
GetFiles(sync.FileSystem)
.Select(x => (x, sync.Read<ContentsModel>(x, log)));

writer.Paragraph($"{models.SelectMany(x => x.Item2.Contents).Count()} content(s).");

var rows =
models
.SelectMany(x => x.Item2.Contents).GroupBy(x => x.Schema)
.Select(x => new object[] { x.Key, x.Count() }).OrderBy(x => x[0])
.ToArray();

if (rows.Length > 0)
{
writer.Table(new[] { "Schema", "Counts" }, rows);
}

return Task.CompletedTask;
}

public async Task ImportAsync(ISyncService sync, SyncOptions options, ISession session)
{
var models =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public interface ISynchronizer

Task CleanupAsync(IFileSystem fs);

Task DescribeAsync(ISyncService sync, MarkdownWriter writer);

Task ExportAsync(ISyncService sync, SyncOptions options, ISession session);

Task ImportAsync(ISyncService sync, SyncOptions options, ISession session);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using System.Globalization;

namespace Squidex.CLI.Commands.Implementation.Sync
{
public sealed class MarkdownWriter
{
private readonly TextWriter writer;

public MarkdownWriter(TextWriter writer)
{
this.writer = writer;
}

public MarkdownWriter H1(string text)
{
writer.Write("# ");
writer.WriteLine(text);
writer.WriteLine();

return this;
}

public MarkdownWriter H2(string text)
{
writer.Write("## ");
writer.WriteLine(text);
writer.WriteLine();

return this;
}

public MarkdownWriter H3(string text)
{
writer.Write("### ");
writer.WriteLine(text);
writer.WriteLine();

return this;
}

public MarkdownWriter Paragraph(string text)
{
writer.WriteLine(text);
writer.WriteLine();

return this;
}

public MarkdownWriter OrderedList(params string[] lines)
{
if (lines.Length == 0)
{
return this;
}

var i = 1;

foreach (var line in lines)
{
writer.Write(' ');
writer.Write(i);
writer.Write(". ");
writer.WriteLine(line);
i++;
}

writer.WriteLine();

return this;
}

public MarkdownWriter BulletPoints(params string[] lines)
{
if (lines.Length == 0)
{
return this;
}

foreach (var line in lines)
{
writer.Write(" * ");
writer.WriteLine(line);
}

writer.WriteLine();

return this;
}

public MarkdownWriter Code(params string[] lines)
{
if (lines.Length == 0)
{
return this;
}

writer.WriteLine("```");

foreach (var line in lines)
{
writer.WriteLine(line);
}

writer.WriteLine("```");
writer.WriteLine();

return this;
}

public MarkdownWriter Table(object[] header, object[][] rows)
{
var allRows = Enumerable.Repeat(header, 1).Union(rows);

var rowTexts = allRows.Select(x => x.Select(y => Convert.ToString(y, CultureInfo.InvariantCulture) ?? string.Empty).ToArray()).ToArray();

var columnsCount = allRows.Max(x => x.Length);
var columnsWidth = new int[columnsCount];

foreach (var row in rowTexts)
{
for (var i = 0; i < row.Length; i++)
{
columnsWidth[i] = Math.Max(columnsWidth[i], row[i].Length);
}
}

void WriteSeparator()
{
writer.Write('|');

foreach (var width in columnsWidth)
{
writer.Write(' ');

for (var i = 0; i < width; i++)
{
writer.Write('-');
}

writer.Write(' ');
writer.Write("|");
}

writer.WriteLine();
}

void WriteLine(string[] line)
{
writer.Write('|');

for (var column = 0; column < columnsCount; column++)
{
var text = column < line.Length ? line[column] : string.Empty;

writer.Write(' ');
writer.Write(text);

var width = columnsWidth[column];

for (var i = text.Length; i < width; i++)
{
writer.Write(' ');
}

writer.Write(' ');
writer.Write("|");
}

writer.WriteLine();
}

WriteLine(rowTexts[0]);
WriteSeparator();

foreach (var row in rowTexts.Skip(1))
{
WriteLine(row);
}

writer.WriteLine();

return this;
}
}
}
Loading

0 comments on commit 1c8547e

Please sign in to comment.