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



101
102
103
104
105
106
107
108
109
# File 'lib/rascut/fcsh_wrapper.rb', line 101

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



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

def compile
  return false if @compile_mutex.locked?

  @compile_mutex.synchronize do
    logger.info "Compile Start"
    call_hook :compile_start
    out = nil
    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_finish
  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

#processObject



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rascut/fcsh_wrapper.rb', line 61

def process
  unless @process
    orig_lang = ENV['LANG']
    ENV['LANG'] = 'C' # for flex3 sdk beta locale
    orig_java_options = ENV['_JAVA_OPTIONS']
    ENV['_JAVA_OPTIONS'] = orig_java_options.to_s + ' -Duser.language=en'
    @process = IO.popen(@config[:fcsh_cmd] + ' 2>&1', 'r+') unless @process
    ENV['LANG'] = orig_lang
    ENV['_JAVA_OPTIONS'] = orig_java_options
  end
  @process
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



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rascut/fcsh_wrapper.rb', line 111

def read_result(process)
  unless @not_first_read 
    # first_time, FIXME uncool...
    begin
      process.expect(FCSH_WAIT_RE)
    rescue NoMethodError => e
      warn 'mxmlc not found. (check PATH)'
      raise e
    end
    @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