This commit is contained in:
BoHung Chiu 2021-05-27 17:43:40 +08:00
parent ba1d01d4ff
commit 474ab7d2e3
1 changed files with 56 additions and 53 deletions

View File

@ -2,65 +2,68 @@ $:.push File.expand_path("../lib", __FILE__)
# Maintain your gem's version: # Maintain your gem's version:
require "faq/version" require "faq/version"
env_pwd = ENV['PWD'] bundle_update_flag = ARGV[0]=='update' || ARGV[0]=='install'
app_path = File.expand_path(__dir__) if bundle_update_flag
template_path = env_pwd + '/app/templates' env_pwd = ENV['PWD']
all_template = Dir.glob(template_path+'/*/') app_path = File.expand_path(__dir__)
default_faq_index_info = JSON.parse(File.read("#{app_path}/modules/faq/info.json"))["frontend"].sort_by{|h| h["filename"].to_i} rescue [] template_path = env_pwd + '/app/templates'
all_template.each do |folder| all_template = Dir.glob(template_path+'/*/')
if !folder.include?('mobile') default_faq_index_info = JSON.parse(File.read("#{app_path}/modules/faq/info.json"))["frontend"].sort_by{|h| h["filename"].to_i} rescue []
info_json_file = "#{folder}modules/faq/info.json" all_template.each do |folder|
if File.exist?(info_json_file) if !folder.include?('mobile')
begin info_json_file = "#{folder}modules/faq/info.json"
file_text = File.read(info_json_file) rescue "" if File.exist?(info_json_file)
encode_file_text = file_text.encode("UTF-8", "UTF-8", invalid: :replace, replace: "???") begin
next if (encode_file_text.include?("???") rescue true) file_text = File.read(info_json_file) rescue ""
info = JSON.parse(encode_file_text) rescue {} encode_file_text = file_text.encode("UTF-8", "UTF-8", invalid: :replace, replace: "???")
flag = (info.count != 0 rescue false) next if (encode_file_text.include?("???") rescue true)
if flag info = JSON.parse(encode_file_text) rescue {}
puts "Checking faq index" flag = (info.count != 0 rescue false)
frontend_info = info["frontend"].sort_by{|h| h["filename"].to_i} rescue [] if flag
update_flag = false puts "Checking faq index"
last_index = frontend_info[-1]["filename"].match(/\d+/)[0].to_i rescue nil frontend_info = info["frontend"].sort_by{|h| h["filename"].to_i} rescue []
if !last_index.nil? update_flag = false
default_faq_index_info.each do |h| last_index = frontend_info[-1]["filename"].match(/\d+/)[0].to_i rescue nil
name_without_index = h["name"]["zh_tw"].sub(/\d+/,'') if !last_index.nil?
frontend_info_index = (frontend_info.index{|hh| hh["name"]["zh_tw"].include?(name_without_index)} rescue -1) default_faq_index_info.each do |h|
if frontend_info_index == -1 || frontend_info_index.nil? name_without_index = h["name"]["zh_tw"].sub(/\d+/,'')
update_flag = true frontend_info_index = (frontend_info.index{|hh| hh["name"]["zh_tw"].include?(name_without_index)} rescue -1)
copy_h = h.dup if frontend_info_index == -1 || frontend_info_index.nil?
h.delete("force_cover") update_flag = true
last_index = last_index + 1 copy_h = h.dup
copy_h["filename"] = copy_h["filename"].sub(/\d+/){|ff| last_index.to_s} h.delete("force_cover")
copy_h["name"].keys.each do |locale| last_index = last_index + 1
copy_h["name"][locale] = copy_h["name"][locale].sub(/\d+/){|ff| last_index.to_s} 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
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
end if update_flag
if update_flag info["frontend"] = frontend_info
info["frontend"] = frontend_info puts "Writing json #{info["frontend"].count} in #{info_json_file}"
puts "Writing json #{info["frontend"].count} in #{info_json_file}" begin
begin info_json = JSON.pretty_generate(info).gsub(":[",":[\n").gsub(":{",":{\n")
info_json = JSON.pretty_generate(info).gsub(":[",":[\n").gsub(":{",":{\n") rescue
rescue info_json = info.to_s.gsub("=>",": \n")
info_json = info.to_s.gsub("=>",": \n") end
File.open(info_json_file,"w+"){|f| f.write(info_json)}
end end
File.open(info_json_file,"w+"){|f| f.write(info_json)}
end 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
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