Class: Baltix::Source::Gem

Inherits:
Base
  • Object
show all
Extended by:
Loader, Loader::Cmake, Loader::GitVersionGen, Loader::Mast, Loader::Pom, Loader::Rookbook, Loader::Yaml
Defined in:
lib/baltix/source/gem.rb

Constant Summary collapse

TYPE =
'Gem::Specification'
BIN_IGNORES =
%w(test)
OPTION_KEYS =
%i(source_file source_names gemspec spec version replace_list aliases alias_names)
EXE_DIRS =
->(s) { s.spec.bindir || s.exedir || nil }
EXT_DIRS =
->(s) do
   s.spec.extensions.map do |file|
      File.dirname(file)
   end.uniq
end
LIB_DIRS =
->(s) { s.require_pure_paths }
DOCSRC_DIRS =
->(s) { s.require_pure_paths }
INC_FILTER =
->(s, f, dir) { s.spec.files.include?(File.join(dir, f)) }
OPTIONS_IN =
{
   spec: true,
}
LOADERS =
{
   /\/pom.xml$/ => :pom,
   /\/(cmake|CMakeLists.txt)$/ => :cmake,
   /\/Rookbook.props$/ => :rookbook,
   /\/GIT-VERSION-GEN$/ => :git_version_gen,
   /\/(MANIFEST|Manifest.txt)$/ => :manifest,
   /\/(#{Rake::Application::DEFAULT_RAKEFILES.join("|")})$/i => :app_file,
   /\.gemspec$/i => [:app_file, :yaml],
}

Constants included from Log

Log::DEFAULT_IO_NAMES

Constants included from Loader::Mast

Loader::Mast::PROPS

Constants inherited from Base

Base::APP_DIRS, Base::CONF_DIRS, Base::DATA_DIRS, Base::DATA_RE, Base::DL_DIRS, Base::DL_RE, Base::DOCSRC_RE, Base::EXT_RE, Base::GROUPS, Base::INC_DIRS, Base::INC_RE, Base::LOG_DIRS, Base::MAN_DIRS, Base::MAN_RE, Base::RI_DIRS, Base::RI_RE, Base::STATE_DIRS, Base::SUP_DIRS, Base::TEST_DIRS

Instance Attribute Summary collapse

Attributes inherited from Base

#loader, #options, #replace_list, #source_file, #source_names

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Loader

app_file, extended, extended_list, load_file, mods, pre_loaders, type_hash

Methods included from Log

default_prefix, io_name_parse, ios, level, #level_match, #log, prefix, prefix_for, setup, setup_kind

Methods included from Loader::Yaml

yaml

Methods included from Loader::Pom

pom

Methods included from Loader::Mast

manifest, transform_version, value_for

Methods included from Loader::Rookbook

rookbook

Methods included from Loader::Cmake

cmake

Methods included from Loader::GitVersionGen

git_version_gen

Methods inherited from Base

#+, #alias_names, #alias_to, #aliases, #default_ridir, #definition, #development_dependencies, #dsl, #has_name?, #if_dir, #if_exist, #if_file, name_or_default, opts, #required_ruby, #rootdir, rootdir_or_default, source_options, #source_path_from, #to_os, #trees, #type

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (protected)



397
398
399
400
401
402
403
404
405
406
# File 'lib/baltix/source/gem.rb', line 397

def method_missing name, *args
   if /^aliased_(?<method>.*)/ =~ name
      with_lock { aliased {|a| a.send(method, *args) } }
   elsif spec.respond_to?(name)
      spec.send(name, *args)
   else
      super
   end
rescue NoMethodError
end

Instance Attribute Details

#gem_version_replaceObject (readonly)

Returns the value of attribute gem_version_replace.



52
53
54
# File 'lib/baltix/source/gem.rb', line 52

def gem_version_replace
  @gem_version_replace
end

Class Method Details

.load(spec_in) ⇒ Object



57
58
59
# File 'lib/baltix/source/gem.rb', line 57

def load spec_in
   Kernel.yaml_load(spec_in)
end

.name_for(options_in = {}) ⇒ Object



81
82
83
# File 'lib/baltix/source/gem.rb', line 81

def name_for options_in = {}
   spec_for(options_in).name
end

.search(dir, options_in = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/baltix/source/gem.rb', line 85

def search dir, options_in = {}
   files = Dir.glob("#{dir}/**/*", File::FNM_DOTMATCH).select {|f| File.file?(f) }.map do |f|
      LOADERS.reduce(nil) { |res, (re, _method_name)| res || re =~ f && [re, f] || nil }
   end.compact.sort do |x,y|
      c = LOADERS.keys.index(x.first) <=> LOADERS.keys.index(y.first)

      c == 0 && x.last <=> y.last || c
   end

   debug("Found source file list: " + files.map {|(_, x)| x }.join("\n\t"))

   specs = files.reduce({}) do |res, (re, f)|
      load_result =
         [LOADERS[re]].flatten.reduce(nil) do |res, method_name|
            next res if res

            result = send(method_name, f)

            result && result.objects.any? && [result, method_name]
         end

      if load_result
         gemspecs = load_result.first.objects.reject do |s|
            s.loaded_from && s.loaded_from !~ /#{dir}/
         end.each {|x| x.loaded_from = f }.compact
         debug("load messages:\n\t" + load_result.first.log.join("\n\t")) if !load_result.first.log.blank?
         debug("Load errors:\n\t" + load_result.errlog.join("\n\t")) if !load_result.first.errlog.blank?

         res.merge({ f => { gemspecs: gemspecs, loader: load_result.last }})
      else
         res
      end
   end.map do |(f, data)|
      gemspecs = data[:gemspecs]
      gemspecs.map do |gemspec|
         self.new(source_options(options_in.merge(spec: gemspec, source_file: f, loader: data[:loader])))
      end
   end.flatten.compact
end

.spec_for(options_in = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/baltix/source/gem.rb', line 61

def spec_for options_in = {}
   spec_in = options_in["spec"]
   spec = spec_in.is_a?(String) && load(spec_in) || spec_in
   version =
      if options_in[:version_replaces] && options_in[:version_replaces][spec.name]
         options_in[:version_replaces][spec.name]
      elsif options_in[:gem_version_replace] && !options_in[:gem_version_replace].empty?
         prever = options_in[:gem_version_replace].find {|(n,v)| n == spec.name }&.last
         /([=><]+\s*)?(?<version>.*)/ =~ prever
         version
      end

   if version
      spec.version = Gem::Version.new(version)
   end
   spec.require_paths = options_in["source-lib-folders"] if options_in["source-lib-folders"]

   spec
end

Instance Method Details

#aliased(&block) ⇒ Object



321
322
323
# File 'lib/baltix/source/gem.rb', line 321

def aliased &block
   aliases.reduce(nil) {|res, a| res || a != self && block[a] }
end

#aliased_locksObject



317
318
319
# File 'lib/baltix/source/gem.rb', line 317

def aliased_locks
   @aliased_locks ||= {}
end

#allfilesObject



205
206
207
208
209
210
211
# File 'lib/baltix/source/gem.rb', line 205

def allfiles
   @allfiles = (
      spec.require_paths.map {|x| File.absolute_path?(x) && x || File.join(x, '**', '*') }.map {|x| Dir[x] }.flatten |
      spec.executables.map {|x| Dir[File.join(spec.bindir, x)] }.flatten |
      spec.files
   )
end

#allfiles_for(list_in) ⇒ Object



213
214
215
216
217
# File 'lib/baltix/source/gem.rb', line 213

def allfiles_for list_in
   list_in.map do |(key, list)|
      [key, list & allfiles.map {|x| /^#{key}\/(?<rest>.*)/.match(x)&.[](:rest) }.compact ]
   end.to_h
end

#compilable?Boolean

Returns:

  • (Boolean)


269
270
271
# File 'lib/baltix/source/gem.rb', line 269

def compilable?
   compilables.any?
end

#compilablesObject



273
274
275
# File 'lib/baltix/source/gem.rb', line 273

def compilables
   extfiles | spec.extensions
end

#datatreeObject

tree



200
201
202
203
# File 'lib/baltix/source/gem.rb', line 200

def datatree
   # TODO deep_merge
   @datatree ||= super { { '.' => spec.files } }
end

#depObject



138
139
140
# File 'lib/baltix/source/gem.rb', line 138

def dep
   Bundler::Dependency.new("#{name}", Gem::Requirement.new(["~> #{version}"]), options: { type: :runtime })
end

#dependencies(type = nil) ⇒ Object



342
343
344
345
346
347
348
349
350
351
# File 'lib/baltix/source/gem.rb', line 342

def dependencies type = nil
   (dsl.dependencies(type) | deps).group_by {|x| x.name }.reduce([]) do |res, (_name, deps)|
      dep =
         deps.reduce do |r, dep|
            Gem::Dependency.new(r.name, r.requirement.merge(dep.requirement), [r.type, dep.type].max)
         end

      !type || dep.type == type ? res | [dep] : res
   end
end

#deps(groups = nil) ⇒ Object



290
291
292
# File 'lib/baltix/source/gem.rb', line 290

def deps groups = nil
   spec&.dependencies&.select { |dep| !groups || [ groups ].flatten.include?(dep.type) }
end

#descriptionsObject



313
314
315
# File 'lib/baltix/source/gem.rb', line 313

def descriptions
   localize(spec.description) || aliased {|s| localize(s.description) }
end

#docsObject



237
238
239
240
241
# File 'lib/baltix/source/gem.rb', line 237

def docs
   # TODO make docs to docdir with lib/.rb replace to .ri
   #require 'pry';binding.ry
   (!spec.rdoc_options.blank? && [ default_ridir ] || files(:lib)) | spec.extra_rdoc_files
end

#docsrctreeObject



233
234
235
# File 'lib/baltix/source/gem.rb', line 233

def docsrctree
   @docsrctree ||= super { { '.' => spec.extra_rdoc_files } }
end

#exetreeObject



227
228
229
230
231
# File 'lib/baltix/source/gem.rb', line 227

def exetree
   @exetree ||= super { Dir.chdir(rootdir) do
         exedirs.map { |dir| [ dir, Dir.chdir(File.join(rootdir, dir)) { Dir.glob("{#{spec.executables.join(',')}}") } ] }.to_h
      end }
end

#extroot_for(file) ⇒ Object

custom



245
246
247
# File 'lib/baltix/source/gem.rb', line 245

def extroot_for file
   extroots.find { |extroot| extroot == file[0...extroot.size] }
end

#exttreeObject



219
220
221
# File 'lib/baltix/source/gem.rb', line 219

def exttree
   @exttree ||= super
end

#fullnameObject



142
143
144
# File 'lib/baltix/source/gem.rb', line 142

def fullname
   [ name, version ].compact.join('-')
end

#gemfileObject



126
127
128
129
130
131
132
# File 'lib/baltix/source/gem.rb', line 126

def gemfile
   @gemfile ||= Baltix::Source::Gemfile.new({
      source_file: gemfile_name && File.join(rootdir, gemfile_name) || dsl.fake_gemfile_path,
      gem_version_replace: gem_version_replace,
      gem_skip_list: [],# dsl.deps.map(&:name) | [name],
      gem_append_list: [ self.dep ]}.to_os)
end

#gemfile_nameObject



134
135
136
# File 'lib/baltix/source/gem.rb', line 134

def gemfile_name
   source_names.find {|x| x =~ /gemfile/i }
end

#gemfile_pathObject



188
189
190
191
192
193
194
195
196
197
# File 'lib/baltix/source/gem.rb', line 188

def gemfile_path
   if gemfile.dsl.valid?
      if @gemfile_file ||= Tempfile.create('Gemfile.')
         @gemfile_file.puts(gemfile.dsl.to_gemfile)
         @gemfile_file.rewind
      end

      @gemfile_path ||= @gemfile_file.path
   end
end

#gemspec_pathObject



179
180
181
182
183
184
185
186
# File 'lib/baltix/source/gem.rb', line 179

def gemspec_path
   if @gemspec_file ||= Tempfile.create('gemspec.')
      @gemspec_file.puts(dsl.to_ruby)
      @gemspec_file.rewind
   end

   @gemspec_path ||= @gemspec_file.path
end

#groupObject

Default group



305
306
307
# File 'lib/baltix/source/gem.rb', line 305

def group
   "Development/Ruby"
end

#licensesObject



357
358
359
# File 'lib/baltix/source/gem.rb', line 357

def licenses
   spec.licenses
end

#localize(text) ⇒ Object



325
326
327
# File 'lib/baltix/source/gem.rb', line 325

def localize text
   text && OpenStruct.new(Baltix::I18n.default_locale => text)
end

#nameObject



168
169
170
# File 'lib/baltix/source/gem.rb', line 168

def name
   spec.name
end

#name_prefixObject

Default prefix “gem” for gem names



330
331
332
# File 'lib/baltix/source/gem.rb', line 330

def name_prefix
   "gem"
end

#original_specObject



146
147
148
149
150
151
152
153
154
155
# File 'lib/baltix/source/gem.rb', line 146

def original_spec
   return @original_spec if @original_spec.is_a?(Gem::Specification)

   @original_spec =
      if @original_spec.is_a?(String)
         YAML.load(@original_spec)
      else
         self.class.spec_for(options)
      end
end

#platformObject



265
266
267
# File 'lib/baltix/source/gem.rb', line 265

def platform
   spec.platform
end

#provideObject



353
354
355
# File 'lib/baltix/source/gem.rb', line 353

def provide
   spec.version && Gem::Dependency.new(spec.name, Gem::Requirement.new(["= #{spec.version}"]), :runtime) || Gem::Dependency.new(spec.name)
end

#rakeObject



300
301
302
# File 'lib/baltix/source/gem.rb', line 300

def rake
   @rake ||= Baltix::Rake.new(File.join(rootdir, Dir["{#{Rake::Application::DEFAULT_RAKEFILES.join(",")}}"].first))
end

#require_pure_pathsObject



294
295
296
297
298
# File 'lib/baltix/source/gem.rb', line 294

def require_pure_paths
   @require_pure_paths ||= (
      paths = spec.require_paths.select { |path| path !~ /^\// }
      paths.any? && paths || ['lib'])
end

#required_ruby_versionObject



282
283
284
# File 'lib/baltix/source/gem.rb', line 282

def required_ruby_version
   spec&.required_ruby_version || super
end

#required_rubygems_versionObject



286
287
288
# File 'lib/baltix/source/gem.rb', line 286

def required_rubygems_version
   spec&.required_rubygems_version || super
end

#specObject



157
158
159
160
161
162
163
164
165
166
# File 'lib/baltix/source/gem.rb', line 157

def spec
   return @spec if @spec

   if aliases.any?
      @spec ||= aliases.reduce(original_spec) { |spec, als|
        spec.merge(als.original_spec) }
   else
      original_spec
   end
end

#summariesObject



309
310
311
# File 'lib/baltix/source/gem.rb', line 309

def summaries
   localize(spec.summary) || aliased {|s| s.localize(s.summary) } || descriptions
end

#testtreeObject



223
224
225
# File 'lib/baltix/source/gem.rb', line 223

def testtree
   @testtree ||= allfiles_for(super)
end

#to_hObject



277
278
279
280
# File 'lib/baltix/source/gem.rb', line 277

def to_h
   # TODO !ruby/array:Files strangely appeared during building the securecompare gem, what leads to exceptions on load
   super.merge(spec: spec.to_yaml.gsub(/!ruby\/array:Files/, ""))
end

#uriObject



334
335
336
# File 'lib/baltix/source/gem.rb', line 334

def uri
   spec.homepage || spec.["homepage_uri"] || aliased_uri
end

#valid?Boolean

valid? returns state of validity of the gem: true or false Returns true when all the conditiona are true:

  • gem’s name of the gem is set

  • gem’s name is not a system’s one (has zero char)

  • gem’s version is present

  • gem’s platform is “ruby” or current one

Returns:

  • (Boolean)


258
259
260
261
262
263
# File 'lib/baltix/source/gem.rb', line 258

def valid?
   !name.nil? &&
      spec.version &&
      (platform == 'ruby' || platform == Gem::Platform::CURRENT) &&
      spec.name !~ /\u0000/
end

#vcsObject



338
339
340
# File 'lib/baltix/source/gem.rb', line 338

def vcs
   spec.&.[]("source_code_uri")
end

#versionObject



172
173
174
175
176
177
# File 'lib/baltix/source/gem.rb', line 172

def version
   version = spec&.version&.to_s || "0"
   parts = version.split(".")

   (parts[0..2] + (parts[3..-1]&.map {|x| x.to_i <= 1024 && x || nil}&.compact || [])).join(".")
end