Sha256: 7efaf60ac274908f40c0bc40e8dd59e0190d2b414ae98be406728db44ddc5bf4

Contents?: true

Size: 1.64 KB

Versions: 7

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

require 'opal/paths'

module Opal
  module CliRunners
    class Nashorn
      def initialize(options)
        @output = options.fetch(:output, $stdout)
      end
      attr_reader :output, :exit_status

      def puts(*args)
        output.puts(*args)
      end

      def run(code, argv)
        require 'tempfile'
        tempfile = Tempfile.new('opal-nashorn-runner-')
        tempfile.write code
        tempfile.close
        system_with_output({}, 'jjs', tempfile.path, *argv)
      rescue Errno::ENOENT
        raise MissingNashorn, 'Please install JDK to be able to run Opal scripts.'
      end

      # Let's support fake IO objects like StringIO
      def system_with_output(env, *cmd)
        if IO.try_convert(output)
          system(env, *cmd)
          @exit_status = $?.exitstatus
          return
        end

        if RUBY_PLATFORM == 'java'
          # JRuby has issues in dealing with subprocesses (at least up to 1.7.15)
          # @headius told me it's mostly fixed on master, but while we wait for it
          # to ship here's a tempfile workaround.
          require 'tempfile'
          require 'shellwords'
          tempfile = Tempfile.new('opal-nashorn-output')
          system(env, cmd.shelljoin + " > #{tempfile.path}")
          @exit_status = $?.exitstatus
          captured_output = File.read tempfile.path
          tempfile.close
        else
          require 'open3'
          captured_output, status = Open3.capture2(env, *cmd)
          @exit_status = status.exitstatus
        end

        output.write captured_output
      end

      class MissingNashorn < RunnerError
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
opal-1.0.5 lib/opal/cli_runners/nashorn.rb
opal-1.0.4 lib/opal/cli_runners/nashorn.rb
opal-1.0.3 lib/opal/cli_runners/nashorn.rb
opal-1.0.2 lib/opal/cli_runners/nashorn.rb
opal-1.0.1 lib/opal/cli_runners/nashorn.rb
opal-1.0.0 lib/opal/cli_runners/nashorn.rb
opal-1.0.0.beta1 lib/opal/cli_runners/nashorn.rb