Class: Bindeps::Dependency

Inherits:
Object
  • Object
show all
Includes:
Which
Defined in:
lib/bindeps.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, binaries, versionconfig, urlconfig, unpack, libraries = []) ⇒ Dependency

Returns a new instance of Dependency.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/bindeps.rb', line 65

def initialize(name, binaries, versionconfig, urlconfig,
               unpack, libraries=[])
  @name = name
  unless binaries.is_a? Array
    raise ArgumentError,
          "binaries must be an array"
  end
  @binaries = binaries
  @libraries = libraries
  @version = versionconfig['number']
  @version_cmd = versionconfig['command']
  @url = choose_url urlconfig
  @unpack = unpack
end

Instance Attribute Details

#binariesObject (readonly)

Returns the value of attribute binaries.



61
62
63
# File 'lib/bindeps.rb', line 61

def binaries
  @binaries
end

#librariesObject (readonly)

Returns the value of attribute libraries.



61
62
63
# File 'lib/bindeps.rb', line 61

def libraries
  @libraries
end

#nameObject (readonly)

Returns the value of attribute name.



61
62
63
# File 'lib/bindeps.rb', line 61

def name
  @name
end

#versionObject (readonly)

Returns the value of attribute version.



61
62
63
# File 'lib/bindeps.rb', line 61

def version
  @version
end

Instance Method Details

#all_installed?Boolean

Returns:

  • (Boolean)


150
151
152
153
154
155
156
157
# File 'lib/bindeps.rb', line 150

def all_installed?
  @binaries.each do |bin|
    unless installed? bin
      return false
    end
  end
  true
end

#choose_url(urlconfig) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bindeps.rb', line 88

def choose_url urlconfig
  arch = System.arch
  if urlconfig.key? arch
    sys = urlconfig[arch]
    os = System.os.to_s
    if sys.key? os
      return sys[os]
    else
      raise UnsupportedSystemError,
            "bindeps config for #{@name} #{arch} doesn't " +
            "contain an entry for #{os}"
    end
  else
    raise UnsupportedSystemError,
          "bindeps config for #{@name} doesn't contain an " +
          "entry for #{arch}"
  end
end

#downloadObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/bindeps.rb', line 107

def download
  wget = which('wget').first
  curl = which('curl').first
  if wget
    cmd = "#{wget} #{@url}"
    stdout, stderr, status = Open3.capture3 cmd
  elsif curl
    cmd = "#{curl} -O -J -L #{@url}"
    stdout, stderr, status = Open3.capture3 cmd
  else
    msg = "You don't have curl or wget?! What kind of computer is "
    msg << "this?! Windows?! BeOS? OS/2?"
    raise DownloadFailedError.new(msg)
  end
  if !status.success?
    raise DownloadFailedError,
          "download of #{@url} for #{@name} failed:\n#{stdout}\n#{stderr}"
  end
end

#install(src, destprefix, destdir = '') ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/bindeps.rb', line 178

def install(src, destprefix, destdir = '')
  gem_home = ENV['GEM_HOME']
  home = ENV['HOME']
  basedir = File.join(home, '.local')
  if destdir.length > 0
    basedir = destdir
  elsif gem_home.nil?
    ENV['PATH'] = ENV['PATH'] + ":#{File.join(basedir, 'bin')}"
  else
    basedir = ENV['GEM_HOME']
  end
  FileUtils.mkdir_p File.join(basedir, 'bin')
  FileUtils.mkdir_p File.join(basedir, 'lib')
  destprefix = 'bin' if destprefix == '.'
  install_location = File.join(basedir, destprefix, File.basename(src))
  FileUtils.install(src, install_location, :mode => 0775)
end

#install_missing(destdir = '') ⇒ Object



80
81
82
83
84
85
86
# File 'lib/bindeps.rb', line 80

def install_missing destdir=''
  unless all_installed?
    puts "Installing #{@name} (#{@version})..."
    download
    unpack destdir
  end
end

#installed?(bin) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/bindeps.rb', line 159

def installed? bin
  path = which(bin)
  if path.length > 0
    ret = `#{@version_cmd} 2>&1`.split("\n").map{ |l| l.strip }.join('|')
    if ret && (/#{@version}/ =~ ret)
      return path
    end
  else
    if Dir.exist?("#{ENV['HOME']}/.local/bin")
      ENV['PATH'] = ENV['PATH'] + ":#{ENV['HOME']}/.local/bin"
      path = which(bin)
      if path.length > 0
        return path
      end
    end
  end
  false
end

#unpack(destdir = '') ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/bindeps.rb', line 127

def unpack destdir = ''
  archive = File.basename(@url)
  Unpacker.archive? archive
  if @unpack
    Unpacker.unpack(archive) do |dir|
      Dir.chdir dir do
        Dir['**/*'].each do |extracted|
          file = File.basename(extracted)
          if @binaries.include?(file) || @libraries.include?(file)
            dir = File.dirname(extracted).split(File::PATH_SEPARATOR).last
            dir = %w[bin lib].include?(dir) ? dir : '.'
            unless File.directory?(extracted)
              install(extracted, dir, destdir)
            end
          end
        end
      end
    end
  else
    install(@binaries.first, 'bin', destdir)
  end
end