From 29cc7d9f2767ce806976afcca780bcaa479fa418 Mon Sep 17 00:00:00 2001 From: Edouard CHIN Date: Wed, 28 Nov 2018 00:38:19 +0100 Subject: [PATCH] Add a way to customize the `DEPENDENCIES_NEXT` env: - Bootboot by default uses `DEPENDENCIES_NEXT` and `DEPENDENCIES_PREVIOUS` environment variables to update your Gemfile.lock or Gemfile_next.lock. This PR adds a way to configure the `DEPENDENCIES` prefix. If one wanted to dualboot when `SHOPIFY_NEXT` env is present, you can just add `Bundler.settigs.set_local('bootboot_env_prefix', 'SHOPIFY')` anywhere in the Gemfile. --- README.md | 10 +++++++++- lib/bootboot.rb | 18 ++++++++++++++++-- lib/bootboot/command.rb | 4 ++-- lib/bootboot/gemfile_next_auto_sync.rb | 8 ++++---- test/bootboot_test.rb | 23 +++++++++++++++++++++++ 5 files changed, 54 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 918a939..46e5ffb 100644 --- a/README.md +++ b/README.md @@ -55,8 +55,16 @@ Dual boot it! ------------ If you want to boot using the dependencies from the `Gemfile_next.lock`, run any bundler command prefixed with the `DEPENDENCIES_NEXT=1` ENV variable. I.e. `DEPENDENCIES_NEXT=1 bundle exec irb`. +Configuration (Optional) +------------------------ +By default Bootboot will use the `DEPENDENCIES_NEXT` environment variable to update your Gemfile_next.lock. You can however configure it. For example, if you want the dualboot to happen when the `SHOPIFY_NEXT` env variable is present, you simply have to add this in your Gemfile: -Keep` the Gemfile_next.lock` in sync +```ruby +# Gemfile +Bundler.settings.set_local('booboot_env_previx', 'SHOPIFY') +``` + +Keep the `Gemfile_next.lock` in sync ------------ When a developer bumps or adds a dependency, Bootboot will ensure that the `Gemfile_next.lock` snapshot gets updated. diff --git a/lib/bootboot.rb b/lib/bootboot.rb index aa52eea..88e5c58 100644 --- a/lib/bootboot.rb +++ b/lib/bootboot.rb @@ -7,11 +7,25 @@ module Bootboot GEMFILE = Bundler.default_gemfile GEMFILE_LOCK = Pathname("#{GEMFILE}.lock") GEMFILE_NEXT_LOCK = Pathname("#{GEMFILE}_next.lock") - DUALBOOT_NEXT = 'DEPENDENCIES_NEXT' - DUALBOOT_PREVIOUS = 'DEPENDENCIES_PREVIOUS' autoload :GemfileNextAutoSync, 'bootboot/gemfile_next_auto_sync' autoload :Command, 'bootboot/command' + + class << self + def env_next + env_prefix + '_NEXT' + end + + def env_previous + env_prefix + '_PREVIOUS' + end + + private + + def env_prefix + Bundler.settings['bootboot_env_prefix'] || 'DEPENDENCIES' + end + end end Bootboot::GemfileNextAutoSync.new.setup diff --git a/lib/bootboot/command.rb b/lib/bootboot/command.rb index a4d3d7a..369b6ec 100644 --- a/lib/bootboot/command.rb +++ b/lib/bootboot/command.rb @@ -15,11 +15,11 @@ def exec(_cmd, _args) f.write(<<-EOM) Plugin.send(:load_plugin, 'bootboot') if Plugin.installed?('bootboot') -if ENV['DEPENDENCIES_NEXT'] +if ENV['#{Bootboot.env_next}'] enable_dual_booting if Plugin.installed?('bootboot') # Add any gem you want here, they will be loaded only when running - # bundler command prefixed with `#{DUALBOOT_NEXT}=1`. + # bundler command prefixed with `#{Bootboot.env_next}=1`. end EOM end diff --git a/lib/bootboot/gemfile_next_auto_sync.rb b/lib/bootboot/gemfile_next_auto_sync.rb index 49d798c..e9b244e 100644 --- a/lib/bootboot/gemfile_next_auto_sync.rb +++ b/lib/bootboot/gemfile_next_auto_sync.rb @@ -32,8 +32,8 @@ def opt_in next if !GEMFILE_NEXT_LOCK.exist? || nothing_changed?(current_definition) || - ENV[DUALBOOT_NEXT] || - ENV[DUALBOOT_PREVIOUS] + ENV[Bootboot.env_next] || + ENV[Bootboot.env_previous] update!(current_definition) end @@ -62,9 +62,9 @@ def update!(current_definition) def which_env if Bundler.default_lockfile.to_s =~ /_next\.lock/ - DUALBOOT_PREVIOUS + Bootboot.env_previous else - DUALBOOT_NEXT + Bootboot.env_next end end diff --git a/test/bootboot_test.rb b/test/bootboot_test.rb index d3afd0c..9de0b89 100644 --- a/test/bootboot_test.rb +++ b/test/bootboot_test.rb @@ -60,6 +60,29 @@ def test_sync_the_gemfile_next_after_removal_of_gem end end + def test_sync_the_gemfile_next_after_installation_of_new_gem_with_custom_bootboot_env + write_gemfile("source 'https://rubygems.org'\n#{plugin}\n") do |file, _dir| + File.write(file, <<-EOM, mode: 'a') + Bundler.settings.set_local('bootboot_env_prefix', 'SHOPIFY') + + if ENV['SHOPIFY_NEXT'] + gem 'minitest', '5.11.3' + end + EOM + + run_bundler_command('bundle bootboot', file.path) + + run_bundler_command('bundle install', file.path, env: { 'SHOPIFY_NEXT' => '1' }) + output = run_bundler_command( + 'bundle exec ruby -e "require \'minitest\';puts Minitest::VERSION"', + file.path, + env: { 'SHOPIFY_NEXT' => '1' } + ) + + assert_equal '5.11.3', output.strip + end + end + def test_sync_the_gemfile_next_after_update_of_gem write_gemfile do |file, _dir| FileUtils.cp("#{file.path}.lock", gemfile_next(file))