2019-07-05 02:33:05 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "yaml"
|
|
|
|
|
|
|
|
#
|
|
|
|
# 2 different formats are accepted:
|
|
|
|
#
|
2020-04-14 22:17:00 +08: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-05 02:33:05 +08:00
|
|
|
#
|
2020-04-14 22:17:00 +08:00
|
|
|
# == YAML file formats
|
|
|
|
#
|
|
|
|
# theme_name: https://github.com/example/theme.git
|
|
|
|
# OR
|
2019-07-05 02:33:05 +08:00
|
|
|
# theme_name:
|
2020-04-14 22:17:00 +08: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-05 02:33:05 +08:00
|
|
|
#
|
2020-04-14 22:17:00 +08:00
|
|
|
# In the first form, only the url is required.
|
2019-07-05 02:33:05 +08:00
|
|
|
#
|
|
|
|
desc "Install themes & theme components"
|
2020-04-14 22:17:00 +08:00
|
|
|
task "themes:install" => :environment do |task, args|
|
|
|
|
theme_args = (STDIN.tty?) ? "" : STDIN.read
|
|
|
|
use_json = theme_args == ""
|
|
|
|
|
|
|
|
theme_args =
|
|
|
|
begin
|
2021-10-27 16:39:28 +08:00
|
|
|
use_json ? JSON.parse(ARGV.last.gsub("--", "")) : YAML.safe_load(theme_args)
|
2020-04-14 22:17:00 +08:00
|
|
|
rescue StandardError
|
|
|
|
puts use_json ? "Invalid JSON input. \n#{ARGV.last}" : "Invalid YML: \n#{theme_args}"
|
|
|
|
exit 1
|
|
|
|
end
|
2019-07-05 02:33:05 +08:00
|
|
|
|
2020-04-14 22:17:00 +08:00
|
|
|
log, counts = ThemesInstallTask.install(theme_args)
|
2019-07-05 02:33:05 +08:00
|
|
|
|
|
|
|
puts log
|
|
|
|
|
|
|
|
puts
|
|
|
|
puts "Results:"
|
|
|
|
puts " Installed: #{counts[:installed]}"
|
|
|
|
puts " Updated: #{counts[:updated]}"
|
|
|
|
puts " Errors: #{counts[:errors]}"
|
2021-05-01 00:31:41 +08:00
|
|
|
puts " Skipped: #{counts[:skipped]}"
|
2019-07-05 02:33:05 +08:00
|
|
|
|
|
|
|
exit 1 if counts[:errors] > 0
|
|
|
|
end
|
2020-06-03 11:19:42 +08:00
|
|
|
|
2024-08-22 19:07:27 +08:00
|
|
|
# env THEME_ARCHIVE - path to the archive
|
|
|
|
# env UPDATE_COMPONENTS - 0 to skip updating components
|
2022-11-05 05:42:07 +08:00
|
|
|
desc "Install themes & theme components from an archive"
|
|
|
|
task "themes:install:archive" => :environment do |task, args|
|
|
|
|
filename = ENV["THEME_ARCHIVE"]
|
2024-08-22 19:07:27 +08:00
|
|
|
update_components = ENV["UPDATE_COMPONENTS"] == "0" ? false : true
|
|
|
|
RemoteTheme.update_zipped_theme(filename, File.basename(filename), update_components:)
|
2022-11-05 05:42:07 +08:00
|
|
|
end
|
|
|
|
|
2021-11-25 22:28:28 +08:00
|
|
|
def update_themes
|
2021-11-25 01:12:49 +08:00
|
|
|
Theme
|
|
|
|
.includes(:remote_theme)
|
|
|
|
.where(enabled: true, auto_update: true)
|
|
|
|
.find_each do |theme|
|
2024-04-25 15:19:23 +08:00
|
|
|
begin
|
|
|
|
theme.transaction do
|
|
|
|
remote_theme = theme.remote_theme
|
|
|
|
next if remote_theme.blank? || remote_theme.remote_url.blank?
|
2023-01-09 20:10:19 +08:00
|
|
|
|
2024-04-25 15:19:23 +08:00
|
|
|
print "Checking '#{theme.name}' for '#{RailsMultisite::ConnectionManagement.current_db}'... "
|
2024-04-25 12:41:14 +08:00
|
|
|
|
2024-04-25 15:19:23 +08:00
|
|
|
remote_theme.update_remote_version
|
2024-04-25 12:41:14 +08:00
|
|
|
|
2024-04-25 15:19:23 +08:00
|
|
|
if remote_theme.out_of_date?
|
|
|
|
puts "updating from #{remote_theme.local_version[0..7]} to #{remote_theme.remote_version[0..7]}"
|
|
|
|
remote_theme.update_from_remote(already_in_transaction: true)
|
|
|
|
else
|
|
|
|
puts "up to date"
|
|
|
|
end
|
2023-01-09 20:10:19 +08:00
|
|
|
|
2024-04-25 15:19:23 +08:00
|
|
|
if remote_theme.last_error_text.present?
|
|
|
|
raise RemoteTheme::ImportError.new(remote_theme.last_error_text)
|
|
|
|
end
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2020-11-16 20:44:09 +08:00
|
|
|
rescue => e
|
2024-04-25 15:19:23 +08:00
|
|
|
$stderr.puts "[#{RailsMultisite::ConnectionManagement.current_db}] Failed to update '#{theme.name}' (#{theme.id}): #{e}"
|
2022-03-23 01:02:14 +08:00
|
|
|
raise if ENV["RAISE_THEME_ERRORS"] == "1"
|
2022-01-22 02:23:26 +08:00
|
|
|
end
|
2020-11-16 20:44:09 +08:00
|
|
|
end
|
2021-11-25 22:28:28 +08:00
|
|
|
|
|
|
|
true
|
2020-11-16 20:44:09 +08:00
|
|
|
end
|
|
|
|
|
2021-08-19 14:41:58 +08:00
|
|
|
desc "Update themes & theme components"
|
2023-10-19 07:00:23 +08:00
|
|
|
task "themes:update": %w[environment assets:precompile:theme_transpiler] do
|
2021-08-19 14:41:58 +08:00
|
|
|
if ENV["RAILS_DB"].present?
|
|
|
|
update_themes
|
|
|
|
else
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection { update_themes }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-03 11:19:42 +08: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 20:10:19 +08:00
|
|
|
end
|
2020-06-03 11:19:42 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
puts
|
|
|
|
puts "Selectable components"
|
|
|
|
puts "---------------------"
|
|
|
|
components.each { |repo| puts repo }
|
|
|
|
end
|
2021-04-12 20:02:58 +08:00
|
|
|
|
|
|
|
desc "Run QUnit tests of a theme/component"
|
2021-04-29 04:12:08 +08:00
|
|
|
task "themes:qunit", :type, :value do |t, args|
|
|
|
|
type = args[:type]
|
|
|
|
value = args[:value]
|
2023-11-17 07:17:32 +08:00
|
|
|
raise <<~TEXT if !%w[name url id ids].include?(type) || value.blank?
|
2021-04-29 04:12:08 +08:00
|
|
|
Wrong arguments type:#{type.inspect}, value:#{value.inspect}"
|
2021-04-12 20:02:58 +08:00
|
|
|
Usage:
|
2021-05-21 05:30:12 +08:00
|
|
|
`bundle exec rake "themes:qunit[url,<theme_url>]"`
|
2021-04-29 04:12:08 +08:00
|
|
|
OR
|
2021-05-21 05:30:12 +08:00
|
|
|
`bundle exec rake "themes:qunit[name,<theme_name>]"`
|
2021-04-12 20:02:58 +08:00
|
|
|
OR
|
2021-05-21 05:30:12 +08:00
|
|
|
`bundle exec rake "themes:qunit[id,<theme_id>]"`
|
2023-11-17 07:17:32 +08:00
|
|
|
OR
|
|
|
|
`bundle exec rake "themes:qunit[ids,<theme_id|theme_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-03-01 03:50:55 +08:00
|
|
|
TEXT
|
2023-11-17 07:17:32 +08:00
|
|
|
|
2021-04-29 04:12:08 +08:00
|
|
|
ENV["THEME_#{type.upcase}"] = value.to_s
|
2021-05-24 20:35:10 +08:00
|
|
|
ENV["QUNIT_RAILS_ENV"] ||= "development" # qunit:test will switch to `test` by default
|
2021-04-29 04:12:08 +08: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 12:38:43 +08: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 19:50:54 +08:00
|
|
|
next if ENV["DONT_UNSET_#{key}"] == "1"
|
2021-06-23 12:38:43 +08:00
|
|
|
ENV[key] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
redis = TemporaryRedis.new
|
|
|
|
redis.start
|
2022-01-09 06:39:46 +08:00
|
|
|
Discourse.redis = redis.instance
|
2021-04-29 04:12:08 +08: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 12:38:43 +08:00
|
|
|
ENV["DISCOURSE_REDIS_PORT"] = redis.port.to_s
|
2021-04-29 04:12:08 +08: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 12:38:43 +08:00
|
|
|
redis&.remove
|
2021-04-12 20:02:58 +08:00
|
|
|
end
|
2023-11-16 07:11:35 +08:00
|
|
|
|
2023-11-17 07:17:32 +08:00
|
|
|
desc "Clones all official themes"
|
2023-11-16 07:11:35 +08:00
|
|
|
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}"
|
2023-11-17 07:17:32 +08:00
|
|
|
path = File.join(Rails.root, "tmp/themes/#{theme_name}")
|
2023-11-16 07:11:35 +08:00
|
|
|
|
|
|
|
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
|
2023-11-17 07:17:32 +08:00
|
|
|
|
2024-05-21 10:38:41 +08:00
|
|
|
desc "pull compatible theme versions for all themes"
|
|
|
|
task "themes:pull_compatible_all" do |t|
|
|
|
|
Dir
|
|
|
|
.glob(File.expand_path("#{Rails.root}/tmp/themes/*"))
|
|
|
|
.select { |f| File.directory? f }
|
|
|
|
.each do |theme_path|
|
|
|
|
next unless File.directory?(theme_path + "/.git")
|
|
|
|
|
|
|
|
theme_name = File.basename(theme_path)
|
|
|
|
checkout_version = Discourse.find_compatible_git_resource(theme_path)
|
|
|
|
|
|
|
|
# Checkout value of the version compat
|
|
|
|
if checkout_version
|
|
|
|
puts "checking out compatible #{theme_name} version: #{checkout_version}"
|
|
|
|
|
|
|
|
update_status =
|
|
|
|
system(
|
|
|
|
"git -C '#{theme_path}' cat-file -e #{checkout_version} || git -C '#{theme_path}' fetch --depth 1 $(git -C '#{theme_path}' rev-parse --symbolic-full-name @{upstream} | awk -F '/' '{print $3}') #{checkout_version}; git -C '#{theme_path}' reset --hard #{checkout_version}",
|
|
|
|
)
|
|
|
|
|
|
|
|
abort("Unable to checkout a compatible theme version") unless update_status
|
|
|
|
else
|
|
|
|
puts "#{theme_name} is already at latest compatible version"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-11-17 07:17:32 +08:00
|
|
|
# Note that this should only be used in CI where it is safe to mutate the database without rolling back since running
|
|
|
|
# the themes QUnit tests requires the themes to be installed in the database.
|
|
|
|
desc "Runs qunit tests for all official themes"
|
2024-06-04 21:16:44 +08:00
|
|
|
task "themes:qunit_all_official" => :environment do |task, args|
|
2023-11-17 07:17:32 +08:00
|
|
|
theme_ids_with_qunit_tests = []
|
|
|
|
|
|
|
|
ThemeMetadata::OFFICIAL_THEMES.each do |theme_name|
|
|
|
|
path = File.join(Rails.root, "tmp/themes/#{theme_name}")
|
|
|
|
|
|
|
|
if Dir.glob("#{File.join(path, "test")}/**/*.{js,es6}").any?
|
|
|
|
theme = RemoteTheme.import_theme_from_directory(path)
|
|
|
|
theme_ids_with_qunit_tests << theme.id
|
|
|
|
else
|
|
|
|
puts "Skipping #{theme_name} as no QUnit tests have been detected"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Rake::Task["themes:qunit"].reenable
|
|
|
|
Rake::Task["themes:qunit"].invoke("ids", theme_ids_with_qunit_tests.join("|"))
|
|
|
|
end
|