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
|
|
|
|
use_json ? JSON.parse(ARGV.last.gsub('--', '')) : YAML::load(theme_args)
|
|
|
|
rescue
|
|
|
|
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
|
|
|
|
|
|
|
if counts[:errors] > 0
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
2020-06-02 23:19:42 -04:00
|
|
|
|
2021-08-19 02:41:58 -04:00
|
|
|
def update_themes
|
2021-02-03 22:44:07 -05:00
|
|
|
Theme.where(auto_update: true, enabled: true).find_each do |theme|
|
2020-11-16 07:44:09 -05:00
|
|
|
begin
|
|
|
|
if theme.remote_theme.present?
|
2021-08-19 02:41:58 -04:00
|
|
|
puts "Updating '#{theme.name}' for '#{RailsMultisite::ConnectionManagement.current_db}'..."
|
2020-11-16 07:44:09 -05:00
|
|
|
theme.remote_theme.update_from_remote
|
2021-02-03 22:44:07 -05:00
|
|
|
theme.save!
|
2021-04-05 18:53:34 -04:00
|
|
|
unless theme.remote_theme.last_error_text.nil?
|
2021-08-19 02:41:58 -04:00
|
|
|
puts "Error updating '#{theme.name}': #{theme.remote_theme.last_error_text}"
|
2021-04-05 18:53:34 -04:00
|
|
|
exit 1
|
|
|
|
end
|
2020-11-16 07:44:09 -05:00
|
|
|
end
|
|
|
|
rescue => e
|
2021-08-19 02:41:58 -04:00
|
|
|
STDERR.puts "Failed to update '#{theme.name}'"
|
2020-11-16 07:44:09 -05:00
|
|
|
STDERR.puts e
|
|
|
|
STDERR.puts e.backtrace
|
2021-04-05 18:53:34 -04:00
|
|
|
exit 1
|
2020-11-16 07:44:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-19 02:41:58 -04:00
|
|
|
desc "Update themes & theme components"
|
|
|
|
task "themes:update" => :environment do
|
|
|
|
if ENV['RAILS_DB'].present?
|
|
|
|
update_themes
|
|
|
|
else
|
|
|
|
RailsMultisite::ConnectionManagement.each_connection do
|
|
|
|
update_themes
|
|
|
|
end
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
puts
|
|
|
|
puts "Selectable components"
|
|
|
|
puts "---------------------"
|
|
|
|
components.each do |repo|
|
|
|
|
puts repo
|
|
|
|
end
|
|
|
|
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]
|
|
|
|
if !%w(name url id).include?(type) || value.blank?
|
2021-04-12 08:02:58 -04:00
|
|
|
raise <<~MSG
|
2021-04-28 16:12:08 -04:00
|
|
|
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>]"`
|
2021-04-12 08:02:58 -04:00
|
|
|
MSG
|
|
|
|
end
|
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(1200000, "/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
|
|
|
|
$redis = redis.instance # rubocop:disable Style/GlobalVars
|
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
|