class RBT::ChangePrefix

Public Class Methods

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

RBT::ChangePrefix[]

#
# File lib/rbt/utility_scripts/change_prefix.rb, line 313
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 41
def initialize(
    commandline_arguments = nil,
    run_already           = true
  )
  reset
  set_commandline_arguments(
    commandline_arguments
  )
  run if run_already
end

Public Instance Methods

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 116
def modify_this_file?
  @modify_this_file
end
modify_which_file?()
Alias for: modify_this_file?
of_this_makefile()
Alias for: modify_this_file?
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 242
def perform_the_modification
  _ = main_file?
  if File.exist? _
    # ===================================================================== #
    # Read in the dataset into a variable next.
    # ===================================================================== #
    old_dataset = File.readlines(_)
    pattern = @search_for_this_pattern
    joined_dataset = old_dataset.join
    if joined_dataset =~ pattern # Must be a Regex.
      opne "A match was found within the file #{sfile(_)},"
      opne "using the pattern #{steelblue(pattern)}."
      new_file_content = ''.dup
      old_dataset.each {|line|
        if line =~ pattern
          # =============================================================== #
          # Here we have to check for two cases:
          #
          #   1) prefix is downcased
          #   2) PREFIX is upcased
          #
          # =============================================================== #
          if line.include? 'prefix '
            # ============================================================= #
            # Then use the downcased variant.
            # ============================================================= #
            line = 'prefix = '+@use_this_as_the_new_prefix
          elsif line.include? 'PREFIX='
            line = 'PREFIX = '+@use_this_as_the_new_prefix
          else
            # ============================================================= #
            # Else use the upcased variant.
            # ============================================================= #
            line = 'PREFIX = '+@use_this_as_the_new_prefix
          end
          opne "The new line will be: #{lightgreen(line)}"
        end
        new_file_content << "#{line.rstrip}\n"
      }
      opne "Storing into the file `#{sfile(_)}`."
      write_what_into(new_file_content, _)
    # ===================================================================== #
    # The next clause is specifically for hdparm.
    # ===================================================================== #
    elsif !joined_dataset.include?('PREFIX') and joined_dataset.include?('DESTDIR')
      joined_dataset.gsub!(
        /^DESTDIR =/,
        'DESTDIR = '+use_this_prefix?.to_s
      )
      opne "Storing into the file `#{sfile(_)}`."
      write_what_into(joined_dataset, _)
    else
      opne 'The pattern '+sfancy(pattern)+' could '\
           'not be found in the file '+sfile(_)+'.'
    end
  else
    opnn; no_file_exists_at(_)
  end
end
reset() click to toggle source
#

reset (reset tag)

#
Calls superclass method RBT::Base#reset
# File lib/rbt/utility_scripts/change_prefix.rb, line 55
def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # Designate which file is to be modified next:
  # ======================================================================= #
  set_modify_this_file(:default_file)
  # ======================================================================= #
  # === @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.
  #
  # See:
  #
  #   https://rubular.com/r/iAYqSQTaSllK5f
  #
  # ======================================================================= #
  @search_for_this_pattern = /^(PREFIX\s*=) ?(.+)/i
  # ======================================================================= #
  # 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 305
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 185
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 223
def set_modify_this_file(i = :default)
  case i
  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 102
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 125
def show_help
  e
  e 'Presently this class has no help option documented.'
  e
end
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 109
def use_this_as_the_new_prefix?
  @use_this_as_the_new_prefix
end
Also aliased as: use_this_prefix?
use_this_prefix?()