From dfdefa303239e1d9ff80cbdc5fbeb3ff37a8921d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Sun, 28 Jan 2024 15:09:09 +0800 Subject: [PATCH] Fix bug. Fix bug. --- .../admin/event_news_controller.rb | 2 +- event_news_mod.gemspec | 40 ++++++++++++++++++- update_event_news.rb | 2 +- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/event_news_controller.rb b/app/controllers/admin/event_news_controller.rb index cf136dd..1a42086 100644 --- a/app/controllers/admin/event_news_controller.rb +++ b/app/controllers/admin/event_news_controller.rb @@ -468,7 +468,7 @@ class Admin::EventNewsController < OrbitAdminController content = "UNICORN_PID=\"`fuser tmp/pids/unicorn.sock tmp/sockets/unicorn.sock tmp/unicorn.sock` `cat tmp/pids/unicorn.pid `\" && kill -s USR2 $UNICORN_PID ; n=20; while (kill -0 $UNICORN_PID > /dev/null 2>&1) && test $n -ge 0; do printf '.' && sleep 1 && n=$(( $n - 1 )); done ; if test $n -lt 0; then kill -s TERM $UNICORN_PID; sleep 3; bundle exec unicorn_rails -c config/unicorn.rb -D -E #{Rails.env}; else kill -s QUIT $UNICORN_PID; fi" system(content) end - redirect_to action: :index + redirect_to admin_event_news_index_path end private diff --git a/event_news_mod.gemspec b/event_news_mod.gemspec index d8f749a..22ec4cb 100644 --- a/event_news_mod.gemspec +++ b/event_news_mod.gemspec @@ -3,6 +3,7 @@ $:.push File.expand_path("../lib", __FILE__) # Maintain your gem's version: require "event_news_mod/version" +require "json" bundle_update_flag = ARGV[0]=='update' || ARGV[0]=='install' if bundle_update_flag require File.expand_path("../update_event_news", __FILE__) @@ -24,7 +25,9 @@ if bundle_update_flag app_path = File.expand_path(__dir__) template_path = env_pwd + '/app/templates' all_template = Dir.glob(template_path+'/*/') - default_event_news_widget_info = JSON.parse(File.read("#{app_path}/modules/event_news_mod/info.json"))["widgets"].sort_by{|h| h["filename"].to_i} 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_widget_info = default_info_contents["widgets"].sort_by{|h| h["filename"].to_f} rescue [] all_template.each do |folder| if !folder.include?('mobile') if Dir.exist?("#{folder}modules/event_news") @@ -44,6 +47,41 @@ if bundle_update_flag info = JSON.parse(encode_file_text) rescue {} flag = (info.count != 0 rescue false) if flag + puts "Checking event_news index" + 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 + if !last_index.nil? + idx_regex = /^(\d+[\. \t]*)|[ \t]+$/ + default_event_news_index_info.each do |h| + 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) + if index_info_index == -1 + 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 + index_info << copy_h + bundler_with_clean_env{%x[cp -f #{app_path}/modules/event_news_mod/#{h["filename"]}.html.erb #{folder}modules/event_news_mod/#{copy_h["filename"]}.html.erb]} + elsif h["force_cover"] == "true" + 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 + if update_flag + info["frontend"] = index_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 puts "Checking event_news widgets" widget_info = info["widgets"].sort_by{|h| h["filename"].to_i} rescue [] update_flag = false diff --git a/update_event_news.rb b/update_event_news.rb index 0b1189f..2141a98 100644 --- a/update_event_news.rb +++ b/update_event_news.rb @@ -47,7 +47,7 @@ def add_css_style_for_block(c,head_key,value_key,css_key) [flag,c_tp] end def update_event_news_template(folder) - Dir["#{folder}modules/event_news/*.html.erb"].each do |f| + Dir["#{folder}modules/event_news_mod/*.html.erb"].each do |f| c = File.open(f,'r'){|f1| f1.read}.encode("UTF-8", "UTF-8", invalid: :replace, replace: "???") next if (c.include?("???") rescue true) flag1,c = add_css_style_for_block(c,'speaker-head','speaker','speaker-css')