Top Level Namespace

Defined Under Namespace

Modules: ZSV

Constant Summary collapse

ZSV_VERSION =

ZSV version to compile against

'1.4.3'
ZSV_URL =

zsv C library version (not gem version)

"https://github.com/liquidaty/zsv/archive/refs/tags/v#{ZSV_VERSION}.tar.gz".freeze
EXTCONF_DIR =

Use absolute path relative to the original extconf.rb location

File.expand_path(__dir__)
VENDOR_DIR =
File.join(EXTCONF_DIR, 'vendor')
ZSV_DIR =
File.join(VENDOR_DIR, "zsv-#{ZSV_VERSION}")

Instance Method Summary collapse

Instance Method Details

#build_zsvObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'ext/zsv/extconf.rb', line 87

def build_zsv
  puts 'Building zsv library...'

  # Build zsv static library
  Dir.chdir(ZSV_DIR) do
    # Configure zsv
    system('./configure') or abort('Failed to configure zsv') unless File.exist?('config.mk')

    # Build the library
    Dir.chdir('src') do
      system('make', 'build') or abort('Failed to build zsv library')
    end
  end

  puts 'zsv library built successfully'
end

#download_and_extract_zsvObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'ext/zsv/extconf.rb', line 70

def download_and_extract_zsv
  return if File.directory?(ZSV_DIR)

  puts "Downloading zsv #{ZSV_VERSION}..."
  FileUtils.mkdir_p(VENDOR_DIR)

  tarball = File.join(VENDOR_DIR, 'zsv.tar.gz')
  download_file(ZSV_URL, tarball)

  puts 'Extracting zsv...'
  extract_tar_gz(tarball, VENDOR_DIR)
  FileUtils.rm_f(tarball)

  abort('zsv directory not found after extraction') unless File.directory?(ZSV_DIR)
  puts "zsv #{ZSV_VERSION} downloaded successfully"
end

#download_file(url, destination, redirect_limit = 10, verify_ssl = true) ⇒ 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
# File 'ext/zsv/extconf.rb', line 19

def download_file(url, destination, redirect_limit = 10, verify_ssl = true)
  abort('Too many redirects') if redirect_limit.zero?

  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)

  if uri.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = verify_ssl ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
    http.ca_file = ENV['SSL_CERT_FILE'] if ENV['SSL_CERT_FILE'] && verify_ssl
  end

  request = Net::HTTP::Get.new(uri.request_uri)

  begin
    response = http.request(request)
  rescue OpenSSL::SSL::SSLError => e
    if verify_ssl
      # Retry without SSL verification (GitHub is trusted)
      warn "SSL verification failed (#{e.message}), retrying without verification..."
      return download_file(url, destination, redirect_limit, false)
    end
    raise
  end

  case response
  when Net::HTTPRedirection
    download_file(response['location'], destination, redirect_limit - 1, verify_ssl)
  when Net::HTTPSuccess
    File.binwrite(destination, response.body)
  else
    abort("Failed to download: #{response.code} #{response.message}")
  end
end

#extract_tar_gz(tarball, destination) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'ext/zsv/extconf.rb', line 54

def extract_tar_gz(tarball, destination)
  Gem::Package::TarReader.new(Zlib::GzipReader.open(tarball)) do |tar|
    tar.each do |entry|
      dest_path = File.join(destination, entry.full_name)

      if entry.directory?
        FileUtils.mkdir_p(dest_path)
      elsif entry.file?
        FileUtils.mkdir_p(File.dirname(dest_path))
        File.binwrite(dest_path, entry.read)
        FileUtils.chmod(entry.header.mode, dest_path)
      end
    end
  end
end