Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 711af2cff275
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 4e764e881f75
Choose a head ref
  • 2 commits
  • 4 files changed
  • 1 contributor

Commits on Oct 21, 2014

  1. Copy the full SHA
    4cd5bc0 View commit details
  2. Copy the full SHA
    4e764e8 View commit details
Showing with 66 additions and 4 deletions.
  1. +4 −3 lib/opal/cli.rb
  2. +1 −1 lib/opal/cli_options.rb
  3. +1 −0 lib/opal/cli_runners.rb
  4. +60 −0 lib/opal/cli_runners/apple_script.rb
7 changes: 4 additions & 3 deletions lib/opal/cli.rb
Original file line number Diff line number Diff line change
@@ -70,9 +70,10 @@ def run

def runner
@runner ||= case @runner_type
when :server; CliRunners::Server.new(output, port)
when :nodejs; CliRunners::Nodejs.new(output)
when :phantomjs; CliRunners::Phantomjs.new(output)
when :server; CliRunners::Server.new(output, port)
when :nodejs; CliRunners::Nodejs.new(output)
when :phantomjs; CliRunners::Phantomjs.new(output)
when :applescript; CliRunners::AppleScript.new(output)
else raise ArgumentError, @runner_type.inspect
end
end
2 changes: 1 addition & 1 deletion lib/opal/cli_options.rb
Original file line number Diff line number Diff line change
@@ -79,7 +79,7 @@ def initialize
options[:compile] = true
end

on('--runner RUNNER', %w[nodejs server phantomjs], 'Choose the runner: nodejs (default), server') do |runner|
on('-R', '--runner RUNNER', %w[nodejs server phantomjs applescript], 'Choose the runner: nodejs (default), server') do |runner|
options[:runner] = runner.to_sym
end

1 change: 1 addition & 0 deletions lib/opal/cli_runners.rb
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ class RunnerError < StandardError
end
end

require 'opal/cli_runners/apple_script'
require 'opal/cli_runners/phantomjs'
require 'opal/cli_runners/nodejs'
require 'opal/cli_runners/server'
60 changes: 60 additions & 0 deletions lib/opal/cli_runners/apple_script.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require 'opal/cli_runners'

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

@output ||= output
end
attr_reader :output

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)
io_output = IO.try_convert(output)
return system(env,*cmd) if io_output

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}")
captured_output = File.read tempfile.path
tempfile.close
else
require 'open3'
captured_output, status = Open3.capture2(env,*cmd)
end
output.write captured_output
end

class MissingJavaScriptSupport < RunnerError
end

class MissingAppleScript < RunnerError
end
end
end
end