2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-12 09:52:52 -05:00
|
|
|
class ThemeField < ActiveRecord::Base
|
FEATURE: Theme settings migrations (#24071)
This commit introduces a new feature that allows theme developers to manage the transformation of theme settings over time. Similar to Rails migrations, the theme settings migration system enables developers to write and execute migrations for theme settings, ensuring a smooth transition when changes are required in the format or structure of setting values.
Example use cases for the theme settings migration system:
1. Renaming a theme setting.
2. Changing the data type of a theme setting (e.g., transforming a string setting containing comma-separated values into a proper list setting).
3. Altering the format of data stored in a theme setting.
All of these use cases and more are now possible while preserving theme setting values for sites that have already modified their theme settings.
Usage:
1. Create a top-level directory called `migrations` in your theme/component, and then within the `migrations` directory create another directory called `settings`.
2. Inside the `migrations/settings` directory, create a JavaScript file using the format `XXXX-some-name.js`, where `XXXX` is a unique 4-digit number, and `some-name` is a descriptor of your choice that describes the migration.
3. Within the JavaScript file, define and export (as the default) a function called `migrate`. This function will receive a `Map` object and must also return a `Map` object (it's acceptable to return the same `Map` object that the function received).
4. The `Map` object received by the `migrate` function will include settings that have been overridden or changed by site administrators. Settings that have never been changed from the default will not be included.
5. The keys and values contained in the `Map` object that the `migrate` function returns will replace all the currently changed settings of the theme.
6. Migrations are executed in numerical order based on the XXXX segment in the migration filenames. For instance, `0001-some-migration.js` will be executed before `0002-another-migration.js`.
Here's a complete example migration script that renames a setting from `setting_with_old_name` to `setting_with_new_name`:
```js
// File name: 0001-rename-setting.js
export default function migrate(settings) {
if (settings.has("setting_with_old_name")) {
settings.set("setting_with_new_name", settings.get("setting_with_old_name"));
}
return settings;
}
```
Internal topic: t/109980
2023-11-02 00:10:15 -05:00
|
|
|
MIGRATION_NAME_PART_MAX_LENGTH = 150
|
|
|
|
|
2024-02-16 05:16:54 -06:00
|
|
|
# This string is not 'secret'. It's just randomized to avoid accidental clashes with genuine theme field content.
|
|
|
|
CSP_NONCE_PLACEHOLDER = "__CSP__NONCE__PLACEHOLDER__f72bff1b1768168a34ee092ce759f192__"
|
|
|
|
|
2017-05-08 10:38:48 -05:00
|
|
|
belongs_to :upload
|
2018-10-14 23:55:23 -05:00
|
|
|
has_one :javascript_cache, dependent: :destroy
|
2022-06-08 18:24:30 -05:00
|
|
|
has_one :upload_reference, as: :target, dependent: :destroy
|
FEATURE: Theme settings migrations (#24071)
This commit introduces a new feature that allows theme developers to manage the transformation of theme settings over time. Similar to Rails migrations, the theme settings migration system enables developers to write and execute migrations for theme settings, ensuring a smooth transition when changes are required in the format or structure of setting values.
Example use cases for the theme settings migration system:
1. Renaming a theme setting.
2. Changing the data type of a theme setting (e.g., transforming a string setting containing comma-separated values into a proper list setting).
3. Altering the format of data stored in a theme setting.
All of these use cases and more are now possible while preserving theme setting values for sites that have already modified their theme settings.
Usage:
1. Create a top-level directory called `migrations` in your theme/component, and then within the `migrations` directory create another directory called `settings`.
2. Inside the `migrations/settings` directory, create a JavaScript file using the format `XXXX-some-name.js`, where `XXXX` is a unique 4-digit number, and `some-name` is a descriptor of your choice that describes the migration.
3. Within the JavaScript file, define and export (as the default) a function called `migrate`. This function will receive a `Map` object and must also return a `Map` object (it's acceptable to return the same `Map` object that the function received).
4. The `Map` object received by the `migrate` function will include settings that have been overridden or changed by site administrators. Settings that have never been changed from the default will not be included.
5. The keys and values contained in the `Map` object that the `migrate` function returns will replace all the currently changed settings of the theme.
6. Migrations are executed in numerical order based on the XXXX segment in the migration filenames. For instance, `0001-some-migration.js` will be executed before `0002-another-migration.js`.
Here's a complete example migration script that renames a setting from `setting_with_old_name` to `setting_with_new_name`:
```js
// File name: 0001-rename-setting.js
export default function migrate(settings) {
if (settings.has("setting_with_old_name")) {
settings.set("setting_with_new_name", settings.get("setting_with_old_name"));
}
return settings;
}
```
Internal topic: t/109980
2023-11-02 00:10:15 -05:00
|
|
|
has_one :theme_settings_migration
|
2022-06-08 18:24:30 -05:00
|
|
|
|
2023-06-13 12:07:47 -05:00
|
|
|
validates :value, { length: { maximum: 1024**2 } }
|
|
|
|
|
FEATURE: Theme settings migrations (#24071)
This commit introduces a new feature that allows theme developers to manage the transformation of theme settings over time. Similar to Rails migrations, the theme settings migration system enables developers to write and execute migrations for theme settings, ensuring a smooth transition when changes are required in the format or structure of setting values.
Example use cases for the theme settings migration system:
1. Renaming a theme setting.
2. Changing the data type of a theme setting (e.g., transforming a string setting containing comma-separated values into a proper list setting).
3. Altering the format of data stored in a theme setting.
All of these use cases and more are now possible while preserving theme setting values for sites that have already modified their theme settings.
Usage:
1. Create a top-level directory called `migrations` in your theme/component, and then within the `migrations` directory create another directory called `settings`.
2. Inside the `migrations/settings` directory, create a JavaScript file using the format `XXXX-some-name.js`, where `XXXX` is a unique 4-digit number, and `some-name` is a descriptor of your choice that describes the migration.
3. Within the JavaScript file, define and export (as the default) a function called `migrate`. This function will receive a `Map` object and must also return a `Map` object (it's acceptable to return the same `Map` object that the function received).
4. The `Map` object received by the `migrate` function will include settings that have been overridden or changed by site administrators. Settings that have never been changed from the default will not be included.
5. The keys and values contained in the `Map` object that the `migrate` function returns will replace all the currently changed settings of the theme.
6. Migrations are executed in numerical order based on the XXXX segment in the migration filenames. For instance, `0001-some-migration.js` will be executed before `0002-another-migration.js`.
Here's a complete example migration script that renames a setting from `setting_with_old_name` to `setting_with_new_name`:
```js
// File name: 0001-rename-setting.js
export default function migrate(settings) {
if (settings.has("setting_with_old_name")) {
settings.set("setting_with_new_name", settings.get("setting_with_old_name"));
}
return settings;
}
```
Internal topic: t/109980
2023-11-02 00:10:15 -05:00
|
|
|
validate :migration_filename_is_valid, if: :migration_field?
|
|
|
|
|
2022-06-08 18:24:30 -05:00
|
|
|
after_save do
|
|
|
|
if self.type_id == ThemeField.types[:theme_upload_var] && saved_change_to_upload_id?
|
|
|
|
UploadReference.ensure_exist!(upload_ids: [self.upload_id], target: self)
|
|
|
|
end
|
|
|
|
end
|
2017-05-08 10:38:48 -05:00
|
|
|
|
2018-08-07 23:46:34 -05:00
|
|
|
scope :find_by_theme_ids,
|
2023-11-28 23:38:07 -06:00
|
|
|
->(theme_ids) do
|
2024-05-27 05:27:13 -05:00
|
|
|
return none if theme_ids.blank?
|
2018-08-07 23:46:34 -05:00
|
|
|
|
|
|
|
where(theme_id: theme_ids).joins(
|
|
|
|
"JOIN (
|
2019-01-17 05:46:11 -06:00
|
|
|
SELECT #{theme_ids.map.with_index { |id, idx| "#{id.to_i} AS theme_id, #{idx} AS theme_sort_column" }.join(" UNION ALL SELECT ")}
|
2018-08-07 23:46:34 -05:00
|
|
|
) as X ON X.theme_id = theme_fields.theme_id",
|
2019-01-17 05:46:11 -06:00
|
|
|
).order("theme_sort_column")
|
2023-11-28 23:38:07 -06:00
|
|
|
end
|
2019-01-17 05:46:11 -06:00
|
|
|
|
2019-02-26 08:22:02 -06:00
|
|
|
scope :filter_locale_fields,
|
2023-11-28 23:38:07 -06:00
|
|
|
->(locale_codes) do
|
2024-05-27 05:27:13 -05:00
|
|
|
return none if locale_codes.blank?
|
2019-01-17 05:46:11 -06:00
|
|
|
|
2019-02-26 08:22:02 -06:00
|
|
|
where(target_id: Theme.targets[:translations], name: locale_codes).joins(
|
2020-12-10 17:56:26 -06:00
|
|
|
DB.sql_fragment(
|
2019-02-26 08:22:02 -06:00
|
|
|
"JOIN (
|
|
|
|
SELECT * FROM (VALUES #{locale_codes.map { "(?)" }.join(",")}) as Y (locale_code, locale_sort_column)
|
|
|
|
) as Y ON Y.locale_code = theme_fields.name",
|
|
|
|
*locale_codes.map.with_index { |code, index| [code, index] },
|
2020-12-10 17:56:26 -06:00
|
|
|
),
|
2019-02-26 08:22:02 -06:00
|
|
|
).order("Y.locale_sort_column")
|
2023-11-28 23:38:07 -06:00
|
|
|
end
|
2023-01-09 06:20:10 -06:00
|
|
|
|
2019-01-17 05:46:11 -06:00
|
|
|
scope :find_first_locale_fields,
|
2023-11-28 23:38:07 -06:00
|
|
|
->(theme_ids, locale_codes) do
|
2019-02-26 08:22:02 -06:00
|
|
|
find_by_theme_ids(theme_ids)
|
|
|
|
.filter_locale_fields(locale_codes)
|
|
|
|
.reorder("X.theme_sort_column", "Y.locale_sort_column")
|
2019-01-17 05:46:11 -06:00
|
|
|
.select("DISTINCT ON (X.theme_sort_column) *")
|
2023-11-28 23:38:07 -06:00
|
|
|
end
|
2018-08-07 23:46:34 -05:00
|
|
|
|
2023-03-14 13:11:45 -05:00
|
|
|
scope :svg_sprite_fields,
|
2023-11-28 23:38:07 -06:00
|
|
|
-> do
|
2023-03-14 13:11:45 -05:00
|
|
|
where(type_id: ThemeField.theme_var_type_ids, name: SvgSprite.theme_sprite_variable_name)
|
2023-11-28 23:38:07 -06:00
|
|
|
end
|
2023-03-14 13:11:45 -05:00
|
|
|
|
2017-05-02 15:01:01 -05:00
|
|
|
def self.types
|
|
|
|
@types ||=
|
|
|
|
Enum.new(
|
|
|
|
html: 0,
|
|
|
|
scss: 1,
|
|
|
|
theme_upload_var: 2,
|
2019-01-18 06:03:55 -06:00
|
|
|
theme_color_var: 3, # No longer used
|
|
|
|
theme_var: 4, # No longer used
|
2019-06-03 04:41:00 -05:00
|
|
|
yaml: 5,
|
|
|
|
js: 6,
|
|
|
|
)
|
2017-05-02 15:01:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.theme_var_type_ids
|
2019-01-18 06:03:55 -06:00
|
|
|
@theme_var_type_ids ||= [2]
|
2017-05-02 15:01:01 -05:00
|
|
|
end
|
|
|
|
|
2021-02-11 14:48:57 -06:00
|
|
|
def self.css_theme_type_ids
|
|
|
|
@css_theme_type_ids ||= [0, 1]
|
|
|
|
end
|
|
|
|
|
2019-01-10 04:06:01 -06:00
|
|
|
def self.force_recompilation!
|
|
|
|
find_each do |field|
|
|
|
|
field.compiler_version = 0
|
|
|
|
field.ensure_baked!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-19 09:10:44 -06:00
|
|
|
validates :name,
|
|
|
|
format: {
|
|
|
|
with: /\A[a-z_][a-z0-9_-]*\z/i,
|
|
|
|
},
|
|
|
|
if: Proc.new { |field| ThemeField.theme_var_type_ids.include?(field.type_id) }
|
|
|
|
|
2017-04-12 09:52:52 -05:00
|
|
|
belongs_to :theme
|
|
|
|
|
|
|
|
def process_html(html)
|
2019-01-17 05:46:11 -06:00
|
|
|
errors = []
|
2018-10-14 23:55:23 -05:00
|
|
|
javascript_cache || build_javascript_cache
|
2019-01-17 05:46:11 -06:00
|
|
|
|
2019-11-12 08:30:19 -06:00
|
|
|
errors << I18n.t("themes.errors.optimized_link") if contains_optimized_link?(html)
|
|
|
|
|
2019-05-24 09:25:55 -05:00
|
|
|
js_compiler = ThemeJavascriptCompiler.new(theme_id, self.theme.name)
|
2017-04-19 15:46:28 -05:00
|
|
|
|
2020-05-04 22:46:57 -05:00
|
|
|
doc = Nokogiri::HTML5.fragment(html)
|
2019-01-17 05:46:11 -06:00
|
|
|
|
2017-04-12 09:52:52 -05:00
|
|
|
doc
|
|
|
|
.css('script[type="text/x-handlebars"]')
|
|
|
|
.each do |node|
|
|
|
|
name = node["name"] || node["data-template-name"] || "broken"
|
2023-01-20 12:52:49 -06:00
|
|
|
is_raw = name =~ /\.(raw|hbr)\z/
|
2019-01-17 05:46:11 -06:00
|
|
|
hbs_template = node.inner_html
|
2023-01-09 06:20:10 -06:00
|
|
|
|
2019-01-17 05:46:11 -06:00
|
|
|
begin
|
|
|
|
if is_raw
|
|
|
|
js_compiler.append_raw_template(name, hbs_template)
|
|
|
|
else
|
2023-08-18 06:07:10 -05:00
|
|
|
js_compiler.append_ember_template(
|
|
|
|
"discourse/templates/#{name.delete_prefix("/")}",
|
|
|
|
hbs_template,
|
|
|
|
)
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
2019-01-17 05:46:11 -06:00
|
|
|
rescue ThemeJavascriptCompiler::CompileError => ex
|
2022-10-18 12:20:10 -05:00
|
|
|
js_compiler.append_js_error("discourse/templates/#{name}", ex.message)
|
2019-01-17 05:46:11 -06:00
|
|
|
errors << ex.message
|
|
|
|
end
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2018-10-14 23:55:23 -05:00
|
|
|
node.remove
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
2021-04-12 07:02:58 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
doc
|
2021-04-12 07:02:58 -05:00
|
|
|
.css('script[type="text/discourse-plugin"]')
|
|
|
|
.each_with_index do |node, index|
|
|
|
|
version = node["version"]
|
|
|
|
next if version.blank?
|
2023-01-09 06:20:10 -06:00
|
|
|
|
2021-04-12 07:02:58 -05:00
|
|
|
initializer_name =
|
|
|
|
"theme-field" + "-#{self.id}" + "-#{Theme.targets[self.target_id]}" +
|
|
|
|
"-#{ThemeField.types[self.type_id]}" + "-script-#{index + 1}"
|
2019-01-17 05:46:11 -06:00
|
|
|
begin
|
2021-04-12 07:02:58 -05:00
|
|
|
js = <<~JS
|
|
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: #{initializer_name.inspect},
|
|
|
|
after: "inject-objects",
|
|
|
|
|
|
|
|
initialize() {
|
|
|
|
withPluginApi(#{version.inspect}, (api) => {
|
|
|
|
#{node.inner_html}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
JS
|
|
|
|
|
|
|
|
js_compiler.append_module(
|
|
|
|
js,
|
|
|
|
"discourse/initializers/#{initializer_name}",
|
2023-10-02 05:36:06 -05:00
|
|
|
"js",
|
2021-04-12 07:02:58 -05:00
|
|
|
include_variables: true,
|
|
|
|
)
|
2019-01-17 05:46:11 -06:00
|
|
|
rescue ThemeJavascriptCompiler::CompileError => ex
|
2022-10-18 12:20:10 -05:00
|
|
|
js_compiler.append_js_error("discourse/initializers/#{initializer_name}", ex.message)
|
2019-01-17 05:46:11 -06:00
|
|
|
errors << ex.message
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
2019-01-17 05:46:11 -06:00
|
|
|
|
|
|
|
node.remove
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
2022-10-18 12:20:10 -05:00
|
|
|
doc
|
|
|
|
.css("script")
|
|
|
|
.each_with_index do |node, index|
|
2024-02-16 05:16:54 -06:00
|
|
|
if inline_javascript?(node)
|
|
|
|
js_compiler.append_raw_script(
|
|
|
|
"_html/#{Theme.targets[self.target_id]}/#{name}_#{index + 1}.js",
|
|
|
|
node.inner_html,
|
|
|
|
)
|
|
|
|
node.remove
|
|
|
|
else
|
|
|
|
node["nonce"] = CSP_NONCE_PLACEHOLDER
|
|
|
|
end
|
2018-10-14 23:55:23 -05:00
|
|
|
end
|
|
|
|
|
2020-05-01 11:51:11 -05:00
|
|
|
settings_hash = theme.build_settings_hash
|
2022-10-18 12:20:10 -05:00
|
|
|
if js_compiler.has_content? && settings_hash.present?
|
|
|
|
js_compiler.prepend_settings(settings_hash)
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
2019-01-17 05:46:11 -06:00
|
|
|
javascript_cache.content = js_compiler.content
|
2022-10-18 12:20:10 -05:00
|
|
|
javascript_cache.source_map = js_compiler.source_map
|
2018-10-14 23:55:23 -05:00
|
|
|
javascript_cache.save!
|
|
|
|
|
2022-11-30 12:43:01 -06:00
|
|
|
doc.add_child(<<~HTML.html_safe) if javascript_cache.content.present?
|
2024-02-16 05:16:54 -06:00
|
|
|
<script defer src='#{javascript_cache.url}' data-theme-id='#{theme_id}' nonce="#{CSP_NONCE_PLACEHOLDER}"></script>
|
|
|
|
HTML
|
2017-04-19 15:46:28 -05:00
|
|
|
[doc.to_s, errors&.join("\n")]
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
2021-07-14 14:18:29 -05:00
|
|
|
def validate_svg_sprite_xml
|
|
|
|
upload =
|
2023-01-09 06:20:10 -06:00
|
|
|
begin
|
2021-07-14 14:18:29 -05:00
|
|
|
Upload.find(self.upload_id)
|
|
|
|
rescue StandardError
|
|
|
|
nil
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
2021-07-14 14:18:29 -05:00
|
|
|
|
|
|
|
if Discourse.store.external?
|
2023-05-11 04:27:27 -05:00
|
|
|
external_copy = Discourse.store.download_safe(upload)
|
|
|
|
path = external_copy&.path
|
2021-07-14 14:18:29 -05:00
|
|
|
else
|
|
|
|
path = Discourse.store.path_for(upload)
|
|
|
|
end
|
|
|
|
|
|
|
|
error = nil
|
|
|
|
|
|
|
|
begin
|
2021-07-25 21:35:27 -05:00
|
|
|
content = File.read(path)
|
2023-09-01 12:22:58 -05:00
|
|
|
if content.to_s.bytesize > SvgSprite::MAX_THEME_SPRITE_SIZE
|
|
|
|
error = "Error with #{self.name}: Icon sprite file is too large"
|
|
|
|
else
|
|
|
|
Nokogiri.XML(content) { |config| config.options = Nokogiri::XML::ParseOptions::NOBLANKS }
|
|
|
|
end
|
2021-07-14 14:18:29 -05:00
|
|
|
rescue => e
|
|
|
|
error = "Error with #{self.name}: #{e.inspect}"
|
|
|
|
end
|
|
|
|
error
|
|
|
|
end
|
|
|
|
|
2019-01-25 08:19:01 -06:00
|
|
|
def raw_translation_data(internal: false)
|
2019-01-17 05:46:11 -06:00
|
|
|
# Might raise ThemeTranslationParser::InvalidYaml
|
2019-01-25 08:19:01 -06:00
|
|
|
ThemeTranslationParser.new(self, internal: internal).load
|
2019-01-17 05:46:11 -06:00
|
|
|
end
|
|
|
|
|
2019-02-26 08:22:02 -06:00
|
|
|
def translation_data(with_overrides: true, internal: false, fallback_fields: nil)
|
|
|
|
fallback_fields ||= theme.theme_fields.filter_locale_fields(I18n.fallbacks[name])
|
2019-01-17 05:46:11 -06:00
|
|
|
|
|
|
|
fallback_data =
|
|
|
|
fallback_fields.each_with_index.map do |field, index|
|
|
|
|
begin
|
2019-01-25 08:19:01 -06:00
|
|
|
field.raw_translation_data(internal: internal)
|
2019-01-17 05:46:11 -06:00
|
|
|
rescue ThemeTranslationParser::InvalidYaml
|
|
|
|
# If this is the locale with the error, raise it.
|
|
|
|
# If not, let the other theme_field raise the error when it processes itself
|
|
|
|
raise if field.id == id
|
|
|
|
{}
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
2019-01-17 05:46:11 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: Deduplicate the fallback data in the same way as JSLocaleHelper#load_translations_merged
|
|
|
|
# this would reduce the size of the payload, without affecting functionality
|
|
|
|
data = {}
|
|
|
|
fallback_data.each { |hash| data.merge!(hash) }
|
2021-11-11 11:11:23 -06:00
|
|
|
|
|
|
|
if with_overrides
|
|
|
|
overrides = theme.translation_override_hash.deep_symbolize_keys
|
|
|
|
data.deep_merge!(overrides)
|
|
|
|
end
|
|
|
|
|
2019-01-17 05:46:11 -06:00
|
|
|
data
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_translation
|
|
|
|
errors = []
|
|
|
|
javascript_cache || build_javascript_cache
|
2019-05-24 09:25:55 -05:00
|
|
|
js_compiler = ThemeJavascriptCompiler.new(theme_id, self.theme.name)
|
2019-01-17 05:46:11 -06:00
|
|
|
begin
|
|
|
|
data = translation_data
|
|
|
|
|
|
|
|
js = <<~JS
|
2019-11-05 05:54:12 -06:00
|
|
|
export default {
|
|
|
|
name: "theme-#{theme_id}-translations",
|
|
|
|
initialize() {
|
|
|
|
/* Translation data for theme #{self.theme_id} (#{self.name})*/
|
|
|
|
const data = #{data.to_json};
|
|
|
|
|
|
|
|
for (let lang in data){
|
|
|
|
let cursor = I18n.translations;
|
|
|
|
for (let key of [lang, "js", "theme_translations"]){
|
|
|
|
cursor = cursor[key] = cursor[key] || {};
|
|
|
|
}
|
|
|
|
cursor[#{self.theme_id}] = data[lang];
|
|
|
|
}
|
2019-01-17 05:46:11 -06:00
|
|
|
}
|
2019-11-05 05:54:12 -06:00
|
|
|
};
|
2019-01-17 05:46:11 -06:00
|
|
|
JS
|
|
|
|
|
2019-11-05 05:54:12 -06:00
|
|
|
js_compiler.append_module(
|
|
|
|
js,
|
|
|
|
"discourse/pre-initializers/theme-#{theme_id}-translations",
|
2023-10-02 05:36:06 -05:00
|
|
|
"js",
|
2019-11-05 05:54:12 -06:00
|
|
|
include_variables: false,
|
|
|
|
)
|
2019-01-17 05:46:11 -06:00
|
|
|
rescue ThemeTranslationParser::InvalidYaml => e
|
|
|
|
errors << e.message
|
|
|
|
end
|
|
|
|
|
|
|
|
javascript_cache.content = js_compiler.content
|
2022-10-18 12:20:10 -05:00
|
|
|
javascript_cache.source_map = js_compiler.source_map
|
2019-01-17 05:46:11 -06:00
|
|
|
javascript_cache.save!
|
|
|
|
doc = ""
|
2022-11-30 12:43:01 -06:00
|
|
|
doc = <<~HTML.html_safe if javascript_cache.content.present?
|
2024-02-16 05:16:54 -06:00
|
|
|
<script defer src="#{javascript_cache.url}" data-theme-id="#{theme_id}" nonce="#{ThemeField::CSP_NONCE_PLACEHOLDER}"></script>
|
2022-11-30 12:43:01 -06:00
|
|
|
HTML
|
2019-01-17 05:46:11 -06:00
|
|
|
[doc, errors&.join("\n")]
|
|
|
|
end
|
|
|
|
|
2018-03-04 18:04:23 -06:00
|
|
|
def validate_yaml!
|
|
|
|
return unless self.name == "yaml"
|
|
|
|
|
|
|
|
errors = []
|
2024-02-20 18:08:26 -06:00
|
|
|
|
2018-03-04 18:04:23 -06:00
|
|
|
begin
|
|
|
|
ThemeSettingsParser
|
|
|
|
.new(self)
|
|
|
|
.load do |name, default, type, opts|
|
|
|
|
setting = ThemeSetting.new(name: name, data_type: type, theme: theme)
|
|
|
|
translation_key = "themes.settings_errors"
|
2023-01-09 06:20:10 -06:00
|
|
|
|
2018-03-04 18:04:23 -06:00
|
|
|
if setting.invalid?
|
|
|
|
setting.errors.details.each_pair do |attribute, _errors|
|
|
|
|
_errors.each do |hash|
|
|
|
|
errors << I18n.t("#{translation_key}.#{attribute}_#{hash[:error]}", name: name)
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
2018-03-04 18:04:23 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-20 18:08:26 -06:00
|
|
|
unless ThemeSettingsValidator.is_value_present?(default)
|
|
|
|
errors << I18n.t("#{translation_key}.default_value_missing", name: name)
|
|
|
|
next
|
2018-03-04 18:04:23 -06:00
|
|
|
end
|
|
|
|
|
2024-02-20 18:08:26 -06:00
|
|
|
unless ThemeSettingsValidator.is_valid_value_type?(default, type)
|
2018-03-04 18:04:23 -06:00
|
|
|
errors << I18n.t("#{translation_key}.default_not_match_type", name: name)
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
2024-02-20 18:08:26 -06:00
|
|
|
|
|
|
|
if (setting_errors = ThemeSettingsValidator.validate_value(default, type, opts)).present?
|
|
|
|
errors << I18n.t(
|
|
|
|
"#{translation_key}.default_value_not_valid",
|
|
|
|
name: name,
|
|
|
|
error_messages: setting_errors.join(" "),
|
|
|
|
)
|
|
|
|
end
|
2018-03-04 18:04:23 -06:00
|
|
|
end
|
|
|
|
rescue ThemeSettingsParser::InvalidYaml => e
|
|
|
|
errors << e.message
|
|
|
|
end
|
|
|
|
|
2018-12-19 09:36:31 -06:00
|
|
|
self.error = errors.join("\n").presence
|
2018-03-04 18:04:23 -06:00
|
|
|
end
|
|
|
|
|
2019-01-17 05:46:11 -06:00
|
|
|
def self.guess_type(name:, target:)
|
2019-04-12 05:36:08 -05:00
|
|
|
if basic_targets.include?(target.to_s) && html_fields.include?(name.to_s)
|
2017-05-02 15:01:01 -05:00
|
|
|
types[:html]
|
2019-04-12 05:36:08 -05:00
|
|
|
elsif basic_targets.include?(target.to_s) && scss_fields.include?(name.to_s)
|
2017-05-02 15:01:01 -05:00
|
|
|
types[:scss]
|
2019-04-12 05:36:08 -05:00
|
|
|
elsif target.to_s == "extra_scss"
|
|
|
|
types[:scss]
|
FEATURE: Theme settings migrations (#24071)
This commit introduces a new feature that allows theme developers to manage the transformation of theme settings over time. Similar to Rails migrations, the theme settings migration system enables developers to write and execute migrations for theme settings, ensuring a smooth transition when changes are required in the format or structure of setting values.
Example use cases for the theme settings migration system:
1. Renaming a theme setting.
2. Changing the data type of a theme setting (e.g., transforming a string setting containing comma-separated values into a proper list setting).
3. Altering the format of data stored in a theme setting.
All of these use cases and more are now possible while preserving theme setting values for sites that have already modified their theme settings.
Usage:
1. Create a top-level directory called `migrations` in your theme/component, and then within the `migrations` directory create another directory called `settings`.
2. Inside the `migrations/settings` directory, create a JavaScript file using the format `XXXX-some-name.js`, where `XXXX` is a unique 4-digit number, and `some-name` is a descriptor of your choice that describes the migration.
3. Within the JavaScript file, define and export (as the default) a function called `migrate`. This function will receive a `Map` object and must also return a `Map` object (it's acceptable to return the same `Map` object that the function received).
4. The `Map` object received by the `migrate` function will include settings that have been overridden or changed by site administrators. Settings that have never been changed from the default will not be included.
5. The keys and values contained in the `Map` object that the `migrate` function returns will replace all the currently changed settings of the theme.
6. Migrations are executed in numerical order based on the XXXX segment in the migration filenames. For instance, `0001-some-migration.js` will be executed before `0002-another-migration.js`.
Here's a complete example migration script that renames a setting from `setting_with_old_name` to `setting_with_new_name`:
```js
// File name: 0001-rename-setting.js
export default function migrate(settings) {
if (settings.has("setting_with_old_name")) {
settings.set("setting_with_new_name", settings.get("setting_with_old_name"));
}
return settings;
}
```
Internal topic: t/109980
2023-11-02 00:10:15 -05:00
|
|
|
elsif %w[migrations extra_js].include?(target.to_s)
|
2019-06-03 04:41:00 -05:00
|
|
|
types[:js]
|
2019-04-12 05:36:08 -05:00
|
|
|
elsif target.to_s == "settings" || target.to_s == "translations"
|
2018-03-04 18:04:23 -06:00
|
|
|
types[:yaml]
|
2017-05-02 15:01:01 -05:00
|
|
|
end
|
|
|
|
end
|
2017-04-12 09:52:52 -05:00
|
|
|
|
|
|
|
def self.html_fields
|
2023-02-06 10:10:50 -06:00
|
|
|
@html_fields ||= %w[body_tag head_tag header footer after_header embedded_header]
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
2017-04-19 15:46:28 -05:00
|
|
|
def self.scss_fields
|
2020-08-18 12:02:13 -05:00
|
|
|
@scss_fields ||= %w[scss embedded_scss color_definitions]
|
2017-04-19 15:46:28 -05:00
|
|
|
end
|
|
|
|
|
2019-04-12 05:36:08 -05:00
|
|
|
def self.basic_targets
|
|
|
|
@basic_targets ||= %w[common desktop mobile]
|
|
|
|
end
|
|
|
|
|
|
|
|
def basic_html_field?
|
|
|
|
ThemeField.basic_targets.include?(Theme.targets[self.target_id].to_s) &&
|
|
|
|
ThemeField.html_fields.include?(self.name)
|
|
|
|
end
|
|
|
|
|
2019-06-03 04:41:00 -05:00
|
|
|
def extra_js_field?
|
|
|
|
Theme.targets[self.target_id] == :extra_js
|
|
|
|
end
|
|
|
|
|
2021-04-12 07:02:58 -05:00
|
|
|
def js_tests_field?
|
|
|
|
Theme.targets[self.target_id] == :tests_js
|
|
|
|
end
|
|
|
|
|
2019-04-12 05:36:08 -05:00
|
|
|
def basic_scss_field?
|
|
|
|
ThemeField.basic_targets.include?(Theme.targets[self.target_id].to_s) &&
|
|
|
|
ThemeField.scss_fields.include?(self.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def extra_scss_field?
|
|
|
|
Theme.targets[self.target_id] == :extra_scss
|
|
|
|
end
|
|
|
|
|
|
|
|
def settings_field?
|
|
|
|
Theme.targets[:settings] == self.target_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def translation_field?
|
|
|
|
Theme.targets[:translations] == self.target_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def svg_sprite_field?
|
|
|
|
ThemeField.theme_var_type_ids.include?(self.type_id) &&
|
|
|
|
self.name == SvgSprite.theme_sprite_variable_name
|
|
|
|
end
|
|
|
|
|
FEATURE: Theme settings migrations (#24071)
This commit introduces a new feature that allows theme developers to manage the transformation of theme settings over time. Similar to Rails migrations, the theme settings migration system enables developers to write and execute migrations for theme settings, ensuring a smooth transition when changes are required in the format or structure of setting values.
Example use cases for the theme settings migration system:
1. Renaming a theme setting.
2. Changing the data type of a theme setting (e.g., transforming a string setting containing comma-separated values into a proper list setting).
3. Altering the format of data stored in a theme setting.
All of these use cases and more are now possible while preserving theme setting values for sites that have already modified their theme settings.
Usage:
1. Create a top-level directory called `migrations` in your theme/component, and then within the `migrations` directory create another directory called `settings`.
2. Inside the `migrations/settings` directory, create a JavaScript file using the format `XXXX-some-name.js`, where `XXXX` is a unique 4-digit number, and `some-name` is a descriptor of your choice that describes the migration.
3. Within the JavaScript file, define and export (as the default) a function called `migrate`. This function will receive a `Map` object and must also return a `Map` object (it's acceptable to return the same `Map` object that the function received).
4. The `Map` object received by the `migrate` function will include settings that have been overridden or changed by site administrators. Settings that have never been changed from the default will not be included.
5. The keys and values contained in the `Map` object that the `migrate` function returns will replace all the currently changed settings of the theme.
6. Migrations are executed in numerical order based on the XXXX segment in the migration filenames. For instance, `0001-some-migration.js` will be executed before `0002-another-migration.js`.
Here's a complete example migration script that renames a setting from `setting_with_old_name` to `setting_with_new_name`:
```js
// File name: 0001-rename-setting.js
export default function migrate(settings) {
if (settings.has("setting_with_old_name")) {
settings.set("setting_with_new_name", settings.get("setting_with_old_name"));
}
return settings;
}
```
Internal topic: t/109980
2023-11-02 00:10:15 -05:00
|
|
|
def migration_field?
|
|
|
|
Theme.targets[:migrations] == self.target_id
|
|
|
|
end
|
|
|
|
|
2017-04-12 09:52:52 -05:00
|
|
|
def ensure_baked!
|
2020-05-29 07:04:51 -05:00
|
|
|
needs_baking = !self.value_baked || compiler_version != Theme.compiler_version
|
2019-04-12 05:36:08 -05:00
|
|
|
return unless needs_baking
|
|
|
|
|
|
|
|
if basic_html_field? || translation_field?
|
|
|
|
self.value_baked, self.error =
|
|
|
|
translation_field? ? process_translation : process_html(self.value)
|
2024-05-27 05:27:13 -05:00
|
|
|
self.error = nil if self.error.blank?
|
2020-05-29 07:04:51 -05:00
|
|
|
self.compiler_version = Theme.compiler_version
|
2023-07-12 09:49:28 -05:00
|
|
|
CSP::Extension.clear_theme_extensions_cache!
|
2021-04-12 07:02:58 -05:00
|
|
|
elsif extra_js_field? || js_tests_field?
|
2022-10-17 09:04:04 -05:00
|
|
|
self.error = nil
|
|
|
|
self.value_baked = "baked"
|
2020-05-29 07:04:51 -05:00
|
|
|
self.compiler_version = Theme.compiler_version
|
2019-04-12 05:36:08 -05:00
|
|
|
elsif basic_scss_field?
|
|
|
|
ensure_scss_compiles!
|
2023-07-12 09:49:28 -05:00
|
|
|
Stylesheet::Manager.clear_theme_cache!
|
2019-04-12 05:36:08 -05:00
|
|
|
elsif settings_field?
|
|
|
|
validate_yaml!
|
2023-07-12 09:49:28 -05:00
|
|
|
CSP::Extension.clear_theme_extensions_cache!
|
|
|
|
SvgSprite.expire_cache
|
2019-04-12 05:36:08 -05:00
|
|
|
self.value_baked = "baked"
|
2020-05-29 07:04:51 -05:00
|
|
|
self.compiler_version = Theme.compiler_version
|
2019-04-12 05:36:08 -05:00
|
|
|
elsif svg_sprite_field?
|
2023-07-12 09:49:28 -05:00
|
|
|
SvgSprite.expire_cache
|
2021-07-14 14:18:29 -05:00
|
|
|
self.error = validate_svg_sprite_xml
|
2019-04-12 05:36:08 -05:00
|
|
|
self.value_baked = "baked"
|
2020-05-29 07:04:51 -05:00
|
|
|
self.compiler_version = Theme.compiler_version
|
FEATURE: Theme settings migrations (#24071)
This commit introduces a new feature that allows theme developers to manage the transformation of theme settings over time. Similar to Rails migrations, the theme settings migration system enables developers to write and execute migrations for theme settings, ensuring a smooth transition when changes are required in the format or structure of setting values.
Example use cases for the theme settings migration system:
1. Renaming a theme setting.
2. Changing the data type of a theme setting (e.g., transforming a string setting containing comma-separated values into a proper list setting).
3. Altering the format of data stored in a theme setting.
All of these use cases and more are now possible while preserving theme setting values for sites that have already modified their theme settings.
Usage:
1. Create a top-level directory called `migrations` in your theme/component, and then within the `migrations` directory create another directory called `settings`.
2. Inside the `migrations/settings` directory, create a JavaScript file using the format `XXXX-some-name.js`, where `XXXX` is a unique 4-digit number, and `some-name` is a descriptor of your choice that describes the migration.
3. Within the JavaScript file, define and export (as the default) a function called `migrate`. This function will receive a `Map` object and must also return a `Map` object (it's acceptable to return the same `Map` object that the function received).
4. The `Map` object received by the `migrate` function will include settings that have been overridden or changed by site administrators. Settings that have never been changed from the default will not be included.
5. The keys and values contained in the `Map` object that the `migrate` function returns will replace all the currently changed settings of the theme.
6. Migrations are executed in numerical order based on the XXXX segment in the migration filenames. For instance, `0001-some-migration.js` will be executed before `0002-another-migration.js`.
Here's a complete example migration script that renames a setting from `setting_with_old_name` to `setting_with_new_name`:
```js
// File name: 0001-rename-setting.js
export default function migrate(settings) {
if (settings.has("setting_with_old_name")) {
settings.set("setting_with_new_name", settings.get("setting_with_old_name"));
}
return settings;
}
```
Internal topic: t/109980
2023-11-02 00:10:15 -05:00
|
|
|
elsif migration_field?
|
|
|
|
self.value_baked = "baked"
|
|
|
|
self.compiler_version = Theme.compiler_version
|
2019-04-12 05:36:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if self.will_save_change_to_value_baked? || self.will_save_change_to_compiler_version? ||
|
|
|
|
self.will_save_change_to_error?
|
|
|
|
self.update_columns(
|
|
|
|
value_baked: value_baked,
|
|
|
|
compiler_version: compiler_version,
|
|
|
|
error: error,
|
|
|
|
)
|
2017-04-19 15:46:28 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-19 10:22:24 -06:00
|
|
|
def compile_scss(prepended_scss = nil)
|
|
|
|
prepended_scss ||= Stylesheet::Importer.new({}).prepended_scss
|
2021-02-02 12:09:41 -06:00
|
|
|
|
2021-04-27 08:33:43 -05:00
|
|
|
self.theme.with_scss_load_paths do |load_paths|
|
|
|
|
Stylesheet::Compiler.compile(
|
2023-12-06 16:25:00 -06:00
|
|
|
"#{prepended_scss} #{self.theme.scss_variables} #{self.value}",
|
2021-04-27 08:33:43 -05:00
|
|
|
"#{Theme.targets[self.target_id]}.scss",
|
|
|
|
theme: self.theme,
|
|
|
|
load_paths: load_paths,
|
|
|
|
)
|
|
|
|
end
|
2019-04-12 05:36:08 -05:00
|
|
|
end
|
2017-04-20 15:55:09 -05:00
|
|
|
|
2021-02-19 10:22:24 -06:00
|
|
|
def compiled_css(prepended_scss)
|
2021-02-04 07:51:18 -06:00
|
|
|
css, _source_map =
|
|
|
|
begin
|
2021-02-19 10:22:24 -06:00
|
|
|
compile_scss(prepended_scss)
|
2021-02-04 07:51:18 -06:00
|
|
|
rescue SassC::SyntaxError => e
|
|
|
|
# We don't want to raise a blocking error here
|
|
|
|
# admin theme editor or discourse_theme CLI will show it nonetheless
|
|
|
|
Rails.logger.error "SCSS compilation error: #{e.message}"
|
|
|
|
["", nil]
|
|
|
|
end
|
|
|
|
css
|
|
|
|
end
|
|
|
|
|
2019-04-12 05:36:08 -05:00
|
|
|
def ensure_scss_compiles!
|
|
|
|
result = ["failed"]
|
|
|
|
begin
|
|
|
|
result = compile_scss
|
2019-11-12 08:30:19 -06:00
|
|
|
if contains_optimized_link?(self.value)
|
|
|
|
self.error = I18n.t("themes.errors.optimized_link")
|
2021-02-11 14:48:57 -06:00
|
|
|
elsif contains_ember_css_selector?(self.value)
|
|
|
|
self.error = I18n.t("themes.ember_selector_error")
|
2019-11-12 08:30:19 -06:00
|
|
|
else
|
|
|
|
self.error = nil unless error.nil?
|
|
|
|
end
|
2023-02-07 09:24:57 -06:00
|
|
|
rescue SassC::SyntaxError, SassC::NotRenderedError => e
|
2019-04-12 05:36:08 -05:00
|
|
|
self.error = e.message unless self.destroyed?
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
2020-05-29 07:04:51 -05:00
|
|
|
self.compiler_version = Theme.compiler_version
|
2019-04-12 05:36:08 -05:00
|
|
|
self.value_baked = Digest::SHA1.hexdigest(result.join(",")) # We don't use the compiled CSS here, we just use it to invalidate the stylesheet cache
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def target_name
|
2019-04-12 05:36:08 -05:00
|
|
|
Theme.targets[target_id].to_s
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
2019-11-12 08:30:19 -06:00
|
|
|
def contains_optimized_link?(text)
|
|
|
|
OptimizedImage::URL_REGEX.match?(text)
|
|
|
|
end
|
|
|
|
|
2021-02-11 14:48:57 -06:00
|
|
|
def contains_ember_css_selector?(text)
|
|
|
|
text.match(/#ember\d+|[.]ember-view/)
|
|
|
|
end
|
|
|
|
|
2019-01-23 08:40:21 -06:00
|
|
|
class ThemeFileMatcher
|
|
|
|
OPTIONS = %i[name type target]
|
|
|
|
# regex: used to match file names to fields (import).
|
|
|
|
# can contain named capture groups for name/type/target
|
|
|
|
# canonical: a lambda which converts name/type/target
|
|
|
|
# to filename (export)
|
|
|
|
# targets/names/types: can be nil if any value is allowed
|
|
|
|
# single value
|
|
|
|
# array of allowed values
|
|
|
|
def initialize(regex:, canonical:, targets:, names:, types:)
|
|
|
|
@allowed_values = {}
|
|
|
|
@allowed_values[:names] = Array(names) if names
|
|
|
|
@allowed_values[:targets] = Array(targets) if targets
|
|
|
|
@allowed_values[:types] = Array(types) if types
|
|
|
|
@canonical = canonical
|
|
|
|
@regex = regex
|
|
|
|
end
|
|
|
|
|
|
|
|
def opts_from_filename(filename)
|
|
|
|
match = @regex.match(filename)
|
|
|
|
return false unless match
|
|
|
|
hash = {}
|
|
|
|
OPTIONS.each do |option|
|
|
|
|
plural = :"#{option}s"
|
2021-04-12 07:02:58 -05:00
|
|
|
hash[option] = @allowed_values[plural][0] if @allowed_values[plural]&.length == 1
|
2019-01-23 08:40:21 -06:00
|
|
|
hash[option] = match[option] if hash[option].nil?
|
|
|
|
end
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def filename_from_opts(opts)
|
|
|
|
is_match =
|
|
|
|
OPTIONS.all? do |option|
|
|
|
|
plural = :"#{option}s"
|
|
|
|
next true if @allowed_values[plural] == nil # Allows any value
|
|
|
|
next true if @allowed_values[plural].include?(opts[option]) # Value is allowed
|
|
|
|
end
|
|
|
|
is_match ? @canonical.call(opts) : nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
FILE_MATCHERS = [
|
|
|
|
ThemeFileMatcher.new(
|
|
|
|
regex:
|
2023-01-20 12:52:49 -06:00
|
|
|
%r{\A(?<target>(?:mobile|desktop|common))/(?<name>(?:head_tag|header|after_header|body_tag|footer))\.html\z},
|
2019-01-23 08:40:21 -06:00
|
|
|
targets: %i[mobile desktop common],
|
|
|
|
names: %w[head_tag header after_header body_tag footer],
|
|
|
|
types: :html,
|
|
|
|
canonical: ->(h) { "#{h[:target]}/#{h[:name]}.html" },
|
|
|
|
),
|
|
|
|
ThemeFileMatcher.new(
|
2023-01-20 12:52:49 -06:00
|
|
|
regex: %r{\A(?<target>(?:mobile|desktop|common))/(?:\k<target>)\.scss\z},
|
2019-01-23 08:40:21 -06:00
|
|
|
targets: %i[mobile desktop common],
|
|
|
|
names: "scss",
|
|
|
|
types: :scss,
|
|
|
|
canonical: ->(h) { "#{h[:target]}/#{h[:target]}.scss" },
|
|
|
|
),
|
|
|
|
ThemeFileMatcher.new(
|
2023-01-20 12:52:49 -06:00
|
|
|
regex: %r{\Acommon/embedded\.scss\z},
|
2019-01-23 08:40:21 -06:00
|
|
|
targets: :common,
|
|
|
|
names: "embedded_scss",
|
|
|
|
types: :scss,
|
|
|
|
canonical: ->(h) { "common/embedded.scss" },
|
|
|
|
),
|
2020-08-18 12:02:13 -05:00
|
|
|
ThemeFileMatcher.new(
|
2023-01-20 12:52:49 -06:00
|
|
|
regex: %r{\Acommon/color_definitions\.scss\z},
|
2020-08-18 12:02:13 -05:00
|
|
|
targets: :common,
|
|
|
|
names: "color_definitions",
|
|
|
|
types: :scss,
|
|
|
|
canonical: ->(h) { "common/color_definitions.scss" },
|
|
|
|
),
|
2019-05-31 09:40:41 -05:00
|
|
|
ThemeFileMatcher.new(
|
2023-01-20 12:52:49 -06:00
|
|
|
regex: %r{\A(?:scss|stylesheets)/(?<name>.+)\.scss\z},
|
2019-04-12 05:36:08 -05:00
|
|
|
targets: :extra_scss,
|
|
|
|
names: nil,
|
|
|
|
types: :scss,
|
2019-05-31 09:40:41 -05:00
|
|
|
canonical: ->(h) { "stylesheets/#{h[:name]}.scss" },
|
|
|
|
),
|
2019-06-03 04:41:00 -05:00
|
|
|
ThemeFileMatcher.new(
|
2023-01-20 12:52:49 -06:00
|
|
|
regex: %r{\Ajavascripts/(?<name>.+)\z},
|
2019-06-03 04:41:00 -05:00
|
|
|
targets: :extra_js,
|
|
|
|
names: nil,
|
|
|
|
types: :js,
|
|
|
|
canonical: ->(h) { "javascripts/#{h[:name]}" },
|
|
|
|
),
|
2021-04-12 07:02:58 -05:00
|
|
|
ThemeFileMatcher.new(
|
2023-01-20 12:52:49 -06:00
|
|
|
regex: %r{\Atest/(?<name>.+)\z},
|
2021-04-12 07:02:58 -05:00
|
|
|
targets: :tests_js,
|
|
|
|
names: nil,
|
|
|
|
types: :js,
|
|
|
|
canonical: ->(h) { "test/#{h[:name]}" },
|
|
|
|
),
|
2019-01-23 08:40:21 -06:00
|
|
|
ThemeFileMatcher.new(
|
2023-01-20 12:52:49 -06:00
|
|
|
regex: /\Asettings\.ya?ml\z/,
|
2019-01-23 08:40:21 -06:00
|
|
|
names: "yaml",
|
|
|
|
types: :yaml,
|
|
|
|
targets: :settings,
|
|
|
|
canonical: ->(h) { "settings.yml" },
|
|
|
|
),
|
|
|
|
ThemeFileMatcher.new(
|
2023-01-20 12:52:49 -06:00
|
|
|
regex: %r{\Alocales/(?<name>(?:#{I18n.available_locales.join("|")}))\.yml\z},
|
2019-01-23 08:40:21 -06:00
|
|
|
names: I18n.available_locales.map(&:to_s),
|
|
|
|
types: :yaml,
|
|
|
|
targets: :translations,
|
|
|
|
canonical: ->(h) { "locales/#{h[:name]}.yml" },
|
|
|
|
),
|
|
|
|
ThemeFileMatcher.new(
|
|
|
|
regex: /(?!)/, # Never match uploads by filename, they must be named in about.json
|
|
|
|
names: nil,
|
|
|
|
types: :theme_upload_var,
|
|
|
|
targets: :common,
|
2019-02-27 03:45:22 -06:00
|
|
|
canonical: ->(h) { "assets/#{h[:name]}#{File.extname(h[:filename])}" },
|
|
|
|
),
|
FEATURE: Theme settings migrations (#24071)
This commit introduces a new feature that allows theme developers to manage the transformation of theme settings over time. Similar to Rails migrations, the theme settings migration system enables developers to write and execute migrations for theme settings, ensuring a smooth transition when changes are required in the format or structure of setting values.
Example use cases for the theme settings migration system:
1. Renaming a theme setting.
2. Changing the data type of a theme setting (e.g., transforming a string setting containing comma-separated values into a proper list setting).
3. Altering the format of data stored in a theme setting.
All of these use cases and more are now possible while preserving theme setting values for sites that have already modified their theme settings.
Usage:
1. Create a top-level directory called `migrations` in your theme/component, and then within the `migrations` directory create another directory called `settings`.
2. Inside the `migrations/settings` directory, create a JavaScript file using the format `XXXX-some-name.js`, where `XXXX` is a unique 4-digit number, and `some-name` is a descriptor of your choice that describes the migration.
3. Within the JavaScript file, define and export (as the default) a function called `migrate`. This function will receive a `Map` object and must also return a `Map` object (it's acceptable to return the same `Map` object that the function received).
4. The `Map` object received by the `migrate` function will include settings that have been overridden or changed by site administrators. Settings that have never been changed from the default will not be included.
5. The keys and values contained in the `Map` object that the `migrate` function returns will replace all the currently changed settings of the theme.
6. Migrations are executed in numerical order based on the XXXX segment in the migration filenames. For instance, `0001-some-migration.js` will be executed before `0002-another-migration.js`.
Here's a complete example migration script that renames a setting from `setting_with_old_name` to `setting_with_new_name`:
```js
// File name: 0001-rename-setting.js
export default function migrate(settings) {
if (settings.has("setting_with_old_name")) {
settings.set("setting_with_new_name", settings.get("setting_with_old_name"));
}
return settings;
}
```
Internal topic: t/109980
2023-11-02 00:10:15 -05:00
|
|
|
ThemeFileMatcher.new(
|
|
|
|
regex: %r{\Amigrations/settings/(?<name>[^/]+)\.js\z},
|
|
|
|
names: nil,
|
|
|
|
types: :js,
|
|
|
|
targets: :migrations,
|
|
|
|
canonical: ->(h) { "migrations/settings/#{h[:name]}.js" },
|
|
|
|
),
|
2019-01-23 08:40:21 -06:00
|
|
|
]
|
|
|
|
|
|
|
|
# For now just work for standard fields
|
|
|
|
def file_path
|
|
|
|
FILE_MATCHERS.each do |matcher|
|
|
|
|
if filename =
|
|
|
|
matcher.filename_from_opts(
|
|
|
|
target: target_name.to_sym,
|
|
|
|
name: name,
|
|
|
|
type: ThemeField.types[type_id],
|
|
|
|
filename: upload&.original_filename,
|
|
|
|
)
|
|
|
|
return filename
|
|
|
|
end
|
|
|
|
end
|
|
|
|
nil # Not a file (e.g. a theme variable/color)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.opts_from_file_path(filename)
|
|
|
|
FILE_MATCHERS.each do |matcher|
|
|
|
|
if opts = matcher.opts_from_filename(filename)
|
|
|
|
return opts
|
|
|
|
end
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2019-04-12 05:36:08 -05:00
|
|
|
def dependent_fields
|
|
|
|
if extra_scss_field?
|
|
|
|
return(
|
|
|
|
theme.theme_fields.where(
|
|
|
|
target_id: ThemeField.basic_targets.map { |t| Theme.targets[t.to_sym] },
|
|
|
|
name: ThemeField.scss_fields,
|
|
|
|
)
|
2023-01-09 06:20:10 -06:00
|
|
|
)
|
2019-04-12 05:36:08 -05:00
|
|
|
elsif settings_field?
|
|
|
|
return(
|
|
|
|
theme.theme_fields.where(
|
|
|
|
target_id: ThemeField.basic_targets.map { |t| Theme.targets[t.to_sym] },
|
|
|
|
name: ThemeField.scss_fields + ThemeField.html_fields,
|
|
|
|
)
|
2023-01-09 06:20:10 -06:00
|
|
|
)
|
2023-11-18 06:36:25 -06:00
|
|
|
elsif translation_field? && name == "en" # en is fallback for all other locales
|
|
|
|
return theme.theme_fields.where(target_id: Theme.targets[:translations]).where.not(name: "en")
|
2019-04-12 05:36:08 -05:00
|
|
|
end
|
|
|
|
ThemeField.none
|
|
|
|
end
|
|
|
|
|
|
|
|
def invalidate_baked!
|
|
|
|
update_column(:value_baked, nil)
|
|
|
|
dependent_fields.update_all(value_baked: nil)
|
|
|
|
end
|
2018-12-19 09:36:31 -06:00
|
|
|
|
2019-04-12 05:36:08 -05:00
|
|
|
before_save do
|
2020-11-24 17:49:12 -06:00
|
|
|
if (will_save_change_to_value? || will_save_change_to_upload_id?) &&
|
|
|
|
!will_save_change_to_value_baked?
|
2017-04-12 09:52:52 -05:00
|
|
|
self.value_baked = nil
|
|
|
|
end
|
2022-04-06 16:58:10 -05:00
|
|
|
if upload && upload.extension == "js"
|
|
|
|
if will_save_change_to_upload_id? || !javascript_cache
|
|
|
|
javascript_cache ||= build_javascript_cache
|
|
|
|
javascript_cache.content = upload.content
|
|
|
|
end
|
|
|
|
end
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
2023-03-14 13:11:45 -05:00
|
|
|
def upsert_svg_sprite!
|
|
|
|
begin
|
|
|
|
content = upload.content
|
|
|
|
rescue => e
|
|
|
|
Discourse.warn_exception(e, message: "Failed to fetch svg sprite for theme field #{id}")
|
|
|
|
else
|
2023-09-01 12:22:58 -05:00
|
|
|
if content.length > SvgSprite::MAX_THEME_SPRITE_SIZE
|
2023-03-14 13:11:45 -05:00
|
|
|
Rails.logger.warn(
|
|
|
|
"can't store theme svg sprite for theme #{theme_id} and upload #{upload_id}, sprite too big",
|
|
|
|
)
|
|
|
|
else
|
|
|
|
ThemeSvgSprite.upsert(
|
|
|
|
{ theme_id: theme_id, upload_id: upload_id, sprite: content },
|
|
|
|
unique_by: :theme_id,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2023-03-14 13:11:45 -05:00
|
|
|
after_save do
|
|
|
|
dependent_fields.each(&:invalidate_baked!)
|
|
|
|
|
|
|
|
if upload && svg_sprite_field?
|
|
|
|
upsert_svg_sprite!
|
2023-07-12 09:49:28 -05:00
|
|
|
SvgSprite.expire_cache
|
2023-03-14 13:11:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after_destroy do
|
|
|
|
if svg_sprite_field?
|
|
|
|
ThemeSvgSprite.where(theme_id: theme_id).delete_all
|
|
|
|
|
2023-07-12 09:49:28 -05:00
|
|
|
SvgSprite.expire_cache
|
2023-03-14 13:11:45 -05:00
|
|
|
end
|
|
|
|
end
|
2021-07-14 14:18:29 -05:00
|
|
|
|
2018-11-01 15:01:46 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
JAVASCRIPT_TYPES = %w[text/javascript application/javascript application/ecmascript]
|
|
|
|
|
|
|
|
def inline_javascript?(node)
|
|
|
|
if node["src"].present?
|
|
|
|
false
|
|
|
|
elsif node["type"].present?
|
|
|
|
JAVASCRIPT_TYPES.include?(node["type"].downcase)
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
FEATURE: Theme settings migrations (#24071)
This commit introduces a new feature that allows theme developers to manage the transformation of theme settings over time. Similar to Rails migrations, the theme settings migration system enables developers to write and execute migrations for theme settings, ensuring a smooth transition when changes are required in the format or structure of setting values.
Example use cases for the theme settings migration system:
1. Renaming a theme setting.
2. Changing the data type of a theme setting (e.g., transforming a string setting containing comma-separated values into a proper list setting).
3. Altering the format of data stored in a theme setting.
All of these use cases and more are now possible while preserving theme setting values for sites that have already modified their theme settings.
Usage:
1. Create a top-level directory called `migrations` in your theme/component, and then within the `migrations` directory create another directory called `settings`.
2. Inside the `migrations/settings` directory, create a JavaScript file using the format `XXXX-some-name.js`, where `XXXX` is a unique 4-digit number, and `some-name` is a descriptor of your choice that describes the migration.
3. Within the JavaScript file, define and export (as the default) a function called `migrate`. This function will receive a `Map` object and must also return a `Map` object (it's acceptable to return the same `Map` object that the function received).
4. The `Map` object received by the `migrate` function will include settings that have been overridden or changed by site administrators. Settings that have never been changed from the default will not be included.
5. The keys and values contained in the `Map` object that the `migrate` function returns will replace all the currently changed settings of the theme.
6. Migrations are executed in numerical order based on the XXXX segment in the migration filenames. For instance, `0001-some-migration.js` will be executed before `0002-another-migration.js`.
Here's a complete example migration script that renames a setting from `setting_with_old_name` to `setting_with_new_name`:
```js
// File name: 0001-rename-setting.js
export default function migrate(settings) {
if (settings.has("setting_with_old_name")) {
settings.set("setting_with_new_name", settings.get("setting_with_old_name"));
}
return settings;
}
```
Internal topic: t/109980
2023-11-02 00:10:15 -05:00
|
|
|
|
|
|
|
def migration_filename_is_valid
|
|
|
|
if !name.match?(/\A\d{4}-[a-zA-Z0-9]+/)
|
|
|
|
self.errors.add(
|
|
|
|
:base,
|
|
|
|
I18n.t("themes.import_error.migrations.invalid_filename", filename: name),
|
|
|
|
)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# the 5 here is the length of the first 4 digits and the dash that follows
|
|
|
|
# them
|
|
|
|
if name.size - 5 > MIGRATION_NAME_PART_MAX_LENGTH
|
|
|
|
self.errors.add(
|
|
|
|
:base,
|
|
|
|
I18n.t(
|
|
|
|
"themes.import_error.migrations.name_too_long",
|
|
|
|
count: MIGRATION_NAME_PART_MAX_LENGTH,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: theme_fields
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# theme_id :integer not null
|
2017-05-02 15:01:01 -05:00
|
|
|
# target_id :integer not null
|
2019-04-23 06:34:32 -05:00
|
|
|
# name :string(255) not null
|
2017-04-12 09:52:52 -05:00
|
|
|
# value :text not null
|
|
|
|
# value_baked :text
|
2019-01-11 13:29:56 -06:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2019-06-03 10:38:02 -05:00
|
|
|
# compiler_version :string(50) default("0"), not null
|
2017-04-20 14:47:25 -05:00
|
|
|
# error :string
|
2017-05-02 15:01:01 -05:00
|
|
|
# upload_id :integer
|
|
|
|
# type_id :integer default(0), not null
|
2017-04-12 09:52:52 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2017-05-02 15:01:01 -05:00
|
|
|
# theme_field_unique_index (theme_id,target_id,type_id,name) UNIQUE
|
2017-04-12 09:52:52 -05:00
|
|
|
#
|