Class: DependencyBumper::Updater

Inherits:
Object
  • Object
show all
Defined in:
lib/dependency_bumper/updater.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, cmd_ln_opt) ⇒ Updater

Returns a new instance of Updater.



9
10
11
12
# File 'lib/dependency_bumper/updater.rb', line 9

def initialize(config, cmd_ln_opt)
  @config = config
  @cmd_ln_opt = cmd_ln_opt
end

Instance Attribute Details

#cmd_ln_optObject (readonly)

Returns the value of attribute cmd_ln_opt.



7
8
9
# File 'lib/dependency_bumper/updater.rb', line 7

def cmd_ln_opt
  @cmd_ln_opt
end

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/dependency_bumper/updater.rb', line 7

def config
  @config
end

#reportObject (readonly)

Returns the value of attribute report.



7
8
9
# File 'lib/dependency_bumper/updater.rb', line 7

def report
  @report
end

Instance Method Details

#create_git_branch(report) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dependency_bumper/updater.rb', line 37

def create_git_branch(report)
  branch_name = "gem-bump-#{Time.now.strftime('%d-%m-%Y')}"
  repo_path = exec(['git', 'rev-parse', '--show-toplevel']).first.strip
  git_repo = Git.open(repo_path, log: Console.logger)

  git_config_username_email(git_repo)
  git_repo.checkout(branch_name, new_branch: true)

  output = <<~END
    Updating gems #{Time.now.strftime('%d-%m-%Y')}
    #{report}
  END

  git_repo.add(all: true)
  git_repo.commit(output)
rescue Git::GitExecuteError => e
  Console.logger.error(e)
end

#git_config_username_email(repo) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/dependency_bumper/updater.rb', line 56

def git_config_username_email(repo)
  if repo.config['user.email'].nil? || repo.config['user.name'].nil?
    Console.logger.info('Setting up temporary username and email for committing please update git config')
    repo.config('user.name', 'Your name')
    repo.config('user.email', '[email protected]')
  else
    Console.logger.info('User name and email is set, read to commit')
  end
  repo.config('commit.gpgsign', config.dig('git', 'commit', 'gpgsign').to_s)
end

#major_minor_patch(old_version, new_version) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/dependency_bumper/updater.rb', line 95

def major_minor_patch(old_version, new_version)
  old_versions = old_version.canonical_segments
  new_versions = new_version.canonical_segments

  if new_versions[0] - old_versions[0] > 1
    return :major
  else # check cases like 1.9 to 2.0
    return :major if new_versions[1].nil? || new_versions[1].nil?

    return :minor if new_versions[1] < old_versions[1]
  end

  if new_versions[1] - old_versions[1] > 1
    return :minor
  else
    return :patch if new_versions[2].nil? || new_versions[2].nil?

    return :patch if new_versions[2] < old_versions[2]
  end

  return :patch if new_versions[2] - old_versions[2] >= 1
end

#report_resultObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dependency_bumper/updater.rb', line 67

def report_result
  master_lockfile = Bundler::LockfileParser.new(Bundler.read_file(current_gem_file))

  gems = {}

  master_lockfile.specs.each do |spec|
    gems[spec.name] = { from: spec.version }
  end

  yield

  updated_lockfile = Bundler::LockfileParser.new(Bundler.read_file(current_gem_file))

  updated_lockfile.specs.each do |spec|
    gems[spec.name][:to] = spec.version if gems[spec.name]
  end

  output = ''
  gems.each do |k, v|
    next if v[:from] == v[:to]

    message = "#{k} From #{v[:from]} To #{v[:to]} update level: #{major_minor_patch(v[:from], v[:to])} \n"
    output += message
  end

  output
end

#runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dependency_bumper/updater.rb', line 14

def run
  gem_list = outdated_gems
  return if gem_list == []

  commands = generate_update_arguments(gem_list)
  options = { 'jobs' => Etc.nprocessors }

  @report = report_result do
    commands.each do |group, gems|
      Bundler.settings.temporary(no_install: cmd_ln_opt.fetch(:dry, false)) do
        Bundler::CLI::Update.new(options.merge({ group => true }), gems).run
      end
    end
  end

  if report.empty?
    Console.logger.info('No gem updated')
  else
    Console.logger.info(report)
    create_git_branch(report) if cmd_ln_opt.fetch(:git, false)
  end
end