Method: Bcpm::Match.run_build_script

Defined in:
lib/bcpm/match.rb

.run_build_script(target_dir, build_file, log_file, target, run_live = false) ⇒ Object

Runs the battlecode Ant script.



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/bcpm/match.rb', line 195

def self.run_build_script(target_dir, build_file, log_file, target, run_live = false)
  if run_live
    Dir.chdir target_dir do
      command = Shellwords.shelljoin(['ant', '-noinput', '-buildfile',
                                      build_file, target])

      # Start the build as a subprocess, dump its output to the queue as
      # string fragments. nil means the subprocess completed.
      queue = Queue.new
      thread = Thread.start do
        IO.popen command do |f|
          begin
            loop { queue << f.readpartial(1024) }
          rescue EOFError
            queue << nil
          end
        end
      end

      build_output = ''
      while fragment = queue.pop
        # Dump the build output to the screen as the simulation happens.
        print fragment
        STDOUT.flush
        build_output << fragment
      
        # Let bcpm carry on when the simulation completes.
        break if build_output.index(run_live)
      end
      build_output << "\n" if build_output[-1] != ?\n
      
      # Pretend everything was put in a log file.
      File.open(log_file, 'wb') { |f| f.write build_output }
      return thread
    end
  else
    command = Shellwords.shelljoin(['ant', '-noinput', '-buildfile',
                                    build_file, '-logfile', log_file, target])
    if /mingw/ =~ RUBY_PLATFORM ||
        (/win/ =~ RUBY_PLATFORM && /darwin/ !~ RUBY_PLATFORM)
      Dir.chdir target_dir do
        output = Kernel.`(command)
        # If there is no log file, dump the output to the log.
        unless File.exist?(log_file)
          File.open(log_file, 'wb') { |f| f.write output }
        end
      end
    else
      pid = fork do
        Dir.chdir target_dir do
          output = Kernel.`(command)
          # If there is no log file, dump the output to the log.
          unless File.exist?(log_file)
            File.open(log_file, 'wb') { |f| f.write output }
          end
        end
      end
      Process.wait pid
    end
  end
end