require "gems" # Usage: # # Lambdagem::Extract::Gem.new("pg-0.21.0", # s3: "lambdagems", # build_root: cache_area, # defaults to /tmp/lambdagem # dest: cache_area, # defaults to . (project_root) # ).run # module Lambdagem::Extract class Gem < Base VERSION_PATTERN = /-(\d+\.\d+\.\d+.*)/ def run puts "Looking for #{full_gem_name} gem in: #{@options[:lambdagems_url]}" clean_downloads(:gems) if @options[:clean] tarball_path = download_gem remove_current_gem unpack_tarball(tarball_path) end # ensure that we always have the full gem name def full_gem_name return @full_gem_name if @full_gem_name @name # TODO: make sure that we always have the full gem name with version number if @name.match(VERSION_PATTERN) @full_gem_name = @name return @full_gem_name end # name doesnt have a version yet, so grab the latest version and add it version = Gems.versions(@name).first @full_gem_name = "#{@name}-#{version["number"]}" end def gem_name full_gem_name.gsub(VERSION_PATTERN,'') # folder: byebug end # Downloads and extracts the linux gem into the proper directory. # Extracts to: . (current directory) # # It produces a `bundled` folder. # The folder contains the re-produced directory structure. Example with # the gem: byebug-9.1.0 # # bundled/gems/ruby/2.5.0/extensions/x86_64-darwin-16/2.5.0-static/byebug-9.1.0 # def download_gem # download - also move to /tmp/jets/demo/compiled_gems folder url = gem_url tarball_dest = download_file(url, download_path(File.basename(url))) unless tarball_dest message = "Url: #{url} not found" if @options[:exit_on_error] puts message exit else raise NotFound.new(message) end end puts "Tarball downloaded to: #{tarball_dest}" tarball_dest end def download_path(filename) "#{@build_root}/downloads/gems/#{filename}" end # Finds any currently install gems that matched with the gem name and version # and remove them first. # We clean up the current install gems first in case it was previously installed # and has different *.so files that can be accidentally required. This # happened with the pg gem. def remove_current_gem puts "Removing current #{full_gem_name} gem installation:" gem_dirs = Dir.glob("#{project_root}/**/*").select do |path| File.directory?(path) && path =~ %r{bundled/gems} && File.basename(path) == full_gem_name end gem_dirs.each do |dir| puts " rm -rf #{dir}" FileUtils.rm_rf(dir) end end # full_gem_name: byebug-9.1.0 def gem_url "#{lambdagems_url}/gems/#{jets_ruby_version}/#{gem_name}/#{full_gem_name}-x86_64-linux.tgz" end end end