Class: Rails::Gem

Inherits:
Object
  • Object
show all
Includes:
SmartProperties
Defined in:
lib/project_types/rails/gem.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.binary_path_for(ctx, binary) ⇒ Object



21
22
23
24
# File 'lib/project_types/rails/gem.rb', line 21

def binary_path_for(ctx, binary)
  path_to_binary = File.join(gem_home(ctx), "bin", binary)
  File.exist?(path_to_binary) ? path_to_binary : binary
end

.gem_home(ctx) ⇒ Object



26
27
28
# File 'lib/project_types/rails/gem.rb', line 26

def gem_home(ctx)
  ctx.getenv("GEM_HOME") || apply_gem_home(ctx)
end

.gem_path(ctx) ⇒ Object



30
31
32
# File 'lib/project_types/rails/gem.rb', line 30

def gem_path(ctx)
  ctx.getenv("GEM_PATH") || apply_gem_path(ctx)
end

.install(ctx, *args) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/project_types/rails/gem.rb', line 13

def install(ctx, *args)
  name = args.shift
  version = args.shift
  gem = new(ctx: ctx, name: name, version: version)
  ctx.debug(ctx.message("rails.gem.installed_debug", name, gem.installed?))
  gem.installed? ? true : gem.install!
end

Instance Method Details

#gem_satisfies_version?(path) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/project_types/rails/gem.rb', line 96

def gem_satisfies_version?(path)
  if version
    # there was a specific version given during new(), so
    # check version of gem found to determine match
    require "semantic/semantic"
    found_version, _ = path.match(%r{/#{Regexp.quote(name)}-(\d\.\d\.\d)})&.captures
    found_version.nil? ? false : Semantic::Version.new(found_version).satisfies?(version)
  else
    # otherwise ignore the actual version number,
    # just check there's an initial digit
    %r{/#{Regexp.quote(name)}-\d}.match?(path)
  end
end

#install!Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/project_types/rails/gem.rb', line 79

def install!
  spin = CLI::UI::SpinGroup.new
  spin.add(ctx.message("rails.gem.installing", name)) do |spinner|
    args = ["#{ENV["RUBY_BINDIR"]}gem", "install", name]
    unless version.nil?
      if ctx.windows? && version.include?("~")
        args.push("-v", "\"#{version}\"")
      else
        args.push("-v", version)
      end
    end
    ctx.system(*args)
    spinner.update_title(ctx.message("rails.gem.installed", name))
  end
  spin.wait
end

#installed?Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/project_types/rails/gem.rb', line 66

def installed?
  found = false
  paths = self.class.gem_path(ctx).split(File::PATH_SEPARATOR)
  paths.each do |path|
    ctx.debug(ctx.message("rails.gem.checking_installation_path", "#{path}/gems/", name))
    found = !!Dir.glob("#{path}/gems/#{name}-*").detect do |f|
      gem_satisfies_version?(f)
    end
    break if found
  end
  found
end