Class: HomebrewAutomation::Formula

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

Overview

An in-memory, programmable representation of some Formula.rb Ruby source file, containing the definition of a Homebrew Bottle. See Homebrew docs for concepts: docs.brew.sh/Bottles.html

Instance methods produce new instances where sensible, leaving all instances free from mutation.

#== and #hash delegates to the underlying Parser::AST::Node.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ast) ⇒ Formula

Take an post-parsing abstract syntax tree representation of a Homebrew Formula. This is mostly not intended for common use-cases.

Parameters:

  • ast (Parser::AST::Node)


33
34
35
# File 'lib/homebrew_automation/formula.rb', line 33

def initialize ast
  @ast = ast
end

Class Method Details

.parse_string(s) ⇒ Formula

Parse the string form of a Homebrew Formula source file into a HomebrewAutomation::Formula.

#to_s is the inverse of this method.

Returns:



25
26
27
# File 'lib/homebrew_automation/formula.rb', line 25

def self.parse_string s
  Formula.new (Parser::CurrentRuby.parse s)
end

Instance Method Details

#==(o) ⇒ Boolean Also known as: eql?

Both formulae are ==, as per Parser::AST::Node#==.

In practice, I think this means both formulae are equivalent in terms of Ruby semantics.

Parameters:

Returns:

  • (Boolean)


106
107
108
# File 'lib/homebrew_automation/formula.rb', line 106

def == o
  self.class == o.class && @ast == o.ast
end

#hashInteger

Hash of the Parser::AST::Node

Returns:

  • (Integer)


113
114
115
# File 'lib/homebrew_automation/formula.rb', line 113

def hash
  @ast.hash
end

#put_bottle(os, sha256) ⇒ Formula

Insert or replace the Homebrew Bottle for a given OS

Parameters:

  • os (String)

    Operating system name, e.g. “yosemite”, as per Homebrew’s conventions

  • sha256 (String)

    Checksum of the binary “Bottle” tarball

Returns:

  • (Formula)

    a new instance of Formula with the changes applied



78
79
80
81
82
83
# File 'lib/homebrew_automation/formula.rb', line 78

def put_bottle os, sha256
  Formula.new update(
    @ast,
    bot_begin_path,
    put_bottle_version(os, sha256))
end

#put_sdist(url, sha256) ⇒ Formula

Update two fields together

Parameters:

  • url (String)

    URL of source tarball

  • sha256 (String)

    SHA256 sum of source tarball

Returns:



68
69
70
71
# File 'lib/homebrew_automation/formula.rb', line 68

def put_sdist url, sha256
  update_field("url", url).
  update_field("sha256", sha256)
end

#rm_all_bottlesFormula

Remove sha256 references to all bottles

This may leave the Formula in a non-standard state. Please add at lease one bottle referene back, otherwise why are you even declaring a bottles block at all.

Returns:

  • (Formula)

    a new instance of Formula with the changes applied



92
93
94
95
96
97
# File 'lib/homebrew_automation/formula.rb', line 92

def rm_all_bottles
  Formula.new update(
    @ast,
    bot_begin_path,
    rm_all_bottle_sha256s)
end

#to_sString

Produce Homebrew Formula source code as a string, suitable for saving as a Ruby source file.

This is the inverse of parse_string.

Returns:

  • (String)


43
44
45
# File 'lib/homebrew_automation/formula.rb', line 43

def to_s
  Unparser.unparse @ast
end

#update_field(field, value) ⇒ Formula

Update a top-level field in the Formula

Parameters:

  • field (String)

    Name of the Formula field, e.g. url

  • value (String)

    Value of the Formula field, e.g. https://github.com/easoncxz/homebrew-automation

Returns:

  • (Formula)

    a new instance of Formula with the changes applied



52
53
54
55
56
57
58
59
60
61
# File 'lib/homebrew_automation/formula.rb', line 52

def update_field field, value
  Formula.new update(
    @ast,
    [ by_type('begin'),
      by_both(
        by_type('send'),
        by_msg(field)),
      by_type('str')],
    -> (n) { n.updated(nil, [value]) })
end