lib/bundler/alive/doctor.rb in bundler-alive-0.1.4 vs lib/bundler/alive/doctor.rb in bundler-alive-0.1.5
- old
+ new
@@ -1,118 +1,95 @@
# frozen_string_literal: true
require "bundler"
require "octokit"
-require "toml-rb"
module Bundler
module Alive
#
- # Diagnoses a `Gemfile.lock` with a TOML file
+ # Diagnoses a `Gemfile.lock`
#
class Doctor
#
# A new instance of Doctor
#
# @param [String] lock_file lock file of gem
- # @param [String] result_file file of result
+ # @param [String] config_file config file
+ # @param [Array<String>] ignore_gems ignore gems
#
- def initialize(lock_file, result_file)
+ def initialize(lock_file, config_file, ignore_gems)
@lock_file = lock_file
- @result_file = result_file
- @gem_client = Client::GemsApiClient.new
+ @gem_client = Client::GemsApiClient.new(config_file)
+ @ignore_gems = ignore_gems
@result = nil
@rate_limit_exceeded = false
- @announcer = Announcer.new
@error_messages = []
end
#
- # Diagnoses gems in lock file of gem
+ # Diagnoses gems in Gemfile.lock
#
# @raise [Client::SourceCodeClient::RateLimitExceededError]
# When exceeded access rate limit
#
# @raise [StandardError]
# When raised unexpected error
#
# @return [Report]
#
def diagnose
- $stdout.puts "#{collection_from_gemfile.total_size} gems are in Gemfile.lock"
+ message = "#{collection_from_gemfile.total_size + ignore_gems.size} gems are in Gemfile.lock"
+ message = "#{message} (#{ignore_gems.size} gems are ignored)" if ignore_gems.size.positive?
+ $stdout.puts message
+
result = _diagnose
Report.new(result)
end
private
- attr_reader :lock_file, :result_file, :gem_client, :announcer,
+ attr_reader :lock_file, :gem_client, :ignore_gems,
:result, :error_messages, :rate_limit_exceeded
- #
- # @return [Array<String>]
- #
- def no_need_to_get_gems
- return [] unless File.exist?(result_file)
-
- toml_hash = TomlRB.load_file(result_file)
- toml_hash.each_with_object([]) do |(gem_name, v), array|
- alive = v["alive"]
- array << gem_name unless alive
- end
- end
-
def diagnose_by_service(service, urls)
client = Client::SourceCodeClient.new(service_name: service)
- client.query(urls: urls) do
- announcer.announce
- end
+ client.query(urls: urls)
end
+ # @return [StatusResult]
def result_by_search(collection)
- gems_api_response = gem_client.gems_api_response(collection.names) do
- announcer.announce
- end
-
+ gems_api_response = gem_client.gems_api_response(collection.names)
service_with_urls = gems_api_response.service_with_urls
error_messages.concat(gems_api_response.error_messages)
result = StatusResult.new
service_with_urls.each do |service, urls|
result = result.merge(diagnose_by_service(service, urls))
end
result
end
- def fetch_target_collection(base_collection, gem_names)
- collection = StatusCollection.new
- base_collection.each do |name, status|
- next if gem_names.include?(name)
-
- collection = collection.add(name, status)
- end
- collection
- end
-
+ # @return [StatusCollection]
def collection_from_gemfile
gems_from_lockfile.each_with_object(StatusCollection.new) do |gem, collection|
gem_name = gem.name
+ next if ignore_gems.include?(gem_name)
+
status = Status.new(name: gem_name,
repository_url: nil,
alive: nil,
checked_at: nil)
collection.add(gem_name, status)
end
end
+ # @return [StatusResult]
def _diagnose
- collection = fetch_target_collection(collection_from_gemfile, no_need_to_get_gems)
+ collection = collection_from_gemfile
result = result_by_search(collection)
- collection_from_toml_file = StatusCollection.new_from_toml_file(result_file)
- new_collection = collection_from_gemfile.merge(collection_from_toml_file)
- .merge(result.collection)
-
+ new_collection = collection.merge(result.collection)
messages = error_messages.concat(result.error_messages)
+
StatusResult.new(collection: new_collection,
error_messages: messages,
rate_limit_exceeded: result.rate_limit_exceeded)
end