Class: Jets::Gems::Extract::Gem

Inherits:
Base
  • Object
show all
Includes:
Api::Concern
Defined in:
lib/jets/gems/extract/gem.rb

Constant Summary collapse

VERSION_PATTERN =
/-(\d+\.\d+.*)/

Instance Attribute Summary

Attributes inherited from Base

#project_root

Instance Method Summary collapse

Methods included from Api::Concern

#api

Methods inherited from Base

#clean_downloads, #download_file, #initialize, #log_level=, #say, #sh, #unzip

Constructor Details

This class inherits a constructor from Jets::Gems::Extract::Base

Instance Method Details

#download_gemObject

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

vendor/gems/ruby/2.5.0/extensions/x86_64-darwin-16/2.5.0-static/byebug-9.1.0


51
52
53
54
55
56
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
# File 'lib/jets/gems/extract/gem.rb', line 51

def download_gem
  # download - also move to /tmp/jets/demo/compiled_gems folder
  begin
    @retries ||= 0
    url = gem_url
    basename = File.basename(url).gsub(/\?.*/,'') # remove query string info
    tarball_dest = download_file(url, download_path(basename))
  rescue OpenURI::HTTPError => e
    url_without_query = url.gsub(/\?.*/,'')
    puts "Error downloading #{url_without_query}"
    @retries += 1
    if @retries < 3
      sleep 1
      puts "Retrying download. Retry attempt: #{@retries}"
      retry
    else
      raise e
    end
  end

  unless tarball_dest
    message = "Url: #{url} not found"
    if @options[:exit_on_error]
      say message
      exit
    else
      raise NotFound.new(message)
    end
  end
  say "Downloaded to: #{tarball_dest}"
  tarball_dest
end

#download_path(filename) ⇒ Object



95
96
97
# File 'lib/jets/gems/extract/gem.rb', line 95

def download_path(filename)
  "#{@downloads_root}/downloads/gems/#{filename}"
end

#full_gem_nameObject

ensure that we always have the full gem name



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jets/gems/extract/gem.rb', line 25

def full_gem_name
  return @full_gem_name if @full_gem_name

  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

#gem_nameObject



38
39
40
# File 'lib/jets/gems/extract/gem.rb', line 38

def gem_name
  full_gem_name.gsub(VERSION_PATTERN,'') # folder: byebug
end

#gem_urlObject

full_gem_name: byebug-9.1.0



85
86
87
88
89
90
91
92
93
# File 'lib/jets/gems/extract/gem.rb', line 85

def gem_url
  data = api.download_url(gem_name: full_gem_name, project: Jets.config.project_name)
  if data["download_url"]
    data["download_url"]
  else
    puts data["message"].color(:red)
    exit 1
  end
end

#remove_current_gemObject

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.



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/jets/gems/extract/gem.rb', line 104

def remove_current_gem
  say "Removing current #{full_gem_name} gem installation:"
  gem_dirs = Dir.glob("#{project_root}/**/*").select do |path|
              File.directory?(path) &&
              path =~ %r{vendor/gems} &&
              File.basename(path) == full_gem_name
            end
  gem_dirs.each do |dir|
    say "  rm -rf #{dir}"
    FileUtils.rm_rf(dir)
  end
end

#runObject



8
9
10
11
12
13
14
15
# File 'lib/jets/gems/extract/gem.rb', line 8

def run
  say "Will download and extract gem: #{full_gem_name}"
  clean_downloads(:gems) if @options[:clean]
  zipfile_path = download_gem
  remove_current_gem if Jets.config.gems.clean
  unzip_file(zipfile_path)
  say("Gem #{full_gem_name} unpacked at #{project_root}")
end

#unzip_file(zipfile_path) ⇒ Object



17
18
19
20
21
22
# File 'lib/jets/gems/extract/gem.rb', line 17

def unzip_file(zipfile_path)
  dest = "#{Jets.build_root}/stage/opt"
  say "Unpacking into #{dest}"
  FileUtils.mkdir_p(dest)
  unzip(zipfile_path, dest)
end