Skip to content

Instantly share code, notes, and snippets.

@elim
Created June 3, 2020 23:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elim/ab5ebcd3bd7b9f34366fd269275569bf to your computer and use it in GitHub Desktop.
Save elim/ab5ebcd3bd7b9f34366fd269275569bf to your computer and use it in GitHub Desktop.
sider_rubocop_config_updater.rb
require 'bundler'
require 'yaml'
class SiderRuboCopConfigUpdater
IGNORE_GEM_NAME_RE = /rubocop-(?:ast|portal)/.freeze
def initialize(lock_fname: 'Gemfile.lock',
sider_fname: 'sider.yml')
@lock_fname = lock_fname
@sider_fname = sider_fname
end
def update!
updated = updated_sider_settings
File.open(@sider_fname, 'w') do |f|
f.puts(updated)
end
end
private
def lockfile_cop_gem_entries
lockfile = Bundler.read_file(@lock_fname)
gem_specs = Bundler::LockfileParser.new(lockfile).specs
cop_specs = gem_specs
.select {|s| s.name =~ /^rubocop/ }
.reject {|s| s.name.match(IGNORE_GEM_NAME_RE) }
cop_specs.map do |cop|
[cop.name, {
'name' => cop.name,
'version' => cop.version.to_s
}]
end.to_h
end
def sider_gem_entries
raw_entries = sider_settings['linter']['rubocop']['gems']
raw_entries.map {|e| [e['name'], e] }.to_h
end
def sider_settings
@sider_settings ||= YAML.load_file(@sider_fname)
end
def updated_gem_entries
sider_gem_entries
.merge(lockfile_cop_gem_entries)
.sort
.to_h
.values
end
def updated_sider_settings
settings = sider_settings
settings['linter']['rubocop']['gems'] = updated_gem_entries
settings.to_yaml
end
end
if $PROGRAM_NAME == __FILE__
updater = SiderRuboCopConfigUpdater.new
updater.update!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment