2019-07-04 14:33:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "yaml"
|
|
|
|
|
|
|
|
#
|
|
|
|
# 2 different formats are accepted:
|
|
|
|
#
|
2020-04-14 10:17:00 -04:00
|
|
|
# == JSON format
|
|
|
|
#
|
|
|
|
# bin/rake themes:install -- '--{"discourse-something": "https://github.com/discourse/discourse-something"}'
|
|
|
|
# OR
|
|
|
|
# bin/rake themes:install -- '--{"discourse-something": {"url": "https://github.com/discourse/discourse-something", default: true}}'
|
2019-07-04 14:33:05 -04:00
|
|
|
#
|
2020-04-14 10:17:00 -04:00
|
|
|
# == YAML file formats
|
|
|
|
#
|
|
|
|
# theme_name: https://github.com/example/theme.git
|
|
|
|
# OR
|
2019-07-04 14:33:05 -04:00
|
|
|
# theme_name:
|
2020-04-14 10:17:00 -04:00
|
|
|
# url: https://github.com/example/theme_name.git
|
|
|
|
# branch: "master"
|
|
|
|
# private_key: ""
|
|
|
|
# default: false
|
|
|
|
# add_to_all_themes: false # only for components - install on every theme
|
2019-07-04 14:33:05 -04:00
|
|
|
#
|
2020-04-14 10:17:00 -04:00
|
|
|
# In the first form, only the url is required.
|
2019-07-04 14:33:05 -04:00
|
|
|
#
|
|
|
|
desc "Install themes & theme components"
|
2020-04-14 10:17:00 -04:00
|
|
|
task "themes:install" => :environment do |task, args|
|
|
|
|
theme_args = (STDIN.tty?) ? "" : STDIN.read
|
|
|
|
use_json = theme_args == ""
|
|
|
|
|
|
|
|
theme_args =
|
|
|
|
begin
|
2021-10-27 04:39:28 -04:00
|
|
|
use_json ? JSON.parse(ARGV.last.gsub("--", "")) : YAML.safe_load(theme_args)
|
2020-04-14 10:17:00 -04:00
|
|
|
rescue StandardError
|
|
|
|
puts use_json ? "Invalid JSON input. \n#{ARGV.last}" : "Invalid YML: \n#{theme_args}"
|
|
|
|
exit 1
|
|
|
|
end
|
2019-07-04 14:33:05 -04:00
|
|
|
|
2020-04-14 10:17:00 -04:00
|
|
|
log, counts = ThemesInstallTask.install(theme_args)
|
2019-07-04 14:33:05 -04:00
|
|
|
|
|
|
|
puts log
|
|
|
|
|
|
|
|
puts
|
|
|
|
puts "Results:"
|
|
|
|
puts " Installed: #{counts[:installed]}"
|
|
|
|
puts " Updated: #{counts[:updated]}"
|
|
|
|
puts " Errors: #{counts[:errors]}"
|
2021-04-30 12:31:41 -04:00
|
|
|
puts " Skipped: #{counts[:skipped]}"
|
2019-07-04 14:33:05 -04:00
|
|
|
|
|
|
|
exit 1 if counts[:errors] > 0
|
|
|
|
end
|
2020-06-02 23:19:42 -04:00
|
|
|
|
2022-11-04 17:42:07 -04:00
|
|
|
desc "Install themes & theme components from an archive"
|
|
|
|
task "themes:install:archive" => :environment do |task, args|
|
|
|
|
filename = ENV["THEME_ARCHIVE"]
|
|
|
|
RemoteTheme.update_zipped_theme(filename, File.basename(filename))
|
|
|
|
end
|
|
|
|
|
2021-11-25 09:28:28 -05:00
|
|
|
def update_themes
|
2021-11-24 12:12:49 -05:00
|
|
|
Theme
|
|
|
|
.includes(:remote_theme)
|
|
|
|
.where(enabled: true, auto_update: true)
|
|
|
|
.find_each do |theme|
|
2023-04-28 13:11:53 -04:00
|
|
|
theme.transaction do
|
2021-11-25 09:28:28 -05:00
|
|
|
remote_theme = theme.remote_theme
|
2021-12-01 11:57:36 -05:00
|
|
|
next if remote_theme.blank? || remote_theme.remote_url.blank?
|
2023-01-09 07:10:19 -05:00
|
|
|
|
2022-01-21 13:23:26 -05:00
|
|
|
print "Checking '#{theme.name}' for '#{RailsMultisite::ConnectionManagement.current_db}'... "
|
|
|
|
remote_theme.update_remote_version
|
|
|
|
if remote_theme.out_of_date?
|
|
|
|
puts "updating from #{remote_theme.local_version[0..7]} to #{remote_theme.remote_version[0..7]}"
|
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 01:10:15 -04:00
|
|
|
remote_theme.update_from_remote(already_in_transaction: true)
|
2022-01-21 13:23:26 -05:00
|
|
|
else
|
|
|
|
puts "up to date"
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
|
|
|
|
2021-11-25 09:28:28 -05:00
|
|
|
if remote_theme.last_error_text.present?
|
|
|
|
raise RemoteTheme::ImportError.new(remote_theme.last_error_text)
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
2020-11-16 07:44:09 -05:00
|
|
|
rescue => e
|
2021-11-23 06:55:09 -05:00
|
|
|
STDERR.puts "Failed to update '#{theme.name}': #{e}"
|
2022-03-22 13:02:14 -04:00
|
|
|
raise if ENV["RAISE_THEME_ERRORS"] == "1"
|
2022-01-21 13:23:26 -05:00
|
|
|
end
|
2020-11-16 07:44:09 -05:00
|
|
|
end
|
2021-11-25 09:28:28 -05:00
|
|
|
|
|
|
|
true
|
2020-11-16 07:44:09 -05:00
|
|
|
end
|
|
|
|
|
2021-08-19 02:41:58 -04:00
|
|
|
desc "Update themes & theme components"
|
2023-10-18 19:00:23 -04:00
|
|
|
task "themes:update": %w[environment assets:precompile:theme_transpiler] do
|
2021-08-19 02:41:58 -04:00
|
|
|
if ENV["RAILS_DB"].present?
|
|
|
|
update_themes
|
|
|
|
else
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection { update_themes }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-02 23:19:42 -04:00
|
|
|
desc "List all the installed themes on the site"
|
|
|
|
task "themes:audit" => :environment do
|
|
|
|
components = Set.new
|
|
|
|
puts "Selectable themes"
|
|
|
|
puts "-----------------"
|
|
|
|
|
|
|
|
Theme
|
|
|
|
.where("(enabled OR user_selectable) AND NOT component")
|
|
|
|
.each do |theme|
|
|
|
|
puts theme.remote_theme&.remote_url || theme.name
|
|
|
|
theme.child_themes.each do |child|
|
|
|
|
if child.enabled
|
|
|
|
repo = child.remote_theme&.remote_url || child.name
|
|
|
|
components << repo
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
2020-06-02 23:19:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
puts
|
|
|
|
puts "Selectable components"
|
|
|
|
puts "---------------------"
|
|
|
|
components.each { |repo| puts repo }
|
|
|
|
end
|
2021-04-12 08:02:58 -04:00
|
|
|
|
|
|
|
desc "Run QUnit tests of a theme/component"
|
2021-04-28 16:12:08 -04:00
|
|
|
task "themes:qunit", :type, :value do |t, args|
|
|
|
|
type = args[:type]
|
|
|
|
value = args[:value]
|
|
|
|
raise <<~TEXT if !%w[name url id].include?(type) || value.blank?
|
|
|
|
Wrong arguments type:#{type.inspect}, value:#{value.inspect}"
|
2021-04-12 08:02:58 -04:00
|
|
|
Usage:
|
2021-05-20 17:30:12 -04:00
|
|
|
`bundle exec rake "themes:qunit[url,<theme_url>]"`
|
2021-04-28 16:12:08 -04:00
|
|
|
OR
|
2021-05-20 17:30:12 -04:00
|
|
|
`bundle exec rake "themes:qunit[name,<theme_name>]"`
|
2021-04-12 08:02:58 -04:00
|
|
|
OR
|
2021-05-20 17:30:12 -04:00
|
|
|
`bundle exec rake "themes:qunit[id,<theme_id>]"`
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 14:50:55 -05:00
|
|
|
TEXT
|
2021-04-28 16:12:08 -04:00
|
|
|
ENV["THEME_#{type.upcase}"] = value.to_s
|
2021-05-24 08:35:10 -04:00
|
|
|
ENV["QUNIT_RAILS_ENV"] ||= "development" # qunit:test will switch to `test` by default
|
2021-04-28 16:12:08 -04:00
|
|
|
Rake::Task["qunit:test"].reenable
|
|
|
|
Rake::Task["qunit:test"].invoke(1_200_000, "/theme-qunit")
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "Install a theme/component on a temporary DB and run QUnit tests"
|
2021-06-23 00:38:43 -04:00
|
|
|
task "themes:isolated_test" => :environment do |t, args|
|
|
|
|
# This task can be called in a production environment that likely has a bunch
|
|
|
|
# of DISCOURSE_* env vars that we don't want to be picked up by the Unicorn
|
|
|
|
# server that will be spawned for the tests. So we need to unset them all
|
|
|
|
# before we proceed.
|
|
|
|
# Make this behavior opt-in to make it very obvious.
|
|
|
|
if ENV["UNSET_DISCOURSE_ENV_VARS"] == "1"
|
|
|
|
ENV.keys.each do |key|
|
|
|
|
next if !key.start_with?("DISCOURSE_")
|
2021-06-23 07:50:54 -04:00
|
|
|
next if ENV["DONT_UNSET_#{key}"] == "1"
|
2021-06-23 00:38:43 -04:00
|
|
|
ENV[key] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
redis = TemporaryRedis.new
|
|
|
|
redis.start
|
2022-01-08 17:39:46 -05:00
|
|
|
Discourse.redis = redis.instance
|
2021-04-28 16:12:08 -04:00
|
|
|
db = TemporaryDb.new
|
|
|
|
db.start
|
|
|
|
db.migrate
|
|
|
|
ActiveRecord::Base.establish_connection(
|
|
|
|
adapter: "postgresql",
|
|
|
|
database: "discourse",
|
|
|
|
port: db.pg_port,
|
|
|
|
host: "localhost",
|
|
|
|
)
|
|
|
|
|
|
|
|
seeded_themes = Theme.pluck(:id)
|
|
|
|
Rake::Task["themes:install"].invoke
|
|
|
|
themes = Theme.pluck(:name, :id)
|
|
|
|
|
|
|
|
ENV["PGPORT"] = db.pg_port.to_s
|
|
|
|
ENV["PGHOST"] = "localhost"
|
|
|
|
ENV["QUNIT_RAILS_ENV"] = "development"
|
|
|
|
ENV["DISCOURSE_DEV_DB"] = "discourse"
|
2021-06-23 00:38:43 -04:00
|
|
|
ENV["DISCOURSE_REDIS_PORT"] = redis.port.to_s
|
2021-04-28 16:12:08 -04:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
themes.each do |(name, id)|
|
|
|
|
if seeded_themes.include?(id)
|
|
|
|
puts "Skipping seeded theme #{name} (id: #{id})"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
puts "Running tests for theme #{name} (id: #{id})..."
|
|
|
|
Rake::Task["themes:qunit"].reenable
|
|
|
|
Rake::Task["themes:qunit"].invoke("id", id)
|
|
|
|
count += 1
|
|
|
|
end
|
|
|
|
raise "Error: No themes were installed" if count == 0
|
|
|
|
ensure
|
|
|
|
db&.stop
|
|
|
|
db&.remove
|
2021-06-23 00:38:43 -04:00
|
|
|
redis&.remove
|
2021-04-12 08:02:58 -04:00
|
|
|
end
|
2023-11-15 18:11:35 -05:00
|
|
|
|
|
|
|
desc "Clones all official themes."
|
|
|
|
task "themes:clone_all_official" do |task, args|
|
|
|
|
require "theme_metadata"
|
|
|
|
FileUtils.rm_rf("tmp/themes")
|
|
|
|
|
|
|
|
official_themes =
|
|
|
|
ThemeMetadata::OFFICIAL_THEMES.each do |theme_name|
|
|
|
|
repo = "https://github.com/discourse/#{theme_name}"
|
|
|
|
path = File.expand_path("tmp/themes/#{theme_name}")
|
|
|
|
|
|
|
|
attempts = 0
|
|
|
|
|
|
|
|
begin
|
|
|
|
attempts += 1
|
|
|
|
system("git clone #{repo} #{path}", exception: true)
|
|
|
|
rescue StandardError
|
|
|
|
abort("Failed to clone #{repo}") if attempts >= 3
|
|
|
|
STDERR.puts "Failed to clone #{repo}... trying again..."
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|