class RBT::RegisterProgramFilesIntoGlobalDatabase

Constants

YAML_DATABASE_INSTALLED_FILES
#

YAML_DATABASE_INSTALLED_FILES

#

Public Class Methods

new( program = nil, run_already = true ) click to toggle source
#

initialize

#
# File lib/rbt/utility_scripts/register_program_files_into_global_database.rb, line 33
def initialize(
    program     = nil,
    run_already = true
  )
  set_program(program)
  run if run_already
end
show_registered_keys() click to toggle source
#

RBT::RegisterProgramFilesIntoGlobalDatabase.show_registered_keys

#
# File lib/rbt/utility_scripts/register_program_files_into_global_database.rb, line 161
def self.show_registered_keys
  new.show_registered_keys
end

Public Instance Methods

load_yaml_database() click to toggle source
#

load_yaml_database

#
# File lib/rbt/utility_scripts/register_program_files_into_global_database.rb, line 126
def load_yaml_database
  _ = "#{YAML_DATABASE_DIRECTORY}Database_InstalledFiles.yml"
  _ = YAML.load_file(_) if File.exist? _
  _
end
program?() click to toggle source
#

program?

#
# File lib/rbt/utility_scripts/register_program_files_into_global_database.rb, line 75
def program?
  @program
end
register( program = program? )
register_this_program( program = program? ) click to toggle source
#

register_this_program

This method registers a (single) program residing under the /Programs hierarchy into the yaml Database.

I recommend to give the absolute location as argument, but the short version should work as well.

#
# File lib/rbt/utility_scripts/register_program_files_into_global_database.rb, line 183
def register_this_program(
    program = program?
  )
  if !program.include?('/')
    program = programs_dir?+program.capitalize+'/Current'
  end
  program = File.absolute_path(program)
  _ = "#{program}/InstalledFiles"
  unless File.exist? _
    _ = "#{program}yaml/registered_files.yml"
  end
  if File.exist? _ # something like "/Programs/Gif2apng/Current/InstalledFiles"
    _ = read_file(_)
    save_installed_files_into_yaml_database(_)
  else # if we do not have it, use another file.
    if Dir[program+'/*'].size > 1 # Assume that more than one file here
                                  # means we installed something worthy.
      # save_installed_files_information(program)
      current = program
      if File.symlink? current
        opnwarn '___ '+program
        current = File.readlink(program)
        if ! current.include? '/' # assume that we have a "Current -> 0.1.7" structure, rather than a "Current -> /Programs/Foo/0.17" structure.
          current = program.split('/')[0..-2].join('/') +'/'+current
        end
      end
      current << '/' unless current.end_with? '/' # append trailing /
      _ = "#{current}InstalledFiles"
      remove_file(_) # just to make sure.
      opne sfancy('Creating the file ')+
           sfile(_)+
           simp(' now.')
      array = Dir["#{current}**/*"]
      # =================================================================== #
      # Select only files next.
      # =================================================================== #
      array.select! {|f| File.file? f }
      create_file(_)
      append_what_into(array.join("\n"), _)
    end
  end
end
Also aliased as: register
reset() click to toggle source
#

reset

#
Calls superclass method RBT::Base#reset
# File lib/rbt/utility_scripts/register_program_files_into_global_database.rb, line 44
def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # === @program
  # ======================================================================= #
  @program = 'htop' # ← Need a default program.
  # ======================================================================= #
  # === @debug
  # ======================================================================= #
  @debug = false
  # ======================================================================= #
  # === @run_simulation
  # ======================================================================= #
  @run_simulation = false
end
run() click to toggle source
#

run

#
# File lib/rbt/utility_scripts/register_program_files_into_global_database.rb, line 236
def run
  register_this_program
end
save_installed_files_into_yaml_database(data) click to toggle source
#

save_installed_files_into_yaml_database

Use this method to store info in our yaml Database. Note that we must first load the yaml file if it exists, before we can save new information.

Important:

The argument `data` must be an array listing which files will
be added to the yaml database.
#
# File lib/rbt/utility_scripts/register_program_files_into_global_database.rb, line 92
def save_installed_files_into_yaml_database(data)
  if @debug
    ewarn "DEBUG INFO FROM save_installed_files_into_yaml_database"
    cliner { pp data }
    pp "^^^^ check this data ..."
  end
  if data.empty?
    opnwarn 'For the program `'+simp(@program.to_s)+'` we found '\
            'an error: '+@program.to_s
    opnwarn '(Line '+__LINE__.to_s+') Sorry, dataset to the '\
            'method save_installed_files_into_yaml_database (data) '\
            'is empty.'
  end
  _ = YAML_DATABASE_INSTALLED_FILES
  if @run_simulation
    opne simp('We run in simulation mode, hence we will not save '\
         'the program into the yaml file ')+sfile('  '+_)+
         simp(' now')+'.'
  else
    hash = {}
    name = data.last
    name = name.split('/')[2,2].join('-').downcase if name # assume the name of the entry here.
    begin
      hash[name.strip] = data.map {|x| x.chomp} # we hate newlines, so get rid of them.
      save_into_yaml_file_after_permission_check(_, hash)
    rescue Exception => error
      opnn; pp error
    end
  end
end
save_into_yaml_file_after_permission_check(this_file, hash) click to toggle source
#

save_into_yaml_file_after_permission_check

#
# File lib/rbt/utility_scripts/register_program_files_into_global_database.rb, line 135
def save_into_yaml_file_after_permission_check(this_file, hash)
  if File.exist? this_file
    if File.writable? this_file
      opne simp('Saving into the yaml file ')
      opne sfile("  #{this_file}")
      opne simp('now.')
      # =================================================================== #
      # Load in the old stuff into our hash.
      # =================================================================== #
      if hash && File.exist?(this_file) # if it exists, load it.
        hash = hash.merge( load_yaml_database() ) if File.size(this_file) > 0
      end
      save_yaml(this_file, hash)
    else
      opnwarn 'You do not have sufficient permissions to modify '
      opne sfile("  #{this_file}")
    end
  else
    opne "The file `#{sfile(this_file)}` does not exist."
    opne 'Please create it.'
  end
end
save_yaml(into, what) click to toggle source
#

save_yaml

#
# File lib/rbt/utility_scripts/register_program_files_into_global_database.rb, line 229
def save_yaml(into, what)
  write_what_into(what, into)
end
set_program(i) click to toggle source
#

set_program

This method will set the @program variable.

#
# File lib/rbt/utility_scripts/register_program_files_into_global_database.rb, line 66
def set_program(i)
  i = return_pwd if i.nil?
  i = File.absolute_path(i)
  @program = i
end
show_registered_keys() click to toggle source
#

show_registered_keys

#
# File lib/rbt/utility_scripts/register_program_files_into_global_database.rb, line 168
def show_registered_keys
  keys = load_yaml_database.keys
  opne 'The registered keys in our database are:'
  opne  '  [ "'+keys.join('", "')+'" ]'
end