auto update frontend show page

This commit is contained in:
Peter Chiu 2024-07-22 13:24:22 +08:00
parent b9a2074f11
commit d07d74c0bb
1 changed files with 26 additions and 18 deletions

View File

@ -28,6 +28,7 @@ if bundle_update_flag
default_info_contents = JSON.parse(File.read("#{app_path}/modules/event_news_mod/info.json")) rescue {} default_info_contents = JSON.parse(File.read("#{app_path}/modules/event_news_mod/info.json")) rescue {}
default_event_news_index_info = default_info_contents["frontend"].sort_by{|h| h["filename"].to_f} rescue [] default_event_news_index_info = default_info_contents["frontend"].sort_by{|h| h["filename"].to_f} rescue []
default_event_news_widget_info = default_info_contents["widgets"].sort_by{|h| h["filename"].to_f} rescue [] default_event_news_widget_info = default_info_contents["widgets"].sort_by{|h| h["filename"].to_f} rescue []
default_event_news_show_info = default_info_contents["show"] rescue nil
all_template.each do |folder| all_template.each do |folder|
if !folder.include?('mobile') if !folder.include?('mobile')
if Dir.exist?("#{folder}modules/event_news") if Dir.exist?("#{folder}modules/event_news")
@ -49,15 +50,15 @@ if bundle_update_flag
if flag if flag
puts "Checking event_news index" puts "Checking event_news index"
index_info = info["frontend"].sort_by{|h| h["filename"].to_i} rescue [] index_info = info["frontend"].sort_by{|h| h["filename"].to_i} rescue []
update_flag = false
last_index = index_info.collect{|v| v["filename"].to_s.scan(/\d+/).collect{|v1| v1.to_i}}.flatten.sort[-1] rescue nil last_index = index_info.collect{|v| v["filename"].to_s.scan(/\d+/).collect{|v1| v1.to_i}}.flatten.sort[-1] rescue nil
update_index_flag = false
if !last_index.nil? if !last_index.nil?
idx_regex = /^(\d+[\. \t]*)|[ \t]+$/ idx_regex = /^(\d+[\. \t]*)|[ \t]+$/
default_event_news_index_info.each do |h| default_event_news_index_info.each do |h|
name_without_index = h["name"]["zh_tw"].gsub(idx_regex,'') name_without_index = h["name"]["zh_tw"].gsub(idx_regex,'')
index_info_index = (index_info.index{|hh| hh["name"]["zh_tw"].gsub(idx_regex,'') == name_without_index}||-1 rescue -1) index_info_index = (index_info.index{|hh| hh["name"]["zh_tw"].gsub(idx_regex,'') == name_without_index}||-1 rescue -1)
if index_info_index == -1 if index_info_index == -1
update_flag = true update_index_flag = true
copy_h = h.dup copy_h = h.dup
h.delete("force_cover") h.delete("force_cover")
last_index = last_index + 1 last_index = last_index + 1
@ -71,28 +72,22 @@ if bundle_update_flag
bundler_with_clean_env{%x[cp -f #{app_path}/modules/event_news_mod/#{h["filename"]}.html.erb #{folder}modules/event_news_mod/#{index_info[index_info_index]["filename"]}.html.erb]} bundler_with_clean_env{%x[cp -f #{app_path}/modules/event_news_mod/#{h["filename"]}.html.erb #{folder}modules/event_news_mod/#{index_info[index_info_index]["filename"]}.html.erb]}
end end
end end
if update_flag if update_index_flag
info["frontend"] = index_info info["frontend"] = index_info
puts "Writing json #{info["frontend"].count} in #{info_json_file}" 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 end
puts "Checking event_news widgets" puts "Checking event_news widgets"
widget_info = info["widgets"].sort_by{|h| h["filename"].to_i} rescue [] widget_info = info["widgets"].sort_by{|h| h["filename"].to_i} rescue []
update_flag = false
last_index = widget_info.collect{|v| v["filename"].to_s.scan(/\d+/).collect{|v1| v1.to_i}}.flatten.sort[-1] rescue nil last_index = widget_info.collect{|v| v["filename"].to_s.scan(/\d+/).collect{|v1| v1.to_i}}.flatten.sort[-1] rescue nil
update_widget_flag = false
if !last_index.nil? if !last_index.nil?
idx_regex = /^(\d+[\. \t]*)|[ \t]+$/ idx_regex = /^(\d+[\. \t]*)|[ \t]+$/
default_event_news_widget_info.each do |h| default_event_news_widget_info.each do |h|
name_without_index = h["name"]["zh_tw"].gsub(idx_regex,'') name_without_index = h["name"]["zh_tw"].gsub(idx_regex,'')
widget_info_index = (widget_info.index{|hh| hh["name"]["zh_tw"].gsub(idx_regex,'') == name_without_index}||-1 rescue -1) widget_info_index = (widget_info.index{|hh| hh["name"]["zh_tw"].gsub(idx_regex,'') == name_without_index}||-1 rescue -1)
if widget_info_index == -1 if widget_info_index == -1
update_flag = true update_widget_flag = true
copy_h = h.dup copy_h = h.dup
h.delete("force_cover") h.delete("force_cover")
last_index = last_index + 1 last_index = last_index + 1
@ -106,9 +101,23 @@ if bundle_update_flag
bundler_with_clean_env{%x[cp -f #{app_path}/modules/event_news_mod/_#{h["filename"]}.html.erb #{folder}modules/event_news_mod/_#{widget_info[widget_info_index]["filename"]}.html.erb]} bundler_with_clean_env{%x[cp -f #{app_path}/modules/event_news_mod/_#{h["filename"]}.html.erb #{folder}modules/event_news_mod/_#{widget_info[widget_info_index]["filename"]}.html.erb]}
end end
end end
if update_flag if update_widget_flag
info["widgets"] = widget_info info["widgets"] = widget_info
puts "Writing json #{info["widgets"].count} in #{info_json_file}" puts "Writing json #{info["widgets"].count} in #{info_json_file}"
end
end
update_show_flag = false
if !default_event_news_show_info.nil?
update_show_flag = info["show"] != default_event_news_show_info
info["show"] = default_event_news_show_info
default_event_news_show_info.each do |info_detail|
filename = info_detail['filename']
bundler_with_clean_env{%x[cp -f #{app_path}/modules/event_news_mod/#{filename}.html.erb #{folder}modules/event_news_mod/#{filename}.html.erb]}
end
end
if update_index_flag || update_widget_flag || update_show_flag
begin begin
info_json = JSON.pretty_generate(info).gsub(":[",":[\n").gsub(":{",":{\n") info_json = JSON.pretty_generate(info).gsub(":[",":[\n").gsub(":{",":{\n")
rescue rescue
@ -117,7 +126,6 @@ if bundle_update_flag
File.open(info_json_file,"w+"){|f| f.write(info_json)} File.open(info_json_file,"w+"){|f| f.write(info_json)}
end end
end end
end
rescue => e rescue => e
puts e puts e
puts "There has some error when checking event_news widgets" puts "There has some error when checking event_news widgets"