Module: Prebake::Extractor

Defined in:
lib/prebake/extractor.rb

Constant Summary collapse

BINARY_EXTENSIONS =
%w[.so .bundle .dll].freeze

Class Method Summary collapse

Class Method Details

.install(gem_path, spec) ⇒ Object



11
12
13
14
15
16
17
18
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
# File 'lib/prebake/extractor.rb', line 11

def self.install(gem_path, spec)
  Logger.debug "Extracting precompiled binaries from #{File.basename(gem_path)}"

  extracted_count = 0

  Dir.mktmpdir("prebake-extract") do |tmpdir|
    # Extract all files from the gem into a temp directory
    Gem::Package.new(gem_path).extract_files(tmpdir)

    # Copy only binary files (.so, .bundle, .dll) to extension_dir
    Dir.glob(File.join(tmpdir, "**/*.{so,bundle,dll}")).each do |binary|
      # Reject symlinks and empty files
      next if File.symlink?(binary)
      next if File.size(binary).zero?

      # Verify path is within tmpdir (prevent traversal)
      real_binary = File.realpath(binary)
      real_tmpdir = File.realpath(tmpdir)
      next unless real_binary.start_with?("#{real_tmpdir}/")

      relative = binary.sub("#{tmpdir}/", "")

      # Normalize paths from cached gems where binaries were packaged
      # from gem_dir build artifacts or dirty extension_dirs.
      # ext/<name>/<name>.so               → <name>.so       (build artifact)
      # lib/<name>/<name>.so               → <name>/<name>.so (gem lib path)
      # extension/<platform>/<ver>/<name>.so → <name>.so       (extension_dir artifact)
      relative = relative.sub(%r{\Aext/[^/]+/}, "") if relative.start_with?("ext/")
      relative = relative.sub(%r{\Alib/}, "") if relative.start_with?("lib/")
      relative = relative.sub(%r{\Aextensions?/[^/]+/[^/]+/}, "") if relative.start_with?("extension/", "extensions/")

      dest = File.join(spec.extension_dir, relative)
      FileUtils.mkdir_p(File.dirname(dest))
      FileUtils.cp(binary, dest)
      extracted_count += 1
    end
  end

  # Mark this extension_dir as prebake-managed for post-install validation
  FileUtils.touch(File.join(spec.extension_dir, ".prebake")) if extracted_count > 0

  Logger.info "Installed precompiled #{File.basename(gem_path)} " \
              "(#{extracted_count} binary files)"

  extracted_count
rescue StandardError => e
  Logger.warn "Extraction failed for #{File.basename(gem_path)}: #{e.message}"
  raise
end