Class: Azuki::Plugin

Inherits:
Object
  • Object
show all
Extended by:
Helpers
Includes:
Helpers
Defined in:
lib/azuki/plugin.rb

Defined Under Namespace

Classes: ErrorUpdatingSymlinkPlugin

Constant Summary collapse

DEPRECATED_PLUGINS =
%w(
  azuki-cedar
  azuki-certs
  azuki-credentials
  azuki-kill
  azuki-labs
  azuki-logging
  azuki-netrc
  azuki-pgdumps
  azuki-postgresql
  azuki-releases
  azuki-shared-postgresql
  azuki-sql-console
  azuki-status
  azuki-stop
  azuki-suggest
  pgbackups-automate
  pgcmd
  azuki-fork
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

action, ask, confirm, confirm_billing, confirm_command, create_git_remote, deprecate, display, display_header, display_object, display_row, display_table, error, 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?, home_directory, hprint, hputs, included, included_into, json_decode, json_encode, launchy, line_formatter, longest, output_with_bang, quantify, redisplay, retry_on_exception, run_command, running_on_a_mac?, running_on_windows?, set_buffer, shell, spinner, status, string_distance, styled_array, styled_error, styled_hash, styled_header, suggestion, time_ago, truncate, with_tty

Constructor Details

#initialize(uri) ⇒ Plugin

Returns a new instance of Plugin.



94
95
96
97
# File 'lib/azuki/plugin.rb', line 94

def initialize(uri)
  @uri = uri
  guess_name(uri)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



31
32
33
# File 'lib/azuki/plugin.rb', line 31

def name
  @name
end

#uriObject (readonly)

Returns the value of attribute uri.



31
32
33
# File 'lib/azuki/plugin.rb', line 31

def uri
  @uri
end

Class Method Details

.check_for_deprecation(plugin) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/azuki/plugin.rb', line 80

def self.check_for_deprecation(plugin)
  return unless STDIN.isatty

  if DEPRECATED_PLUGINS.include?(plugin)
    if confirm "The plugin #{plugin} has been deprecated. Would you like to remove it? (y/N)"
      remove_plugin(plugin)
    end
  end
end

.directoryObject



33
34
35
# File 'lib/azuki/plugin.rb', line 33

def self.directory
  File.expand_path("#{home_directory}/.azuki/plugins")
end

.listObject



37
38
39
40
41
# File 'lib/azuki/plugin.rb', line 37

def self.list
  Dir["#{directory}/*"].sort.map do |folder|
    File.basename(folder)
  end
end

.load!Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/azuki/plugin.rb', line 43

def self.load!
  list.each do |plugin|
    check_for_deprecation(plugin)
    next if skip_plugins.include?(plugin)
    load_plugin(plugin)
  end
  # check to see if we are using ddollar/azuki-accounts
  if list.include?('azuki-accounts') && Azuki::Auth.methods.include?(:fetch_from_account)
    # setup netrc to match the default, if one exists
    if  = %x{ git config azuki.account }.chomp
       = Azuki::Auth. rescue nil
      if  && Azuki::Auth.read_credentials != [Azuki::Auth.user, Azuki::Auth.password]
        Azuki::Auth.credentials = [Azuki::Auth.user, Azuki::Auth.password]
        Azuki::Auth.write_credentials
        load("#{File.dirname(__FILE__)}/command/accounts.rb")
        # kill memoization in case '--account' was passed
        Azuki::Auth.instance_variable_set(:@account, nil)
      end
    end
  end
end

.load_plugin(plugin) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/azuki/plugin.rb', line 65

def self.load_plugin(plugin)
  begin
    folder = "#{self.directory}/#{plugin}"
    $: << "#{folder}/lib"    if File.directory? "#{folder}/lib"
    load "#{folder}/init.rb" if File.exists?  "#{folder}/init.rb"
  rescue ScriptError, StandardError => error
    styled_error(error, "Unable to load plugin #{plugin}.")
    false
  end
end

.remove_plugin(plugin) ⇒ Object



76
77
78
# File 'lib/azuki/plugin.rb', line 76

def self.remove_plugin(plugin)
  FileUtils.rm_rf("#{self.directory}/#{plugin}")
end

.skip_pluginsObject



90
91
92
# File 'lib/azuki/plugin.rb', line 90

def self.skip_plugins
  @skip_plugins ||= ENV["SKIP_PLUGINS"].to_s.split(/[ ,]/)
end

Instance Method Details

#installObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/azuki/plugin.rb', line 107

def install
  if File.directory?(path)
    uninstall
  end
  FileUtils.mkdir_p(self.class.directory)
  Dir.chdir(self.class.directory) do
    git("clone #{uri}")
    unless $?.success?
      FileUtils.rm_rf path
      return false
    end
  end
  true
end

#pathObject



103
104
105
# File 'lib/azuki/plugin.rb', line 103

def path
  "#{self.class.directory}/#{name}"
end

#to_sObject



99
100
101
# File 'lib/azuki/plugin.rb', line 99

def to_s
  name
end

#uninstallObject



122
123
124
125
# File 'lib/azuki/plugin.rb', line 122

def uninstall
  ensure_plugin_exists
  FileUtils.rm_r(path)
end

#updateObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/azuki/plugin.rb', line 127

def update
  ensure_plugin_exists
  if File.symlink?(path)
    raise Azuki::Plugin::ErrorUpdatingSymlinkPlugin
  else
    Dir.chdir(path) do
      unless git('config --get branch.master.remote').empty?
        message = git("pull")
        unless $?.success?
          error("Unable to update #{name}.\n" + message)
        end
      else
        error(<<-ERROR)
#{name} is a legacy plugin installation.
Enable updating by reinstalling with `azuki plugins:install`.
ERROR
      end
    end
  end
end