Module: BBLib::System

Defined in:
lib/bblib/system/system.rb

Constant Summary collapse

SPECIAL_PROGRAMS =
['pry', 'irb.cmd', 'irb'].freeze

Class Method Summary collapse

Class Method Details

.cmd_line(*args, include_args: true, include_ruby: true, prefix: nil, suffix: nil) ⇒ Object

A string representation of the command line that evoked this ruby instance (platform agnostic)



6
7
8
9
10
11
12
# File 'lib/bblib/system/system.rb', line 6

def self.cmd_line(*args, include_args: true, include_ruby: true, prefix: nil, suffix: nil)
  args = ARGV if args.empty?
  include_ruby = false if special_program?
  "#{prefix}#{include_ruby ? Command.quote(Gem.ruby) : nil} #{Command.quote($PROGRAM_NAME)}" \
  " #{include_args ? args.map { |a| Command.quote(a) }.join(' ') : nil}#{suffix}"
    .strip
end

.reload(include_args: true) ⇒ Object

EXPERIMENTAL: Reloads the original file that was called Use at your own risk, this could cause some weird issues



16
17
18
19
# File 'lib/bblib/system/system.rb', line 16

def self.reload(include_args: true)
  return false if special_program?
  load cmd_line(*args, include_ruby: false, include_args: include_args)
end

.restart(*args, include_args: true, stay_alive: 1) ⇒ Object

EXPERIMENTAL: Restart the ruby process that is currently running. Use at your own risk



23
24
25
26
27
28
29
30
31
# File 'lib/bblib/system/system.rb', line 23

def self.restart(*args, include_args: true, stay_alive: 1)
  exit(0)
rescue SystemExit
  opts = BBLib::OS.windows? ? { new_pgroup: true } : { pgroup: true }
  pid = spawn(cmd_line(*args, include_args: include_args, prefix: (BBLib::OS.windows? ? 'start ' : nil)), **opts)
  Process.detach(pid)
  sleep(stay_alive)
  exit(0) if special_program?
end

.special_program?Boolean

Detects whether a console like irb or pry was used to launch this process

Returns:

  • (Boolean)


34
35
36
# File 'lib/bblib/system/system.rb', line 34

def self.special_program?
  SPECIAL_PROGRAMS.include?($PROGRAM_NAME)
end