Class: Bindeps::Dependency

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

Instance Method Summary collapse

Constructor Details

#initialize(name, binaries, versionconfig, urlconfig) ⇒ Dependency

Returns a new instance of Dependency.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bindeps.rb', line 30

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

Instance Method Details

#all_installed?Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
# File 'lib/bindeps.rb', line 89

def all_installed?
  @binaries.each do |bin|
    unless installed? bin
      puts "required binary #{bin} is not installed"
      return false
    end
  end
  true
end

#choose_url(urlconfig) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bindeps.rb', line 51

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



68
69
70
71
72
73
74
# File 'lib/bindeps.rb', line 68

def download
  `curl -O -J -L #{@url}`
  unless $?.to_i == 0
    raise DownloadFailedError,
          "download of #{@url} for #{@name} failed"
  end
end

#install(bin) ⇒ Object



116
117
118
119
120
# File 'lib/bindeps.rb', line 116

def install bin
  bindir = File.join(ENV['GEM_HOME'], 'bin')
  puts "installing #{bin} to #{bindir}"
  FileUtils.cp(bin, File.join(bindir, File.basename(bin)))
end

#install_missingObject



42
43
44
45
46
47
48
49
# File 'lib/bindeps.rb', line 42

def install_missing
  unless all_installed?
    puts "binary dependency #{@name} not installed"
    puts "it will now be downloaded and installed"
    download
    unpack
  end
end

#installed?(bin) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bindeps.rb', line 99

def installed? bin
  path = Which.which(bin)
  if path
    ret = `#{@version_cmd} 2>&1`.split("\n").map{ |l| l.strip }.join('|')
    if ret && (/#{@version}/ =~ ret)
      return path
    else
      puts "installed version should have been #{@version}"
      puts "but it was:"
      puts ret
    end
  else
    puts "binary #{bin} is not in the PATH"
  end
  false
end

#unpackObject



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bindeps.rb', line 76

def unpack
  archive = File.basename(@url)
  Unpacker.unpack(archive) do |dir|
    Dir.chdir dir do
      Dir['**/*'].each do |extracted|
        if @binaries.include? File.basename(extracted)
          install(extracted) unless File.directory?(extracted)
        end
      end
    end
  end
end