-
Notifications
You must be signed in to change notification settings - Fork 211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
VP-1558: Merge settings between base and current themes #383
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,7 +94,12 @@ public ShopifyLiquidThemeEngine(IStorefrontMemoryCache memoryCache, IWorkContext | |
private string CurrentThemePath => Path.Combine("Themes", WorkContext.CurrentStore.Id, CurrentThemeName); | ||
|
||
//Relative path to the discovery of theme resources that weren't found by the current path. | ||
private string BaseThemePath => !string.IsNullOrEmpty(_options.BaseThemeName) ? Path.Combine("Themes", _options.BaseThemeName, "default") : null; | ||
private string BaseThemePath => | ||
!string.IsNullOrEmpty(_options.BaseThemePath) ? Path.Combine("Themes", _options.BaseThemePath) : | ||
#pragma warning disable 618 | ||
// We need to use obsolete value here for backward compatibility. | ||
!string.IsNullOrEmpty(_options.BaseThemeName) ? Path.Combine("Themes", _options.BaseThemeName, "default") : null; | ||
#pragma warning restore 618 | ||
private string BaseThemeSettingPath => BaseThemePath != null ? Path.Combine(BaseThemePath, "config", "settings_data.json") : null; | ||
public string BaseThemeLocalePath => BaseThemePath != null ? Path.Combine(BaseThemePath, "locales") : null; | ||
|
||
|
@@ -347,44 +352,28 @@ public ValueTask<string> RenderTemplateAsync(string templateContent, string temp | |
public IDictionary<string, object> GetSettings(string defaultValue = null) | ||
{ | ||
var cacheKey = CacheKey.With(GetType(), "GetSettings", CurrentThemeSettingPath, defaultValue); | ||
return _memoryCache.GetOrCreateExclusive(cacheKey, (cacheItem) => | ||
return _memoryCache.GetOrCreateExclusive(cacheKey, cacheItem => | ||
{ | ||
cacheItem.AddExpirationToken(new CompositeChangeToken(new[] { ThemeEngineCacheRegion.CreateChangeToken(), _themeBlobProvider.Watch(CurrentThemeSettingPath) })); | ||
var retVal = new Dictionary<string, object>().WithDefaultValue(defaultValue); | ||
//Load all data from current theme config | ||
var resultSettings = InnerGetAllSettings(_themeBlobProvider, CurrentThemeSettingPath); | ||
if (resultSettings == null && BaseThemeSettingPath != null) | ||
|
||
JObject result; | ||
var baseThemeSettings = new JObject(); | ||
var currentThemeSettings = result = GetCurrentSettingsPreset(InnerGetAllSettings(_themeBlobProvider, CurrentThemeSettingPath)); | ||
|
||
//Try to load settings from base theme path and merge them with resources for local theme | ||
if ((_options.MergeBaseSettings || currentThemeSettings == null) && !string.IsNullOrEmpty(BaseThemeSettingPath)) | ||
{ | ||
resultSettings = InnerGetAllSettings(_themeBlobProvider, BaseThemeSettingPath); | ||
cacheItem.AddExpirationToken(new CompositeChangeToken(new[] { ThemeEngineCacheRegion.CreateChangeToken(), _themeBlobProvider.Watch(BaseThemeSettingPath) })); | ||
result = baseThemeSettings = GetCurrentSettingsPreset(InnerGetAllSettings(_themeBlobProvider, BaseThemeSettingPath)); | ||
} | ||
if (resultSettings != null) | ||
{ | ||
//Get actual preset from merged config | ||
var currentPreset = resultSettings.GetValue("current"); | ||
if (currentPreset is JValue) | ||
{ | ||
var currentPresetName = ((JValue)currentPreset).Value.ToString(); | ||
if (!(resultSettings.GetValue("presets") is JObject presets) || !presets.Children().Any()) | ||
{ | ||
throw new StorefrontException("Setting presets not defined"); | ||
} | ||
|
||
IList<JProperty> allPresets = presets.Children().Cast<JProperty>().ToList(); | ||
resultSettings = allPresets.FirstOrDefault(p => p.Name == currentPresetName).Value as JObject; | ||
if (resultSettings == null) | ||
{ | ||
throw new StorefrontException($"Setting preset with name '{currentPresetName}' not found"); | ||
} | ||
} | ||
if (currentPreset is JObject) | ||
{ | ||
resultSettings = (JObject)currentPreset; | ||
} | ||
|
||
retVal = resultSettings.ToObject<Dictionary<string, object>>().ToDictionary(x => x.Key, x => x.Value).WithDefaultValue(defaultValue); | ||
if (_options.MergeBaseSettings) | ||
{ | ||
result = baseThemeSettings; | ||
result.Merge(currentThemeSettings ?? new JObject(), new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Merge }); | ||
} | ||
|
||
return retVal; | ||
return result.ToObject<Dictionary<string, object>>().ToDictionary(x => x.Key, x => x.Value).WithDefaultValue(defaultValue); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}); | ||
} | ||
|
||
|
@@ -475,16 +464,47 @@ private static JObject InnerGetAllSettings(IContentBlobProvider themeBlobProvide | |
throw new ArgumentNullException(nameof(settingsPath)); | ||
} | ||
|
||
JObject retVal = null; | ||
var result = new JObject(); | ||
|
||
if (themeBlobProvider.PathExists(settingsPath)) | ||
{ | ||
using (var stream = themeBlobProvider.OpenRead(settingsPath)) | ||
{ | ||
retVal = JsonConvert.DeserializeObject<JObject>(stream.ReadToString()); | ||
result = JsonConvert.DeserializeObject<JObject>(stream.ReadToString()); | ||
} | ||
} | ||
return retVal; | ||
return result; | ||
} | ||
|
||
/// <summary> | ||
/// Get actual preset from config | ||
/// </summary> | ||
/// <returns></returns> | ||
private static JObject GetCurrentSettingsPreset(JObject allSettings) | ||
{ | ||
var result = allSettings; | ||
var currentPreset = allSettings.GetValue("current"); | ||
if (currentPreset is JValue currentPresetValue) | ||
{ | ||
var currentPresetName = currentPresetValue.Value.ToString(); | ||
if (!(allSettings.GetValue("presets") is JObject presets) || !presets.Children().Any()) | ||
{ | ||
throw new StorefrontException("Setting presets not defined"); | ||
} | ||
|
||
IList<JProperty> allPresets = presets.Children().Cast<JProperty>().ToList(); | ||
result = allPresets.FirstOrDefault(p => p.Name == currentPresetName)?.Value as JObject; | ||
if (result == null) | ||
{ | ||
throw new StorefrontException($"Setting preset with name '{currentPresetName}' not found"); | ||
} | ||
} | ||
if (currentPreset is JObject preset) | ||
{ | ||
result = preset; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private string ReadTemplateByPath(string templatePath) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extract this nested ternary operation into an independent statement.