Class: Bruh::Homebrew

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/bruh/homebrew.rb

Overview

Manages Homebrew formula creation and updates

Instance Method Summary collapse

Constructor Details

#initialize(interactive: true) ⇒ Homebrew

Returns a new instance of Homebrew.



12
13
14
15
16
# File 'lib/bruh/homebrew.rb', line 12

def initialize(interactive: true)
  @interactive = interactive
  @tap_dir = T.let(find_homebrew_tap, T.nilable(String))
  @formula_path = T.let(find_formula_path, T.nilable(String))
end

Instance Method Details

#update(version, sha256) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bruh/homebrew.rb', line 19

def update(version, sha256)
  unless @formula_path && File.exist?(@formula_path)
    puts "Error: Formula file not found at #{@formula_path}"
    return false
  end

  puts "Updating Homebrew formula with new version #{version} and SHA256 #{sha256}..."

  # Read current formula
  formula_content = File.read(@formula_path)

  # Verify markers exist
  unless formula_content.include?('url') && formula_content.include?('sha256')
    puts 'ERROR: Required fields not found in formula. Cannot update safely.'
    return false
  end

  # Update version in URL
  formula_content.gsub!(/url\s+"[^"]+"/, "url \"https://hackage.haskell.org/package/#{package_name}-#{version}/#{package_name}-#{version}.tar.gz\"")

  # Update SHA256
  formula_content.gsub!(/sha256\s+"[a-f0-9]+"/, "sha256 \"#{sha256}\"")

  # Write updated formula
  File.write(@formula_path, formula_content)

  # Verify the update was successful
  updated_content = File.read(@formula_path)
  if !updated_content.include?("#{package_name}-#{version}.tar.gz") ||
     !updated_content.include?("sha256 \"#{sha256}\"")
    puts 'ERROR: Formula update verification failed.'
    return false
  end

  puts 'Formula updated successfully with new version and SHA256.'

  # Commit the changes if interactive
  if @interactive && yes_no_prompt('Commit formula changes?')
    Dir.chdir(File.dirname(@formula_path)) do
      system("git add #{File.basename(@formula_path)}")
      system("git commit -m \"Update formula to version #{version}\"")

      system('git push origin main') if yes_no_prompt('Push formula changes to origin?', default_no: true)
    end
  end

  true
end

#update_bottle_info(bottle_info) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/bruh/homebrew.rb', line 69

def update_bottle_info(bottle_info)
  unless @formula_path && File.exist?(@formula_path)
    puts 'Error: Formula file not found'
    return false
  end

  macos_version = bottle_info[:macos_version]
  bottle_sha = bottle_info[:sha256]
  rebuild_num = bottle_info[:rebuild] || 0

  formula_content = File.read(@formula_path)

  # Check if bottle block exists
  if formula_content.include?('bottle do')
    # Update existing bottle block

    # Update rebuild directive if needed
    if rebuild_num.positive?
      if formula_content =~ /\s+rebuild\s+\d+/
        formula_content.gsub!(/(\s+rebuild\s+)\d+/, "\\1#{rebuild_num}")
      else
        formula_content.gsub!(/(\s+root_url.*)$/, "\\1\n    rebuild #{rebuild_num}")
      end
    else
      # Remove rebuild directive if it exists
      formula_content.gsub!(/\s+rebuild\s+\d+\n/, "\n")
    end

    # Update SHA256 for the specific platform
    if formula_content =~ /sha256\s+cellar:[^,]+,\s+arm64_[^:]+:/
      formula_content.gsub!(/sha256\s+cellar:[^,]+,\s+arm64_[^:]+:\s+"[a-f0-9]+"/,
                            "sha256 cellar: :any, arm64_#{macos_version}: \"#{bottle_sha}\"")
    else
      # Add line for this platform
      formula_content.gsub!(/(\s+root_url\s+"[^"]+".*$)/,
                            "\\1\n    sha256 cellar: :any, arm64_#{macos_version}: \"#{bottle_sha}\"")
    end
  else
    # Create bottle block after sha256 line
    bottle_block = <<~BOTTLE

      bottle do
        root_url "https://github.com/#{repo_owner}/#{repo_name}/releases/download/v#{bottle_info[:version]}"
        sha256 cellar: :any, arm64_#{macos_version}: "#{bottle_sha}"
      end
    BOTTLE

    formula_content.gsub!(/sha256\s+"[a-f0-9]+"/, "\\0#{bottle_block}")
  end

  # Write updated formula
  File.write(@formula_path, formula_content)

  # Verify the update was successful
  updated_content = File.read(@formula_path)
  unless updated_content.include?("arm64_#{macos_version}: \"#{bottle_sha}\"")
    puts 'ERROR: Bottle SHA update failed.'
    return false
  end

  puts 'Bottle information updated successfully.'

  # Commit the changes if interactive
  if @interactive && yes_no_prompt('Commit bottle changes?')
    Dir.chdir(File.dirname(@formula_path)) do
      system("git add #{File.basename(@formula_path)}")
      system("git commit -m \"Add bottle for version #{bottle_info[:version]}\"")

      system('git push origin main') if yes_no_prompt('Push bottle changes to origin?', default_no: true)
    end
  end

  true
end