announcement-test/temp_file/config/set_global_variable.rb

109 lines
4.6 KiB
Ruby

$:.push File.expand_path("../..", __FILE__)
require 'bundler'
require 'config/boot'
require 'rails'
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)}
require "config/application"
require 'lib/orbit_category/categorizable'
require 'lib/orbit_tag/taggable'
require 'lib/orbit_model/impression'
require 'lib/orbit_model/status'
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 "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'
orm = 'mongoid'
require "impressionist/models/#{orm}/impression.rb"
require "impressionist/models/#{orm}/impressionist/impressionable.rb"
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("#{ENV['PWD']}/app/templates/*").collect{|temp| temp.split('/').last}-['mobile']).first
site.save
end
begin
require 'config/process_share_wrapper'
@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] = ProcessShareWrapper.new(tmp)
end
end
s = Site.first
@shared_hash['current_site'] = ProcessShareWrapper.new({"site"=> s})
@shared_hash['privileged_page_count'] = ProcessShareWrapper.new((s.privileged_page_count rescue 0))
Thread.current[:shared_hash] = @shared_hash
Thread.current[:shared_mutex] = @mutex
rescue LoadError => e
env_pwd = ENV['PWD']
begin
require ::File.expand_path('app/helpers/bundler_helper.rb', env_pwd)
include 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
reset_paths = ["config/application.rb", "config/unicorn.rb", "config/set_global_variable.rb", "config/process_share_wrapper.rb", "lib/orbit_app", "lib/orbit_core_lib.rb"]
all_command = reset_paths.map do |reset_path|
"git clean -f -- #{reset_path} && git reset origin -- #{reset_path} && git checkout -- #{reset_path}"
end.join('&&')
bundler_with_clean_env do
system("cd #{env_pwd} && #{all_command}")
end
puts ['error in unicorn',e,e.backtrace]
end