Module: Diffend::Plugin

Defined in:
lib/diffend/plugin.rb

Class Method Summary collapse

Class Method Details

.build_outdated_version_message(version) ⇒ String

Parameters:

  • version (Hash)

    installed version

Returns:

  • (String)


109
110
111
112
113
114
# File 'lib/diffend/plugin.rb', line 109

def build_outdated_version_message(version)
  "    \\nYou are running an outdated version (\#{version}) of the plugin, which will lead to issues.\n    \\nPlease upgrade to the latest one (\#{VERSION}) by executing \"rm -rf .bundle/plugin\".\\n\n  MSG\nend\n"

.commandString

Command that was run with bundle

Returns:

  • (String)


119
120
121
# File 'lib/diffend/plugin.rb', line 119

def command
  ARGV.first || ::Bundler.feature_flag.default_cli_command.to_s
end

.enabled?Boolean

Checks if plugin is enabled

Returns:

  • (Boolean)

    true if enabled, false otherwise



95
96
97
98
99
100
101
102
103
104
# File 'lib/diffend/plugin.rb', line 95

def enabled?
  ::Bundler
    .default_gemfile
    .read
    .split("\n")
    .reject(&:empty?)
    .map(&:strip)
    .select { |line| line.start_with?('plugin') }
    .any? { |line| line.include?('diffend') }
end

.executeObject

Execute diffend plugin



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/diffend/plugin.rb', line 45

def execute
  return unless enabled?

  verify_version

  config = Diffend::Config.call

  Diffend::Execute.call(command, config)
rescue Diffend::Errors::HandledException
  return if ENV['DIFFEND_IGNORE_ERRORS'] == 'true'

  exit 255
rescue StandardError => e
  Diffend::HandleErrors::Report.call(
    exception: e,
    config: config,
    message: :unhandled_exception,
    report: true,
    raise_exception: false
  )

  return if ENV['DIFFEND_IGNORE_ERRORS'] == 'true'

  exit 255
end

.installed_versionString

Returns installed plugin version.

Returns:

  • (String)

    installed plugin version



82
83
84
85
86
87
88
89
90
# File 'lib/diffend/plugin.rb', line 82

def installed_version
  ::Bundler::Plugin
    .index
    .plugin_path('diffend')
    .basename
    .to_s
    .split('-')
    .last
end

.registerObject

Registers the plugin and add before install all hook



38
39
40
41
42
# File 'lib/diffend/plugin.rb', line 38

def register
  ::Bundler::Plugin.add_hook('before-install-all') do |_|
    execute
  end
end

.verify_versionObject



71
72
73
74
75
76
77
78
79
# File 'lib/diffend/plugin.rb', line 71

def verify_version
  return if ENV['DIFFEND_DEVELOPMENT'] == 'true'
  return if installed_version == Diffend::VERSION

  build_outdated_version_message(installed_version)
    .tap(&::Bundler.ui.method(:error))

  exit 2
end