2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-24 04:55:17 +08:00
|
|
|
class SiteSettingsTask
|
2019-05-28 14:20:18 +08:00
|
|
|
def self.export_to_hash(include_defaults: false, include_hidden: false)
|
2021-04-07 23:51:19 +08:00
|
|
|
site_settings = SiteSetting.all_settings(include_hidden: include_hidden)
|
2018-03-24 04:55:17 +08:00
|
|
|
h = {}
|
|
|
|
site_settings.each do |site_setting|
|
2018-10-08 08:54:52 +08:00
|
|
|
next if site_setting[:default] == site_setting[:value] if !include_defaults
|
2018-03-24 04:55:17 +08:00
|
|
|
h.store(site_setting[:setting].to_s, site_setting[:value])
|
|
|
|
end
|
|
|
|
h
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.import(yml)
|
2019-05-28 14:20:18 +08:00
|
|
|
h = SiteSettingsTask.export_to_hash(include_defaults: true, include_hidden: true)
|
2018-03-24 04:55:17 +08:00
|
|
|
counts = { updated: 0, not_found: 0, errors: 0 }
|
|
|
|
log = []
|
|
|
|
|
2021-10-27 16:39:28 +08:00
|
|
|
site_settings = YAML.safe_load(yml)
|
2018-03-24 04:55:17 +08:00
|
|
|
site_settings.each do |site_setting|
|
|
|
|
key = site_setting[0]
|
|
|
|
val = site_setting[1]
|
|
|
|
if h.has_key?(key)
|
|
|
|
if val != h[key] #only update if different
|
|
|
|
begin
|
|
|
|
result = SiteSetting.set_and_log(key, val)
|
|
|
|
log << "Changed #{key} FROM: #{result.previous_value} TO: #{result.new_value}"
|
|
|
|
counts[:updated] += 1
|
|
|
|
rescue => e
|
|
|
|
log << "ERROR: #{e.message}"
|
|
|
|
counts[:errors] += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
log << "NOT FOUND: existing site setting not found for #{key}"
|
|
|
|
counts[:not_found] += 1
|
|
|
|
end
|
|
|
|
end
|
2019-11-15 04:10:51 +08:00
|
|
|
[log, counts]
|
2018-03-24 04:55:17 +08:00
|
|
|
end
|
2023-08-29 23:42:52 +08:00
|
|
|
|
|
|
|
def self.names
|
|
|
|
SiteSetting
|
|
|
|
.all_settings(include_hidden: true)
|
|
|
|
.map { |site_setting| site_setting[:setting].to_s }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.rg_installed?
|
|
|
|
!`which rg`.strip.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.directory_path(directory_name)
|
|
|
|
all_the_parent_dir = ENV["ALL_THE_PARENT_DIR"]
|
|
|
|
if all_the_parent_dir
|
|
|
|
File.expand_path(File.join(all_the_parent_dir, directory_name))
|
|
|
|
else
|
|
|
|
File.expand_path(File.join(Dir.pwd, "..", directory_name))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.directories_to_check
|
|
|
|
%w[all-the-themes all-the-custom-themes all-the-plugins all-the-custom-plugins]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.directories
|
|
|
|
directories = [Dir.pwd]
|
|
|
|
SiteSettingsTask.directories_to_check.each do |d|
|
|
|
|
if Dir.exist? SiteSettingsTask.directory_path(d)
|
|
|
|
directories << SiteSettingsTask.directory_path(d)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
directories
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.rg_search_count(term, directory)
|
|
|
|
`rg -l --no-ignore "#{term}" "#{directory}" -g '!config' -g '!db/migrate' | wc -l`.strip.to_i
|
|
|
|
end
|
2018-03-24 04:55:17 +08:00
|
|
|
end
|