2019-07-04 13:33:05 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ThemesInstallTask
|
2020-04-14 09:17:00 -05:00
|
|
|
def self.install(themes)
|
2021-04-30 11:31:41 -05:00
|
|
|
counts = { installed: 0, updated: 0, errors: 0, skipped: 0 }
|
2019-07-04 13:33:05 -05:00
|
|
|
log = []
|
2020-04-14 09:17:00 -05:00
|
|
|
themes.each do |name, val|
|
2019-07-04 13:33:05 -05:00
|
|
|
installer = new(val)
|
2020-04-14 09:17:00 -05:00
|
|
|
next if installer.url.nil?
|
2019-07-04 13:33:05 -05:00
|
|
|
|
|
|
|
if installer.theme_exists?
|
2021-04-30 11:31:41 -05:00
|
|
|
if installer.options.fetch(:skip_update, nil)
|
|
|
|
log << "#{name}: is already installed. Skipping update."
|
|
|
|
counts[:skipped] += 1
|
|
|
|
elsif installer.update
|
2021-04-05 16:52:50 -05:00
|
|
|
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
|
2019-07-04 13:33:05 -05:00
|
|
|
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
|
|
|
|
|
2019-11-14 14:10:51 -06:00
|
|
|
[log, counts]
|
2019-07-04 13:33:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :url, :options
|
|
|
|
|
|
|
|
def initialize(url_or_options = nil)
|
|
|
|
if url_or_options.is_a?(Hash)
|
2020-04-14 09:17:00 -05:00
|
|
|
url_or_options.deep_symbolize_keys!
|
|
|
|
@url = url_or_options.fetch(:url, nil)
|
2019-07-04 13:33:05 -05:00
|
|
|
@options = url_or_options
|
|
|
|
else
|
|
|
|
@url = url_or_options
|
|
|
|
@options = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-14 14:20:21 -05:00
|
|
|
def repo_name
|
|
|
|
@url.gsub(Regexp.union("git@github.com:", "https://github.com/", ".git"), "")
|
|
|
|
end
|
|
|
|
|
2019-07-04 13:33:05 -05:00
|
|
|
def theme_exists?
|
2020-04-14 14:20:21 -05:00
|
|
|
@remote_theme =
|
|
|
|
RemoteTheme
|
|
|
|
.where("remote_url like ?", "%#{repo_name}%")
|
|
|
|
.where(branch: @options.fetch(:branch, nil))
|
|
|
|
.first
|
2020-04-14 09:17:00 -05:00
|
|
|
@theme = @remote_theme&.theme
|
|
|
|
@theme.present?
|
2019-07-04 13:33:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def install
|
2020-04-14 09:17:00 -05:00
|
|
|
@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
|
2020-06-29 13:49:02 -05:00
|
|
|
@theme.save
|
2020-04-14 09:17:00 -05:00
|
|
|
add_component_to_all_themes
|
2021-04-05 16:52:50 -05:00
|
|
|
@remote_theme.last_error_text.nil?
|
2020-04-14 09:17:00 -05:00
|
|
|
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
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
2020-04-14 09:17:00 -05:00
|
|
|
parent_theme.add_relative_theme!(:child, @theme)
|
|
|
|
end
|
2019-07-04 13:33:05 -05:00
|
|
|
end
|
|
|
|
end
|