Class: Rascut::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/rascut/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

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

#configObject (readonly)

Returns the value of attribute config.



64
65
66
# File 'lib/rascut/command.rb', line 64

def config
  @config
end

#file_observerObject (readonly)

Returns the value of attribute file_observer.



64
65
66
# File 'lib/rascut/command.rb', line 64

def file_observer
  @file_observer
end

#loggerObject

Returns the value of attribute logger.



13
14
15
# File 'lib/rascut/command.rb', line 13

def logger
  @logger
end

#rootObject (readonly)

Returns the value of attribute root.



64
65
66
# File 'lib/rascut/command.rb', line 64

def root
  @root
end

#target_scriptObject (readonly)

Returns the value of attribute target_script.



64
65
66
# File 'lib/rascut/command.rb', line 64

def target_script
  @target_script
end

#wrapperObject (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_procObject



105
106
107
108
109
# File 'lib/rascut/command.rb', line 105

def compile_success_proc
  if @httpd
    @httpd.reload!
  end
end

#exitObject



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_handlerObject



75
76
77
# File 'lib/rascut/command.rb', line 75

def file_update_handler
  @wrapper.compile
end

#init_pluginsObject



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_loopObject



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|
    flashlog_timestamp ||= log.mtime

    log.open('r') do |f|
      f.read
      loop do
        if log.mtime > flashlog_timestamp
          f.rewind
          text = f.read
          if text.length == 0
            f.rewind
            text = f.read
          end
          logger.info("FLASHLOG\n" + text) unless text.strip.empty?
          flashlog_timestamp = 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_signalsObject



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_intObject



128
129
130
131
# File 'lib/rascut/command.rb', line 128

def sig_int
  logger.debug 'SIG_INT'
  self.exit()
end

#sig_usr2Object



133
134
135
136
# File 'lib/rascut/command.rb', line 133

def sig_usr2
  logger.debug 'SIG_USR2'
  @wrapper.compile
end

#start_serverObject



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