Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Commit

Permalink
Auto merge of #4891 - bundler:seg-feature-flag, r=indirect
Browse files Browse the repository at this point in the history
Add FeatureFlag class

Main use will be code showing what is true setting vs what is flagged

Also can be used to develop for future major versions without needing a separate dev branch
  • Loading branch information
homu committed Aug 20, 2016
2 parents 9fe7b6c + 4a78799 commit 58d8edb
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 8 deletions.
5 changes: 5 additions & 0 deletions lib/bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module Bundler
autoload :EndpointSpecification, "bundler/endpoint_specification"
autoload :Env, "bundler/env"
autoload :Fetcher, "bundler/fetcher"
autoload :FeatureFlag, "bundler/feature_flag"
autoload :GemHelper, "bundler/gem_helper"
autoload :GemHelpers, "bundler/gem_helpers"
autoload :GemVersionPromoter, "bundler/gem_version_promoter"
Expand Down Expand Up @@ -389,6 +390,10 @@ def git_present?
@git_present = Bundler.which("git") || Bundler.which("git.exe")
end

def feature_flag
@feature_flag ||= FeatureFlag.new(VERSION)
end

def reset!
@root = nil
@settings = nil
Expand Down
4 changes: 2 additions & 2 deletions lib/bundler/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def help(cli = nil)
end

def self.handle_no_command_error(command, has_namespace = $thor_runner)
if Bundler.settings[:plugins] && Bundler::Plugin.command?(command)
if Bundler.feature_flag.plugins? && Bundler::Plugin.command?(command)
return Bundler::Plugin.exec_command(command, ARGV[1..-1])
end

Expand Down Expand Up @@ -491,7 +491,7 @@ def doctor
Doctor.new(options).run
end

if Bundler.settings[:plugins]
if Bundler.feature_flag.plugins?
require "bundler/cli/plugin"
desc "plugin SUBCOMMAND ...ARGS", "manage the bundler plugins"
subcommand "plugin", Plugin
Expand Down
2 changes: 1 addition & 1 deletion lib/bundler/cli/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def run
# rubygems plugins sometimes hook into the gem install process
Gem.load_env_plugins if Gem.respond_to?(:load_env_plugins)

Plugin.gemfile_install(Bundler.default_gemfile) if Bundler.settings[:plugins]
Plugin.gemfile_install(Bundler.default_gemfile) if Bundler.feature_flag.plugins?

definition = Bundler.definition
definition.validate_ruby!
Expand Down
2 changes: 1 addition & 1 deletion lib/bundler/cli/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(options, gems)
def run
Bundler.ui.level = "error" if options[:quiet]

Plugin.gemfile_install(Bundler.default_gemfile) if Bundler.settings[:plugins]
Plugin.gemfile_install(Bundler.default_gemfile) if Bundler.feature_flag.plugins?

sources = Array(options[:source])
groups = Array(options[:group]).map(&:to_sym)
Expand Down
27 changes: 27 additions & 0 deletions lib/bundler/feature_flag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true
module Bundler
class FeatureFlag
def self.settings_flag(flag)
unless Bundler::Settings::BOOL_KEYS.include?(flag.to_s)
raise "Cannot use `#{flag}` as a settings feature flag since it isn't a bool key"
end
define_method("#{flag}?") { Bundler.settings[flag] }
end

(1..10).each {|v| define_method("bundler_#{v}_mode?") { major_version >= v } }

settings_flag :allow_offline_install
settings_flag :plugins

def initialize(bundler_version)
@bundler_version = Gem::Version.create(bundler_version)
end

def major_version
@bundler_version.segments.first
end
private :major_version

class << self; private :settings_flag; end
end
end
2 changes: 1 addition & 1 deletion lib/bundler/fetcher/compact_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def compact_fetcher
begin
downloader.fetch(fetch_uri + path, headers)
rescue NetworkDownError => e
raise unless Bundler.settings[:allow_offline_install] && headers["If-None-Match"]
raise unless Bundler.feature_flag.allow_offline_install? && headers["If-None-Match"]
Bundler.ui.warn "Using the cached data for the new index because of a network error: #{e}"
Net::HTTPNotModified.new(nil, nil, nil)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/bundler/inline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def Bundler.root
end
ENV["BUNDLE_GEMFILE"] ||= "Gemfile"

Bundler::Plugin.gemfile_install(&gemfile) if Bundler.settings[:plugins]
Bundler::Plugin.gemfile_install(&gemfile) if Bundler.feature_flag.plugins?
builder = Bundler::Dsl.new
builder.instance_eval(&gemfile)

Expand Down
2 changes: 1 addition & 1 deletion lib/bundler/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def add_hook(event, &block)
#
# @param [String] event
def hook(event, *args, &arg_blk)
return unless Bundler.settings[:plugins]
return unless Bundler.feature_flag.plugins?

plugins = index.hook_plugins(event)
return unless plugins.any?
Expand Down
2 changes: 1 addition & 1 deletion lib/bundler/source/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def git_proxy
def fetch
git_proxy.checkout
rescue GitError
raise unless Bundler.settings[:allow_offline_install]
raise unless Bundler.feature_flag.allow_offline_install?
Bundler.ui.warn "Using cached git data because of network errors"
end
end
Expand Down

0 comments on commit 58d8edb

Please sign in to comment.