Class: Modulorails::SelfUpdateGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/modulorails/self_update/self_update_generator.rb

Overview

Author: Matthieu ‘ciappa_m’ Ciappara This updates modulorails by editing the gemfile and running a bundle update

Constant Summary collapse

LATEST_VERSION_URL =
'https://rubygems.org/api/v1/versions/modulorails.json'

Instance Method Summary collapse

Instance Method Details

#create_config_fileObject



14
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
# File 'lib/generators/modulorails/self_update/self_update_generator.rb', line 14

def create_config_file
  Modulorails.deprecator.warn(<<~MESSAGE)
    Modulorails::SelfUpdateGenerator is deprecated and will be removed in version 2.0.
  MESSAGE

  # Get the last published version
  versions = HTTParty.get(LATEST_VERSION_URL).parsed_response
  last_published_version = versions&.reject { |version| Gem::Version.new(version['number']).prerelease? }&.first

  # Do nothing if we could not fetch the last published version (whatever the reason)
  return if last_published_version.nil?

  requirement = last_published_version['ruby_version']
  unless ruby_version_supported_by_next_gem_version?(requirement)
    warn("Next Modulorails version requires Ruby version #{requirement}. You should update.")
    return
  end

  modulorails_version = Gem::Version.new(Modulorails::VERSION)

  # Do nothing if the current version is the same as the last published version
  version = Gem::Version.new(last_published_version['number'])
  return if version <= modulorails_version

  # Add gem to Gemfile
  gsub_file 'Gemfile', /^\s*gem\s['"]modulorails['"].*$/,
            "gem 'modulorails', '= #{version}'"

  # Update the gem and the Gemfile.lock
  run('bundle install -q')
rescue StandardError => e
  warn("[Modulorails] Error: cannot generate health_check configuration: #{e.message}")
end