Class: MRuby::Gem::List

Inherits:
Object show all
Includes:
Enumerable
Defined in:
ext/enterprise_script_service/mruby/lib/mruby/gem.rb

Overview

Version

Constant Summary

Constants included from Enumerable

Enumerable::NONE

Instance Method Summary collapse

Methods included from Enumerable

#all?, #any?, #chain, #collect, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_slice, #each_with_index, #each_with_object, #entries, #filter_map, #find_all, #find_index, #first, #flat_map, #grep, #group_by, #hash, #include?, #inject, #lazy, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reject, #reverse_each, #sort, #sort_by, #take, #take_while, #tally, #to_h, #uniq, #zip

Constructor Details

#initializeList

Returns a new instance of List.


353
354
355
# File 'ext/enterprise_script_service/mruby/lib/mruby/gem.rb', line 353

def initialize
  @ary = []
end

Instance Method Details

#<<(gem) ⇒ Object


365
366
367
368
369
370
371
# File 'ext/enterprise_script_service/mruby/lib/mruby/gem.rb', line 365

def <<(gem)
  unless @ary.detect {|g| g.dir == gem.dir }
    @ary << gem
  else
    # GEM was already added to this list
  end
end

#[](name) ⇒ Object


361
362
363
# File 'ext/enterprise_script_service/mruby/lib/mruby/gem.rb', line 361

def [](name)
  @ary.detect {|g| g.name == name}
end

#check(build) ⇒ Object


468
469
470
471
472
473
474
475
476
477
478
# File 'ext/enterprise_script_service/mruby/lib/mruby/gem.rb', line 468

def check(build)
  gem_table = generate_gem_table build

  @ary = tsort_dependencies gem_table.keys, gem_table, true

  each(&:setup_compilers)

  each do |g|
    import_include_paths(g)
  end
end

#default_gem_params(dep) ⇒ Object


377
378
379
380
381
382
383
384
# File 'ext/enterprise_script_service/mruby/lib/mruby/gem.rb', line 377

def default_gem_params dep
  if dep[:default]; dep
  elsif File.exist? "#{MRUBY_ROOT}/mrbgems/#{dep[:gem]}" # check core
    { :gem => dep[:gem], :default => { :core => dep[:gem] } }
  else # fallback to mgem-list
    { :gem => dep[:gem], :default => { :mgem => dep[:gem] } }
  end
end

#each(&b) ⇒ Object


357
358
359
# File 'ext/enterprise_script_service/mruby/lib/mruby/gem.rb', line 357

def each(&b)
  @ary.each(&b)
end

#empty?Boolean

Returns:

  • (Boolean)

373
374
375
# File 'ext/enterprise_script_service/mruby/lib/mruby/gem.rb', line 373

def empty?
  @ary.empty?
end

#generate_gem_table(build) ⇒ Object


386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'ext/enterprise_script_service/mruby/lib/mruby/gem.rb', line 386

def generate_gem_table build
  gem_table = each_with_object({}) { |spec, h| h[spec.name] = spec }

  default_gems = {}
  each do |g|
    g.dependencies.each do |dep|
      default_gems[dep[:gem]] ||= default_gem_params(dep)
    end
  end

  until default_gems.empty?
    def_name, def_gem = default_gems.shift
    next if gem_table[def_name]

    spec = gem_table[def_name] = build.gem(def_gem[:default])
    fail "Invalid gem name: #{spec.name} (Expected: #{def_name})" if spec.name != def_name
    spec.setup

    spec.dependencies.each do |dep|
      default_gems[dep[:gem]] ||= default_gem_params(dep)
    end
  end

  each do |g|
    g.dependencies.each do |dep|
      name = dep[:gem]
      req_versions = dep[:requirements]
      dep_g = gem_table[name]

      # check each GEM dependency against all available GEMs
      if dep_g.nil?
        fail "The GEM '#{g.name}' depends on the GEM '#{name}' but it could not be found"
      end
      unless dep_g.version_ok? req_versions
        fail "#{name} version should be #{req_versions.join(' and ')} but was '#{dep_g.version}'"
      end
    end

    cfls = g.conflicts.select { |c|
      cfl_g = gem_table[c[:gem]]
      cfl_g and cfl_g.version_ok?(c[:requirements] || ['>= 0.0.0'])
    }.map { |c| "#{c[:gem]}(#{gem_table[c[:gem]].version})" }
    fail "Conflicts of gem `#{g.name}` found: #{cfls.join ', '}" unless cfls.empty?
  end

  gem_table
end

#import_include_paths(g) ⇒ Object


480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'ext/enterprise_script_service/mruby/lib/mruby/gem.rb', line 480

def import_include_paths(g)
  gem_table = each_with_object({}) { |spec, h| h[spec.name] = spec }

  g.dependencies.each do |dep|
    dep_g = gem_table[dep[:gem]]
    # We can do recursive call safely
    # as circular dependency has already detected in the caller.
    import_include_paths(dep_g)

    dep_g.export_include_paths.uniq!
    g.compilers.each do |compiler|
      compiler.include_paths += dep_g.export_include_paths
      g.export_include_paths += dep_g.export_include_paths
      compiler.include_paths.uniq!
      g.export_include_paths.uniq!
    end
  end
end

#linker_attrsObject


499
500
501
# File 'ext/enterprise_script_service/mruby/lib/mruby/gem.rb', line 499

def linker_attrs
  map{|g| g.linker.run_attrs}.transpose
end

#tsort_dependencies(ary, table, all_dependency_listed = false) ⇒ Object


434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'ext/enterprise_script_service/mruby/lib/mruby/gem.rb', line 434

def tsort_dependencies ary, table, all_dependency_listed = false
  unless all_dependency_listed
    left = ary.dup
    until left.empty?
      v = left.pop
      table[v].dependencies.each do |dep|
        left.push dep[:gem]
        ary.push dep[:gem]
      end
    end
  end

  ary.uniq!
  table.instance_variable_set :@root_gems, ary
  class << table
    include TSort
    def tsort_each_node &b
      @root_gems.each &b
    end

    def tsort_each_child(n, &b)
      fetch(n).dependencies.each do |v|
        b.call v[:gem]
      end
    end
  end

  begin
    table.tsort.map { |v| table[v] }
  rescue TSort::Cyclic => e
    fail "Circular mrbgem dependency found: #{e.message}"
  end
end