Module: Homebrew::Tools

Defined in:
app/models/homebrew/tools.rb

Overview

Serie of tools to work with Homebrew formula files

Author:

  • guillaumeh

Class Method Summary collapse

Class Method Details

.conflicts_because_check(conflicts) ⇒ Object


33
34
35
36
37
38
39
40
41
# File 'app/models/homebrew/tools.rb', line 33

def self.conflicts_because_check(conflicts)
  # Looks like it is a common type mistake:
  #  because => beacuse
  if conflicts.include?(':beacuse')
    Rails.logger.warn('Formula conflict "because" type mistake detected!')
    conflicts.gsub!(/:beacuse/, ':because')
  end
  conflicts
end

.extract_attributes(formula) ⇒ Object


24
25
26
27
28
29
30
31
# File 'app/models/homebrew/tools.rb', line 24

def self.extract_attributes(formula)
  attributes_to_extract = %w(homepage version)
  # Extract the formula attributes shown on brewformulas.org
  extracted_attributes = formula.scan(
    /^\s+(#{attributes_to_extract.join("|")})\s+(?:'|")(.*)(?:'|")$/
  )
  Hash[*extracted_attributes.flatten]
end

.extract_class_name(formula) ⇒ Object

Extract the formula class name In the case the formula declare more than one Formula class use the latest one as the other are dependencies


11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/homebrew/tools.rb', line 11

def self.extract_class_name(formula)
  inheriting_classes = %w(
    Formula
    ScriptFileFormula
    AmazonWebServicesFormula
    GithubGistFormula
  )
  class_name = formula.scan(
    /^class\s(\w+)\s<\s(?:#{inheriting_classes.join("|")})$/
  )
  class_name.flatten.last
end

.extract_conflict_reason_if_possible!(conflicts) ⇒ Object


43
44
45
46
47
48
49
50
51
# File 'app/models/homebrew/tools.rb', line 43

def self.extract_conflict_reason_if_possible!(conflicts)
  conflicts = conflicts_because_check(conflicts)

  if conflicts.include?(':because')
    conflicts, because = conflicts.split(':because')
    because.gsub!(/\s+=>\s+/, '')
  end
  [conflicts, because]
end

.find_or_create_formula!(filename, name) ⇒ Homebrew::Formula

Find or create the Homebrew::Formula for a given filename and name

In the case the method create the formula, it is created with the external field to true so that, during the import, if a file has been parsed for this formula, the external field will be updated to false.

Parameters:

  • filename (String)

    Filename of the formula (without extention)

  • name (String)

    Name to be displayed

Returns:


91
92
93
94
95
96
# File 'app/models/homebrew/tools.rb', line 91

def self.find_or_create_formula!(filename, name)
  formula = Homebrew::Formula.find_by(filename: filename)
  return formula if formula

  Homebrew::Formula.create!(filename: filename, name: name, external: true)
end

.get_conflicts(formula) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/homebrew/tools.rb', line 53

def self.get_conflicts(formula)
  conflicts = formula.scan(/^\s+conflicts_with\s+(.*)$/).flatten.first

  # No conflict found
  return [] unless conflicts

  conflicts.gsub!(/('|")/, '')

  conflicts, _ = extract_conflict_reason_if_possible!(conflicts)

  conflicting_formulas = conflicts.strip.split(',').map(&:strip)
  conflicting_formulas.map do |name|
    find_or_create_formula!(name, name.classify)
  end
end

.get_dependencies(formula) ⇒ Object


69
70
71
72
73
74
75
76
77
# File 'app/models/homebrew/tools.rb', line 69

def self.get_dependencies(formula)
  dependency_names = formula.scan(
    /^\s+depends_on\s(?:\'|\:)([\w\+\-]+).*$/
  )
  dependency_names.flatten!
  dependency_names.map do |name|
    find_or_create_formula!(name, name.classify)
  end
end