Skip to content

Commit

Permalink
Also map strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed May 21, 2022
1 parent 5e023eb commit 471e711
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 46 deletions.
2 changes: 1 addition & 1 deletion cli/Squidex.CLI/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/Squidex/squidex/</PackageProjectUrl>
<PackageTags>Squidex HeadlessCMS</PackageTags>
<Version>8.14</Version>
<Version>8.15</Version>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public interface ISession

string ClientSecret { get; }

string Url { get; }

DirectoryInfo WorkingDirectory { get; }

IAppsClient Apps { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ public sealed class ContentsModel
{
[Required]
public List<ContentModel> Contents { get; set; }

public string? SourceApp { get; set; }

public string? SourceUrl { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ Task SaveAsync()
{
var model = new ContentsModel
{
Contents = contents
Contents = contents,
SourceApp = session.App,
SourceUrl = session.Url
};

return log.DoSafeAsync($"Exporting {schema.Name} ({contentBatch})", async () =>
Expand Down Expand Up @@ -113,16 +115,13 @@ public async Task ImportAsync(ISyncService sync, SyncOptions options, ISession s
var schemas = await session.Schemas.GetSchemasAsync(session.App);
var schemaMap = schemas.Items.ToDictionary(x => x.Name, x => x.Id);

var mapper = new Extensions.Mapper(session.Url, session.App, options.Languages);

foreach (var (file, model) in models)
{
if (model?.Contents?.Count > 0)
{
model.Clear(options.Languages);

foreach (var content in model.Contents)
{
content.MapComponents(schemaMap);
}
mapper.Map(model);

var client = session.Contents(model.Contents[0].Schema);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,161 @@ public static void MapComponents(this DynamicContent content, Dictionary<string,
MapComponents(content.Data, map);
}

public static void MapComponents(this ContentModel content, Dictionary<string, string> map)
public static void MapComponents(this ContentModel model, Dictionary<string, string> map)
{
MapComponents(content.Data, map);
MapComponents(model.Data, map);
}

public sealed class Mapper
{
private readonly string url;
private readonly string pathToAssets;
private readonly string pathToContent;
private readonly HashSet<string>? languages;
private string? sourceUrl;
private string? sourceApp;
private string? sourcePathToAssets;
private string? sourcePathToContent;

public Mapper(string url, string app, string[]? languages)
{
this.url = url.TrimEnd('/');

pathToAssets = $"api/assets/{app}/";
pathToContent = $"api/content/{app}/";

this.languages = languages?.ToHashSet();
}

public void Map(ContentsModel model)
{
if (model.Contents == null || model.Contents.Count == 0)
{
return;
}

sourceUrl = model.SourceUrl?.TrimEnd('/');

if (model.SourceApp != sourceApp)
{
sourceApp = model.SourceApp;

if (sourceApp != null)
{
sourcePathToAssets = $"api/assets/{sourceApp}/";
sourcePathToContent = $"api/content/{sourceApp}/";
}
}

foreach (var content in model.Contents)
{
ClearLanguages(content);

MapStrings(content);
}
}

private void MapStrings(ContentModel content)
{
if (string.IsNullOrWhiteSpace(sourceUrl))
{
return;
}

string? ReplaceUrl(string? value)
{
if (string.IsNullOrWhiteSpace(value))
{
return value;
}

if (sourceUrl != null)
{
value = value?.Replace(sourceUrl, url, StringComparison.OrdinalIgnoreCase);
}

if (sourcePathToAssets != null)
{
value = value?.Replace(sourcePathToAssets, pathToAssets, StringComparison.OrdinalIgnoreCase);
}

if (sourcePathToContent != null)
{
value = value?.Replace(sourcePathToContent, pathToContent, StringComparison.OrdinalIgnoreCase);
}

return value;
}

void Map(JToken? value)
{
switch (value)
{
case JArray array:
{
for (var i = 0; i < array.Count; i++)
{
var item = array[i];

if (item.Type == JTokenType.String)
{
array[i] = ReplaceUrl(item?.ToString());
}
else
{
Map(item);
}
}

break;
}

case IDictionary<string, JToken?> obj:
{
foreach (var (key, item) in obj.ToList())
{
if (item?.Type == JTokenType.String)
{
obj[key] = ReplaceUrl(item?.ToString());
}
else
{
Map(item);
}
}

break;
}
}
}

foreach (var item in content.Data.Values)
{
Map(item);
}
}

private void ClearLanguages(ContentModel model)
{
if (languages == null || languages.Count > 0)
{
return;
}

foreach (var field in model.Data.Values.OfType<JObject>())
{
foreach (var property in field.Properties().ToList())
{
if (property.Name != "iv" && !languages.Contains(property.Name))
{
field.Remove(property.Name);
}
}
}
}
}

private static void MapComponents(this DynamicData data, Dictionary<string, string> map)
public static void MapComponents(this DynamicData data, Dictionary<string, string> map)
{
static void Map(JToken token, Dictionary<string, string> map)
{
Expand Down Expand Up @@ -85,39 +234,5 @@ static void Map(JToken token, Dictionary<string, string> map)
Map(field, map);
}
}

public static void Clear(this ContentsModel model, string[] languages)
{
if (languages?.Length > 0 && model.Contents?.Count > 0)
{
var allowedLanguages = languages.ToHashSet();

var toClear = new List<string>();

foreach (var content in model.Contents)
{
foreach (var field in content.Data.Values.OfType<JObject>())
{
foreach (var language in field.Children<JProperty>().Select(x => x.Name))
{
if (language != "iv" && !allowedLanguages.Contains(language))
{
toClear.Add(language);
}
}

if (toClear.Count > 0)
{
foreach (var language in toClear)
{
field.Remove(language);
}

toClear.Clear();
}
}
}
}
}
}
}
5 changes: 5 additions & 0 deletions cli/Squidex.CLI/Squidex.CLI.Core/Configuration/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public string ClientSecret
get => clientManager.Options.ClientSecret;
}

public string Url
{
get => clientManager.Options.Url;
}

public Session(string app, DirectoryInfo workingDirectory, SquidexClientManager clientManager)
{
App = app;
Expand Down
1 change: 0 additions & 1 deletion cli/Squidex.CLI/Squidex.CLI/Commands/App_Assets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using HeyRed.Mime;
using Squidex.CLI.Commands.Implementation;
using Squidex.CLI.Commands.Implementation.FileSystem;
using Squidex.CLI.Commands.Implementation.Sync;
using Squidex.CLI.Commands.Implementation.Sync.Assets;
using Squidex.CLI.Configuration;
using Squidex.ClientLibrary;
Expand Down

0 comments on commit 471e711

Please sign in to comment.