Class: Rascut::Command

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

asdoc_home, home, path_escape, rascut_db, rascut_db_path, rascut_db_read

Constructor Details

#initializeCommand

Returns a new instance of Command.



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

def initialize
  @logger = Logger.new(STDOUT)
  @httpd = nil
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



62
63
64
# File 'lib/rascut/command.rb', line 62

def config
  @config
end

#file_observerObject (readonly)

Returns the value of attribute file_observer.



62
63
64
# File 'lib/rascut/command.rb', line 62

def file_observer
  @file_observer
end

#loggerObject

Returns the value of attribute logger.



17
18
19
# File 'lib/rascut/command.rb', line 17

def logger
  @logger
end

#rootObject (readonly)

Returns the value of attribute root.



62
63
64
# File 'lib/rascut/command.rb', line 62

def root
  @root
end

#target_scriptObject (readonly)

Returns the value of attribute target_script.



62
63
64
# File 'lib/rascut/command.rb', line 62

def target_script
  @target_script
end

#wrapperObject (readonly)

Returns the value of attribute wrapper.



62
63
64
# File 'lib/rascut/command.rb', line 62

def wrapper
  @wrapper
end

Instance Method Details

#compile_success_procObject



103
104
105
106
107
# File 'lib/rascut/command.rb', line 103

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

#exitObject



136
137
138
139
140
141
142
143
144
145
# File 'lib/rascut/command.rb', line 136

def exit
  logger.info 'exiting...'
  begin
    @wrapper.close 
    @httpd.stop if @httpd
  rescue Exception => e
    logger.error e.inspect
  end
  Kernel::exit 1
end

#file_update_handlerObject



73
74
75
# File 'lib/rascut/command.rb', line 73

def file_update_handler
  @wrapper.compile
end

#init_pluginsObject



64
65
66
67
68
69
70
71
# File 'lib/rascut/command.rb', line 64

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rascut/command.rb', line 77

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



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
# File 'lib/rascut/command.rb', line 19

def run(argv)
  @config = Config.new

  if home.parent && home.parent.join('.rascutrc').readable?
    @config.merge_config home.parent.join('.rascutrc')
  end

  if File.readable?('.rascut') && File.file?('.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



115
116
117
118
119
120
121
122
123
124
# File 'lib/rascut/command.rb', line 115

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



126
127
128
129
# File 'lib/rascut/command.rb', line 126

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

#sig_usr2Object



131
132
133
134
# File 'lib/rascut/command.rb', line 131

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

#start_serverObject



109
110
111
112
113
# File 'lib/rascut/command.rb', line 109

def start_server
  require 'rascut/httpd'
  @httpd = Httpd.new(self)
  @httpd.run
end