class RBT::SymlinkThisProgram
Constants
- ARRAY_ALLOWED_SUBDIRECTORIES
#¶ ↑
ARRAY_ALLOWED_SUBDIRECTORIES
¶ ↑Which subdirectories are allowed for this class. bin/, sbin/ and lib/ are the three main directories.
#¶ ↑
- DO_REGISTER_INTO_THE_LOCAL_YAML_DATABASE
#¶ ↑
DO_REGISTER_INTO_THE_LOCAL_YAML_DATABASE
¶ ↑#¶ ↑
- NAMESPACE
#¶ ↑
NAMESPACE
¶ ↑#¶ ↑
Public Class Methods
#¶ ↑
initialize¶ ↑
#¶ ↑
# File lib/rbt/symlink_this_program/initialize.rb, line 42 def initialize( commandline_arguments = ARGV, run_already = true ) reset set_commandline_arguments( commandline_arguments ) case run_already when :do_not_run_yet, :dont_run_yet run_already = false end if block_given? yielded = yield 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 run if run_already end
Public Instance Methods
#¶ ↑
consider_symlinking_headers
¶ ↑
This must be conditionally - some programs will specify that we symlink headers whereas others will not.
#¶ ↑
# File lib/rbt/symlink_this_program/misc.rb, line 22 def consider_symlinking_headers _ = program_name? require 'rbt/cookbooks/class/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::Cookbook.new(_) { :bypass_menu_check } shall_we_symlink_the_headers = @dataset.shall_we_symlink_the_headers? 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.
#¶ ↑
# File lib/rbt/symlink_this_program/symlink.rb, line 19 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 opnn; e 'A minor problem - appdir_target does not respond to the method .split.' opnn; e 'We will still continue, though.' end if @dataset.symlink_pkgconfig_files? and appdir_target.include?(programs_dir?) require 'rbt/utility_scripts/symlink_pkgconfig_files.rb' opnn; e "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/symlink_this_program/misc.rb, line 102 def current_target? _ = programs_directory?.to_s.dup if name_of_the_program? if do_capitalize? _ << name_of_the_program?.capitalize else _ << name_of_the_program? end else #opnn; e swarn('No name of the program is known.') end _ << '/Current/' _ = rds(_) return _ end
#¶ ↑
determine_which_program_to_symlink
¶ ↑
This method will determine which program to symlink; and also which version we will symlink.
Invocation example:
rnsymc php 7.3.7
#¶ ↑
# File lib/rbt/symlink_this_program/misc.rb, line 353 def determine_which_program_to_symlink first = @commandline_arguments.first second = @commandline_arguments[1] # Tap into the second element. if first.nil? first = return_pwd end # ======================================================================= # # Try to set the full path to the target program next. We need to # accomodate different ways how the user provided input into this # class. # ======================================================================= # if File.exist? first set_full_path_to_the_target_program(first) else if second # In this case, the program version has been provided. set_full_path_to_the_target_program( programs_dir?+first.capitalize+'/'+second+'/' ) elsif first # =================================================================== # # Ok - in this case we know that the user supplied a name, but not # a version; and that this program name does NOT include a full # path (yet). So we will try to find such a path. # =================================================================== # _ = programs_dir?+first.to_s.capitalize+'/Current/' if File.exist? _ set_full_path_to_the_target_program(_) end end end if first.include? '/' use_this_regex = /^#{Regexp.quote(programs_dir?)}/ _ = first.sub(use_this_regex,'') _.chop! if _.end_with? '/' # ===================================================================== # # We assume that the input at this point may look like this: # 'Ruby/2.6.3/' # ===================================================================== # second = ProgramInformation.return_version(_.tr('/','-')) first = ProgramInformation.return_name(_.tr('/','-')) end set_program_version(second) if second # Only do so if provided. set_symlink_this_program(first) end
#¶ ↑
do_keep_empty_directories
¶ ↑
#¶ ↑
# File lib/rbt/symlink_this_program/misc.rb, line 189 def do_keep_empty_directories @remove_empty_directories = false end
#¶ ↑
do_not_register_into_the_local_yaml_database
¶ ↑
#¶ ↑
# File lib/rbt/symlink_this_program/misc.rb, line 121 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/symlink_this_program/misc.rb, line 421 def do_register_into_yaml_database( i = full_path? ) if i unless i.include? '/' i = static_appdir_prefix_of?(i, program_version?) end if RBT.register_program_files_into_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/symlink_this_program/misc.rb, line 168 def do_remove_empty_directories if run_simulation? opnn; e 'As we are running in simulation mode, we will not remove ' opnn; e 'any of the following directories:' e pp remove_these_directories? e else # Removing the directories next. unless remove_these_directories?.empty? opnn; e "Removing these #{slateblue('(empty)')} directories next:" remove_these_directories?.each {|entry| e " #{sdir(entry)}" } remove(remove_these_directories?) end end end
#¶ ↑
do_symlink_all_entries
¶ ↑
#¶ ↑
# File lib/rbt/symlink_this_program/misc.rb, line 238 def do_symlink_all_entries if @full_path_to_the_target_program and File.exist?(@full_path_to_the_target_program) _ = @full_path_to_the_target_program inform_the_user_what_will_be_done # ===================================================================== # # Obtain the toplevel-directories next: # ===================================================================== # toplevel_directories = Dir[_+'**'].select {|entry| File.directory?(entry) } # ===================================================================== # # Iterate through them: # ===================================================================== # toplevel_directories.each {|entry| # =================================================================== # # If the directory in question is empty, we eliminate it. # =================================================================== # if Dir.empty?(entry) register_for_removal(entry) else entries = Dir[entry+'/**'] # Always obtain the entries. name_of_the_subdirectory = File.basename(entry) # ================================================================= # # We will only handle allowed subdirectories past the following # point. # ================================================================= # next unless ARRAY_ALLOWED_SUBDIRECTORIES.include? name_of_the_subdirectory @will_we_symlink = true # Reset our flag here. case name_of_the_subdirectory # (case tag) # ================================================================= # # === pkgconfig # ================================================================= # when 'pkgconfig' target = RBT.pkgconfig_directory? # ================================================================= # # === include # # This is equivalent towards targets such as '/usr/include'. # ================================================================= # when 'include' target = sysinclude_directory? # ================================================================= # # === lib # # Symlink into lib here. # ================================================================= # when 'lib', 'lib64' # lib64 could be controversial - hmmm. target = syslib_directory? # ================================================================= # # === bin # ================================================================= # when 'bin', 'sbin' target = sysbin_directory? # ================================================================= # # === share # ================================================================= # when 'share', 'shared', 'Shared' target = sysshare_directory? # ================================================================= # # === etc # ================================================================= # when 'etc' target = sysetc_directory? # ================================================================= # # === 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' opnn; e "=> #{sdir(entry)}"\ "#{swarn(' does not exist, we thus ignore it.')}" end end if run_simulation? opnn; e 'We run in simulation mode, thus we will not ' opnn; e "symlink #{sfile(entry)} into #{sfile(target)}." else if @will_we_symlink if be_verbose? opnn; e 'Processing '+sfancy(name_of_the_subdirectory)+ ' from '+royalblue(entry)+' next:' end symlink_these_entries(entries, to: target) unless entries.empty? end end end } end end
#¶ ↑
ensure_that_the_current_symlink_exists
¶ ↑
This method will ensure that there is a symlink called “Current”.
#¶ ↑
# File lib/rbt/symlink_this_program/misc.rb, line 60 def ensure_that_the_current_symlink_exists # ======================================================================= # # If we will work on the /Programs hierarchy, then enter the following # subsection. # ======================================================================= # if current_target?.include? programs_dir? dirname = rds(File.dirname(current_target?)+'/') current_symlink = dirname+'Current' if File.exist?(current_symlink) and File.symlink?(current_symlink) and !input?.include?(File.readlink(current_symlink)) opnn; e 'Deleting the symlink found at '+sfancy(current_symlink)+' next.' # =================================================================== # # Ok, must delete the old symlink next then. # =================================================================== # remove_file(current_symlink) end unless File.exist?(current_symlink) and File.symlink?(current_symlink) target_program_version = return_program_version_of_this_program_at(dirname) # If the input-directory exists, we can use it instead. if full_path? if File.directory?(full_path?) target_program_version = full_path? end opnn; e 'Now symlinking from '+sdir(target_program_version)+ ' to '+ssym(current_symlink)+'.' do_symlink(target_program_version, current_symlink) end end end end
#¶ ↑
full_path_to_the_target_program?¶ ↑
#¶ ↑
# File lib/rbt/symlink_this_program/misc.rb, line 219 def full_path_to_the_target_program? @full_path_to_the_target_program end
#¶ ↑
inform_the_user_what_will_be_done
(inform tag)¶ ↑
# ¶ ↑
# File lib/rbt/symlink_this_program/misc.rb, line 227 def inform_the_user_what_will_be_done opnn; e "Working on #{sfancy(program_name?)}. The "\ "subdirectories found at" opnn; e "`"\ "#{sfancy(@full_path_to_the_target_program)}` "\ "will be symlinked." 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/symlink_this_program/misc.rb, line 412 def keep_track_which_program_was_last_symlinked what = program_name? into = file_last_symlinked_program? write_what_into(what, into) end
#¶ ↑
register_for_removal
¶ ↑
This method will register a directory for removal. Store the absolute path here.
The check whether the directory is empty or not is done in another part of this class.
#¶ ↑
# File lib/rbt/symlink_this_program/misc.rb, line 151 def register_for_removal(i) @array_remove_these_directories << i end
#¶ ↑
remove_and_store_commandline_arguments_from
¶ ↑
Keep in mind that the input may be either an Array or a String.
Presently we will support only Array as input here - Strings are ignored.
#¶ ↑
# File lib/rbt/symlink_this_program/misc.rb, line 132 def remove_and_store_commandline_arguments_from(i) if i.is_a? Array if i.any? {|entry| entry.include? '--' } menu(i.select {|entry| entry.include? '--' }) i.reject! {|entry| entry.include? '--'} end end return i end
#¶ ↑
reset (reset tag)¶ ↑
#¶ ↑
RBT::Base#reset
# File lib/rbt/symlink_this_program/reset.rb, line 14 def reset super() @namespace = NAMESPACE @be_verbose = true @use_opn = true @program_version = nil # ======================================================================= # # The next variable determines if we will actually symlink or whether # we will not. # ======================================================================= # @will_we_symlink = true # ======================================================================= # # The next variable will store the NAME of the program at hand, in # a downcased manner. We must initialize it to nil here first, though. # ======================================================================= # @symlink_this_program = nil @full_path_to_the_target_program = nil # ======================================================================= # # 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 @array_remove_these_directories = [] @shall_we_symlink_the_headers = false @do_register_into_the_local_yaml_database = DO_REGISTER_INTO_THE_LOCAL_YAML_DATABASE end
#¶ ↑
return_program_version_of_this_program_at
¶ ↑
This method will attempt to determine the version of a given program to which the “Current” symlink should point at.
#¶ ↑
# File lib/rbt/symlink_this_program/misc.rb, line 49 def return_program_version_of_this_program_at(i) directories = Dir["#{i}*"].select {|entry| File.directory? entry } result = directories.sort.first return result end
#¶ ↑
run_everything
¶ ↑
#¶ ↑
# File lib/rbt/symlink_this_program/run.rb, line 21 def run_everything if @will_we_symlink determine_which_program_to_symlink # ===================================================================== # # We must next ensure that the -> Current symlink exists. # ===================================================================== # ensure_that_the_current_symlink_exists do_symlink_all_entries consider_removing_empty_directories # ===================================================================== # # Register into the yaml database, before symlinking pkgconfig # files. # ===================================================================== # do_register_into_yaml_database if @do_register_into_the_local_yaml_database 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 end
#¶ ↑
set_full_path_to_the_target_program
¶ ↑
#¶ ↑
# File lib/rbt/symlink_this_program/setters.rb, line 53 def set_full_path_to_the_target_program( i = :default ) case i when :default i = static_appdir_prefix_of?(name_of_the_program?, program_version?) else unless i.include? '/' i = static_appdir_prefix_of?(i, program_version?) end end @full_path_to_the_target_program = i end
#¶ ↑
set_program_version
¶ ↑
#¶ ↑
# File lib/rbt/symlink_this_program/setters.rb, line 46 def set_program_version(i) @program_version = i end
#¶ ↑
set_symlink_this_program
¶ ↑
#¶ ↑
# File lib/rbt/symlink_this_program/setters.rb, line 16 def set_symlink_this_program( i = :default ) case i when :default i = return_pwd else i = i.dup if i.frozen? i.downcase! end if i i = i.first if i.is_a? Array if i.is_a? String i = rds(i) # Get rid of double slashes, just in case. end @symlink_this_program = i end
#¶ ↑
symlink_these_entries
¶ ↑
The second argument can be a Hash or a String - at the least this is what we expect. The second argument, if it is a String, will then be assumed to be the target onto which we will symlink entries onto.
#¶ ↑
# File lib/rbt/symlink_this_program/symlink.rb, line 50 def symlink_these_entries( entries, hash = {} ) if hash.is_a? Hash target = hash.delete(:to) if hash.has_key? :to end entries.each {|entry| symlink( entry, target+File.basename(entry) ) } end