Skip to content

Commit

Permalink
flatten the get content logic in templates. (#717)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinJump authored Feb 14, 2025
1 parent 47fe34d commit e47d2e9
Showing 1 changed file with 18 additions and 28 deletions.
46 changes: 18 additions & 28 deletions uSync.Core/Serialization/Serializers/TemplateSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ protected override async Task<SyncAttempt<ITemplate>> ProcessDeleteAsync(Guid ke
var item = default(ITemplate);

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

return item ?? await FindItemAsync(alias);

}
Expand Down Expand Up @@ -149,35 +149,25 @@ protected override async Task<SyncAttempt<ITemplate>> DeserializeCoreAsync(XElem
logger.LogDebug("Getting content for Template from XML");
return Attempt.Succeed(GetContentFromConfig(node));
}
else
{
logger.LogDebug("Loading template content from disk");

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 ?
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));
}

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 -->");
}
}
if (!ViewsAreCompiled(options))
{
// template is missing and the views are not compiled , then 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."));
}
// 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

0 comments on commit e47d2e9

Please sign in to comment.