mirror of
https://github.com/discourse/discourse.git
synced 2024-11-27 09:33:37 +08:00
3cadd6769e
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
95 lines
2.5 KiB
Ruby
95 lines
2.5 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class ThemesInstallTask
|
|
def self.install(themes)
|
|
counts = { installed: 0, updated: 0, errors: 0, skipped: 0 }
|
|
log = []
|
|
themes.each do |name, val|
|
|
installer = new(val)
|
|
next if installer.url.nil?
|
|
|
|
if installer.theme_exists?
|
|
if installer.options.fetch(:skip_update, nil)
|
|
log << "#{name}: is already installed. Skipping update."
|
|
counts[:skipped] += 1
|
|
elsif installer.update
|
|
log << "#{name}: is already installed. Updating from remote."
|
|
counts[:updated] += 1
|
|
else
|
|
log << "#{name}: is already installed, but there was an error updating from remote."
|
|
counts[:errors] += 1
|
|
end
|
|
else
|
|
begin
|
|
installer.install
|
|
log << "#{name}: installed from #{installer.url}"
|
|
counts[:installed] += 1
|
|
rescue RemoteTheme::ImportError, Discourse::InvalidParameters => err
|
|
log << "#{name}: #{err.message}"
|
|
counts[:errors] += 1
|
|
end
|
|
end
|
|
end
|
|
|
|
[log, counts]
|
|
end
|
|
|
|
attr_reader :url, :options
|
|
|
|
def initialize(url_or_options = nil)
|
|
if url_or_options.is_a?(Hash)
|
|
url_or_options.deep_symbolize_keys!
|
|
@url = url_or_options.fetch(:url, nil)
|
|
@options = url_or_options
|
|
else
|
|
@url = url_or_options
|
|
@options = {}
|
|
end
|
|
end
|
|
|
|
def repo_name
|
|
@url.gsub(Regexp.union("git@github.com:", "https://github.com/", ".git"), "")
|
|
end
|
|
|
|
def theme_exists?
|
|
@remote_theme =
|
|
RemoteTheme
|
|
.where("remote_url like ?", "%#{repo_name}%")
|
|
.where(branch: @options.fetch(:branch, nil))
|
|
.first
|
|
@theme = @remote_theme&.theme
|
|
@theme.present?
|
|
end
|
|
|
|
def install
|
|
@theme =
|
|
RemoteTheme.import_theme(
|
|
@url,
|
|
Discourse.system_user,
|
|
private_key: @options[:private_key],
|
|
branch: @options[:branch],
|
|
)
|
|
@theme.set_default! if @options.fetch(:default, false)
|
|
add_component_to_all_themes
|
|
end
|
|
|
|
def update
|
|
@remote_theme.update_from_remote(raise_if_theme_save_fails: false)
|
|
add_component_to_all_themes
|
|
@remote_theme.last_error_text.nil?
|
|
end
|
|
|
|
def add_component_to_all_themes
|
|
return if (!@options.fetch(:add_to_all_themes, false) || !@theme.component)
|
|
|
|
Theme
|
|
.where(component: false)
|
|
.each do |parent_theme|
|
|
if ChildTheme.where(parent_theme_id: parent_theme.id, child_theme_id: @theme.id).exists?
|
|
next
|
|
end
|
|
parent_theme.add_relative_theme!(:child, @theme)
|
|
end
|
|
end
|
|
end
|