Skip to content

Commit

Permalink
Fix: Path for master templates is set wrong on import. (#716)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinJump authored Feb 14, 2025
1 parent 96b6b7c commit 47fe34d
Showing 1 changed file with 69 additions and 77 deletions.
146 changes: 69 additions & 77 deletions uSync.Core/Serialization/Serializers/TemplateSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,64 +61,50 @@ protected override async Task<SyncAttempt<ITemplate>> ProcessDeleteAsync(Guid ke
return SyncAttempt<ITemplate>.Succeed(alias, ChangeType.Hidden);
}

protected override async Task<SyncAttempt<ITemplate>> DeserializeCoreAsync(XElement node, SyncSerializerOptions options)
private async Task<ITemplate?> FindTemplateFromNodeAsync(XElement node)
{
var key = node.GetKey();
var alias = node.GetAlias();

var name = node.Element("Name").ValueOrDefault(string.Empty);
var item = default(ITemplate);

var details = new List<uSyncChange>();

if (key != Guid.Empty)
item = await FindItemAsync(key);
item = await FindItemAsync(key);

return item ?? await FindItemAsync(alias);

item ??= await FindItemAsync(alias);
}

if (item == null)
{
item = new Template(_shortStringHelper, name, alias);
details.AddNew(alias, alias, "Template");
protected override async Task<SyncAttempt<ITemplate>> DeserializeCoreAsync(XElement node, SyncSerializerOptions options)
{
var key = node.GetKey();
var alias = node.GetAlias();
var name = node.Element("Name").ValueOrDefault(string.Empty);

if (ShouldGetContentFromNode(node, options))
{
logger.LogDebug("Getting content for Template from XML");
item.Content = GetContentFromConfig(node);
}
else
{
logger.LogDebug("Loading template content from disk");
var contentAttempt = GetContentForTemplate(node, options);
if (!contentAttempt) return SyncAttempt<ITemplate>.Fail(name, ChangeType.Import, contentAttempt.Exception?.Message ?? "Failed to get content");

var templatePath = ViewPath(alias);
if (templatePath is not null && _viewFileSystem?.FileExists(templatePath) is true)
{
logger.LogDebug("Reading {path} contents", templatePath);
item.Content = GetContentFromFile(templatePath);
item.Path = templatePath;
}
else
{
if (!ViewsAreCompiled(options))
{
// template is missing
// we can't create
logger.LogWarning("Failed to create template {path} the local file is missing", templatePath);
return SyncAttempt<ITemplate>.Fail(name, ChangeType.Import, $"The template {templatePath} file is missing.");
}
else
{
// template is not on disk, we could use the viewEngine to find the view
// if this finds the view it tells us that the view is somewhere else ?
var details = new List<uSyncChange>();

logger.LogDebug("Failed to find content, but UsingRazorViews so will create anyway, then delete the file");
item.Content = $"<!-- [uSyncMarker:{this.Id}] template content - will be removed -->";
}
}
}
var item = await FindTemplateFromNodeAsync(node);
if (item is null)
{
var userKey = await _userIdKeyResolver.GetAsync(options.UserId);
var attempt = await _templateService.CreateAsync(
name,
alias,
contentAttempt.Result,
userKey, key);

if (attempt.Success is false)
return SyncAttempt<ITemplate>.Fail(name, ChangeType.Import, "Failed to create template");

item = attempt.Result;
details.AddNew(alias, alias, "Template");
logger.LogDebug("New Template: {alias} {path}", item.Alias, item.Path);
}

if (item == null)
if (item is null)
{
// creating went wrong
logger.LogWarning("Failed to create template");
Expand Down Expand Up @@ -153,18 +139,45 @@ protected override async Task<SyncAttempt<ITemplate>> DeserializeCoreAsync(XElem
}
}

//var master = node.Element("Parent").ValueOrDefault(string.Empty);
//if (master != string.Empty)
//{
// var masterItem = fileService.GetTemplate(master);
// if (masterItem != null)
// item.SetMasterTemplate(masterItem);
//}
return SyncAttempt<ITemplate>.Succeed(item.Name, item, ChangeType.Import, details);
}

// Deserialize now takes care of the save.
// fileService.SaveTemplate(item);
private Attempt<string?> GetContentForTemplate(XElement node, SyncSerializerOptions options)
{
if (ShouldGetContentFromNode(node, options))
{
logger.LogDebug("Getting content for Template from XML");
return Attempt.Succeed(GetContentFromConfig(node));
}
else
{
logger.LogDebug("Loading template content from disk");

return SyncAttempt<ITemplate>.Succeed(item.Name, item, ChangeType.Import, details);
var templatePath = ViewPath(node.GetAlias());
if (templatePath is not null && _viewFileSystem?.FileExists(templatePath) is true)
{
logger.LogDebug("Reading {path} contents", templatePath);
return Attempt.Succeed(GetContentFromFile(templatePath));
}
else
{
if (!ViewsAreCompiled(options))
{
// template is missing
// we can't create
logger.LogWarning("Failed to create template {path} the local file is missing", templatePath);
return Attempt.Fail("", new Exception($"The template {templatePath} file is missing."));
}
else
{
// template is not on disk, we could use the viewEngine to find the view
// if this finds the view it tells us that the view is somewhere else ?

logger.LogDebug("Failed to find content, but UsingRazorViews so will create anyway, then delete the file");
return Attempt.Succeed($"<!-- [uSyncMarker:{this.Id}] template content - will be removed -->");
}
}
}
}

/// <summary>
Expand Down Expand Up @@ -230,23 +243,6 @@ public override async Task<SyncAttempt<ITemplate>> DeserializeSecondPassAsync(IT
var details = new List<uSyncChange>();
var saved = false;

var master = node.Element("Parent").ValueOrDefault(string.Empty);
if (master != string.Empty && item.MasterTemplateAlias != master)
{
logger.LogDebug("Looking for master {master}", master);
var masterItem = await FindItemAsync(master);
if (masterItem != null && item.MasterTemplateAlias != master)
{
details.AddUpdate("Parent", item.MasterTemplateAlias ?? string.Empty, master);

logger.LogDebug("Setting Master {alias}", masterItem.Alias);
// item.SetMasterTemplate(masterItem);

await SaveItemAsync(item);
saved = true;
}
}

if (ViewsAreCompiled(options))
{
// using razor views - we delete the template file at the end (because its in a razor view).
Expand All @@ -257,7 +253,7 @@ public override async Task<SyncAttempt<ITemplate>> DeserializeSecondPassAsync(IT

if (System.IO.File.Exists(fullPath))
{
var content = System.IO.File.ReadAllText(fullPath);
var content = await System.IO.File.ReadAllTextAsync(fullPath);
if (content.Contains($"[uSyncMarker:{this.Id}]"))
{
logger.LogDebug("Removing the file from disk, because it exists in a razor view {templatePath}", templatePath);
Expand Down Expand Up @@ -329,7 +325,7 @@ public override async Task SaveItemAsync(ITemplate item)
item.UpdateDate = existing.UpdateDate;
}

logger.LogDebug("Save Template {name} {alias} [{contentLength}] {userKey} {key}", item.Name, item.Alias, item.Content?.Length ?? 0, userKey, item.Key);
logger.LogDebug("Saving: {alias} {path}", item.Alias, item.Path);

if (existing is null)
{
Expand All @@ -341,10 +337,6 @@ public override async Task SaveItemAsync(ITemplate item)
var result = await _templateService.UpdateAsync(item, userKey);
logger.LogDebug("Update Template Result: [{key}] {result} {status}", item.Key, result.Success, result.Status);
}

var templates = await _templateService.GetAllAsync();
logger.LogDebug("[Templates]: {count} {names}",
templates.Count(), string.Join(",", templates.Select(x => $"{x.Alias}-{x.Key}")));
}

public override async Task SaveAsync(IEnumerable<ITemplate> items)
Expand Down

0 comments on commit 47fe34d

Please sign in to comment.