class RBT::ChangePrefix

Constants

REGEX_FOR_UPPCASE_PREFIX
#

REGEX_FOR_UPPCASE_PREFIX

#

Public Class Methods

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

RBT::ChangePrefix[]

#
# File lib/rbt/utility_scripts/change_prefix.rb, line 382
def self.[](i = '')
  new(i)
end
new( commandline_arguments = nil, run_already = true ) click to toggle source
#

initialize

#
# File lib/rbt/utility_scripts/change_prefix.rb, line 46
def initialize(
    commandline_arguments = nil,
    run_already           = true
  )
  reset
  set_commandline_arguments(
    commandline_arguments
  )
  run if run_already
end

Public Instance Methods

contine_with_this_regex_on_that_dataset( use_this_regex, dataset_as_string ) click to toggle source
#

contine_with_this_regex_on_that_dataset

#
# File lib/rbt/utility_scripts/change_prefix.rb, line 289
def contine_with_this_regex_on_that_dataset(
    use_this_regex,
    dataset_as_string
  )
  main_file = main_file?
  if dataset_as_string =~ use_this_regex
    # ===================================================================== #
    # First inform the user that we have, indeed, found a match.
    # ===================================================================== #
    notify_the_user_that_a_match_was_found_in_this_file_using_that_pattern(
      main_file, use_this_regex
    )
    result = ''.dup
    dataset_as_string.split("\n").each {|line|
      if line =~ use_this_regex
        use_that_for_the_inner_regex = /(prefix\s*\??=\s*)(.+)/ # See: https://rubular.com/r/SKxjzFXhjMctH4
        if line.start_with?('prefix ') and !@we_have_already_modified_the_makefile
          # ============================================================= #
          # Then use the downcased variant.
          # ============================================================= #
          line =~ use_that_for_the_inner_regex
          line = $1.to_s.dup+@use_this_as_the_new_prefix.to_s
          @we_have_already_modified_the_makefile = true
        elsif line.start_with?('PREFIX') and !@we_have_already_modified_the_makefile
          line =~ use_that_for_the_inner_regex
          line = $1.to_s.dup+@use_this_as_the_new_prefix.to_s
          @we_have_already_modified_the_makefile = true
        else
          # ============================================================= #
          # Else do nothing for now.
          # ============================================================= #
        end
        unless @we_have_already_modified_the_makefile
          opne "The new line will be: #{lightgreen(line)}"
        end
      end
      result << "#{line.rstrip}\n"
    }
    opne "Storing into the file `#{sfile(main_file)}`."
    write_what_into(result, main_file)
  else
    opne 'The pattern '+sfile(use_this_regex.to_s)+' was not found.'
  end
end
do_work_on_this_file(i) click to toggle source
#

do_work_on_this_file

#
# File lib/rbt/utility_scripts/change_prefix.rb, line 337
def do_work_on_this_file(i)
  if i and File.exist?(i)
    dataset = default_readlines(i)
    # ===================================================================== #
    # Next we must determine which regex-pattern to use.
    #
    # In order to determine this, we run a simple .include?() check
    # first.
    # ===================================================================== #
    dataset_as_string = dataset.join
    if dataset_as_string.include?('prefix ')
      use_this_regex = regex1
      contine_with_this_regex_on_that_dataset(use_this_regex, dataset_as_string)
    elsif dataset_as_string.include?('PREFIX ')
      use_this_regex = regex2
      contine_with_this_regex_on_that_dataset(use_this_regex, dataset_as_string)
    else
      opne 'Neither the pattern for '+sfancy('prefix')+' nor for the pattern'
      opne sfancy('PREFIX')+' could be found in the file at `'+sfile(i)+'`.'
    end
  else
    opnn; no_file_exists_at(i)
  end
end
lowercase_prefix_pattern?() click to toggle source
#

lowercase_prefix_pattern? (regex1 tag)

This method will return the main regex, as-is, but with PREFIX being downcased to prefix.

#
# File lib/rbt/utility_scripts/change_prefix.rb, line 274
def lowercase_prefix_pattern?
  return /^(prefix\s*\??=) ?(.+)/i
end
Also aliased as: pattern?, regex1
main_file?()
Alias for: modify_this_file?
menu( i = commandline_arguments? ) click to toggle source
#

menu (menu tag)

#
modify_this_file?() click to toggle source
#

modify_this_file?

#
# File lib/rbt/utility_scripts/change_prefix.rb, line 133
def modify_this_file?
  @modify_this_file
end
modify_which_file?()
Alias for: modify_this_file?
notify_the_user_that_a_match_was_found_in_this_file_using_that_pattern( _, pattern ) click to toggle source
#

notify_the_user_that_a_match_was_found_in_this_file_using_that_pattern

#
# File lib/rbt/utility_scripts/change_prefix.rb, line 260
def notify_the_user_that_a_match_was_found_in_this_file_using_that_pattern(
    _,
    pattern
  )
  opne "A match was found within the file #{sfile(_)},"
  opne "using the pattern #{steelblue(pattern)}."
end
of_this_makefile()
Alias for: modify_this_file?
pattern?()
perform_the_modification() click to toggle source
#

perform_the_modification

This is the method that will be used to modify the Makefile at hand.

#
# File lib/rbt/utility_scripts/change_prefix.rb, line 367
def perform_the_modification
  do_work_on_this_file(main_file?)
end
regex1()
regex2()
reset() click to toggle source
#

reset (reset tag)

#
Calls superclass method RBT::LeanPrototype#reset
# File lib/rbt/utility_scripts/change_prefix.rb, line 60
def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # Designate which file is to be modified next:
  # ======================================================================= #
  set_modify_this_file(:default_file)
  # ======================================================================= #
  # === @we_have_already_modified_the_makefile
  # ======================================================================= #
  @we_have_already_modified_the_makefile = false
  # ======================================================================= #
  # === @search_for_this_pattern
  #
  # Designate which pattern we are trying to find in that file.
  #
  # Unfortunately there does not seem to be a general consensus in
  # what is allowed and what is not in a given Makefile. For example,
  # the program called 'mxk-1.10' makes use of exactly this line
  # "prefix  = /usr/local", whereas the program called 'libcli-1.9.8-4'
  # makes use of exactly this line "PREFIX = /usr/local". Thus, we
  # will have to check for these variants, which is why we will
  # ask rubular for help here.
  #
  # In June 2023 I discovered a Makefile that contains this dual-prefix:
  #
  #   prefix      ?= /usr/local
  #   PREFIX      ?= $(prefix)
  #
  # So it seems as if we also have to match against the "?" part there.
  #
  # See:
  #
  #   https://rubular.com/r/jUCLVDCCVRqZb4
  #
  # ======================================================================= #
  @search_for_this_pattern =
    REGEX_FOR_UPPCASE_PREFIX
  # ======================================================================= #
  # Infer the name and program version from return_pwd, via
  # ProgramInformation.
  # ======================================================================= #
  program_information = ProgramInformation.new(return_pwd)
  version = program_information.version?
  name    = program_information.program_name?
  @use_this_as_the_new_prefix = 
    rds(
      programs_dir?+
      name.capitalize+'/'+
      version+'/'
    )
end
run() click to toggle source
#

run (run tag)

#
# File lib/rbt/utility_scripts/change_prefix.rb, line 374
def run
  menu
  perform_the_modification
end
set_commandline_arguments(i = '') click to toggle source
#

set_input

#
# File lib/rbt/utility_scripts/change_prefix.rb, line 202
def set_commandline_arguments(i = '')
  if i.is_a? Hash
    # ===================================================================== #
    # If a Hash is given then we will modify this Hash a bit before
    # continuing here.
    # ===================================================================== #
    if i.has_key? :use_this_makefile
      set_modify_this_file(
        i.delete(:use_this_makefile)
      )
    end
    # ===================================================================== #
    # Query for another prefix that is to be used next:
    # ===================================================================== #
    if i.has_key? :use_this_as_the_new_prefix
      new_value = i.delete(:use_this_as_the_new_prefix)
      set_use_this_as_the_new_prefix(new_value)
    end
  end
  i = [i].flatten.compact
  if i.empty?
    # ===================================================================== #
    # In that case we will attempt to use a Makefile in the current
    # directory.
    # ===================================================================== #
    i << 'Makefile'
  end
  @internal_hash[:commandline_arguments] = i
end
set_main_file(i = :default)
set_modify_this_file(i = :default) click to toggle source
#

set_modify_this_file

This method can be used to designate where the target “Makefile” is exactly.

Do note that set_main_file() is an alias to this method here.

#
# File lib/rbt/utility_scripts/change_prefix.rb, line 240
def set_modify_this_file(i = :default)
  case i
  # ======================================================================= #
  # === :default
  # ======================================================================= #
  when :default,
       :default_file
    i = '/Depot/jjj/libcli-1.9.8-4/Makefile' 
  end
  i = i.dup if i.frozen?
  unless i.include? '/'
    i = File.absolute_path(i)
  end
  i = rds(i)
  @modify_this_file = i
end
Also aliased as: set_main_file
set_use_this_as_the_new_prefix(i) click to toggle source
#

set_use_this_as_the_new_prefix

This method can be used to designate another prefix that is to be used - the one that will be written into the “Makefile”.

#
# File lib/rbt/utility_scripts/change_prefix.rb, line 119
def set_use_this_as_the_new_prefix(i)
  @use_this_as_the_new_prefix = i
end
show_help() click to toggle source
#

show_help (help tag)

#
# File lib/rbt/utility_scripts/change_prefix.rb, line 142
def show_help
  e
  e 'Presently this class has no help option documented.'
  e
end
uppercase_prefix_pattern?() click to toggle source
#

uppercase_prefix_pattern? (regex2 tag)

#
# File lib/rbt/utility_scripts/change_prefix.rb, line 282
def uppercase_prefix_pattern?
  REGEX_FOR_UPPCASE_PREFIX
end
Also aliased as: regex2
use_this_as_the_new_prefix?() click to toggle source
#

use_this_as_the_new_prefix?

#
# File lib/rbt/utility_scripts/change_prefix.rb, line 126
def use_this_as_the_new_prefix?
  @use_this_as_the_new_prefix
end
Also aliased as: use_this_prefix?
use_this_prefix?()