announcement-test/temp_file/config/set_global_variable.rb

172 lines
5.6 KiB
Ruby
Raw Normal View History

2022-05-29 04:59:54 +00:00
$:.push File.expand_path("../..", __FILE__)
require 'bundler'
require 'config/boot'
require 'lib/orbit_category/categorizable'
require 'lib/orbit_tag/taggable'
require 'lib/orbit_model/impression'
require 'lib/orbit_model/status'
require 'rails'
require "mongoid"
require "kaminari/mongoid"
require "mongoid/railtie"
require 'carrierwave/mongoid'
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"
require "mongoid"
require "mongoid/railtie"
require "bson"
require 'app/models/concerns/slug'
require 'app/uploaders/image_uploader'
require 'app/models/tag'
require 'app/models/category'
Mongoid.load!("config/mongoid.yml")
require 'impressionist'
require 'app/models/site'
require 'app/models/page_set' #need for site save
if Site.count == 0
site = Site.new
extra_trans = {}
extra_langs = File.read('config/extra_lang.txt').scan(/.*extra_.*/).select{|v| v.exclude? '#'}[0].to_s.split(/extra_language.*:|,| /).select{|v| !v.empty?}.map{|v| v.to_sym} rescue []
extra_langs.each do |extra_lang|
extra_trans[extra_lang] = 'orbit'
end
site.title_translations = {:en=>"Orbit",:zh_tw=>"Orbit"}.merge(extra_trans)
site.valid_locales = [:en, :zh_tw] + extra_langs
site.in_use_locales = site.valid_locales
site.site_type = "orbit"
site.template = (Dir.glob("#{Rails.root}/app/templates/*").collect{|temp| temp.split('/').last}-['mobile']).first
site.save
end
built_in_ext_lines = File.readlines('built_in_extensions.rb').map{|s| s.split("#")[0].to_s} rescue []
downloaded_ext_lines = File.readlines('downloaded_extensions.rb').map{|s| s.split("#")[0].to_s} rescue []
extras_ext_lines = File.readlines('extra_gems.rb').map{|s| s.split("#")[0].to_s} rescue []
orbit_ext_lines = (built_in_ext_lines + downloaded_ext_lines + extras_ext_lines)
orbit_ext_gems = orbit_ext_lines.map{|l| l.match(/gem\s+['"](\w+)['"]/)[1] rescue nil}.select{|s| s.present?}.uniq
orbit_gem_specs = Gem::Specification.find_all.select{|g| orbit_ext_gems.include?(g.name) & (g.source.class != Bundler::Source::Rubygems)}
Thread.current[:dependency_gems] = orbit_gem_specs.flat_map{|spec| spec.dependent_specs.map{|s| s.name}}.uniq
orbit_gems_paths = orbit_gem_specs.map{|gem| gem.full_gem_path}
orbit_ext_gems_models_paths = orbit_gems_paths.map{|d| Dir.glob("#{d}/app/models/*")}
orbit_models_categorized = orbit_ext_gems_models_paths.map.with_index do |models_paths,i|
gem_name = orbit_gem_specs[i].name
categorized = models_paths.select{|fn| File.read(fn).include?("OrbitCategory::Categorizable") rescue false}.map{|fn| File.basename(fn).split(".")[0].classify}
[gem_name, categorized]
end.to_h
orbit_ext_gems_models = orbit_ext_gems_models_paths.flatten.map{|fn| File.basename(fn).split(".")[0].classify}
File.open("orbit_ext_gems_tmp.txt","w+"){|f| f.write(orbit_ext_gems.to_json)}
File.open("orbit_ext_gems_models_tmp.txt","w+"){|f| f.write(orbit_ext_gems_models.to_json)}
File.open("orbit_categorized_tmp.txt","w+"){|f| f.write(orbit_models_categorized.to_json)}
begin
require 'process_shared'
class ProcesssShareWraper
attr_accessor :data
attr_accessor :need_process
def parse_value(v)
if v[0] == String
v[1].read_string.force_encoding('utf-8')
elsif v[0] == Fixnum
v[1].read_int64
elsif v[0] == Float
v[1].read_float
else
v[1].read_object
end
end
def save_value(h,v)
if h[0] == String
h[1].write_string(v)
elsif h[0] == Fixnum
h[1].write_int64(v)
elsif h[0] == Float
h[1].write_float(v)
else
h[1].write_object(v)
end
end
def initialize(data)
self.need_process = true
def process_value(v)
case v
when String
str_len = v.bytesize * 2
str_len = 250 if str_len < 250
tmp = ProcessShared::SharedMemory.new(:uint8, str_len)
tmp.write_string(v)
[String,tmp]
when Fixnum
tmp = ProcessShared::SharedMemory.new(:int64)
tmp.write_int64(v)
[Fixnum,tmp]
when Float
tmp = ProcessShared::SharedMemory.new(:float)
tmp.write_float(v)
[Float,tmp]
else
tmp_obj = Marshal.dump(v)
tmp = ProcessShared::SharedMemory.new(:uint8, tmp_obj.bytesize*2)
tmp.write_string(tmp_obj)
[Object,tmp]
end
end
case data
when Hash
self.data = data.map do |k,v|
[k,process_value(v)]
end.to_h
when Array
self.data = data.map{|v| process_value(v)}
else
self.need_process = false
self.data = process_value(data)
end
end
def value
if self.need_process
case self.data
when Hash
self.data.map{|k,v| [k,parse_value(v)]}.to_h
when Array
self.data.map{|v| parse_value(v)}
end
else
parse_value(self.data)
end
end
def value=(v)
if self.need_process
raise 'You cannot change value.'
else
save_value(self.data,v)
end
end
def [](k)
parse_value(self.data[k])
end
def []=(k,v)
save_value(self.data[k],v)
end
end
@mutex = ProcessShared::Mutex.new
@shared_hash = {}
orbit_gem_specs.each do |v|
v.metadata['_require'].to_s.split(',').each{|m| require(m)}
tmp = v.metadata['global_hash']
tmp = tmp ? eval(tmp) : nil
gem_name = v.name
if tmp
@shared_hash[gem_name] = ProcesssShareWraper.new(tmp)
end
end
Thread.current[:shared_hash] = @shared_hash
Thread.current[:shared_mutex] = @mutex
rescue LoadError => e
puts ['error in unicorn',e,e.backtrace]
end