Class: Rip::Installer

Inherits:
Object
  • Object
show all
Includes:
Memoize
Defined in:
lib/rip/installer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Memoize

included, #memoize, #method_added

Constructor Details

#initializeInstaller

Returns a new instance of Installer.



6
7
8
9
# File 'lib/rip/installer.rb', line 6

def initialize
  @installed = {}
  @uninstalled = {}
end

Instance Attribute Details

#installedObject (readonly)

Returns the value of attribute installed.



5
6
7
# File 'lib/rip/installer.rb', line 5

def installed
  @installed
end

Instance Method Details

#build_extensions(package) ⇒ Object



67
68
69
# File 'lib/rip/installer.rb', line 67

def build_extensions(package)
  Rip::Commands.build({:quiet => true}, package)
end

#cleanup(package) ⇒ Object



88
89
90
# File 'lib/rip/installer.rb', line 88

def cleanup(package)
  FileUtils.rm_rf package.cache_path unless package.cached?
end

#copy_files(package) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rip/installer.rb', line 71

def copy_files(package)
  package_lib = File.join(package.cache_path, 'lib')
  package_bin = File.join(package.cache_path, 'bin')

  dest = Rip::Env.active_dir
  dest_lib = File.join(dest, 'lib')
  dest_bin = File.join(dest, 'bin')

  if File.exists? package_lib
    FileUtils.cp_r package_lib + '/.', dest_lib
  end

  if File.exists? package_bin
    FileUtils.cp_r package_bin + '/.', dest_bin, :preserve => true
  end
end

#install(package, parent = nil) ⇒ Object



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
# File 'lib/rip/installer.rb', line 16

def install(package, parent = nil)
  if !package.exists?
    error = package.to_s
    error += " requested by #{parent} but" if parent
    error += " not found at #{package.source}"
    ui.abort error
  end

  Dir.chdir File.join(Rip.dir, 'rip-packages') do
    begin
      installed = @installed[package.name] || package.installed?

      return if installed
      @installed[package.name] = package

      if !package.meta_package? && !package.version
        ui.abort "can't install #{package} - it has no version"
      end

      package.fetch
      package.unpack

      parent_package = (parent && parent.meta_package?) ? parent.actual_package : parent
      manager.add_package(package, parent)

      install_dependencies(package)
      build_extensions(package)
      copy_files(package)
      cleanup(package)
      ui.puts "Successfully installed #{package}" unless package.meta_package?
      true

    rescue VersionConflict => e
      ui.puts e.message
      rollback
      ui.abort "installation failed"

    rescue => e
      rollback
      raise e
    end
  end
end

#install_dependencies(package) ⇒ Object



60
61
62
63
64
65
# File 'lib/rip/installer.rb', line 60

def install_dependencies(package)
  package.dependencies.each do |dependency|
    success = install(dependency, package)
    package.run_hook(:dependency_installed, dependency, success)
  end
end

#managerObject



12
13
14
# File 'lib/rip/installer.rb', line 12

def manager
  PackageManager.new
end

#rakebinObject



125
126
127
# File 'lib/rip/installer.rb', line 125

def rakebin
  ENV['RAKEBIN'] || 'rake'
end

#rollbackObject



92
93
94
95
96
97
# File 'lib/rip/installer.rb', line 92

def rollback
  @installed.values.each do |package|
    uninstall(package)
    cleanup(package)
  end
end

#uiObject



129
130
131
# File 'lib/rip/installer.rb', line 129

def ui
  Rip.ui
end

#uninstall(package, remove_dependencies = false) ⇒ Object



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
# File 'lib/rip/installer.rb', line 99

def uninstall(package, remove_dependencies = false)
  packages = [package]

  if remove_dependencies
    packages.concat manager.packages_that_depend_on(package.name)
  end

  Dir.chdir Rip::Env.active_dir do
    packages.each do |package|
      begin
        next if @uninstalled[package.name]
        @uninstalled[package.name] = true

        package.files.each do |file|
          FileUtils.rm_rf file
        end

        manager.remove_package(package)
      rescue => e
        ui.puts e.message
        next
      end
    end
  end
end