event_news/event_news_mod.gemspec

162 lines
7.7 KiB
Ruby

# encoding: UTF-8
$:.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__)
env_pwd = ENV['PWD']
begin
require ::File.expand_path('app/helpers/bundler_helper.rb', env_pwd)
extend BundlerHelper
rescue LoadError
def bundler_with_clean_env(&block)
if block_given?
if Bundler.respond_to?(:with_unbundled_env)
Bundler.with_unbundled_env(&block)
else
Bundler.with_clean_env(&block)
end
end
end
end
app_path = File.expand_path(__dir__)
template_path = env_pwd + '/app/templates'
all_template = Dir.glob(template_path+'/*/')
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")
bundler_with_clean_env{system ("cp -rf #{folder}modules/event_news #{folder}modules/event_news_mod && rm -rf #{folder}modules/event_news")}
end
if Dir.exist?("#{folder}modules/event_news_mod/event_news")
bundler_with_clean_env{system ("cp -rf #{folder}modules/event_news_mod/event_news/* #{folder}modules/event_news_mod/. && rm -rf #{folder}modules/event_news_mod/event_news")}
end
info_json_file = "#{folder}modules/event_news_mod/info.json"
if File.exist?(info_json_file)
bundler_with_clean_env{system ('cp -f '+ app_path + '/modules/event_news_mod/show.html.erb ' + "#{folder}modules/event_news_mod/.")}
update_event_news_template(folder)
begin
file_text = File.read(info_json_file) rescue ""
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 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
last_index = widget_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_widget_info.each do |h|
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)
if widget_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
widget_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/_#{widget_info[widget_info_index]["filename"]}.html.erb]}
end
end
if update_flag
info["widgets"] = widget_info
puts "Writing json #{info["widgets"].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 "There has some error when checking event_news widgets"
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
# all_template.each do |folder|
# if !folder.include?('mobile')
# moudle_path = "#{folder}modules/universal_table/"
# begin
# if Dir.exist?(File.dirname(moudle_path))
# bundler_with_clean_env{system ('cp -r '+ app_path + '/modules/ ' + folder)}
# end
# rescue => e
# puts "There was some error when updating event_news widget."
# end
# end
# end
end
# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
s.name = "event_news_mod"
s.version = EventNewsMod::VERSION
s.authors = ["RulingDigital"]
s.email = ["orbit@rulingcom.com"]
s.homepage = "http://www.rulingcom.com"
s.summary = "EventNews for Orbit"
s.description = "EventNews for Orbit"
s.license = "MIT"
s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"]
s.test_files = Dir["test/**/*"]
s.add_dependency "rufus-scheduler", "~> 3.6.0"
end