Auto update faq modules.

This commit is contained in:
BoHung Chiu 2021-05-27 17:42:17 +08:00
parent eca5ae8a98
commit ba1d01d4ff
2 changed files with 55 additions and 5 deletions

View File

@ -6,13 +6,62 @@ env_pwd = ENV['PWD']
app_path = File.expand_path(__dir__) app_path = File.expand_path(__dir__)
template_path = env_pwd + '/app/templates' template_path = env_pwd + '/app/templates'
all_template = Dir.glob(template_path+'/*/') all_template = Dir.glob(template_path+'/*/')
default_faq_index_info = JSON.parse(File.read("#{app_path}/modules/faq/info.json"))["frontend"].sort_by{|h| h["filename"].to_i} rescue []
all_template.each do |folder| all_template.each do |folder|
if !folder.include?('mobile') if !folder.include?('mobile')
begin info_json_file = "#{folder}modules/faq/info.json"
puts "updating faq index page" if File.exist?(info_json_file)
Bundler.with_clean_env{%x[cp -f #{app_path}/modules/faq/faq_index2.html.erb #{folder}modules/faq/faq_index2.html.erb]} begin
rescue file_text = File.read(info_json_file) rescue ""
puts "There has some error when updating faq index page." encode_file_text = file_text.encode("UTF-8", "UTF-8", invalid: :replace, replace: "???")
next if (encode_file_text.include?("???") rescue true)
info = JSON.parse(encode_file_text) rescue {}
flag = (info.count != 0 rescue false)
if flag
puts "Checking faq index"
frontend_info = info["frontend"].sort_by{|h| h["filename"].to_i} rescue []
update_flag = false
last_index = frontend_info[-1]["filename"].match(/\d+/)[0].to_i rescue nil
if !last_index.nil?
default_faq_index_info.each do |h|
name_without_index = h["name"]["zh_tw"].sub(/\d+/,'')
frontend_info_index = (frontend_info.index{|hh| hh["name"]["zh_tw"].include?(name_without_index)} rescue -1)
if frontend_info_index == -1 || frontend_info_index.nil?
update_flag = true
copy_h = h.dup
h.delete("force_cover")
last_index = last_index + 1
copy_h["filename"] = copy_h["filename"].sub(/\d+/){|ff| last_index.to_s}
copy_h["name"].keys.each do |locale|
copy_h["name"][locale] = copy_h["name"][locale].sub(/\d+/){|ff| last_index.to_s}
end
frontend_info << copy_h
Bundler.with_clean_env{%x[cp -f #{app_path}/modules/faq/#{h["filename"]}.html.erb #{folder}modules/faq/#{copy_h["filename"]}.html.erb]}
elsif h["force_cover"] == "true"
Bundler.with_clean_env{%x[cp -f #{app_path}/modules/faq/#{h["filename"]}.html.erb #{folder}modules/faq/#{frontend_info[frontend_info_index]["filename"]}.html.erb]}
end
end
if update_flag
info["frontend"] = frontend_info
puts "Writing json #{info["frontend"].count} in #{info_json_file}"
begin
info_json = JSON.pretty_generate(info).gsub(":[",":[\n").gsub(":{",":{\n")
rescue
info_json = info.to_s.gsub("=>",": \n")
end
File.open(info_json_file,"w+"){|f| f.write(info_json)}
end
end
end
rescue => e
puts e
puts e.backtrace
puts "There has some error when checking faq index"
end
else
if !Dir.exist?(File.dirname(info_json_file)) && Dir.exist?(File.dirname(File.dirname(info_json_file)))
Bundler.with_clean_env{system ('cp -r '+ app_path + '/modules/ ' + folder)}
end
end end
end end
end end

View File

@ -10,6 +10,7 @@
}, },
{ {
"filename" : "faq_index2", "filename" : "faq_index2",
"force_cover": "true",
"name" : { "name" : {
"zh_tw" : "2. 手風琴列表 ( 模組標題, 問題 )", "zh_tw" : "2. 手風琴列表 ( 模組標題, 問題 )",
"en" : "2. Accordion list ( widget-title, question )" "en" : "2. Accordion list ( widget-title, question )"