summaryrefslogtreecommitdiff
path: root/chef-server/tasks/merb.thor/gem_ext.rb
blob: 6b605d7c8c5fbbff98bfd349ecd05b69719eb680 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
require "erb"

Gem.pre_install_hooks.push(proc do |installer|
  unless File.file?(installer.bin_dir / "common.rb")
    FileUtils.mkdir_p(installer.bin_dir)
    FileUtils.cp(File.dirname(__FILE__) / "common.rb", installer.bin_dir / "common.rb")
  end
  
  include ColorfulMessages
  name = installer.spec.name
  if $GEMS && versions = ($GEMS.assoc(name) || [])[1]
    dep = Gem::Dependency.new(name, versions)
    unless dep.version_requirements.satisfied_by?(installer.spec.version)
      error "Cannot install #{installer.spec.full_name} " \
            "for #{$INSTALLING}; " \
            "you required #{dep}"
      ::Thor::Tasks::Merb::Gem.rollback_trans
      exit!
    end
  end
  success "Installing #{installer.spec.full_name}"
end)

class ::Gem::Uninstaller
  def self._with_silent_ui
    
    ui = Gem::DefaultUserInteraction.ui 
    def ui.say(str)
      puts "- #{str}"
    end
    
    yield
    
    class << Gem::DefaultUserInteraction.ui
      remove_method :say
    end 
  end
  
  def self._uninstall(source_index, name, op, version)
    unless source_index.find_name(name, "#{op} #{version}").empty?
      uninstaller = Gem::Uninstaller.new(
        name,
        :version => "#{op} #{version}",
        :install_dir => Dir.pwd / "gems",
        :all => true,
        :ignore => true
      )
      _with_silent_ui { uninstaller.uninstall }
    end
  end
  
  def self._uninstall_others(source_index, name, version)
    _uninstall(source_index, name, "<", version)
    _uninstall(source_index, name, ">", version)
  end
end

Gem.post_install_hooks.push(proc do |installer|
  source_index = installer.instance_variable_get("@source_index")
  ::Gem::Uninstaller._uninstall_others(
    source_index, installer.spec.name, installer.spec.version
  )
end)

class ::Gem::DependencyInstaller
  alias old_fg find_gems_with_sources
  
  def find_gems_with_sources(dep)
    if @source_index.any? { |_, installed_spec|
      installed_spec.satisfies_requirement?(dep)
    }
      return []
    end
    
    old_fg(dep)
  end
end

class ::Gem::SpecFetcher
  alias old_fetch fetch
  def fetch(dependency, all = false, matching_platform = true)
    idx = Gem::SourceIndex.from_installed_gems
    
    reqs = dependency.version_requirements.requirements
    
    if reqs.size == 1 && reqs[0][0] == "="
      dep = idx.search(dependency).sort.last
    end
    
    if dep
      file = dep.loaded_from.dup
      file.gsub!(/specifications/, "cache")
      file.gsub!(/gemspec$/, "gem")
      spec = ::Gem::Format.from_file_by_path(file).spec
      [[spec, file]]
    else
      old_fetch(dependency, all, matching_platform)
    end
  end
end

class ::Gem::Installer
  def app_script_text(bin_file_name)
    template = File.read(File.dirname(__FILE__) / "app_script.rb")
    erb = ERB.new(template)
    erb.result(binding)
  end
end

class ::Gem::Specification
  def recursive_dependencies(from, index = Gem.source_index)
    specs = self.runtime_dependencies.map do |dep|
      spec = index.search(dep).last
      unless spec
        from_name = from.is_a?(::Gem::Specification) ? from.full_name : from.to_s
        wider_net = index.find_name(dep.name).last
        ThorUI.error "Needed #{dep} for #{from_name}, but could not find it"
        ThorUI.error "Found #{wider_net.full_name}" if wider_net
        ::Thor::Tasks::Merb::Gem.rollback_trans
      end
      spec
    end
    specs + specs.map {|s| s.recursive_dependencies(self, index)}.flatten.uniq
  end
end