Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. #!/usr/bin/ruby
  2.  
  3. require 'rubygems'
  4. require 'rubygems/commands/fetch_command'
  5.  
  6. class Gem::Commands::FetchCommand
  7. def add_version_option_with_fetch_depends
  8. add_version_option_without_fetch_depends
  9. add_option('-y', '--[no-]dependencies',
  10. "Fetch dependent gems") do |value, options|
  11. options[:dependencies] = value
  12. end
  13. end
  14.  
  15. def get_all_gem_names_with_fetch_depends
  16. @dependent_gem_names || get_all_gem_names_without_fetch_depends
  17. end
  18.  
  19. def execute_with_fetch_depends
  20. execute_without_fetch_depends
  21. if options[:dependencies] then
  22. @dependent_gem_names = get_dependent_gem_names
  23. options[:version] = nil
  24. execute_without_fetch_depends
  25. end
  26. end
  27.  
  28. [:add_version_option, :get_all_gem_names, :execute].each do |target|
  29. feature = "fetch_depends"
  30. alias_method "#{target}_without_#{feature}", target
  31. alias_method target, "#{target}_with_#{feature}"
  32. end
  33.  
  34. private
  35. def get_dependent_gem_names
  36. version = options[:version] || Gem::Requirement.default
  37. request_gem_names = get_all_gem_names_without_fetch_depends.uniq
  38. to_do = fetch_specs_by_names_and_version(request_gem_names, version)
  39. seen = {}
  40.  
  41. until to_do.empty? do
  42. spec = to_do.shift
  43. next if spec.nil? or seen[spec.name]
  44. seen[spec.name] = true
  45. deps = spec.runtime_dependencies
  46. deps.each do |dep|
  47. requirements = dep.requirement.requirements.map { |req,| req }
  48. all = !dep.prerelease? and
  49. (requirements.length > 1 or
  50. (requirements.first != ">=" and requirements.first != ">"))
  51. result = fetch_spec(dep, all)
  52. to_do.push(result)
  53. end
  54. end
  55.  
  56. gem_names = seen.map { |name,| name }
  57. gem_names.reject { |name| request_gem_names.include? name }
  58. end
  59.  
  60. def fetch_specs_by_names_and_version(gem_names, version)
  61. all = Gem::Requirement.default != version
  62. specs = gem_names.map do |gem_name|
  63. dep = Gem::Dependency.new(gem_name, version)
  64. dep.prerelease = options[:prerelease]
  65. fetch_spec(dep, all)
  66. end
  67. specs.compact
  68. end
  69.  
  70. def fetch_spec(dep, all)
  71. specs_and_sources, errors =
  72. Gem::SpecFetcher.fetcher.fetch_with_errors(dep, all, true,
  73. dep.prerelease?)
  74. if platform = Gem.platforms.last then
  75. filtered = specs_and_sources.select { |s,| s.platform == platform }
  76. specs_and_sources = filtered unless filtered.empty?
  77. end
  78. spec, source_uri = specs_and_sources.sort_by { |s,| s.version }.last
  79. spec
  80. end
  81. end
  82.  
  83. load `which gem`.rstrip
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement