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
#¶ ↑
initialize¶ ↑
#¶ ↑
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 70 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 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
¶ ↑
#¶ ↑
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 383 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
#¶ ↑
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 197 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 = RBT::Cookbooks::SanitizeCookbook.new(_) { :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
¶ ↑
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 590 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?¶ ↑
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 434 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
#¶ ↑
do_keep_empty_directories
¶ ↑
#¶ ↑
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 249 def do_keep_empty_directories @remove_empty_directories = false end
#¶ ↑
do_not_register_into_the_local_yaml_database
¶ ↑
#¶ ↑
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 576 def do_not_register_into_the_local_yaml_database @do_register_into_the_local_yaml_database = false end
#¶ ↑
do_register_into_yaml_database
¶ ↑
#¶ ↑
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 329 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 RBT::RegisterProgramFilesIntoYamlDatabase.new(i) end end end
#¶ ↑
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 266 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? opne "Removing these #{slateblue('(empty)')} directories next:" remove_these_directories?.each {|entry| e " #{sdir(entry)}" } remove(remove_these_directories?) end end end
#¶ ↑
do_symlink
¶ ↑
Since as of January 2020 the method will also support the –force option, which will first delete the target at /usr/bin, before doing the symlink operation. Only use it if you are certain you wish to do so.
#¶ ↑
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 704 def do_symlink will_we_symlink = will_we_symlink? _ = @use_this_program return unless _ and !_.empty? if _ and File.directory?(_) inform_the_user_what_will_be_done # ===================================================================== # # Obtain all subdirectories next (the toplevel-directories). # ===================================================================== # directories = Dir["#{_}**"].select {|entry| File.directory?(entry) } unless directories.empty? opnn if use_opn? e "Symlinking from the directory #{sdir(_)} next." end directories.each {|name_of_the_subdirectory| # =================================================================== # # === Eliminate empty subdirectories next # # If the directory in question is empty, it will be eliminated. # # This is not ideal, though, as it may also remove new directories # created during the postinstallation step. Hence, since as of # December 2022, only certain empty directores will be removed. # =================================================================== # if Dir.empty?(name_of_the_subdirectory) basename = File.basename(name_of_the_subdirectory) if ARRAY_ALLOWED_SUBDIRECTORIES.include?(basename) register_for_removal(name_of_the_subdirectory) end else # ================================================================= # # === Process allowed subdirectories next # # Check whether we have an allowed subdirectory or not first. # This class will only handle allowed subdirectories past the # following point. # # The Array that is processed next may look as follows: # # ["/home/Programs/Pixman/0.42.2/Resources", # "/home/Programs/Pixman/0.42.2/include", # "/home/Programs/Pixman/0.42.2/lib", # "/home/Programs/Pixman/0.42.2/yaml"] # # ================================================================= # if @array_allowed_subdirectories.any? {|entry| name_of_the_subdirectory.end_with? entry } short_name_of_the_subdirectory = File.basename(name_of_the_subdirectory) @will_we_symlink = true # Reset our flag here. if run_simulation? opne 'We run in simulation mode, thus we will not ' opne "symlink #{sfile(entry)} into #{sfile(assumed_target_of(entry))}." else case short_name_of_the_subdirectory # (case tag) # =============================================================== # # === lib # # Symlink into the lib/ subdirectory here; typically into # /usr/lib/. # # This entry is special in that we also have to consider # the /usr/lib/pkgconfig/ subdirectory - otherwise the # relevant .pc files may not be symlinked. # =============================================================== # when /lib$/, /lib64$/ # lib64 could be controversial - hmmm. target = assumed_target_of(name_of_the_subdirectory) if be_verbose? opnn if use_opn?; e 'Processing '+sfancy(name_of_the_subdirectory)+ ' for symlinking into `'+sdir(target)+'` next:' end if will_we_symlink symlink_from_to(name_of_the_subdirectory, target) pkgconfig_subdirectory = name_of_the_subdirectory+'/pkgconfig/' if File.directory?(pkgconfig_subdirectory) and (all_pc_files = Dir[pkgconfig_subdirectory+'*.pc']) unless all_pc_files.empty? # ===================================================== # # Ok, we found at the least one .pc file. Thus we can # try to symlink these .pc files. # ===================================================== # target = '/usr/lib/pkgconfig/' all_pc_files.each {|this_pc_file| real_target = target+ File.basename(this_pc_file) symlink_this_file( this_pc_file, real_target ) } end end end # =============================================================== # # === include # # This is equivalent towards a target such as '/usr/include/'. # =============================================================== # when /include$/ target = include_dir? if @will_we_symlink opnn if use_opn?; e 'Symlinking into the main '+ sdir('include/')+' directory at '+sdir(target)+'.' symlink_from_to(name_of_the_subdirectory, target) { :include_directories } end # =============================================================== # # === bin # # This also includes sbin/ directories. # # This functionality was removed in September 2022. It is # now handled by a toplevel action, via RBT.action(). # =============================================================== # # when /bin$/ # target = assumed_target_of(name_of_the_subdirectory) # # ============================================================= # # # The full_target variable may be "/usr/bin/zip", for example. # # ============================================================= # # full_target = target+ # File.basename(name_of_the_subdirectory) # delete_file_or_do_not_delete_file = false # if full_target.include?('/usr/bin/') and # force_the_symlink_operation? # # ============================================================= # # # For now we will only do this on /usr/bin/ (13.01.2020) - # # at a later time this may be extended. # # ============================================================= # # delete_file_or_do_not_delete_file = true # end # if be_verbose? # opnn if use_opn?; e 'Processing '+sfancy(name_of_the_subdirectory)+ # ' for symlinking into `'+sdir(target)+'` next:' # end # symlink_from_to( # name_of_the_subdirectory, # target, # delete_file_or_do_not_delete_file # ) if @will_we_symlink # =============================================================== # # === share # =============================================================== # when /share$/, /shared$/, /Shared$/ target = assumed_target_of(name_of_the_subdirectory) if be_verbose? opnn if use_opn?; e 'Processing '+sfancy(name_of_the_subdirectory)+ ' for symlinking into `'+sdir(target)+'` next:' end symlink_from_to(name_of_the_subdirectory, target) if @will_we_symlink # =============================================================== # # === etc # =============================================================== # when /etc$/ target = assumed_target_of(name_of_the_subdirectory) if be_verbose? opnn if use_opn?; e 'Processing '+sfancy(name_of_the_subdirectory)+ ' for symlinking into `'+sdir(target)+'` next:' end symlink_from_to(name_of_the_subdirectory, target) if @will_we_symlink # =============================================================== # # === libexec # =============================================================== # when /libexec$/ target = assumed_target_of(name_of_the_subdirectory) if be_verbose? opnn if use_opn?; e 'Processing '+sfancy(name_of_the_subdirectory)+ ' for symlinking into `'+sdir(target)+'` next:' end symlink_from_to(name_of_the_subdirectory, target) if @will_we_symlink # =============================================================== # # === yaml # =============================================================== # when 'yaml' # Ignore this entry. @will_we_symlink = false # =============================================================== # # === man # # For now, we don't have a place for man pages. # =============================================================== # when 'man' @will_we_symlink = false else @will_we_symlink = false # unless (name_of_the_subdirectory == 'yaml') or # (name_of_the_subdirectory == 'include') # opne "=> #{sdir(name_of_the_subdirectory)}"\ # "#{swarn(' does not exist, we thus ignore it.')}" # end end end # else # opnn if use_opn?; e "Unhandled subdirectory: #{sdir(name_of_the_subdirectory)}" end end } end consider_removing_empty_directories # ← Should come before symlinking headers files # ======================================================================= # # Register into the yaml database, before symlinking pkgconfig files. # ======================================================================= # do_register_into_yaml_database if @do_register_into_the_local_yaml_database # ← Should go before symlinking the headers. consider_symlinking_headers # ← Should come before symlinking pkgconfig files # consider_symlinking_pkgconfig_files # ← Should come before saving the name of the program into a local file. keep_track_which_program_was_last_symlinked end
#¶ ↑
first_argument_or_current_working_directory
¶ ↑
#¶ ↑
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 563 def first_argument_or_current_working_directory _ = first? if (_ and _.start_with?('--')) or (_ and _.empty?) or _.nil? _ = return_pwd end return _ end
#¶ ↑
inform_the_user_what_will_be_done
(inform tag)¶ ↑
# ¶ ↑
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 626 def inform_the_user_what_will_be_done _ = "Working on the program #{sfancy(program_name?)}. ".dup if @array_allowed_subdirectories.size == 1 _ << "The subdirectory found at\n" else _ << "The subdirectories found at\n" end opnn if use_opn?; e _ _ = "`#{sfancy(use_this_program?)}` will be symlinked." opnn if use_opn?; e _ end
#¶ ↑
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 365 def keep_track_which_program_was_last_symlinked what = program_name? into = file_last_symlinked_program? write_what_into(what, into) end
#¶ ↑
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 547 def only_allow_bin_as_subdirectory opne "Only the subdirectory at "\ "#{sdir('bin/')} will be symlinked next." @array_allowed_subdirectories.clear @array_allowed_subdirectories << 'bin' # ======================================================================= # # Disable some variables too, in this event. # ======================================================================= # @shall_we_symlink_the_headers = false @do_register_into_the_local_yaml_database = false @symlink_pkgconfig_files = false end
#¶ ↑
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 289 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
¶ ↑
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 234 def register_for_removal(i) i = rds(i) @array_remove_these_directories << i unless (i == '/') end
#¶ ↑
reset (reset tag)¶ ↑
#¶ ↑
RBT::Base#reset
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 119 def reset super() infer_the_namespace # ======================================================================= # # === :be_verbose # ======================================================================= # set_be_verbose # ======================================================================= # # === :use_opn # ======================================================================= # do_use_opn # ======================================================================= # # === @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 # ======================================================================= # # === @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. # ======================================================================= # @remove_empty_directories = true # ======================================================================= # # === @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". # ======================================================================= # @force_symlink = false # ======================================================================= # # === @array_remove_these_directories # ======================================================================= # @array_remove_these_directories = [] # ======================================================================= # # === @shall_we_symlink_the_headers # ======================================================================= # @shall_we_symlink_the_headers = false # ======================================================================= # # === @do_register_into_the_local_yaml_database # ======================================================================= # @do_register_into_the_local_yaml_database = DO_REGISTER_INTO_THE_LOCAL_YAML_DATABASE set_use_this_program( first_argument_or_current_working_directory ) end
#¶ ↑
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 649 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
¶ ↑
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 306 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
#¶ ↑
symlink_this_program?¶ ↑
This method will return the NAME of the program at hand, in a downcased manner.
#¶ ↑
# File lib/rbt/utility_scripts/symlink_from_to_current/symlink_from_to_current.rb, line 409 def symlink_this_program? _ = @use_this_program.to_s.dup if _.include? _ _.sub!(/#{Regexp.quote(programs_dir?)}/,'') # May look like this now: "Vala/0.50.3/" if _.include? '/' _ = _.split('/').first end end _.downcase! return _ end