Class: Tapioca::Gemfile::GemSpec

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Tapioca::GemHelper
Defined in:
lib/tapioca/gemfile.rb

Constant Summary collapse

IGNORED_GEMS =
[
  "sorbet",
  "sorbet-static",
  "sorbet-runtime",
  "sorbet-static-and-runtime",
  "debug",
  "irb",
  "fakefs",
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Tapioca::GemHelper

#gem_in_app_dir?, #gem_in_bundle_path?, #gem_in_ruby_path?, #to_realpath

Constructor Details

#initialize(spec) ⇒ GemSpec

: (Spec spec) -> void



137
138
139
140
141
142
143
144
# File 'lib/tapioca/gemfile.rb', line 137

def initialize(spec)
  @spec = spec #: Tapioca::Gemfile::Spec
  real_gem_path = to_realpath(@spec.full_gem_path)
  @full_gem_path = real_gem_path #: String
  @version = version_string #: String
  @exported_rbi_files = nil #: Array[String]?
  @files = collect_files #: Array[Pathname]
end

Instance Attribute Details

#filesObject (readonly)

: Array



134
135
136
# File 'lib/tapioca/gemfile.rb', line 134

def files
  @files
end

#full_gem_pathObject (readonly)

: String



131
132
133
# File 'lib/tapioca/gemfile.rb', line 131

def full_gem_path
  @full_gem_path
end

#versionObject (readonly)

: String



131
132
133
# File 'lib/tapioca/gemfile.rb', line 131

def version
  @version
end

Class Method Details

.spec_lookup_by_file_pathObject

: -> Hash[String, Gemfile::GemSpec]



111
112
113
114
115
116
117
# File 'lib/tapioca/gemfile.rb', line 111

def spec_lookup_by_file_path
  @lookup ||= [*::Gem::Specification.default_stubs, *::Gem::Specification.stubs]
    .map! { |spec| new(spec.to_spec) }
    .flat_map do |spec|
      spec.files.filter_map { |file| [file.realpath.to_s, spec] if file.exist? }
    end.to_h #: Hash[String, Gemfile::GemSpec]?
end

Instance Method Details

#==(other) ⇒ Object

: (BasicObject other) -> bool



147
148
149
# File 'lib/tapioca/gemfile.rb', line 147

def ==(other)
  GemSpec === other && other.name == name && other.version == version
end

#contains_path?(path) ⇒ Boolean

: (String path) -> bool

Returns:

  • (Boolean)


172
173
174
175
176
177
178
# File 'lib/tapioca/gemfile.rb', line 172

def contains_path?(path)
  if default_gem?
    files.any? { |file| file.to_s == to_realpath(path) }
  else
    path_in_dir?(to_realpath(path), full_gem_path) || has_parent_gemspec?(path)
  end
end

#dependenciesObject

: -> Array



162
163
164
# File 'lib/tapioca/gemfile.rb', line 162

def dependencies
  @spec.dependencies
end

#export_rbi_files?Boolean

: -> bool

Returns:

  • (Boolean)


186
187
188
# File 'lib/tapioca/gemfile.rb', line 186

def export_rbi_files?
  exported_rbi_files.any?
end

#exported_rbi_filesObject

: -> Array



181
182
183
# File 'lib/tapioca/gemfile.rb', line 181

def exported_rbi_files
  @exported_rbi_files ||= Dir.glob("#{full_gem_path}/rbi/**/*.rbi").sort
end

#exported_rbi_treeObject

: -> RBI::MergeTree



191
192
193
194
195
196
197
198
199
200
# File 'lib/tapioca/gemfile.rb', line 191

def exported_rbi_tree
  rewriter = RBI::Rewriters::Merge.new(keep: RBI::Rewriters::Merge::Keep::NONE)

  exported_rbi_files.each do |file|
    rbi = RBI::Parser.parse_file(file)
    rewriter.merge(rbi)
  end

  rewriter.tree
end

#ignore?(gemfile_dir) ⇒ Boolean

: (String gemfile_dir) -> bool

Returns:

  • (Boolean)


152
153
154
# File 'lib/tapioca/gemfile.rb', line 152

def ignore?(gemfile_dir)
  gem_ignored? || gem_in_app_dir?(gemfile_dir, full_gem_path)
end

#nameObject

: -> String



157
158
159
# File 'lib/tapioca/gemfile.rb', line 157

def name
  @spec.name
end

#rbi_file_nameObject

: -> String



167
168
169
# File 'lib/tapioca/gemfile.rb', line 167

def rbi_file_name
  "#{name}@#{version}.rbi"
end

#relative_path_for(file) ⇒ Object

: (Pathname file) -> Pathname



203
204
205
206
207
208
209
# File 'lib/tapioca/gemfile.rb', line 203

def relative_path_for(file)
  if default_gem?
    file.realpath.relative_path_from(RbConfig::CONFIG["rubylibdir"])
  else
    file.realpath.relative_path_from(full_gem_path)
  end
end