Class: Heroku::JSPlugin

Inherits:
Object
  • Object
show all
Extended by:
Helpers
Defined in:
lib/heroku/jsplugin.rb

Class Method Summary collapse

Methods included from Helpers

action, app_owner, ask, confirm, confirm_command, create_git_remote, debug, debugging?, default_org_host, deprecate, display, display_header, display_object, display_row, display_table, error, error_log, error_log_path, error_with_failure, error_with_failure=, extended, extended_into, fail, format_bytes, format_date, format_error, format_with_bang, get_terminal_environment, git, has_git?, has_git_remote?, has_http_git_entry_in_netrc, home_directory, hprint, hputs, included, included_into, json_decode, json_encode, launchy, line_formatter, longest, org?, org_host, output_with_bang, quantify, redisplay, retry_on_exception, run_command, running_on_a_mac?, running_on_windows?, set_buffer, shell, spinner, status, stderr_print, stderr_puts, string_distance, styled_array, styled_error, styled_hash, styled_header, suggestion, time_ago, truncate, update_git_remote, warn_if_using_jruby, with_tty

Class Method Details

.archObject


111
112
113
114
115
116
117
118
119
# File 'lib/heroku/jsplugin.rb', line 111

def self.arch
  case RUBY_PLATFORM
  when /i386/
    "386"
  when /x64/
  else
    "amd64"
  end
end

.binObject


82
83
84
85
86
87
88
# File 'lib/heroku/jsplugin.rb', line 82

def self.bin
  if os == 'windows'
    File.join(Heroku::Helpers.home_directory, ".heroku", "heroku-cli.exe")
  else
    File.join(Heroku::Helpers.home_directory, ".heroku", "heroku-cli")
  end
end

.commandsObject


59
60
61
62
63
64
# File 'lib/heroku/jsplugin.rb', line 59

def self.commands
  commands_info['commands']
rescue
  $stderr.puts "error loading plugin commands"
  return []
end

.commands_infoObject


66
67
68
# File 'lib/heroku/jsplugin.rb', line 66

def self.commands_info
  @commands_info ||= json_decode(`#{bin} commands --json`)
end

.install(name) ⇒ Object


70
71
72
# File 'lib/heroku/jsplugin.rb', line 70

def self.install(name)
  system "#{bin} plugins:install #{name}"
end

.is_plugin_installed?(name) ⇒ Boolean

Returns:

  • (Boolean)

48
49
50
# File 'lib/heroku/jsplugin.rb', line 48

def self.is_plugin_installed?(name)
  plugins.any? { |p| p[:name] == name }
end

.load!Object


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
# File 'lib/heroku/jsplugin.rb', line 8

def self.load!
  return unless setup?
  this = self
  topics.each do |topic|
    Heroku::Command.register_namespace(
      :name => topic['name'],
      :description => " #{topic['description']}"
    ) unless Heroku::Command.namespaces.include?(topic['name'])
  end
  commands.each do |plugin|
    help = "\n\n  #{plugin['fullHelp'].split("\n").join("\n  ")}"
    klass = Class.new do
      def initialize(args, opts)
        @args = args
        @opts = opts
      end
    end
    klass.send(:define_method, :run) do
      this.run(plugin['topic'], plugin['command'], ARGV[1..-1])
    end
    Heroku::Command.register_command(
      :command   => plugin['command'] ? "#{plugin['topic']}:#{plugin['command']}" : plugin['topic'],
      :namespace => plugin['topic'],
      :klass     => klass,
      :method    => :run,
      :banner    => plugin['usage'],
      :summary   => " #{plugin['description']}",
      :help      => help
    )
  end
end

.manifestObject


136
137
138
# File 'lib/heroku/jsplugin.rb', line 136

def self.manifest
  @manifest ||= JSON.parse(Excon.get("http://d1gvo455cekpjp.cloudfront.net/master/manifest.json").body)
end

.osObject


121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/heroku/jsplugin.rb', line 121

def self.os
  case RUBY_PLATFORM
  when /darwin|mac os/
    "darwin"
  when /linux/
    "linux"
  when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
    "windows"
  when /openbsd/
    "openbsd"
  else
    raise "unsupported on #{RUBY_PLATFORM}"
  end
end

.pluginsObject


40
41
42
43
44
45
46
# File 'lib/heroku/jsplugin.rb', line 40

def self.plugins
  return [] unless setup?
  @plugins ||= `#{bin} plugins`.lines.map do |line|
    name, version = line.split
    { :name => name, :version => version }
  end
end

.run(topic, command, args) ⇒ Object


106
107
108
109
# File 'lib/heroku/jsplugin.rb', line 106

def self.run(topic, command, args)
  cmd = command ? "#{topic}:#{command}" : topic
  exec self.bin, cmd, *args
end

.setupObject


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/heroku/jsplugin.rb', line 90

def self.setup
  return if File.exist? bin
  $stderr.print "Installing Heroku Toolbelt v4..."
  FileUtils.mkdir_p File.dirname(bin)
  resp = Excon.get(url, :middlewares => Excon.defaults[:middlewares] + [Excon::Middleware::Decompress])
  open(bin, "wb") do |file|
    file.write(resp.body)
  end
  File.chmod(0755, bin)
  if Digest::SHA1.file(bin).hexdigest != manifest['builds'][os][arch]['sha1']
    File.delete bin
    raise 'SHA mismatch for heroku-cli'
  end
  $stderr.puts " done"
end

.setup?Boolean

Returns:

  • (Boolean)

4
5
6
# File 'lib/heroku/jsplugin.rb', line 4

def self.setup?
  @is_setup ||= File.exists? bin
end

.topicsObject


52
53
54
55
56
57
# File 'lib/heroku/jsplugin.rb', line 52

def self.topics
  commands_info['topics']
rescue
  $stderr.puts "error loading plugin topics"
  return []
end

.uninstall(name) ⇒ Object


74
75
76
# File 'lib/heroku/jsplugin.rb', line 74

def self.uninstall(name)
  system "#{bin} plugins:uninstall #{name}"
end

.urlObject


140
141
142
# File 'lib/heroku/jsplugin.rb', line 140

def self.url
  manifest['builds'][os][arch]['url'] + ".gz"
end

.versionObject


78
79
80
# File 'lib/heroku/jsplugin.rb', line 78

def self.version
  `#{bin} version`
end