# frozen_string_literal: true %w[ bundler ].each(&method(:require)) %w[ build_bundler_definition errors config/fetcher config/file_finder config/validator commands handle_errors/messages handle_errors/build_exception_payload handle_errors/display_to_stdout handle_errors/report request_object request voting track ].each { |file| require "diffend/#{file}" } %w[ versions/local versions/remote ].each { |file| require "diffend/voting/#{file}" } # Diffend main namespace module Diffend # Current plugin version VERSION = '0.2.28' # Diffend homepage HOMEPAGE = 'https://diffend.io' class << self # Registers the plugin and add before install all hook def register Bundler::Plugin.add_hook('before-install-all') do |_| execute end end # Execute diffend plugin def execute return unless enabled? verify_version config = fetch_config Diffend::Voting.call( command, config, Diffend::BuildBundlerDefinition.call( command, Bundler.default_gemfile, Bundler.default_lockfile ) ) 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 def verify_version return if ENV['DIFFEND_DEVELOPMENT'] == 'true' return if installed_version == VERSION build_outdated_version_message(installed_version) .tap(&Bundler.ui.method(:error)) exit 2 end # @return [String] installed plugin version def installed_version Bundler::Plugin .index .plugin_path('diffend') .basename .to_s .split('-') .last end # Checks if plugin is enabled # # @return [Boolean] true if enabled, false otherwise def enabled? Bundler .default_gemfile .read .split("\n") .reject(&:empty?) .map(&:strip) .select { |line| line.start_with?('plugin') } .any? { |line| line.include?('diffend') } end # @param version [Hash] installed version # # @return [String] def build_outdated_version_message(version) <<~MSG \nYou are running an outdated version (#{version}) of the plugin, which will lead to issues. \nPlease upgrade to the latest one (#{VERSION}) by executing "rm -rf .bundle/plugin".\n MSG end # Command that was run with bundle # # @return [String] def command ARGV.first || Bundler.feature_flag.default_cli_command.to_s end # Fetch diffend config file # # @return [OpenStruct, nil] configuration object # # @raise [Errors::MissingConfigurationFile] when no config file def fetch_config Config::Fetcher.call( File.expand_path('..', Bundler.bin_path) ) end end end