class RBT::SymlinkFromToCurrent

Constants

ARRAY_ALLOWED_SUBDIRECTORIES
#

RBT::SymlinkFromToCurrent::ARRAY_ALLOWED_SUBDIRECTORIES

Which subdirectories are allowed for this class. bin/, sbin/ and lib/ are the three main directories.

The trailing ‘/’ can be omitted.

#
DO_REGISTER_INTO_THE_LOCAL_YAML_DATABASE
#

DO_REGISTER_INTO_THE_LOCAL_YAML_DATABASE

#

Public Class Methods

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

RBT::SymlinkFromToCurrent[]

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

initialize

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 72
def initialize(
    commandline_arguments = nil,
    run_already           = true
  )
  reset
  set_commandline_arguments(
    commandline_arguments
  )
  case run_already
  # ======================================================================= #
  # === :do_not_run_yet
  # ======================================================================= #
  when :do_not_run_yet,
       :dont_run_yet
    run_already = false
  end
  # ======================================================================= #
  # === Handle blocks given next
  # ======================================================================= #
  if block_given?
    yielded = yield
    # ===================================================================== #
    # === Handle Hashes next
    # ===================================================================== #
    if yielded.is_a? Hash
      # =================================================================== #
      # === :use_opn
      # =================================================================== #
      if yielded.has_key? :use_opn
        disable_opn if (yielded[:use_opn] == false)
      end
    else
      case yielded
      # =================================================================== #
      # === :do_not_run_yet
      # =================================================================== #
      when :do_not_run_yet
        run_already = false
      # =================================================================== #
      # === :do_not_register
      # =================================================================== #
      when :do_not_register
        do_not_register_into_the_local_yaml_database
      end
    end
  end
  run if run_already
end

Public Instance Methods

assumed_target_of(i) click to toggle source
#

assumed_target_of

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 392
def assumed_target_of(i)
  target = i.dup
  case i
  when /bin$/
    target = usr_bin?
  when /share$/,
       /shared$/,
       /Shared$/
    target = share_dir?
  when /lib$/,
       /lib64$/ # lib64 could be controversial - hmmm.
    target = lib_dir?
  when /etc$/
    target = etc_dir?
  when /libexec$/
    target = '/usr/libexec/'
  end
  return target
end
capitalized(i) click to toggle source
#

capitalized

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 383
def capitalized(i)
  i = i.dup
  i.capitalize! if do_capitalize?
  return i
end
consider_removing_empty_directories() click to toggle source
#

consider_removing_empty_directories

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 189
def consider_removing_empty_directories
  do_remove_empty_directories if remove_empty_directories?
end
consider_symlinking_headers() click to toggle source
#

consider_symlinking_headers

This must be conditionally - some programs will specify that we symlink headers whereas others will not.

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 199
def consider_symlinking_headers
  _ = program_name?
  require 'rbt/requires/require_the_cookbook_class.rb'
  # ======================================================================= #
  # Next, load up the yaml file.
  # ======================================================================= #
  if shall_we_symlink_the_headers?
    shall_we_symlink_the_headers = shall_we_symlink_the_headers?
  else
    @dataset = action(:SanitizeCookbook, _) { :fast }
    unless shall_we_symlink_the_headers? == false
      # =================================================================== #
      # The following check is a bit odd - I did not add a comment as
      # to why I added the next line, but the check above was now
      # added at 02.01.2020, to skip a nonsensical situation.
      # =================================================================== #
      shall_we_symlink_the_headers = @dataset.shall_we_symlink_the_headers?
    end
  end
  if shall_we_symlink_the_headers
    # ===================================================================== #
    # Ok, in this case we delegate to class SymlinkHeaders.
    # ===================================================================== #
    require 'rbt/utility_scripts/symlink_headers.rb'
    RBT::SymlinkHeaders.new(_)
  end
end
consider_symlinking_pkgconfig_files( appdir_target = full_path_to_the_target_program? ) click to toggle source
#

consider_symlinking_pkgconfig_files

This method can be used to symlink .pc files into the /usr/lib/pkgconfig hierarchy.

Since as of February 2023 this method is no longer in use, due to a bug that took too long to understand. We may have to rewrite this class eventually.

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 613
def consider_symlinking_pkgconfig_files(
    appdir_target = full_path_to_the_target_program?
  )
  # ======================================================================= #
  # The appdir_target variable may be incorrect, such as when we symlink
  # another version.
  # ======================================================================= #
  if appdir_target.respond_to? :split
    splitted = appdir_target.split('/')
    unless splitted.last == full_path_to_the_target_program?.split('/').last
      appdir_target = full_path_to_the_target_program?
    end
  else
    opne 'A minor problem - '+steelblue('appdir_target')+' does '\
         'not respond to the method .split.'
    opne 'We will still continue, though.'
  end
  if @symlink_pkgconfig_files and
     appdir_target.include?(programs_dir?)
    require 'rbt/utility_scripts/symlink_pkgconfig_files.rb'
    opne "Next, we will symlink all .pc files found "\
         "under `#{sdir(appdir_target)}`."
    SymlinkPkgconfigFiles.new(appdir_target)
  end
end
current_target?() click to toggle source
#

current_target?

This will yield a String as a result, such as ‘/Programs/Htop/Current/’.

These constitute our the symlink-targets.

We must retain a trailing ‘/’ here.

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 443
def current_target?
  _  = programs_directory?.to_s.dup
  if name_of_the_program?
    _ << capitalized(name_of_the_program?)
  else
    # opnwarn('No name of the program is known.')
  end
  _ << '/Current/' unless _.include? 'Current'
  _ = rds(_)
  return _
end
Also aliased as: target?
do_keep_empty_directories() click to toggle source
#

do_keep_empty_directories

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 258
def do_keep_empty_directories
  @internal_hash[:remove_empty_directories] = false
end
Also aliased as: keep_empty_directories
do_not_register()
do_not_register_into_the_local_yaml_database() click to toggle source
#

do_not_register_into_the_local_yaml_database

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 592
def do_not_register_into_the_local_yaml_database
  @internal_hash[:do_register_into_the_local_yaml_database] = false
end
Also aliased as: do_not_register
do_register_into_the_local_yaml_database?() click to toggle source
#

do_register_into_the_local_yaml_database?

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 599
def do_register_into_the_local_yaml_database?
  @internal_hash[:do_register_into_the_local_yaml_database]
end
do_register_into_yaml_database( i = full_path?, register_program_files_into_the_yaml_database = RBT.register_program_files_into_yaml_database? ) click to toggle source
#

do_register_into_yaml_database

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 338
def do_register_into_yaml_database(
    i                                             = full_path?,
    register_program_files_into_the_yaml_database = RBT.register_program_files_into_yaml_database?
  )
  if i
    unless i.include? '/'
      i = static_appdir_prefix_of?(i, program_version?)
    end
    if register_program_files_into_the_yaml_database 
      action(:RegisterProgramFilesIntoYamlDatabase, i)
    end
  end
end
do_remove_empty_directories() click to toggle source
#

do_remove_empty_directories

This method will remove directories that are empty. For this, we will make of the method remove() which resides in shared.rb.

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 275
def do_remove_empty_directories
  if run_simulation?
    opne 'As we are running in simulation mode, we will not remove '
    opne 'any of the following directories:'
    e
    pp remove_these_directories?
    e
  else # Removing the directories next.
    unless remove_these_directories?.empty?
      orev "Removing these #{slateblue('(empty)')} #{rev}directories next:"
      remove_these_directories?.each {|entry|
        e "  #{sdir(entry)}"
      }
      remove(remove_these_directories?)
    end
  end
end
first_argument_or_current_working_directory() click to toggle source
#

first_argument_or_current_working_directory

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 579
def first_argument_or_current_working_directory
  _ = first?
  if (_ and _.start_with?('--')) or
     (_ and _.empty?) or
     _.nil?
    _ = return_pwd
  end
  return _
end
full_path?()
Alias for: use_this_program?
full_path_to_the_target_program?()
Alias for: use_this_program?
inform_the_user_what_will_be_done( program_name = program_name? ) click to toggle source
#

inform_the_user_what_will_be_done (inform tag)

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 707
def inform_the_user_what_will_be_done(
    program_name = program_name?
  )
  _ = "Working on the program #{sfancy(program_name)}#{rev}. ".dup
  if @array_allowed_subdirectories.size == 1
    _ << "#{rev}The subdirectory found at\n"
  else
    _ << "#{rev}The subdirectories found at\n" 
  end
  opnn if use_opn?; e _
  _ = "#{rev}`#{sfancy(use_this_program?)}#{rev}` will be symlinked."
  opnn if use_opn?; e _
end
input?()
is_it_a_number?(i) click to toggle source
#

is_it_a_number?

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 355
def is_it_a_number?(i)
  i = i.to_s.dup.delete('.') # Ignore '.' tokens.
  return i =~ /^\d+$/
end
keep_empty_directories()
keep_empty_directories?() click to toggle source
#

keep_empty_directories?

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 265
def keep_empty_directories?
  !remove_empty_directories?
end
keep_track_which_program_was_last_symlinked() click to toggle source
#

keep_track_which_program_was_last_symlinked

This method will save the name of the program that this class has been working on, into a local file.

The reasoning behind this behaviour is so that we can use this in other classes, such as when we use :last on the commandline. The class that will act on :last will then pick up the content of the file that we have been using to store this information into. This then allows us to always refer to the “last program that was symlink_program-ed”, which is quite convenient as we don’t have to remember the name.

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 374
def keep_track_which_program_was_last_symlinked
  what = program_name?
  into = file_last_symlinked_program?
  write_what_into(what, into)
end
menu( i = hyphen_commandline_arguments? ) click to toggle source
#

menu (menu tag)

All commandline options for this class can be passed through the menu() method here.

#
name_of_the_program?()
notify_the_user_processing_this_directory_for_symlinking_into_that_target( name_of_the_subdirectory, target ) click to toggle source
#

notify_the_user_processing_this_directory_for_symlinking_into_that_target

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 724
def notify_the_user_processing_this_directory_for_symlinking_into_that_target(
    name_of_the_subdirectory,
    target
  )
  e "#{rev}Processing #{sfancy(name_of_the_subdirectory)}"\
     "#{rev} for symlinking into `#{sdir(target)}"\
     "#{rev}` next:"
end
only_allow_bin_as_subdirectory() click to toggle source
#

only_allow_bin_as_subdirectory

This method will enable that we only symlink the bin/ subdirectory.

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 556
def only_allow_bin_as_subdirectory
  orev "Only the subdirectory at "\
       "#{sdir('bin/')} #{rev}will be symlinked next."
  @array_allowed_subdirectories.clear
  @array_allowed_subdirectories << 'bin'
  # ======================================================================= #
  # Disable some variables too, in this event.
  # ======================================================================= #
  @internal_hash[:shall_we_symlink_the_headers] = false
  @internal_hash[:do_register_into_the_local_yaml_database] = false
  @symlink_pkgconfig_files = false
end
path?()
Alias for: use_this_program?
prefix?()
Alias for: use_this_program?
program?()
program_name?()
program_version?() click to toggle source
#

program_version?

This method should return the program-version of the program at hand.

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 298
def program_version?
  _ = program_name?.to_s.dup
  i.chop! if _.end_with? '/'
  if _.include? '/'
    i = i.split('/').last.to_s
  end
  return _
end
register_for_removal(i) click to toggle source
#

register_for_removal

This method will register a directory for removal. Store the absolute path here - this simplifies removing said directory..

The check whether the directory is empty or not is done in another part of this class.

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 236
def register_for_removal(i)
  i = rds(i)
  @internal_hash[:array_remove_these_directories] << i unless (i == '/')
end
remove_empty_directories?() click to toggle source
#

remove_empty_directories?

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 251
def remove_empty_directories?
  @internal_hash[:remove_empty_directories]
end
remove_these_directories?() click to toggle source
#

remove_these_directories?

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 244
def remove_these_directories?
  @internal_hash[:array_remove_these_directories]
end
reset() click to toggle source
#

reset (reset tag)

#
Calls superclass method RBT::CompileBase#reset
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 124
def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # === :be_verbose
  # ======================================================================= #
  set_be_verbose
  # ======================================================================= #
  # === :use_opn
  # ======================================================================= #
  do_use_opn
  # ======================================================================= #
  # === :force_symlink
  #
  # If this variable is true then we will remove entries at /usr/bin
  # before doing the symlink-operation. Use this only if you want to
  # do so, via the commandline flag --force.
  #
  # By default this is false, aka "disabled".
  # ======================================================================= #
  @internal_hash[:force_symlink] = false
  # ======================================================================= #
  # === :remove_empty_directories
  #
  # The next instance variable determines whether we will remove empty
  # directories or not. By default we will do so, but this behaviour
  # has to be modifiable as not every user may wish to have this,
  # hence why this instance variable exists.
  # ======================================================================= #
  @internal_hash[:remove_empty_directories] = true
  # ======================================================================= #
  # === :shall_we_symlink_the_headers
  # ======================================================================= #
  @internal_hash[:shall_we_symlink_the_headers] = false
  # ======================================================================= #
  # === :array_remove_these_directories
  # ======================================================================= #
  @internal_hash[:array_remove_these_directories] = []
  # ======================================================================= #
  # === :do_register_into_the_local_yaml_database
  # ======================================================================= #
  @internal_hash[:do_register_into_the_local_yaml_database] =
    DO_REGISTER_INTO_THE_LOCAL_YAML_DATABASE
  # ======================================================================= #
  # === @array_allowed_subdirectories
  # ======================================================================= #
  @array_allowed_subdirectories = ARRAY_ALLOWED_SUBDIRECTORIES
  # ======================================================================= #
  # === @will_we_symlink
  #
  # The next variable determines if we will actually symlink or whether
  # we will not.
  # ======================================================================= #
  @will_we_symlink = true
  # ======================================================================= #
  # === @symlink_pkgconfig_files
  #
  # Determine whether the .pc files will be symlinked as well or not.
  # ======================================================================= #
  @symlink_pkgconfig_files = true
end
run() click to toggle source
#

run (run tag)

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 948
def run
  set_use_this_program(
    first_argument_or_current_working_directory
  )
  menu
  do_symlink if will_we_symlink?
end
set_full_path( i = nil )
set_full_path_to_the_target_program( i = nil )
set_name_of_the_program( i = nil )
set_prefix( i = nil )
set_use_this_program( i = nil ) click to toggle source
#

set_use_this_program

Keep in mind that this method has to be flexible; user input may look like this:

rnsymc php 7.3.7

We could also enable input such as ‘Ruby/2.6.3/’ but right now this class is not handling such input.

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 657
def set_use_this_program(
    i = nil
  )
  if i
    # ===================================================================== #
    # First, handle Arrays.
    # ===================================================================== #
    i = i.first if i.is_a? Array
    # ===================================================================== #
    # === Handle Hashes next
    #
    # If a Hash is given to this method then this class will try to
    # assume that it contains a key called :to.
    # ===================================================================== #
    if i.is_a? Hash
      i = i.delete(:to) if i.has_key? :to
    end
    commandline_arguments = commandline_arguments?
    # ===================================================================== #
    # Check for the second argument being a number or not:
    # ===================================================================== #
    if commandline_arguments and is_it_a_number?(commandline_arguments[1])
      i = "#{appdir_prefix?}#{capitalized(i)}/#{commandline_arguments[1]}/" 
    end
    case i
    when :default
      i = static_appdir_prefix_of?(name_of_the_program?, program_version?)
    else
      unless i.include? '/'
        i = appdir_prefix?+capitalized(i)+'/Current/'
      end
    end
    unless i.end_with? '/'
      i << '/'
    end
  end
  if i.is_a? String
    i = rds(i) # Get rid of double slashes, just in case.
  end
  @use_this_program = i
end
show_help() click to toggle source
#

show_help

To invoke this method from the commandline, try:

rnsymc --help
#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 315
def show_help
  e
  e 'The following options are available:'
  e
  eparse '  --symlink-headers # the .h header files will be '\
         'symlinked as well'
  eparse '  --bin             # symlink ONLY the bin/ subdirectory'
  eparse '  --force           # delete the target at '+
         sdir('/usr/bin/')+' before doing the symlink-operation'
  e
end
target?()
Alias for: current_target?
target_directory?()
Alias for: use_this_program?
try_to_save_the_name_of_the_program_into_a_local_file()
use_this_program?() click to toggle source
#

use_this_program?

#
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 490
def use_this_program?
  @use_this_program
end