Class: Bundler::AutoUpdate::GemUpdater

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gem, gemfile, test_command) ⇒ GemUpdater

Returns a new instance of GemUpdater.



44
45
46
# File 'lib/bundler_auto_update.rb', line 44

def initialize(gem, gemfile, test_command)
  @gem, @gemfile, @test_command = gem, gemfile, test_command
end

Instance Attribute Details

#gemObject (readonly)

Returns the value of attribute gem.



42
43
44
# File 'lib/bundler_auto_update.rb', line 42

def gem
  @gem
end

#gemfileObject (readonly)

Returns the value of attribute gemfile.



42
43
44
# File 'lib/bundler_auto_update.rb', line 42

def gemfile
  @gemfile
end

#test_commandObject (readonly)

Returns the value of attribute test_command.



42
43
44
# File 'lib/bundler_auto_update.rb', line 42

def test_command
  @test_command
end

Instance Method Details

#auto_updateObject



48
49
50
51
52
53
54
55
# File 'lib/bundler_auto_update.rb', line 48

def auto_update
  if updatable?
    Logger.log "Updating #{gem.name}."
    update(:patch) and update(:minor) and update(:major)
  else
    Logger.log "#{gem.name} is not auto-updatable, passing it."
  end
end

#commit_new_versionObject



86
87
88
# File 'lib/bundler_auto_update.rb', line 86

def commit_new_version
  run_cmd "git commit Gemfile Gemfile.lock -m 'Auto update #{gem.name} to version #{gem.version}'"
end

#revert_to_previous_versionObject



90
91
92
93
# File 'lib/bundler_auto_update.rb', line 90

def revert_to_previous_version
  run_cmd "git checkout Gemfile Gemfile.lock"
  gemfile.reload!
end

#run_cmd(cmd) ⇒ Object



99
100
101
# File 'lib/bundler_auto_update.rb', line 99

def run_cmd(cmd)
  CommandRunner.system(cmd)
end

#run_test_suiteObject



95
96
97
# File 'lib/bundler_auto_update.rb', line 95

def run_test_suite
  run_cmd test_command
end

#updatable?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/bundler_auto_update.rb', line 82

def updatable?
  gem.version =~ /^\d+\.\d+\.\d+$/ && gem.options.nil?
end

#update(version_type) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bundler_auto_update.rb', line 57

def update(version_type)
  new_version = gem.last_version(version_type)

  if new_version == gem.version
    Logger.log_indent "Current gem already at latest #{version_type} version. Passing this update."
    return
  end

  Logger.log_indent "Updating to #{version_type} version #{new_version}"

  gem.version = new_version

  if gemfile.update_gem(gem) && run_test_suite
    Logger.log_indent "Test suite ran successfully. Committing changes."
    commit_new_version

    true
  else
    Logger.log_indent "Test suite failed to run. Reverting changes."
    revert_to_previous_version

    false
  end
end