class RBT::VersionSwitcher

Public Class Methods

[](i = '') click to toggle source
#

RBT::VersionSwitcher[]

#
# File lib/rbt/utility_scripts/version_switcher.rb, line 306
def self.[](i = '')
  new(i)
end
new( commandline_arguments = ARGV, run_already = true ) { || ... } click to toggle source
#

initialize

#
# File lib/rbt/utility_scripts/version_switcher.rb, line 29
def initialize(
    commandline_arguments = ARGV,
    run_already  = true
  )
  reset
  set_commandline_arguments(
    commandline_arguments
  )
  if block_given?
    yielded = yield
    case yielded
    when :be_quiet
      be_quiet
    when :be_quiet_and_do_not_registered
      be_quiet
      @do_not_register = true
    end
  end
  run if run_already
end

Public Instance Methods

determine_the_available_program_versions() click to toggle source
#

determine_the_available_program_versions

#
# File lib/rbt/utility_scripts/version_switcher.rb, line 135
def determine_the_available_program_versions
  target = "#{programs_dir?}#{program_name?}/*"
  @array_available_program_versions = Dir[target].select {|entry|
    File.directory?(entry)
  }.select {|line|
    line =~ /\d+/ # Only obtain numbers.
  }
end
menu(i) click to toggle source
#

menu (menu tag)

#
path_to_current()
program_name?() click to toggle source
#

program_name?

#
# File lib/rbt/utility_scripts/version_switcher.rb, line 128
def program_name?
  @program_name
end
program_version?() click to toggle source
#

program_version?

#
# File lib/rbt/utility_scripts/version_switcher.rb, line 79
def program_version?
  @program_version
end
report_the_current_version_in_use() click to toggle source
#

report_the_current_version_in_use

This method will simply report the current program version in use.

#
# File lib/rbt/utility_scripts/version_switcher.rb, line 164
def report_the_current_version_in_use
  _ = return_current_program_version
  if _.nil?
    opne 'No current program version could be determined.'
  else
    opne 'The current program version for the program '+
          sfancy(program_name?.downcase)+' in use is: '+
          sfancy(_)
  end
end
reset() click to toggle source
#

reset (reset tag)

#
Calls superclass method RBT::Base#reset
# File lib/rbt/utility_scripts/version_switcher.rb, line 53
def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # === @do_not_register
  #
  # By default, register into the yaml database.
  # ======================================================================= #
  @do_not_register = false
  # ======================================================================= #
  # === @array_available_program_versions
  #
  # The following Array will keep track of the possible python versions.
  # ======================================================================= #
  @array_available_program_versions = []
  # ======================================================================= #
  # === @program_version
  #
  # If the user wants a program version then this variable can be of help.
  # ======================================================================= #
  @program_version = nil
end
return_current_program_version() click to toggle source
#

return_current_program_version

#
# File lib/rbt/utility_scripts/version_switcher.rb, line 185
def return_current_program_version
  target = return_path_to_current
  if File.exist? target
    readlink = File.readlink(target)
    return return_program_version_of(readlink)
  else
    nil
  end
end
return_path_to_current() click to toggle source
#

return_path_to_current

#
# File lib/rbt/utility_scripts/version_switcher.rb, line 178
def return_path_to_current
  "#{programs_dir?}#{program_name?}/Current"
end
Also aliased as: path_to_current
return_program_version_of( i = File.readlink(path_to_current) ) click to toggle source
#

return_program_version_of

#
# File lib/rbt/utility_scripts/version_switcher.rb, line 198
def return_program_version_of(
    i = File.readlink(path_to_current)
  )
  i = File.basename(i) if i.include? '/'
  return i
end
run() click to toggle source
#

run (run tag)

#
# File lib/rbt/utility_scripts/version_switcher.rb, line 295
def run
  # ======================================================================= #
  # First, obtain all available program versions.
  # ======================================================================= #
  determine_the_available_program_versions
  try_to_report_which_program_versions_appear_to_be_available
end
set_commandline_arguments(i) click to toggle source
#

set_commandline_arguments

#
# File lib/rbt/utility_scripts/version_switcher.rb, line 86
def set_commandline_arguments(i)
  i = [i].flatten.compact # <- We need an Array.
  @internal_hash[:commandline_arguments] = i
  # ======================================================================= #
  # We will assume that the first argument given ^^^ is the
  # name of the program at hand.
  # ======================================================================= #
  program_name = @internal_hash[:commandline_arguments].first
  set_program_name(program_name)
  if @internal_hash[:commandline_arguments].size > 1
    set_program_version(@internal_hash[:commandline_arguments][1])
  end
end
set_program_name( i = return_pwd ) click to toggle source
#

set_program_name

#
# File lib/rbt/utility_scripts/version_switcher.rb, line 110
def set_program_name(
    i = return_pwd
  )
  i = i.join(' ').strip if i.is_a? Array
  case i
  when :default, nil
    i = return_pwd
  end
  i = i.to_s.dup
  i = File.basename(i) if i.include? '/'
  i.capitalize!
  @program_name = i
  menu(i)
end
set_program_version(i) click to toggle source
#

set_program_version

#
# File lib/rbt/utility_scripts/version_switcher.rb, line 103
def set_program_version(i)
  @program_version = i
end
show_help() click to toggle source
#

show_help (help tag)

Invocation example:

version_switcher --help
#
# File lib/rbt/utility_scripts/version_switcher.rb, line 152
def show_help
  opne 'These options are available for class RBT::VersionSwitcher:'
  e
  e '  --info # Show some information about what this class is doing.'
  e
end
to_current( the_other_version ) click to toggle source
#

to_current

#
# File lib/rbt/utility_scripts/version_switcher.rb, line 233
def to_current(
    the_other_version
  )
  opne "Now switching to the version `#{sfile(the_other_version)}`."
  if @do_not_register
    RBT::ToCurrent.new(the_other_version) { :do_not_register }
  else
    RBT::ToCurrent.new(the_other_version)
  end
end
try_to_pick_that_particular_program_version(i) click to toggle source
#

try_to_pick_that_particular_program_version

#
# File lib/rbt/utility_scripts/version_switcher.rb, line 247
def try_to_pick_that_particular_program_version(i)
  i = File.basename(i)
  target = "#{programs_directory?}#{program_name?}/#{i}/"
  to_current(target)
end
try_to_report_which_program_versions_appear_to_be_available() click to toggle source
#

try_to_report_which_program_versions_appear_to_be_available

#
# File lib/rbt/utility_scripts/version_switcher.rb, line 256
def try_to_report_which_program_versions_appear_to_be_available
  # ======================================================================= #
  # Now we have all available program versions. We must first present to
  # the user which versions are available.
  # ======================================================================= #
  _ = @array_available_program_versions
  unless _.empty?
    opne 'These program versions appear to be available:'
    e
    sorted = _.sort_by {|entry| entry.to_s } # Better to keep it sorted.
    sorted.each {|entry|
      version = File.basename(entry).to_s
      efancy " #{version}"
    }; e
    report_the_current_version_in_use
    if program_version?
      # =================================================================== #
      # In this case, try to pick that particular program version.
      # =================================================================== #
      try_to_pick_that_particular_program_version(program_version?)
      return
    end
    # ===================================================================== #
    # Next, switch to the other version; this works only if we have
    # exactly two elements.
    # ===================================================================== #
    if _.size == 2
      the_other_version = _.reject {|line|
        line.include? return_current_program_version
      }
      the_other_version = the_other_version.first if the_other_version.is_a? Array
      to_current(the_other_version)
    end
  end
end