Class: Rascut::FcshWrapper

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

Constant Summary collapse

FCSH_RESULT_RE =
/fcsh: Assigned (\d+) as the compile target id/
FCSH_WAIT_RE =
/^\(fcsh\)\s*$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_script, config) ⇒ FcshWrapper

Returns a new instance of FcshWrapper.



16
17
18
19
20
21
22
23
24
25
# File 'lib/rascut/fcsh_wrapper.rb', line 16

def initialize(target_script, config)
  @target_script = target_script
  @config = config
  @hooks = Hash.new {|h, k| h[k] = []}
  @mutex = Mutex.new
  @compile_mutex = Mutex.new
  @compile_id = nil
  @process = nil
  @not_first_read = nil
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



15
16
17
# File 'lib/rascut/fcsh_wrapper.rb', line 15

def config
  @config
end

#filesObject

Returns the value of attribute files.



15
16
17
# File 'lib/rascut/fcsh_wrapper.rb', line 15

def files
  @files
end

#hooksObject

Returns the value of attribute hooks.



15
16
17
# File 'lib/rascut/fcsh_wrapper.rb', line 15

def hooks
  @hooks
end

#original_filesObject

Returns the value of attribute original_files.



15
16
17
# File 'lib/rascut/fcsh_wrapper.rb', line 15

def original_files
  @original_files
end

#target_scriptObject

Returns the value of attribute target_script.



15
16
17
# File 'lib/rascut/fcsh_wrapper.rb', line 15

def target_script
  @target_script
end

Instance Method Details

#call_hook(name, *args) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/rascut/fcsh_wrapper.rb', line 88

def call_hook(name, *args)
  @hooks[name].each do |hook|
    if hook.arity == 0 || args.length == 0
      hook.call
    else
      hook.call(*args)
    end
  end
end

#closeObject



44
45
46
47
48
49
# File 'lib/rascut/fcsh_wrapper.rb', line 44

def close
  if @process
    @process.close
    call_hook :close
  end
end

#compileObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rascut/fcsh_wrapper.rb', line 61

def compile
  return false if @compile_mutex.locked?

  @compile_mutex.synchronize do
    logger.info "Compile Start"
    out = nil
    @process = IO.popen(@config[:fcsh_cmd] + ' 2>&1', 'r+') unless @process
    if @compile_id
      out = process_sync_exec "compile #{@compile_id}"
    else
      out = process_sync_exec mxmlc_cmd
      if m = out.match(FCSH_RESULT_RE)
        @compile_id = m[1]
      else
        raise "Can't get Compile ID\n" + out.to_s
      end
    end
    logger.info out
    if out.match(/bytes\)/)
      call_hook :compile_success, out
    else
      call_hook :compile_error, out
    end
    call_hook :compile, out
  end
end

#loggerObject



51
52
53
# File 'lib/rascut/fcsh_wrapper.rb', line 51

def logger
  @config[:logger]
end

#mxmlc_cmdObject



55
56
57
58
59
# File 'lib/rascut/fcsh_wrapper.rb', line 55

def mxmlc_cmd
  cmd = ['mxmlc', @config[:compile_config], @target_script].join(' ')
  logger.debug cmd
  cmd
end

#process_sync_exec(str, result_get = true) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/rascut/fcsh_wrapper.rb', line 35

def process_sync_exec(str, result_get = true)
  res = nil
  @mutex.synchronize do
    @process.puts str
    res = read_result(@process) if result_get
  end
  res
end

#read_result(process) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/rascut/fcsh_wrapper.rb', line 98

def read_result(process)
  unless @not_first_read 
    # first_time, FIXME uncool...
    process.expect(FCSH_WAIT_RE)
    @not_first_read = true
  end
  
  process.expect(FCSH_WAIT_RE).first.sub(FCSH_WAIT_RE, '')
end

#reload!Object



27
28
29
30
31
32
33
# File 'lib/rascut/fcsh_wrapper.rb', line 27

def reload!
  if @compile_id
    process_sync_exec("clear #{@compile_id}")
    @compile_id = nil
  end
  call_hook :reload, @compile_id
end