Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 57 lines (44 sloc) 1.642 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
class MainWindowController < NSWindowController
  URL = NSURL.URLWithString('http://minerapp.com')

  def self.controller
    alloc.initWithWindowNibName('MainWindow')
  end

  def self.isSelectorExcludedFromWebScript(selector)
    !%w{ installGem: isGemInstalled:version: }.include?(selector.to_s)
  end

  attr_accessor :webView

  def windowWillLoad
    NSUserDefaults.standardUserDefaults.registerDefaults('WebKitDeveloperExtras' => true)

    # workaround for MacRuby JIT bug
    respondsToSelector('installGem:')
    respondsToSelector('isGemInstalled:version:')
  end

  def windowDidLoad
    window.delegate = self

    @webView.customUserAgent = 'MinerApp'
    @webView.frameLoadDelegate = self
    @webView.windowScriptObject.setValue(self, forKey:'MinerApp')
    @webView.mainFrame.loadRequest(NSURLRequest.requestWithURL(URL))
  end

  def close(sender)
    NSApp.terminate(self)
  end
  alias windowWillClose close

  def isGemInstalled(name, version:version)
    File.exist?(File.join(userGemPath, "gems", "#{name}-#{version}"))
  end

  def installGem(name)
    Dispatch::Queue.concurrent(:default).async do
      NSLog("Install gem `#{name}' to gem path `#{userGemPath}'")
      NSLog(`gem install #{name} --install-dir=#{userGemPath}`)
      performSelectorOnMainThread('finishedInstallingGem:', withObject: [name, $?.success?], waitUntilDone: true)
    end
  end

  def finishedInstallingGem(args)
    name, success = args
    @webView.windowScriptObject.evaluateWebScript("finishedInstallingGem('#{name}', #{success});")
  end

  def userGemPath
    @userGemPath ||= File.join(NSHomeDirectory(), ".gem/ruby/1.8")
  end
end
Something went wrong with that request. Please try again.