Class: Bundler::Deployment

Inherits:
Object
  • Object
show all
Defined in:
lib/ndr_dev_support/capistrano/bundler_deployment.rb

Class Method Summary collapse

Class Method Details

.define_task(context, task_method = :task, opts = {}) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ndr_dev_support/capistrano/bundler_deployment.rb', line 15

def self.define_task(context, task_method = :task, opts = {}) # rubocop:disable Metrics/CyclomaticComplexity
  if defined?(Capistrano) && context.is_a?(Capistrano::Configuration)
    context_name = "capistrano"
    role_default = "{:except => {:no_release => true}}"
    error_type = ::Capistrano::CommandError
  else
    context_name = "vlad"
    role_default = "[:app]"
    error_type = ::Rake::CommandFailedError
  end

  roles = context.fetch(:bundle_roles, false)
  opts[:roles] = roles if roles

  context.send :namespace, :bundle do
    send :desc, <<-DESC
      Install the current Bundler environment. By default, gems will be \
      installed to the shared/bundle path. Gems in the development and \
      test group will not be installed. The install command is executed \
      with the --deployment and --quiet flags. If the bundle cmd cannot \
      be found then you can override the bundle_cmd variable to specify \
      which one it should use. The base path to the app is fetched from \
      the :latest_release variable. Set it for custom deploy layouts.

      You can override any of these defaults by setting the variables shown below.

      N.B. bundle_roles must be defined before you require 'bundler/#{context_name}' \
      in your deploy.rb file.

        set :bundle_gemfile,  "Gemfile"
        set :bundle_dir,      File.join(fetch(:shared_path), 'bundle')
        set :bundle_flags,    "--deployment --quiet"
        set :bundle_without,  [:development, :test]
        set :bundle_with,     [:mysql]
        set :bundle_cmd,      "bundle" # e.g. "/opt/ruby/bin/bundle"
        set :bundle_roles,    #{role_default} # e.g. [:app, :batch]
    DESC
    send task_method, :install, opts do
      bundle_cmd     = context.fetch(:bundle_cmd, "bundle")
      bundle_flags   = context.fetch(:bundle_flags, "--deployment --quiet")
      bundle_dir     = context.fetch(:bundle_dir, File.join(context.fetch(:shared_path), "bundle"))
      bundle_gemfile = context.fetch(:bundle_gemfile, "Gemfile")
      bundle_without = [*context.fetch(:bundle_without, [:development, :test])].compact
      bundle_with    = [*context.fetch(:bundle_with, [])].compact
      app_path = context.fetch(:latest_release)
      if app_path.to_s.empty?
        raise error_type.new("Cannot detect current release path - make sure you have deployed at least once.")
      end
      # Separate out flags that need to be sent to `bundle config` with Bundler 4
      if bundle_flags.include?('--deployment')
        bundle_flags = bundle_flags.split(/ /).grep_v('--deployment').join(' ')
        config_settings = ['deployment true']
      else
        config_settings = []
      end

      args = ["--gemfile #{File.join(app_path, bundle_gemfile)}"]
      config_settings << "path #{bundle_dir}" unless bundle_dir.to_s.empty?
      args << bundle_flags.to_s
      config_settings << "without #{bundle_without.join(" ")}" unless bundle_without.empty?
      config_settings << "with #{bundle_with.join(" ")}" unless bundle_with.empty?

      bundle_cmds = config_settings.collect do |settings|
        "#{bundle_cmd} config set --local #{settings}"
      end + ["#{bundle_cmd} install #{args.join(" ")}"]
      run "cd #{app_path} && #{bundle_cmds.join(' && ')}"
    end
  end
end