Class: Bruh::Hackage

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

Overview

Handles interaction with Hackage package manager

Instance Method Summary collapse

Constructor Details

#initialize(interactive: true) ⇒ Hackage

Returns a new instance of Hackage.



14
15
16
# File 'lib/bruh/hackage.rb', line 14

def initialize(interactive: true)
  @interactive = interactive
end

Instance Method Details

#calculate_package_sha256(version) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
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
# File 'lib/bruh/hackage.rb', line 57

def calculate_package_sha256(version)
  puts '=== Calculating SHA256 for Hackage package ==='

  # Wait for package to be available
  puts 'Waiting for Hackage to process the package (10 seconds)...'
  sleep 10 if @interactive

  package_name = File.basename(Dir.pwd)
  hackage_url = "https://hackage.haskell.org/package/#{package_name}-#{version}/#{package_name}-#{version}.tar.gz"

  max_attempts = 3
  attempt = 1
  sha256 = T.let(nil, T.nilable(String))

  while attempt <= max_attempts && sha256.nil?
    puts "Attempt #{attempt} of #{max_attempts} to calculate SHA256..."

    begin
      uri = URI(hackage_url)
      response = Net::HTTP.get_response(uri)

      if response.is_a?(Net::HTTPSuccess) && !response.body.empty?
        sha256 = Digest::SHA256.hexdigest(response.body)

        # Validate SHA256 format (64 hex chars)
        if sha256 =~ /^[0-9a-f]{64}$/
          puts "Valid SHA256 obtained: #{sha256}"
          break
        else
          puts 'Invalid SHA256 obtained. Retrying...'
          sha256 = T.let(nil, T.nilable(String))
        end
      else
        puts 'Package not available yet or empty response.'
      end
    rescue StandardError => e
      puts "Error downloading package: #{e.message}"
    end

    # Wait before retrying
    wait_time = attempt * 5
    puts "Waiting #{wait_time} seconds before retry..."
    sleep wait_time
    attempt += 1
  end

  if sha256.nil?
    puts "Error: Failed to calculate SHA256 after #{max_attempts} attempts."
    puts 'The package might not be available on Hackage yet.'
    nil
  else
    sha256
  end
end

#publish(version) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bruh/hackage.rb', line 19

def publish(version)
  puts '=== Ready to upload to Hackage ==='

  package_path = build_package(version)
  return false unless package_path

  if @interactive
    puts 'The following command will upload the package to Hackage:'
    puts "  cabal upload --publish #{package_path}"

    if yes_no_prompt('Do you want to upload to Hackage now?')
      publish_package(package_path)
    else
      puts 'Skipping Hackage upload. Run the command manually when ready.'
      false
    end
  else
    # In non-interactive mode, just publish
    publish_package(package_path)
  end
end

#publish_non_interactive(version, username, password) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bruh/hackage.rb', line 42

def publish_non_interactive(version, username, password)
  package_path = build_package(version)
  return false unless package_path

  # Use HTTP Basic auth with credentials
  cmd = "curl -X PUT --data-binary \"@#{package_path}\" " \
        "-H 'Content-Type: application/x-tar' " \
        "-H 'Content-Encoding: gzip' " \
        "--user \"#{username}:#{password}\" " \
        '"https://hackage.haskell.org/packages/upload"'

  T.must(system(cmd))
end