Class: Rascut::Command
- Inherits:
-
Object
- Object
- Rascut::Command
- Defined in:
- lib/rascut/command.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#file_observer ⇒ Object
readonly
Returns the value of attribute file_observer.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#target_script ⇒ Object
readonly
Returns the value of attribute target_script.
-
#wrapper ⇒ Object
readonly
Returns the value of attribute wrapper.
Instance Method Summary collapse
- #compile_success_proc ⇒ Object
- #exit ⇒ Object
- #file_update_handler ⇒ Object
- #init_plugins ⇒ Object
-
#initialize ⇒ Command
constructor
A new instance of Command.
- #read_log_loop ⇒ Object
- #run(argv) ⇒ Object
- #setting_signals ⇒ Object
- #sig_int ⇒ Object
- #sig_usr2 ⇒ Object
- #start_server ⇒ Object
Constructor Details
#initialize ⇒ Command
Returns a new instance of Command.
10 11 12 |
# File 'lib/rascut/command.rb', line 10 def initialize @logger = Logger.new(STDOUT) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
64 65 66 |
# File 'lib/rascut/command.rb', line 64 def config @config end |
#file_observer ⇒ Object (readonly)
Returns the value of attribute file_observer.
64 65 66 |
# File 'lib/rascut/command.rb', line 64 def file_observer @file_observer end |
#logger ⇒ Object
Returns the value of attribute logger.
13 14 15 |
# File 'lib/rascut/command.rb', line 13 def logger @logger end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
64 65 66 |
# File 'lib/rascut/command.rb', line 64 def root @root end |
#target_script ⇒ Object (readonly)
Returns the value of attribute target_script.
64 65 66 |
# File 'lib/rascut/command.rb', line 64 def target_script @target_script end |
#wrapper ⇒ Object (readonly)
Returns the value of attribute wrapper.
64 65 66 |
# File 'lib/rascut/command.rb', line 64 def wrapper @wrapper end |
Instance Method Details
#compile_success_proc ⇒ Object
105 106 107 108 109 |
# File 'lib/rascut/command.rb', line 105 def compile_success_proc if @httpd @httpd.reload! end end |
#exit ⇒ Object
138 139 140 141 142 143 144 145 146 |
# File 'lib/rascut/command.rb', line 138 def exit logger.info 'exiting...' begin @wrapper.close rescue Exception => e logger.error e.inspect end Kernel::exit 1 end |
#file_update_handler ⇒ Object
75 76 77 |
# File 'lib/rascut/command.rb', line 75 def file_update_handler @wrapper.compile end |
#init_plugins ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/rascut/command.rb', line 66 def init_plugins @config[:plugin].each do |name| klass_name = name.gsub(/(^|_)(.)/) { $2.upcase } logger.info "Load Plugin: #{klass_name}" require "rascut/plugin/#{name}" ::Rascut::Plugin.const_get(klass_name).new(self).run end if @config[:plugin] end |
#read_log_loop ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rascut/command.rb', line 79 def read_log_loop log = Pathname.new(@config.params[:flashlog]) return unless (log && log.file?) Thread.new(log) do |log| ||= log.mtime log.open('r') do |f| f.read loop do if log.mtime > f.rewind text = f.read if text.length == 0 f.rewind text = f.read end logger.info("FLASHLOG\n" + text) unless text.strip.empty? = log.mtime end sleep 1 end end end end |
#run(argv) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rascut/command.rb', line 15 def run(argv) @config = Config.new if ENV['HOME'] home = Pathname.new ENV['HOME'] end home.join('.rascut').mkpath if home if home && home.join('.rascutrc').readable? @config.merge_config home.join('.rascutrc') end if File.readable?('.rascut') @config.merge_config('.rascut') end @config.parse_argv!(argv) unless @target_script = argv.first warn 'Target script is not found.' Kernel::exit 1 end @root = Pathname.new(@target_script).dirname.realpath @wrapper = FcshWrapper.new(@target_script, @config) start_server if @config[:server] setting_signals @wrapper.hooks[:compile_success] << method(:compile_success_proc) if @config[:file_observing] @file_observer = FileObserver.new(@config[:observe_files], :interval => @config[:interval], :ext => @config[:ext], :logger => @config[:logger], :update_handler => method(:file_update_handler)) @file_observer.run end read_log_loop if @config[:flashlog] init_plugins @wrapper.compile #readline_loop Thread.stop end |
#setting_signals ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/rascut/command.rb', line 117 def setting_signals methods(true).each do |mname| if m = mname.match(/^sig_(.+)$/) begin Signal.trap(m[1].upcase) { method(mname).call } rescue ArgumentError end end end end |
#sig_int ⇒ Object
128 129 130 131 |
# File 'lib/rascut/command.rb', line 128 def sig_int logger.debug 'SIG_INT' self.exit() end |
#sig_usr2 ⇒ Object
133 134 135 136 |
# File 'lib/rascut/command.rb', line 133 def sig_usr2 logger.debug 'SIG_USR2' @wrapper.compile end |
#start_server ⇒ Object
111 112 113 114 115 |
# File 'lib/rascut/command.rb', line 111 def start_server require 'rascut/httpd' @httpd = Httpd.new(self) @httpd.start end |