Sha256: b2432c4538600b7f1cb25c22e1f3c676e5be4aa13d554fef0be8e3cb7c7b0b5b

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

module Opal
  module CliRunners
    class Applescript
      def initialize(options)
        unless system('which osalang > /dev/null')
          raise MissingJavaScriptSupport, 'JavaScript Automation is only supported by OS X Yosemite and above.'
        end

        @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-applescript-runner-')
        # tempfile = File.new('opal-applescript-runner.js', 'w') # for debugging
        tempfile.write code
        tempfile.close
        _successful = system_with_output('osascript', '-l', 'JavaScript', tempfile.path , *argv)
      rescue Errno::ENOENT
        raise MissingAppleScript, 'AppleScript is only available on Mac OS X.'
      end

      # Let's support fake IO objects like StringIO
      def system_with_output(env, *cmd)
        if (_io_output = 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-applescript-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 MissingJavaScriptSupport < RunnerError
      end

      class MissingAppleScript < RunnerError
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
opal-0.11.0 lib/opal/cli_runners/applescript.rb