Skip to content

Commit

Permalink
ADD rubocop (#95)
Browse files Browse the repository at this point in the history
This adds some linting to the project.

Details
* ADD `rubocop` on project and travis
* ADD `memoist` to ease thing
  • Loading branch information
MaximeD authored May 1, 2019
1 parent 6be393b commit 910d30b
Show file tree
Hide file tree
Showing 18 changed files with 168 additions and 120 deletions.
8 changes: 8 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
AllCops:
TargetRubyVersion: 2.5

Metrics/BlockLength:
ExcludedMethods: ['describe', 'context']

Metrics/LineLength:
Max: 100
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ before_install:
rvm:
- 2.6.0
- 2.5.0
- 2.4.0
- 2.3.0
script:
- bundle exec rspec
- bundle exec rubocop
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# Unreleased

Deprecations:
* ruby 2.4 and below are now deprecated

Updates:
* gems and dependencies

Development tools:
* add CI support for ruby 2.6.0
* add sleep stub in specs
* add rubocop

# v3.0.0 (March 10, 2018)

Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec
Expand Down
20 changes: 19 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,27 @@ PATH
gem_updater (3.0.0)
bundler (~> 1.16)
json (~> 2.1)
memoist (~> 0.16.0)
nokogiri (~> 1.8)

GEM
remote: https://rubygems.org/
specs:
ast (2.4.0)
codacy-coverage (2.1.0)
simplecov
diff-lcs (1.3)
docile (1.3.1)
jaro_winkler (1.5.2)
json (2.1.0)
memoist (0.16.0)
mini_portile2 (2.4.0)
nokogiri (1.10.1)
nokogiri (1.10.3)
mini_portile2 (~> 2.4.0)
parallel (1.17.0)
parser (2.6.3.0)
ast (~> 2.4.0)
rainbow (3.0.0)
rake (12.3.2)
rspec (3.8.0)
rspec-core (~> 3.8.0)
Expand All @@ -31,11 +39,20 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.0)
rubocop (0.68.1)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
parser (>= 2.5, != 2.5.1.1)
rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 1.6)
ruby-progressbar (1.10.0)
simplecov (0.16.1)
docile (~> 1.1)
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.2)
unicode-display_width (1.5.0)

PLATFORMS
ruby
Expand All @@ -45,6 +62,7 @@ DEPENDENCIES
gem_updater!
rake
rspec (~> 3.7)
rubocop (~> 0.68.1)
simplecov

BUNDLED WITH
Expand Down
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

require 'bundler/setup'
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new

desc 'Default: run the rspec examples'
task :default => :spec
task default: :spec
4 changes: 3 additions & 1 deletion bin/gem_update
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# frozen_string_literal: true

#!/usr/bin/env ruby
require 'optparse'

# Exit cleanly from an early interrupt
Signal.trap('INT') { exit 1 }

$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
require 'gem_updater'

Bundler.ui = Bundler::UI::Shell.new
Expand Down
19 changes: 12 additions & 7 deletions gem_updater.gemspec
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
# frozen_string_literal: true

Gem::Specification.new do |s|
s.name = 'gem_updater'
s.version = '3.0.0'
s.date = '2018-03-10'
s.summary = 'Update your gems and find their changelogs'
s.description = 'Updates the gems of your Gemfile and fetches the links pointing to where their changelogs are'
s.description = 'Updates the gems of your Gemfile ' \
'and fetches the links pointing to where their changelogs are'
s.authors = ['Maxime Demolin']
s.email = 'akbarova.armia@gmail.com'
s.files = Dir["{lib}/**/*"]
s.files = Dir['{lib}/**/*']
s.homepage = 'https://github.com/MaximeD/gem_updater'
s.license = 'MIT'

s.required_ruby_version = '>= 2.3.0'
s.required_ruby_version = '>= 2.5.0'

s.add_runtime_dependency 'bundler', '~> 1.16'
s.add_runtime_dependency 'json', '~> 2.1'
s.add_runtime_dependency 'nokogiri', '~> 1.8'
s.add_runtime_dependency 'bundler', '~> 1.16'
s.add_runtime_dependency 'json', '~> 2.1'
s.add_runtime_dependency 'memoist', '~> 0.16.0'
s.add_runtime_dependency 'nokogiri', '~> 1.8'

s.add_development_dependency 'rspec', '~> 3.7'
s.add_development_dependency 'rspec', '~> 3.7'
s.add_development_dependency 'rubocop', '~> 0.68.1'

s.executables << 'gem_update'
end
47 changes: 25 additions & 22 deletions lib/gem_updater.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# frozen_string_literal: true

require 'memoist'
require 'gem_updater/gem_file'
require 'gem_updater/ruby_gems_fetcher'
require 'gem_updater/source_page_parser'

# Base lib.
module GemUpdater

# Updater's main responsability is to fill changes
# happened before and after update of `Gemfile`, and then format them.
class Updater
extend Memoist

attr_accessor :gemfile

def initialize
Expand Down Expand Up @@ -43,23 +48,11 @@ def format_diff

# For each gem, retrieve its changelog
def fill_changelogs
threads = []

gemfile.changes.each do |gem_name, details|
threads << Thread.new do
if source_uri = find_source(gem_name, details[:source])
source_page = GemUpdater::SourcePageParser.new(
url: source_uri, version: details[:versions][:new]
)

if source_page.changelog
gemfile.changes[gem_name][:changelog] = source_page.changelog
end
end
[].tap do |threads|
gemfile.changes.each do |gem_name, details|
threads << Thread.new { retrieve_gem_changes(gem_name, details) }
end
end

threads.each(&:join)
end.each(&:join)
end

# Find where is hosted the source of a gem
Expand All @@ -76,16 +69,26 @@ def find_source(gem, source)
end
end

def retrieve_gem_changes(gem_name, details)
source_uri = find_source(gem_name, details[:source])
return unless source_uri

source_page = GemUpdater::SourcePageParser.new(
url: source_uri, version: details[:versions][:new]
)

gemfile.changes[gem_name][:changelog] = source_page.changelog if source_page.changelog
end

# Get the template for gem's diff.
# It can use a custom template.
#
# @return [ERB] the template
def template
@template ||= begin
File.read("#{Dir.home}/.gem_updater_template.erb")
rescue Errno::ENOENT
File.read(File.expand_path('../../lib/gem_updater_template.erb', __FILE__))
end
File.read("#{Dir.home}/.gem_updater_template.erb")
rescue Errno::ENOENT
File.read(File.expand_path('../lib/gem_updater_template.erb', __dir__))
end
memoize :template
end
end
12 changes: 6 additions & 6 deletions lib/gem_updater/gem_file.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# frozen_string_literal: true

require 'bundler/cli'

module GemUpdater

# GemFile is responsible for handling `Gemfile`
class GemFile
attr_accessor :changes
Expand All @@ -21,21 +22,20 @@ def update!(gems)
#
# @return [Hash] gems for which there are differences.
def compute_changes
get_spec_sets
spec_sets_diff!

old_spec_set.each do |old_gem|
updated_gem = new_spec_set.find { |new_gem| new_gem.name == old_gem.name }
next unless updated_gem && old_gem.version != updated_gem.version

if updated_gem && old_gem.version != updated_gem.version
fill_changes(old_gem, updated_gem)
end
fill_changes(old_gem, updated_gem)
end
end

private

# Get the two spec sets (before and after `bundle update`)
def get_spec_sets
def spec_sets_diff!
@old_spec_set = spec_set
reinitialize_spec_set!
@new_spec_set = spec_set
Expand Down
30 changes: 18 additions & 12 deletions lib/gem_updater/ruby_gems_fetcher.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# frozen_string_literal: true

require 'json'
require 'nokogiri'
require 'open-uri'

module GemUpdater

# RubyGemsFetcher is a wrapper around rubygems API.
class RubyGemsFetcher
HTTP_TOO_MANY_REQUESTS = '429'.freeze
HTTP_TOO_MANY_REQUESTS = '429'
GEM_HOMEPAGES = %w[source_code_uri homepage_uri].freeze

attr_reader :gem_name, :source
Expand Down Expand Up @@ -35,11 +36,10 @@ def source_uri
def uri_from_rubygems
return unless source.remotes.map(&:host).include?('rubygems.org')

if response = query_rubygems
response[
GEM_HOMEPAGES.find { |key| response[key] && !response[key].empty? }
]
end
response = query_rubygems
return unless response

response[GEM_HOMEPAGES.find { |key| response[key] && !response[key].empty? }]
end

# Make the real query to rubygems
Expand All @@ -52,14 +52,15 @@ def query_rubygems(tries = 0)
# We may trigger too many requests, in which case give rubygems a break
if e.io.status.include?(HTTP_TOO_MANY_REQUESTS)
if (tries += 1) < 2
sleep 1 and retry
sleep 1 && retry
end
end
end

# Look if gem can be found in another remote
#
# @return [String|nil] uri of source code
# rubocop:disable Metrics/MethodLength
def uri_from_other_sources
uri = nil
source.remotes.each do |remote|
Expand All @@ -70,24 +71,28 @@ def uri_from_other_sources
when 'rails-assets.org'
uri_from_railsassets
else
Bundler.ui.error "Source #{remote} is not supported, feel free to open a PR or an issue on https://github.com/MaximeD/gem_updater"
Bundler.ui.error "Source #{remote} is not supported, ' \
'feel free to open a PR or an issue on https://github.com/MaximeD/gem_updater"
end
end

uri
end
# rubocop:enable Metrics/MethodLength

# Ask rails-assets.org for source uri of gem.
# API is at : https://rails-assets.org/packages/package_name
#
# @return [String|nil] uri of source code
def uri_from_railsassets
if response = query_railsassets
response['url'].gsub(/^git/, 'http')
end
response = query_railsassets
return unless response

response['url'].gsub(/^git/, 'http')
end

# Make the real query to railsassets
# rubocop:disable Lint/HandleExceptions
def query_railsassets
JSON.parse(
open(
Expand All @@ -99,5 +104,6 @@ def query_railsassets
# with html (instead of json) containing a 500...
rescue OpenURI::HTTPError
end
# rubocop:enable Lint/HandleExceptions
end
end
Loading

0 comments on commit 910d30b

Please sign in to comment.