# frozen_string_literal: true # $LOADED_FEATURES.select{ |f| f.index('/app/') || f.index('/lux/') } module Lux::Config extend self LIVE_RELOAD ||= {} # if we have errors in module loading, try to load them one more time @module_error = [] # requires all files recrusive in, with spart sort def require_all(files) files = files.to_s files += '/*' unless files.include?('*') file_errors = [] glob = `echo #{files} #{files}/* #{files}/*/* #{files}/*/*/* #{files}/*/*/*/* |tr ' ' '\n' | grep .rb`.split("\n") glob.reject! { |file| file =~ /_\d+\.rb$/ } glob.select{ |o| o.index('.rb') }.map{ |o| o.split('.rb')[0]}.each do |ruby_file| require ruby_file rescue file_errors.push(ruby_file) end file_errors.each { |klass| @klass = klass require klass } rescue puts "* double load error: require_all #{files}\n #{@klass}.rb\n #{$!.message}".red raise $! end # load specific plugin def plugin(name) files = Dir['%s/lib/plugins/%s/*.rb' % [Lux.fw_root, name]] die('Plugin "%s" load error, no plugin' % name) if files.length == 0 for file in files require file end # Lux.log "* plugin :#{name}" end # preview config in development def show_config for k,v in Lux.config next if v.kind_of?(Hash) puts "* config :#{k} = #{v.kind_of?(Hash) ? '{...}' : v}" end end # check all files and reload if needed def live_require_check! if LIVE_RELOAD.blank? root = Lux.root.to_s for file in $LOADED_FEATURES.select{ |f| f.index(root) || f.index('/lux/') } LIVE_RELOAD[file] = File.mtime(file).to_i end end for file, mtime in LIVE_RELOAD new_file_mtime = File.mtime(file).to_i next if mtime == new_file_mtime LIVE_RELOAD[file] = new_file_mtime Lux.log " Reloaded: .#{file.split(Lux.root.to_s).last.red}" load file end true end def show_load_speed(load_start) speed = ((Time.now - load_start)*1000).round.to_s.sub(/(\d)(\d{3})$/,'\1s \2')+'ms' ram = `ps -o rss -p #{$$}`.chomp.split("\n").last.to_i / 1000 opts = [] opts.push Lux.verbose? ? 'verbose'.yellow : 'no-verbose'.green opts.push Lux.config(:auto_code_reload) ? 'auto-code-reload'.yellow : 'no-code-reload'.green "* #{'Lux'.white} loaded #{Lux.env('RACK_ENV').green} (#{opts.join(', ')}) mode in #{speed.to_s.white}, uses #{ram.to_s.white} MB RAM with total of #{Gem.loaded_specs.keys.length.to_s.white} gems in spec" end end class Object def reload! Lux::Config.live_require_check! end end