Class: Autoproj::Ops::Install

Inherits:
Object
  • Object
show all
Defined in:
lib/autoproj/ops/install.rb

Overview

This class contains the functionality necessary to install autoproj in a clean root

It can be required standalone (i.e. does not depend on anything else than ruby and the ruby standard library)

Defined Under Namespace

Classes: BundlerFailed, UnexpectedBinstub

Constant Summary collapse

RUBYLIB_REINIT =
<<~RUBY
    if defined?(Bundler)
        if Bundler.respond_to?(:with_unbundled_env)
            Bundler.with_unbundled_env do
                exec(Hash['RUBYLIB' => nil], $0, *ARGV)
            end
        else
            Bundler.with_clean_env do
                exec(Hash['RUBYLIB' => nil], $0, *ARGV)
            end
        end
    elsif ENV['RUBYLIB']
        exec(Hash['RUBYLIB' => nil], $0, *ARGV)
    end
RUBY
WITHOUT_BUNDLER =
<<~RUBY
    if defined?(Bundler)
        if Bundler.respond_to?(:with_unbundled_env)
            Bundler.with_unbundled_env do
                exec($0, *ARGV)
            end
        else
            Bundler.with_clean_env do
                exec($0, *ARGV)
            end
        end
    end
RUBY
EXCLUDED_FROM_SHIMS =
%w[rake thor].freeze
ENV_BUNDLE_GEMFILE_RX =
/^(\s*ENV\[['"]BUNDLE_GEMFILE['"]\]\s*)(?:\|\|)?=/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_dir) ⇒ Install

Returns a new instance of Install.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/autoproj/ops/install.rb', line 65

def initialize(root_dir)
    @root_dir = root_dir
    @gem_source = "https://rubygems.org"
    @gemfile = nil
    @skip_stage2 = false

    @autoproj_options = Array.new

    @env = Hash.new
    env["RUBYOPT"] = []
    env["RUBYLIB"] = []
    env["PATH"] = self.class.sanitize_env(ENV["PATH"] || "")
    env["BUNDLE_GEMFILE"] = []
    env["BUNDLER_VERSION"] = []
    env["BUNDLER_SETUP"] = []
    env["BUNDLE_BIN_PATH"] = []
    load_config

    if config["ruby_executable"] != Gem.ruby
        raise "this autoproj installation was already bootstrapped using "\
              "#{config['ruby_executable']}, but you are currently running "\
              "under #{Gem.ruby}. Changing the ruby interpreter in a given "\
              "workspace is not supported, you need to do a clean bootstrap"
    end
    @ruby_executable = config["ruby_executable"]
    @local = false

    @gems_install_path ||= default_gems_install_path
    @gems_install_path = File.expand_path(@gems_install_path)

    env["GEM_HOME"] = [gems_gem_home]
    env["GEM_PATH"] = [gems_gem_home]
end

Instance Attribute Details

#autoproj_optionsObject (readonly)

A set of options that should be passed to autoproj when calling it in a subprocess



59
60
61
# File 'lib/autoproj/ops/install.rb', line 59

def autoproj_options
  @autoproj_options
end

#configObject (readonly)

The configuration hash



56
57
58
# File 'lib/autoproj/ops/install.rb', line 56

def config
  @config
end

#envObject (readonly)

The environment that is passed to the bundler installs



54
55
56
# File 'lib/autoproj/ops/install.rb', line 54

def env
  @env
end

#gem_sourceObject

The URL of the source to be used to get gems



63
64
65
# File 'lib/autoproj/ops/install.rb', line 63

def gem_source
  @gem_source
end

#gemfileObject

Content of the Gemfile generated to install autoproj itself



52
53
54
# File 'lib/autoproj/ops/install.rb', line 52

def gemfile
  @gemfile
end

#gems_install_pathString

The path into which the workspace’s gems should be installed

They are installed in a versioned subdirectory of this path, e.g. #gem_path_suffix.

Returns:

  • (String)


187
188
189
# File 'lib/autoproj/ops/install.rb', line 187

def gems_install_path
  @gems_install_path
end

#local=(value) ⇒ Object (writeonly)

Whether we can access the network while installing



166
167
168
# File 'lib/autoproj/ops/install.rb', line 166

def local=(value)
  @local = value
end

#root_dirObject (readonly)

The created workspace’s root directory



50
51
52
# File 'lib/autoproj/ops/install.rb', line 50

def root_dir
  @root_dir
end

#ruby_executableObject (readonly)

The Ruby interpreter we use for this install



61
62
63
# File 'lib/autoproj/ops/install.rb', line 61

def ruby_executable
  @ruby_executable
end

#skip_stage2=(value) ⇒ Object (writeonly)

Whether the stage2 install should be called or not



159
160
161
# File 'lib/autoproj/ops/install.rb', line 159

def skip_stage2=(value)
  @skip_stage2 = value
end

Class Method Details

.default_bundler_versionObject



787
788
789
790
791
792
793
# File 'lib/autoproj/ops/install.rb', line 787

def self.default_bundler_version
    if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.6.0")
        "2.3.6"
    elsif Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.0.0")
        "2.4.22"
    end
end

.gems_path_suffixObject

The version and platform-specific suffix

This is also the suffix used by bundler to install gems



171
172
173
174
175
176
177
178
179
# File 'lib/autoproj/ops/install.rb', line 171

def self.gems_path_suffix
    return @gems_path_suffix if @gem_path_suffix

    parts = [Gem.ruby_engine]
    unless RbConfig::CONFIG["ruby_version"].empty?
        parts << RbConfig::CONFIG["ruby_version"]
    end
    @gems_path_suffix = File.join parts
end

.guess_gem_programObject

Raises:

  • (ArgumentError)


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/autoproj/ops/install.rb', line 238

def self.guess_gem_program
    ruby_bin = RbConfig::CONFIG["RUBY_INSTALL_NAME"]
    ruby_bindir = RbConfig::CONFIG["bindir"]

    candidates = ["gem"]
    candidates.unshift "gem#{$1}" if ruby_bin =~ /^ruby(.+)$/

    candidates.each do |gem_name|
        if File.file?(gem_full_path = File.join(ruby_bindir, gem_name))
            return gem_full_path
        end
    end
    raise ArgumentError, "cannot find a gem program "\
                         "(tried #{candidates.sort.join(', ')} in #{ruby_bindir})"
end

.has_autoproj_preamble?(script_lines) ⇒ Boolean

Returns:

  • (Boolean)


551
552
553
# File 'lib/autoproj/ops/install.rb', line 551

def self.has_autoproj_preamble?(script_lines)
    script_lines.any? { |l| l =~ /Autoproj generated preamble/ }
end

.in_workspace?(base_dir) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
125
126
127
128
129
130
131
# File 'lib/autoproj/ops/install.rb', line 121

def self.in_workspace?(base_dir)
    path = Pathname.new(base_dir)
    until path.root?
        if path.join(".autoproj").exist? || path.join("autoproj").exist?
            return true
        end

        path = path.parent
    end
    false
end

.new_style_bundler_binstub?(script_lines) ⇒ Boolean

Returns:

  • (Boolean)


547
548
549
# File 'lib/autoproj/ops/install.rb', line 547

def self.new_style_bundler_binstub?(script_lines)
    script_lines.any? { |l| l =~ /This file was generated by Bundler/ }
end

.rewrite_shims(shim_path, ruby_executable, root_dir, autoproj_gemfile_path, gems_gem_home) ⇒ Object



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'lib/autoproj/ops/install.rb', line 506

def self.rewrite_shims(shim_path, ruby_executable,
    root_dir, autoproj_gemfile_path, gems_gem_home)
    FileUtils.mkdir_p shim_path
    File.open(File.join(shim_path, "ruby"), "w") do |io|
        io.puts "#! /bin/sh"
        io.puts "exec #{ruby_executable} \"$@\""
    end
    FileUtils.chmod 0755, File.join(shim_path, "ruby")

    Dir.glob(File.join(shim_path, "*")) do |bin_script|
        next unless File.file?(bin_script)

        bin_name = File.basename(bin_script)
        if EXCLUDED_FROM_SHIMS.include?(bin_name)
            FileUtils.rm_f bin_script
            next
        end
        next if bin_name == "ruby"

        bin_shim = File.join(shim_path, bin_name)
        bin_script_lines = File.readlines(bin_script)
        next if has_autoproj_preamble?(bin_script_lines)
        next unless ruby_script?(bin_script_lines)

        File.open(bin_shim, "w") do |io|
            if bin_name == "bundler" || bin_name == "bundle"
                io.puts shim_bundler(bin_script_lines, ruby_executable,
                                     autoproj_gemfile_path, gems_gem_home)
            else
                io.puts shim_script(bin_script_lines, ruby_executable, root_dir,
                                    autoproj_gemfile_path, gems_gem_home)
            end
        end
        FileUtils.chmod 0755, bin_shim
    end
end

.ruby_script?(script_lines) ⇒ Boolean

Returns:

  • (Boolean)


543
544
545
# File 'lib/autoproj/ops/install.rb', line 543

def self.ruby_script?(script_lines)
    script_lines.first =~ /\#\s*!(.*ruby.*)/
end

.sanitize_env(value) ⇒ Object



116
117
118
119
# File 'lib/autoproj/ops/install.rb', line 116

def self.sanitize_env(value)
    value.split(File::PATH_SEPARATOR)
         .find_all { |p| !in_workspace?(p) }
end

.shim_bundler(script_lines, ruby_executable, autoproj_gemfile_path, gems_gem_home) ⇒ Object



555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/autoproj/ops/install.rb', line 555

def self.shim_bundler(script_lines, ruby_executable, autoproj_gemfile_path, gems_gem_home)
    return shim_bundler_old(ruby_executable, autoproj_gemfile_path, gems_gem_home) \
        unless new_style_bundler_binstub?(script_lines)

    script_lines.insert(1, <<-RESTART_BUNDLER)
#
# This file was generated by Bundler.
#

# Autoproj generated preamble
#{WITHOUT_BUNDLER}
ENV['BUNDLE_GEMFILE'] ||= '#{autoproj_gemfile_path}'
ENV['BUNDLE_LOCKFILE'] ||= '#{autoproj_gemfile_path}.lock'
ENV['GEM_HOME'] = '#{gems_gem_home}'
ENV.delete('GEM_PATH')
Gem.paths = Hash['GEM_HOME' => '#{gems_gem_home}', 'GEM_PATH' => '']
    RESTART_BUNDLER
    script_lines.join
end

.shim_bundler_old(ruby_executable, autoproj_gemfile_path, gems_gem_home) ⇒ Object



575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/autoproj/ops/install.rb', line 575

def self.shim_bundler_old(ruby_executable, autoproj_gemfile_path, gems_gem_home)
    "#! #{ruby_executable}

#{WITHOUT_BUNDLER}
ENV['BUNDLE_GEMFILE'] ||= '#{autoproj_gemfile_path}'
ENV['BUNDLE_LOCKFILE'] ||= '#{autoproj_gemfile_path}.lock'
ENV['GEM_HOME'] = '#{gems_gem_home}'
ENV.delete('GEM_PATH')
Gem.paths = Hash['GEM_HOME' => '#{gems_gem_home}', 'GEM_PATH' => '']

load Gem.bin_path('bundler', 'bundler')"
end

.shim_script(script_lines, ruby_executable, root_dir, autoproj_gemfile_path, gems_gem_home) ⇒ Object



588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'lib/autoproj/ops/install.rb', line 588

def self.shim_script(script_lines, ruby_executable, root_dir,
    autoproj_gemfile_path, gems_gem_home)
    new_style = !script_lines.empty? && script_lines.any? do |l|
        l =~ /This file was generated by Bundler/
    end
    load_line = script_lines.grep(/load Gem.bin_path/).first
    return shim_script_old(ruby_executable, root_dir,
                           autoproj_gemfile_path, gems_gem_home, load_line) \
        unless new_style

    script_lines.insert(1, <<-AUTOPROJ_PREAMBLE)
#
# This file was generated by Bundler.
#

# Autoproj generated preamble, v1
#{RUBYLIB_REINIT}
ENV['BUNDLE_GEMFILE'] = '#{autoproj_gemfile_path}'
ENV['BUNDLE_LOCKFILE'] = '#{autoproj_gemfile_path}.lock'
ENV['AUTOPROJ_CURRENT_ROOT'] = '#{root_dir}'
Gem.paths = Hash['GEM_HOME' => '#{gems_gem_home}', 'GEM_PATH' => '']
    AUTOPROJ_PREAMBLE
    script_lines.join
end

.shim_script_old(ruby_executable, root_dir, autoproj_gemfile_path, gems_gem_home, load_line) ⇒ Object



613
614
615
616
617
618
619
620
621
622
623
624
625
# File 'lib/autoproj/ops/install.rb', line 613

def self.shim_script_old(ruby_executable, root_dir, autoproj_gemfile_path,
    gems_gem_home, load_line)
    "#! #{ruby_executable}

#{RUBYLIB_REINIT}
ENV['BUNDLE_GEMFILE'] = '#{autoproj_gemfile_path}'
ENV['BUNDLE_LOCKFILE'] = '#{autoproj_gemfile_path}.lock'
ENV['AUTOPROJ_CURRENT_ROOT'] = '#{root_dir}'
require 'rubygems'
Gem.paths = Hash['GEM_HOME' => '#{gems_gem_home}', 'GEM_PATH' => '']
require 'bundler/setup'
#{load_line}"
end

Instance Method Details

#add_seed_config(path) ⇒ Object



273
274
275
# File 'lib/autoproj/ops/install.rb', line 273

def add_seed_config(path)
    @config.merge!(load_yaml(File.read(path)))
end

#apply_env(env) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/autoproj/ops/install.rb', line 106

def apply_env(env)
    env.each do |k, v|
        if v
            ENV[k] = v
        else
            ENV.delete(k)
        end
    end
end

#autoproj_config_pathString

The path to the autoproj configuration file

Returns:

  • (String)


150
151
152
# File 'lib/autoproj/ops/install.rb', line 150

def autoproj_config_path
    File.join(dot_autoproj, "config.yml")
end

#autoproj_gemfile_pathString

The path to the gemfile used to install autoproj

Returns:

  • (String)


143
144
145
# File 'lib/autoproj/ops/install.rb', line 143

def autoproj_gemfile_path
    File.join(dot_autoproj, "Gemfile")
end

#autoproj_pathObject



801
802
803
# File 'lib/autoproj/ops/install.rb', line 801

def autoproj_path
    File.join(dot_autoproj, "bin", "autoproj")
end

#bundler_versionObject



358
359
360
# File 'lib/autoproj/ops/install.rb', line 358

def bundler_version
    @config["bundler_version"]
end

#call_stage2Object



827
828
829
830
831
832
833
834
# File 'lib/autoproj/ops/install.rb', line 827

def call_stage2
    clean_env = env_for_child
    stage2_vars = clean_env.map { |k, v| "#{k}=#{v}" }
    puts "starting the newly installed autoproj for stage2 install"
    unless run_autoproj("install-stage2", root_dir, *stage2_vars, *@autoproj_options)
        raise "failed to execute autoproj install-stage2"
    end
end

#default_gemfile_contents(autoproj_version = ">= #{Autoproj::VERSION}") ⇒ String

The content of the default #gemfile

Parameters:

  • autoproj_version (String) (defaults to: ">= #{Autoproj::VERSION}")

    a constraint on the autoproj version that should be used

Returns:

  • (String)


259
260
261
262
263
# File 'lib/autoproj/ops/install.rb', line 259

def default_gemfile_contents(autoproj_version = ">= #{Autoproj::VERSION}")
    ["source \"#{gem_source}\"",
     "ruby \"#{RUBY_VERSION}\" if respond_to?(:ruby)",
     "gem \"autoproj\", \"#{autoproj_version}\""].join("\n")
end

#default_gems_install_pathObject

Get autoproj’s default path for installing gems



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/autoproj/ops/install.rb', line 210

def default_gems_install_path
    xdg_default_gem_path = xdg_var("XDG_DATA_HOME",
                                   File.join(Dir.home, ".local", "share", "autoproj", "gems"))
    default_gem_path = File.join(
        Dir.home, ".autoproj", "gems"
    )

    if File.directory?(xdg_default_gem_path)
        xdg_default_gem_path
    elsif File.directory?(default_gem_path)
        default_gem_path
    else
        xdg_default_gem_path
    end
end

#dot_autoprojString

The path to the .autoproj configuration directory

Returns:

  • (String)


136
137
138
# File 'lib/autoproj/ops/install.rb', line 136

def dot_autoproj
    File.join(root_dir, ".autoproj")
end

#env_for_child(env = self.env) ⇒ Object



99
100
101
102
103
104
# File 'lib/autoproj/ops/install.rb', line 99

def env_for_child(env = self.env)
    env.inject(Hash.new) do |h, (k, v)|
        h[k] = (v.join(File::PATH_SEPARATOR) if v && !v.empty?)
        h
    end
end

#find_bundler(gem_program, version: nil) ⇒ Object



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/autoproj/ops/install.rb', line 362

def find_bundler(gem_program, version: nil)
    bundler_path = File.join(gems_gem_home, "bin", "bundle")
    return unless File.exist?(bundler_path)

    setup_paths =
        if version
            find_versioned_bundler_setup(gem_program, version)
        else
            find_unversioned_bundler_setup(gem_program)
        end

    setup_paths.each do |setup_path|
        return bundler_path if setup_path.start_with?(gems_gem_home)
    end
    nil
end

#find_in_clean_path(command, *additional_paths) ⇒ Object



685
686
687
688
689
690
691
692
693
# File 'lib/autoproj/ops/install.rb', line 685

def find_in_clean_path(command, *additional_paths)
    clean_path = env_for_child["PATH"].split(File::PATH_SEPARATOR) +
                 additional_paths
    clean_path.each do |p|
        full_path = File.join(p, command)
        return full_path if File.file?(full_path)
    end
    nil
end

#find_unversioned_bundler_setup(gem_program) ⇒ Object



390
391
392
393
394
395
396
397
398
399
# File 'lib/autoproj/ops/install.rb', line 390

def find_unversioned_bundler_setup(gem_program)
    setup_paths = IO.popen(
        [env_for_child, Gem.ruby, gem_program,
         "which", "-a", "bundler/setup"],
        &:readlines
    )
    return [] unless $CHILD_STATUS.success?

    setup_paths
end

#find_versioned_bundler_setup(gem_program, version) ⇒ Object



379
380
381
382
383
384
385
386
387
388
# File 'lib/autoproj/ops/install.rb', line 379

def find_versioned_bundler_setup(gem_program, version)
    contents = IO.popen(
        [env_for_child, Gem.ruby, gem_program,
         "contents", "-v", version, "bundler"],
        &:readlines
    )
    return [] unless $CHILD_STATUS.success?

    contents.grep(%r{bundler/setup.rb$})
end

#gem_bindirObject

The path of the bin/ folder for installed gems



696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
# File 'lib/autoproj/ops/install.rb', line 696

def gem_bindir
    return @gem_bindir if @gem_bindir

    # Here, we're getting into the esotheric
    #
    # The problem is that e.g. Ubuntu and Debian install an
    # operating_system.rb file that sets proper OS defaults. Some
    # autoproj installs have it in their RUBYLIB but should not
    # because of limitations of autoproj 1.x. This leads to
    # Gem.bindir being *not* valid for subprocesses
    #
    # So, we're calling 'gem' as a subcommand to discovery the
    # actual bindir
    bindir = IO.popen(
        env_for_child,
        [Gem.ruby, "-e", 'puts "#{Gem.user_dir}/bin"'], # rubocop:disable Lint/InterpolationCheck
        &:read
    )
    if bindir
        @gem_bindir = bindir.chomp
    else
        raise "FATAL: cannot run #{Gem.ruby} -e 'puts Gem.bindir'"
    end
end

#gems_gem_homeString

The GEM_HOME under which the workspace’s gems should be installed

Returns:

  • (String)


192
193
194
# File 'lib/autoproj/ops/install.rb', line 192

def gems_gem_home
    File.join(gems_install_path, self.class.gems_path_suffix)
end

#install(bundler_version: self.bundler_version) ⇒ Object



721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
# File 'lib/autoproj/ops/install.rb', line 721

def install(bundler_version: self.bundler_version)
    if ENV["BUNDLER_GEMFILE"]
        raise "cannot run autoproj_install or autoproj_bootstrap while "\
              "under a 'bundler exec' subcommand or having loaded an "\
              "env.sh. Open a new console and try again"
    end

    gem_program = self.class.guess_gem_program
    puts "Detected 'gem' to be #{gem_program}"
    env["GEM_HOME"] = [gems_gem_home]
    env["GEM_PATH"] = [gems_gem_home]

    if (bundler = find_bundler(gem_program, version: bundler_version))
        puts "Detected bundler at #{bundler}"
    else
        puts "Installing bundler in #{gems_gem_home}"
        bundler = install_bundler(gem_program, version: bundler_version)
        exit(1) unless bundler
    end
    self.class.rewrite_shims(
        File.join(dot_autoproj, "bin"),
        ruby_executable,
        root_dir,
        autoproj_gemfile_path,
        gems_gem_home
    )
    env["PATH"].unshift File.join(dot_autoproj, "bin")
    save_gemfile

    puts "Installing autoproj in #{gems_gem_home}"
    install_autoproj(bundler, bundler_version: bundler_version)
end

#install_autoproj(bundler, bundler_version: self.bundler_version) ⇒ Object



454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/autoproj/ops/install.rb', line 454

def install_autoproj(bundler, bundler_version: self.bundler_version)
    # Force bundler to update. If the user does not want this, let
    # him specify a Gemfile with tighter version constraints
    lockfile = File.join(dot_autoproj, "Gemfile.lock")
    shims_path = File.join(dot_autoproj, "bin")
    FileUtils.rm lockfile if File.exist?(lockfile)

    run_bundler(bundler, "config", "set", "--local", "path", gems_install_path,
                bundler_version: bundler_version)
    run_bundler(bundler, "config", "set", "--local", "shebang", Gem.ruby,
                bundler_version: bundler_version)
    run_bundler(bundler, "config", "set", "--local", "bin", shims_path,
                bundler_version: bundler_version)

    install_args = ["--gemfile=#{autoproj_gemfile_path}"]
    install_args << "--local" if local?
    run_bundler(bundler, "install", *install_args,
                bundler_version: bundler_version)

    run_bundler(bundler, "binstubs", "--all", "--force",
                bundler_version: bundler_version)

    bundle_shim_path = File.join(shims_path, "bundle")
    write_bundle_shim(bundle_shim_path) unless File.exist?(bundle_shim_path)

    self.class.rewrite_shims(
        shims_path, ruby_executable, root_dir,
        autoproj_gemfile_path, gems_gem_home
    )
end

#install_bundler(gem_program, version: nil, silent: false) ⇒ Object



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
433
434
435
436
437
# File 'lib/autoproj/ops/install.rb', line 401

def install_bundler(gem_program, version: nil, silent: false)
    local = ["--local"] if local?

    redirection = Hash.new
    redirection = Hash[out: :close] if silent

    version_args = []
    version_args << "-v" << version if version

    # Shut up the bundler warning about 'bin' not being in PATH
    env = self.env
    env = env.merge(
        { "PATH" => env["PATH"] + [File.join(gems_gem_home, "bin")] }
    )
    result = system(
        env_for_child(env),
        Gem.ruby, gem_program, "install",
        "--env-shebang", "--no-document", "--no-format-executable",
        "--clear-sources", "--source", gem_source,
        "--no-user-install", "--install-dir", gems_gem_home,
        *local, "--bindir=#{File.join(gems_gem_home, 'bin')}",
        "bundler", *version_args, **redirection
    )

    unless result
        STDERR.puts "FATAL: failed to install bundler in #{gems_gem_home}"
        nil
    end

    if (bundler_path = find_bundler(gem_program, version: version))
        bundler_path
    else
        STDERR.puts "gem install bundler returned successfully, but still "\
                    "cannot find bundler in #{gems_gem_home}"
        nil
    end
end

#load_configObject



754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
# File 'lib/autoproj/ops/install.rb', line 754

def load_config
    v1_config_path = File.join(root_dir, "autoproj", "config.yml")

    config = Hash.new
    if File.file?(v1_config_path)
        config.merge!(YAML.load(File.read(v1_config_path)) || Hash.new)
    end
    if File.file?(autoproj_config_path)
        config.merge!(YAML.load(File.read(autoproj_config_path)) || Hash.new)
    end

    ruby = RbConfig::CONFIG["RUBY_INSTALL_NAME"]
    ruby_bindir = RbConfig::CONFIG["bindir"]
    ruby_executable = File.join(ruby_bindir, ruby)
    if (current = config["ruby_executable"]) # When upgrading or reinstalling
        if current != ruby_executable
            raise "this workspace has already been initialized using "\
                  "#{current}, you cannot run autoproj install with "\
                  "#{ruby_executable}. If you know what you're doing, "\
                  "delete the ruby_executable line in config.yml and try again"
        end
    else
        config["ruby_executable"] = ruby_executable
    end

    @config = config
    @config["bundler_version"] ||= self.class.default_bundler_version

    %w[gems_install_path prefer_indep_over_os_packages].each do |flag|
        instance_variable_set "@#{flag}", config.fetch(flag, false)
    end
end

#load_yaml(contents) ⇒ Object



265
266
267
268
269
270
271
# File 'lib/autoproj/ops/install.rb', line 265

def load_yaml(contents)
    if Gem::Version.new(Psych::VERSION) >= Gem::Version.new("3.1.0")
        YAML.safe_load(contents, permitted_classes: [Symbol])
    else
        YAML.safe_load(contents, [Symbol])
    end
end

#local?Boolean

Whether we can access the network while installing

Returns:

  • (Boolean)


162
163
164
# File 'lib/autoproj/ops/install.rb', line 162

def local?
    !!@local
end

#parse_options(args = ARGV) ⇒ Object

Parse the provided command line options and returns the non-options



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/autoproj/ops/install.rb', line 278

def parse_options(args = ARGV)
    options = OptionParser.new do |opt|
        opt.on "--local", "do not access the network (may fail)" do
            @local = true
        end
        opt.on "--skip-stage2", "do not run the stage2 install" do
            @skip_stage2 = true
        end
        opt.on "--debug", "Run in debug mode" do
            @autoproj_options << "--debug"
        end
        opt.on "--gem-source=URL", String, "use this source for RubyGems "\
                                           "instead of rubygems.org" do |url|
            @gem_source = url
        end
        opt.on "--gems-path=PATH", "install gems under this path instead "\
                                   "of #{default_gems_install_path} (do not use with --public-gems)" do |path|
            @gems_install_path = path
        end
        opt.on "--public-gems", "install gems in the default gem location: #{default_gems_install_path}"\
                                " (do not use with --gems-path)" do
            @gems_install_path = default_gems_install_path
        end
        opt.on "--bundler-version=VERSION_CONSTRAINT", String, "use the provided "\
                                                               "string as a version constraint for bundler" do |version|
            @config["bundler_version"] = version
        end
        opt.on "--version=VERSION_CONSTRAINT", String, "use the provided "\
                                                       "string as a version constraint for autoproj" do |version|
            raise "cannot give both --version and --gemfile" if @gemfile

            @gemfile = default_gemfile_contents(version)
        end
        opt.on "--gemfile=PATH", String, "use the given Gemfile to install "\
                                         "autoproj instead of the default" do |path|
            raise "cannot give both --version and --gemfile" if @gemfile

            @gemfile = File.read(path)
        end
        opt.on "--no-seed-config",
               "when reinstalling an existing autoproj workspace, do not "\
               "use the config in .autoproj/ as seed" do
            @config.clear
            @config["bundler_version"] = Install.default_bundler_version
        end
        opt.on "--seed-config=PATH", String, "path to a seed file that "\
                                             "should be used to initialize the configuration" do |path|
            add_seed_config(path)
        end
        opt.on "--prefer-os-independent-packages", "prefer OS-independent "\
                                                   "packages (such as a RubyGem) over their OS-packaged equivalent "\
                                                   "(e.g. the thor gem vs. the ruby-thor debian package)" do
            @prefer_indep_over_os_packages = true
        end
        opt.on "--[no-]color", "do not use colored output (enabled by "\
                               "default if the terminal supports it)" do |color|
            if color then @autoproj_options << "--color"
            else
                @autoproj_options << "--no-color"
            end
        end
        opt.on "--[no-]progress", "do not use progress output (enabled by "\
                                  "default if the terminal supports it)" do |progress|
            if progress then @autoproj_options << "--progress"
            else
                @autoproj_options << "--no-progress"
            end
        end
        opt.on "--[no-]interactive", "if non-interactive, use default "\
                                     "answer for questions" do |flag|
            if flag then @autoproj_options << "--interactive"
            else
                @autoproj_options << "--no-interactive"
            end
        end
    end
    args = options.parse(ARGV)
    @autoproj_options + args
end

#prefer_indep_over_os_packages=(flag) ⇒ Object



234
235
236
# File 'lib/autoproj/ops/install.rb', line 234

def prefer_indep_over_os_packages=(flag)
    @prefer_indep_over_os_packages = !!flag
end

#prefer_indep_over_os_packages?Boolean

Whether autoproj should prefer OS-independent packages over their OS-packaged equivalents (e.g. the thor gem vs. the ruby-thor Debian package)

Returns:

  • (Boolean)


229
230
231
# File 'lib/autoproj/ops/install.rb', line 229

def prefer_indep_over_os_packages?
    @prefer_indep_over_os_packages
end

#run_autoproj(*args) ⇒ Object



805
806
807
808
# File 'lib/autoproj/ops/install.rb', line 805

def run_autoproj(*args)
    system env_for_child.merge("BUNDLE_GEMFILE" => autoproj_gemfile_path),
           Gem.ruby, autoproj_path, *args, *@autoproj_options
end

#run_bundler(bundler, *args, bundler_version: self.bundler_version) ⇒ Object



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/autoproj/ops/install.rb', line 487

def run_bundler(bundler, *args, bundler_version: self.bundler_version)
    clean_env = env_for_child.dup

    version_arg = []
    version_arg << "_#{bundler_version}_" if bundler_version

    result = system(
        clean_env, Gem.ruby, bundler, *version_arg,
        *args, chdir: dot_autoproj
    )

    unless result
        raise BundlerFailed,
              "FAILED: bundler #{args.join(', ')} in #{dot_autoproj}"
    end
end

#save_configObject



795
796
797
798
799
# File 'lib/autoproj/ops/install.rb', line 795

def save_config
    config["gems_install_path"] = gems_install_path
    config["prefer_indep_over_os_packages"] = prefer_indep_over_os_packages?
    File.open(autoproj_config_path, "w") { |io| YAML.dump(config, io) }
end

#save_env_sh(*vars) ⇒ Object



627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
# File 'lib/autoproj/ops/install.rb', line 627

def save_env_sh(*vars)
    env = Autobuild::Environment.new
    env.prepare
    vars.each do |kv|
        k, *v = kv.split("=")
        v = v.join("=")

        if v.empty?
            env.unset k
        else
            env.set k, *v.split(File::PATH_SEPARATOR)
        end
    end
    # Generate environment files right now, we can at least use bundler
    File.open(File.join(dot_autoproj, "env.sh"), "w") do |io|
        env.export_env_sh(io)
    end

    # And now the root envsh
    env = Autobuild::Environment.new
    env.source_before File.join(dot_autoproj, "env.sh")
    env.set("AUTOPROJ_CURRENT_ROOT", root_dir)
    File.open(File.join(root_dir, "env.sh"), "w") do |io|
        env.export_env_sh(io)
    end
end

#save_gemfileObject



654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
# File 'lib/autoproj/ops/install.rb', line 654

def save_gemfile
    gemfile =
        if @gemfile
            @gemfile
        elsif File.file?(autoproj_gemfile_path)
            File.read(autoproj_gemfile_path)
        else
            default_gemfile_contents
        end

    gemfile += [
        "",
        "config_path = File.join(__dir__, 'config.yml')",
        "if File.file?(config_path)",
        "    require 'yaml'",
        "    config = YAML.load(File.read(config_path)) || Hash.new",
        "    (config['plugins'] || Hash.new).",
        "        each do |plugin_name, (version, options)|",
        "            gem plugin_name, version, **options",
        "        end",
        "end"
    ].join("\n")

    FileUtils.mkdir_p File.dirname(autoproj_gemfile_path)
    File.open(autoproj_gemfile_path, "w") do |io|
        io.write gemfile
    end
end

#skip_stage2?Boolean

Whether the stage2 install should be called or not

Returns:

  • (Boolean)


155
156
157
# File 'lib/autoproj/ops/install.rb', line 155

def skip_stage2?
    !!@skip_stage2
end

#stage1Object



815
816
817
818
819
820
821
822
823
824
825
# File 'lib/autoproj/ops/install.rb', line 815

def stage1
    if v1_workspace? && File.file?(v1_envsh = File.join(root_dir, "env.sh"))
        FileUtils.cp v1_envsh, "env.sh-autoproj-v1"
    end
    FileUtils.mkdir_p dot_autoproj
    save_config
    install
rescue Exception
    FileUtils.rm_rf dot_autoproj
    raise
end

#stage2(*vars) ⇒ Object



836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
# File 'lib/autoproj/ops/install.rb', line 836

def stage2(*vars)
    require "autobuild"
    puts "saving temporary env.sh and .autoproj/env.sh"
    save_env_sh(*vars)
    puts "running 'autoproj envsh' to generate a proper env.sh"
    unless system(Gem.ruby, autoproj_path, "envsh", *@autoproj_options)
        STDERR.puts "failed to run autoproj envsh on the newly installed "\
                    "autoproj (#{autoproj_path})"
        exit 1
    end
    # This is really needed on an existing install to install the
    # gems that were present in the v1 layout
    puts "running 'autoproj osdeps' to re-install missing gems"
    unless system(Gem.ruby, autoproj_path, "osdeps", *@autoproj_options)
        STDERR.puts "failed to run autoproj osdeps on the newly installed "\
                    "autoproj (#{autoproj_path})"
        exit 1
    end
end

#v1_workspace?Boolean

Returns:

  • (Boolean)


810
811
812
813
# File 'lib/autoproj/ops/install.rb', line 810

def v1_workspace?
    File.file?(File.join(root_dir, "autoproj", "config.yml")) &&
        !File.directory?(File.join(root_dir, ".autoproj"))
end

#write_bundle_shim(bundle_shim_path) ⇒ Object



439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/autoproj/ops/install.rb', line 439

def write_bundle_shim(bundle_shim_path)
    contents = <<~BUNDLERSHIM
        #!#{Gem.ruby}
        ENV['BUNDLE_GEMFILE'] ||= '#{autoproj_gemfile_path}'
        ENV['BUNDLE_LOCKFILE'] ||= '#{autoproj_gemfile_path}.lock'
        ENV['GEM_HOME'] = '#{gems_gem_home}'
        ENV.delete('GEM_PATH')
        Gem.paths = Hash['GEM_HOME' => '#{gems_gem_home}', 'GEM_PATH' => '']
        exec "#{File.join(gems_gem_home, 'bin', 'bundle')}", *ARGV
    BUNDLERSHIM

    File.write(bundle_shim_path, contents)
    FileUtils.chmod 0755, bundle_shim_path
end