Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StdGems check #130

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@
gem 'binding_of_caller'
gem 'bullet'
gem 'overcommit', require: false
gem 'std_gems', git: '[email protected]:infinum/std_gems.git', require: false
HEREDOC
end
# end
Expand Down Expand Up @@ -360,6 +361,9 @@
BundleCheck:
enabled: true

StdGemsCheck:
enabled: true

RuboCop:
enabled: true
on_warn: fail
Expand Down Expand Up @@ -447,6 +451,64 @@

create_file '.github/dependabot.yml', DEPENDABOT_FILE

# .git-hooks/pre_commit/std_gems_check.rb
STD_GEMS_CHECK_FILE = <<-HEREDOC.strip_heredoc
# frozen_string_literal: true

require 'std_gems'

module Overcommit
module Hook
module PreCommit
class StdGemsCheck < Base
GEMFILE_LOCK = 'Gemfile.lock'
DESCRIPTION = 'Check stdgems versions'

def run
return :pass unless gemfile_lock_changed?
return :pass if (gem_diffs = StdGems.gem_diffs).empty?

[:fail, error_message(gem_diffs)]
end

def description
DESCRIPTION
end

private

def gemfile_lock_changed?
applicable_files.any? { |file_path| file_path.end_with?(GEMFILE_LOCK) }
end

def error_message(gem_diffs)
<<~MSG
Your Gemfile requires different version of gems than what is distributed with your Ruby installation
\#{gem_diffs.map { |gem_diff| format_diff_info(gem_diff) }.join("\\n")}

Lock following gem versions in your Gemfile
# stdgems
\#{gem_diffs.map { |gem_diff| format_gem_version_suggestion(gem_diff) }.join("\\n")}

And run `bundle install`
MSG
end

def format_diff_info(gem_diff)
" * \#{gem_diff.gem_name}:\#{gem_diff.bundle_version} (std: \#{gem_diff.std_version})"
end

def format_gem_version_suggestion(gem_diff)
"gem '\#{gem_diff.gem_name}', '\#{gem_diff.std_version}'"
end
end
end
end
end
HEREDOC

create_file '.git-hooks/pre_commit/std_gems_check.rb', STD_GEMS_CHECK_FILE

# .git-hooks/pre_push/zeitwerk_check.rb
ZEITWERK_CHECK_FILE = <<-HEREDOC.strip_heredoc
# frozen_string_literal: true
Expand Down Expand Up @@ -713,6 +775,9 @@ def run
## Overcommit install and sign
run 'overcommit --install'
run 'overcommit --sign'
run 'git add .git-hooks/pre_commit/std_gems_check.rb'
run 'overcommit --sign pre-commit'
run '.git add git-hooks/pre_push/zeitwerk_check.rb'
run 'overcommit --sign pre-push'

# Fix default rubocop errors
Expand Down