lib/getv/package.rb in getv-0.2.2 vs lib/getv/package.rb in getv-1.0.0

- old
+ new

@@ -1,180 +1,164 @@ +# frozen_string_literal: true + module Getv - class Package + # package class + class Package # rubocop:disable Metrics/ClassLength attr_accessor :name, :opts - def initialize(name, opts={}) + def initialize(name, opts = {}) # rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/AbcSize @name = name - if /rubygem-.*/.match?(name) - opts = {'type' => 'gem'}.merge(opts) - elsif /nodejs-.*/.match?(name) - opts = {'type' => 'npm'}.merge(opts) - elsif /python.*-.*/.match?(name) - opts = {'type' => 'pypi'}.merge(opts) + case name + when /rubygem-.*/ + opts = { 'type' => 'gem' }.merge(opts) + when /nodejs-.*/ + opts = { 'type' => 'npm' }.merge(opts) + when /python.*-.*/ + opts = { 'type' => 'pypi' }.merge(opts) end if opts[:type] == 'github_commit' opts = { - :branch => 'master', - :select_search => '^((\d{8})(\d{6}),(([a-z\d]{7})(.*)))$', - :semantic_only => false + branch: 'master', + select_search: '^((\d{8})(\d{6}),(([a-z\d]{7})(.*)))$', + semantic_only: false }.merge(opts) end opts = { - :type => 'github_release', - :select_search => '^\s*v?((0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)\s*$', - :select_replace => '\1', - :reject => nil, - :semantic_only => true, - :semantic_select => ['*'], - :versions => nil, - :latest_version => nil, + type: 'github_release', + select_search: '^\s*v?((0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)\s*$', # rubocop:disable Layout/LineLength + select_replace: '\1', + reject: nil, + semantic_only: true, + semantic_select: ['*'], + versions: nil, + latest_version: nil }.merge(opts) case opts[:type] when 'docker' - opts = {:owner => 'library', :repo => name, :url=> 'https://registry.hub.docker.com'}.merge(opts) + opts = { owner: 'library', repo: name, url: 'https://registry.hub.docker.com' }.merge(opts) when 'gem' - opts = {:gem => name[/rubygem-(.*)/, 1] || name}.merge(opts) + opts = { gem: name[/rubygem-(.*)/, 1] || name }.merge(opts) when /github.*/ - opts = {:owner => name, :repo => name,}.merge(opts) + opts = { owner: name, repo: name }.merge(opts) when 'index' - opts = {:link => 'content'}.merge(opts) + opts = { link: 'content' }.merge(opts) when 'npm' - opts = {:npm => name[/nodejs-(.*)/, 1] || name}.merge(opts) + opts = { npm: name[/nodejs-(.*)/, 1] || name }.merge(opts) when 'pypi' - opts = {:npm => name[/python.*-(.*)/, 1] || name}.merge(opts) + opts = { npm: name[/python.*-(.*)/, 1] || name }.merge(opts) end @opts = opts end - - def get_latest_version - if opts[:latest_version] == nil - self.update_versions - end + + def latest_version + update_versions if opts[:latest_version].nil? opts[:latest_version] end - def get_versions - if opts[:version] == nil - self.update_versions - end + def versions + update_versions if opts[:versions].nil? opts[:versions] end - def update_versions - case opts[:type] - when 'docker' - versions = get_versions_using_docker - when 'gem' - versions = get_versions_using_gem - when 'get' - versions = get_versions_using_get - when 'github_commit' - versions = get_versions_using_github('commit') - when 'github_release' - versions = get_versions_using_github('release') - when 'github_tag' - versions = get_versions_using_github('tag') - when 'index' - versions = get_versions_using_index - when 'npm' - versions = get_versions_using_npm - when 'pypi' - versions = get_versions_using_pypi - end - if opts[:type] == 'github_commit' - versions.sort! - end + def update_versions # rubocop:disable Metrics/PerceivedComplexity,Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/AbcSize + method = opts[:type].split('_') + method[0] = "versions_using_#{method[0]}" + + versions = send(*method) + versions.sort! if opts[:type] == 'github_commit' select_pattern = Regexp.new(opts[:select_search]) - versions.select! {|v| v =~ select_pattern} - versions.map! {|v| v.sub(select_pattern, opts[:select_replace])} - unless opts[:reject].nil? - versions.reject! {|v| v =~ Regexp.new(opts[:reject])} - end + versions.select! { |v| v =~ select_pattern } + versions.map! { |v| v.sub(select_pattern, opts[:select_replace]) } + versions.reject! { |v| v =~ Regexp.new(opts[:reject]) } unless opts[:reject].nil? if opts[:semantic_only] require 'semantic' require 'semantic/core_ext' - versions.select! {|v| v.is_version?} + versions.select!(&:is_version?) opts[:semantic_select].each do |comparator| - versions.select! {|v| Semantic::Version.new(v).satisfies?(comparator)} + versions.select! { |v| Semantic::Version.new(v).satisfies?(comparator) } end - versions.sort_by! {|v| Semantic::Version.new(v)} + versions.sort_by! { |v| Semantic::Version.new(v) } else - unless opts[:type] == 'github_commit' - versions.sort! - end + versions.sort! unless opts[:type] == 'github_commit' end opts[:versions] = versions - unless opts[:versions].empty? - opts[:latest_version] = opts[:versions][-1] - end + opts[:latest_version] = opts[:versions][-1] unless opts[:versions].empty? end private - def get_versions_using_docker + def versions_using_docker require 'docker_registry2' docker = DockerRegistry2.connect(opts[:url]) - return docker.tags("#{opts[:owner]}/#{opts[:repo]}")['tags'] + docker.tags("#{opts[:owner]}/#{opts[:repo]}")['tags'] end - def get_versions_using_gem + def versions_using_gem require 'json' require 'open-uri' require 'net/http' - return JSON.parse(Net::HTTP.get(URI("https://rubygems.org/api/v1/versions/#{opts[:gem]}.json"))).map {|v| v['number']} + JSON.parse(Net::HTTP.get(URI("https://rubygems.org/api/v1/versions/#{opts[:gem]}.json"))).map do |v| + v['number'] + end end - def get_versions_using_get + def versions_using_get require 'open-uri' require 'net/http' - return Net::HTTP.get(URI(opts[:url])).split("\n") + Net::HTTP.get(URI(opts[:url])).split("\n") end - def get_versions_using_github(method) + def github require 'octokit' if ENV['GITHUB_TOKEN'] - github = Octokit::Client.new(:access_token => ENV['GITHUB_TOKEN']) + github = Octokit::Client.new(access_token: ENV['GITHUB_TOKEN']) user = github.user user.login else - github = Octokit::Client.new() + github = Octokit::Client.new end github.auto_paginate = true - if method == 'release' - return github.releases("#{opts[:owner]}/#{opts[:repo]}").map{|r| r.tag_name} - elsif method == 'commit' - return github.commits("#{opts[:owner]}/#{opts[:repo]}",opts[:branch]).map { |c| + github + end + + def versions_using_github(method) # rubocop:disable Metrics/AbcSize + case method + when 'release' + github.releases("#{opts[:owner]}/#{opts[:repo]}").map(&:tag_name) + when 'commit' + github.commits("#{opts[:owner]}/#{opts[:repo]}", opts[:branch]).map do |c| "#{DateTime.parse(c[:commit][:author][:date].to_s).strftime('%Y%m%d%H%M%S')},#{c[:sha]}" - } + end else - return github.tags("#{opts[:owner]}/#{opts[:repo]}").map{|t| t[:name]} + github.tags("#{opts[:owner]}/#{opts[:repo]}").map { |t| t[:name] } end end - def get_versions_using_index + def versions_using_index require 'open-uri' require 'net/http' require 'nokogiri' - if opts[:link] == 'value' - versions = Nokogiri::HTML(URI.open(opts[:url])).css('a').map{|a| a.values[0]} - else - versions = Nokogiri::HTML(URI.open(opts[:url])).css('a').map{|a| a.public_send(opts[:link])} + + Nokogiri::HTML(URI.open(opts[:url])).css('a').map do |a| # rubocop:disable Security/Open + if opts[:link] == 'value' + a.values[0] + else + a.public_send(opts[:link]) + end end - return versions end - def get_versions_using_npm + def versions_using_npm require 'json' require 'open-uri' require 'net/http' - return JSON.parse(Net::HTTP.get(URI("https://registry.npmjs.org/#{opts[:npm]}")))['versions'].keys + JSON.parse(Net::HTTP.get(URI("https://registry.npmjs.org/#{opts[:npm]}")))['versions'].keys end - def get_versions_using_pypi + def versions_using_pypi require 'json' require 'open-uri' require 'net/http' - return JSON.parse(Net::HTTP.get(URI("https://pypi.org/pypi/#{opts[:pypi]}/json")))['releases'].keys + JSON.parse(Net::HTTP.get(URI("https://pypi.org/pypi/#{opts[:pypi]}/json")))['releases'].keys end - end end