Sha256: 8606ca46cb9e8b5d40612379b14e4403a4282b50309d86b6d4fa5d642baf2608

Contents?: true

Size: 856 Bytes

Versions: 1

Compression:

Stored size: 856 Bytes

Contents

require "delegate"
require "erb"
require "open3"

module Autoterm
  class OSAScript
    attr_reader :project

    TEMPLATE_PATH = File.expand_path(
      "../assets/applescript.erb", __FILE__
    ).freeze

    def initialize(project)
      @project = project
    end

    def render
      ERB.new(template, nil, "-").result(RenderContext.for(project))
    end

    def run
      _stdout, stderr, status = Open3.capture3(
        "osascript -",
        stdin_data: render
      )

      raise ExecutionError.new(stderr) unless status.success?
    end

    private

    def template
      File.read(TEMPLATE_PATH)
    end

    class RenderContext < SimpleDelegator
      def self.for(project)
        new(project).instance_eval { binding }
      end

      private

      def escape(string)
        string.to_s.gsub(%{"}, %{\\"})
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
autoterm-0.2.0 lib/autoterm/osascript.rb