Class: TapDance::Brew

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, version = nil, opts = {}) ⇒ Brew

Returns a new instance of Brew.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tap_dance/brew.rb', line 10

def initialize(name, version=nil, opts={})
  @opts    = opts.dup
  @name    = name.to_s
  @version = version
  @tap     = @opts[:tap]
  @flags   = @opts[:flags] || []

  # Get actual tap object
  unless @tap.nil?
    case @tap
    when String, Symbol
      @tap = @opts[:definition].tap_named @tap.to_s
    when TapDance::Tap
      # Good to go!
    end
    @opts[:tap] = @tap
  end

end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/tap_dance/brew.rb', line 6

def name
  @name
end

#tapObject

Returns the value of attribute tap.



8
9
10
# File 'lib/tap_dance/brew.rb', line 8

def tap
  @tap
end

#versionObject

Returns the value of attribute version.



7
8
9
# File 'lib/tap_dance/brew.rb', line 7

def version
  @version
end

Instance Method Details

#brewable?Boolean

Does the formula exist?

Returns:

  • (Boolean)


57
58
59
# File 'lib/tap_dance/brew.rb', line 57

def brewable?
  BrewCLI.formula_info(canonical).okay?
end

#canonicalObject



48
49
50
51
52
53
54
# File 'lib/tap_dance/brew.rb', line 48

def canonical
  unless @tap.nil?
    "#{@tap.url}/#{@name}"
  else
    @name
  end
end

#formula_versionObject



61
62
63
64
65
66
# File 'lib/tap_dance/brew.rb', line 61

def formula_version
  res = BrewCLI.formula_info(canonical)
  return nil unless res.okay?

  res.out.split($/).first.match(/#{name}: stable ([^\s,]+)/)[1]
end

#formula_versionsObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tap_dance/brew.rb', line 68

def formula_versions
  # Takes a long time to get; cache return
  if @formula_versions.nil?
    @formula_versions = BrewCLI.formula_versions(canonical).
      out.chomp.split($/).map do |v|
      s = v.split(/\s+/)
      version = s[0]
      commit  = s[3]
      path    = s[4]
      [ version, commit, path ]
    end
  else
    return @formula_versions
  end
end

#installObject



30
31
32
33
34
35
36
37
# File 'lib/tap_dance/brew.rb', line 30

def install
  if brewable?
    # Do magic here
    BrewCLI.install canonical, @flags
  else
    TapDance.ui.error "No available formula for \"#{canonical}\""
  end
end

#installed?Boolean

Is a version installed?

Returns:

  • (Boolean)


95
96
97
# File 'lib/tap_dance/brew.rb', line 95

def installed?
  !installed_versions.empty?
end

#installed_versionsObject



84
85
86
87
88
89
90
91
92
# File 'lib/tap_dance/brew.rb', line 84

def installed_versions
  # Would love to use StringScanner...but we don't need to!
  versions = BrewCLI.list_versions(canonical).out.strip.split(/\s+/)
  unless versions.empty?
    return versions[1..-1]
  else
    return []
  end
end

#latest_versionObject



99
100
101
102
103
# File 'lib/tap_dance/brew.rb', line 99

def latest_version
  # Works unless the versions have
  # strings in them, like alpha/beta
  installed_versions.sort.last if installed?
end

#to_sObject



105
106
107
# File 'lib/tap_dance/brew.rb', line 105

def to_s
  return canonical
end

#upgradeObject



39
40
41
42
43
44
45
46
# File 'lib/tap_dance/brew.rb', line 39

def upgrade
  if installed?
    # Do magic here
    BrewCLI.upgrade canonical, @flags
  else
    TapDance.ui.error "You haven't installed \"#{canonical}\""
  end
end