class GenerateRcFile

#

require 'generate_rc_file/constants.rb'

#
#

require 'generate_rc_file/version/version.rb'

#

Constants

ARRAY_ALL_FILES
#

ARRAY_ALL_FILES

#
ARRAY_WHICH_FILES_TO_ALIAS
#

ARRAY_WHICH_FILES_TO_ALIAS

#
ARRAY_WHICH_FILES_TO_EXPORT
#

ARRAY_WHICH_FILES_TO_EXPORT

#
BASE_DIR
#

BASE_DIR

Where to store. The base directory. We need to keep in mind that on some systems this directory does not exist but also can not be created, due to lack of permissions, for instance.

#
HEADER1
#

HEADER1

#
HEADER2
ISO_ENCODING
#

ISO_ENCODING

#
LINUX_YAML
N
RCFILE_GENERATOR_YAML_DIRECTORY
#

RCFILE_GENERATOR_YAML_DIRECTORY

Hardcoded path for now.

#
VERSION
#

GenerateRcFile::VERSION

#

Attributes

use_putty_style_aliases[RW]

Public Class Methods

new( i = ARGV, run_already = true ) click to toggle source
#

initialize

#
# File lib/generate_rc_file/generate_rc_file.rb, line 27
def initialize(
    i           = ARGV,
    run_already = true
  )
  reset
  set_commandline(i)
  parse_commandline
  run if run_already
end

Public Instance Methods

add_header_for(this_file) click to toggle source
#

add_header_for

Use this method if you want to return the default header.

#
# File lib/generate_rc_file/generate_rc_file.rb, line 140
def add_header_for(this_file) # Add the default header here.
  HEADER1+this_file+'_rc'+N+HEADER2
end
base_dir?()
Alias for: base_directory?
base_directory?() click to toggle source
#

base_directory?

#
# File lib/generate_rc_file/generate_rc_file.rb, line 40
def base_directory?
  @base_dir
end
Also aliased as: base_dir?
do_generate_the_rc_file(use_this_input = @commandline) click to toggle source
#

do_generate_the_rc_file

#
# File lib/generate_rc_file/generate_rc_file.rb, line 160
def do_generate_the_rc_file(use_this_input = @commandline)
  if use_this_input.is_a? Array
    if use_this_input.empty?
      use_this_input << ARRAY_ALL_FILES # Add all possible rc files here.
    else
      case use_this_input
      when 'all',:all
        use_this_input << ARRAY_ALL_FILES
      when 'cd',:cd
        use_this_input << 'cd_aliases'
      end
    end
  end
  use_this_input.flatten.each {|generate_this_file|
    generate_this_file = generate_this_file.to_s # We need a String.
    full_path_to_the_file = LINUX_YAML+generate_this_file+'.yml'
    # ===================================================================== #
    # Next, add some exceptions:
    # ===================================================================== #
    if    full_path_to_the_file.include? 'aliases.yml'
      full_path_to_the_file = RCFILE_GENERATOR_YAML_DIRECTORY+'aliases.yml'
    elsif full_path_to_the_file.include? 'cd_aliases.yml'
      full_path_to_the_file = RCFILE_GENERATOR_YAML_DIRECTORY+'cd_aliases.yml'
    elsif full_path_to_the_file.include? 'programs_aliases.yml'
      full_path_to_the_file = RCFILE_GENERATOR_YAML_DIRECTORY+'programs_aliases.yml'
    end
    hash_dataset = load_yaml(full_path_to_the_file)
    _ = ''.dup
    _ << add_header_for(generate_this_file) # Add the default header here.
    # ===================================================================== #
    # We have to find out whether we will use alias-style or export-style
    # next. For alias-style, we also may have to use putty-style or
    # normal-style.
    # ===================================================================== #
    if ARRAY_WHICH_FILES_TO_ALIAS.include? generate_this_file
      # =================================================================== #
      # Ok, alias-style comes here.
      # =================================================================== #
      hash_dataset.each_pair {|key, value|
        key   = ensure_main_encoding(key.to_s)
        value = ensure_main_encoding(value.to_s).strip
        if @use_putty_style_aliases
          _ << "alias "+key.to_s+'="'+value+'"'+N
        else
          if value.include? "'" # Need to use another syntax for included ' characters.
            _ << "alias '#{key}'=\"#{value}\"#{N}"
          else
            _ << "alias '#{key}'='#{value}'#{N}"
          end
        end
      }
    else
      # =================================================================== #
      # Ok, in this case we assume export-style.
      # =================================================================== #
      hash_dataset.each_pair {|key, value|
        value = value.to_s
        _ << "export "+key.to_s+'="'+value+'"'+N
      }
    end
    set_store_here(@base_dir+generate_this_file+'_rc')
    store_what_where(_, @store_here)
  }
end
enable_putty_style() click to toggle source
#

enable_putty_style

#
# File lib/generate_rc_file/generate_rc_file.rb, line 55
def enable_putty_style
  @use_putty_style_aliases = true
end
ensure_main_encoding( i, use_this_encoding = ISO_ENCODING ) click to toggle source
#

ensure_main_encoding

Ensure a uniform encoding here.

#
# File lib/generate_rc_file/generate_rc_file.rb, line 64
def ensure_main_encoding(
    i,
    use_this_encoding = ISO_ENCODING
  )
  if i.is_a?(String)
    i = i.dup if i.frozen?
    i = i.force_encoding(use_this_encoding)
  end
  i
end
load_yaml(i) click to toggle source
#

load_yaml

#
# File lib/generate_rc_file/generate_rc_file.rb, line 147
def load_yaml(i) # A simple, failsafe yaml-wrapper.
  begin
    YAML.load_file(i)
  rescue Exception => error
    opn; e 'We encountered an error in GenerateRCFile, in '\
           'the method load_yaml():'
    pp error
  end
end
menu(i = @commandline)
Alias for: parse_commandline
parse_commandline(i = @commandline) click to toggle source
#

parse_commandline

#
# File lib/generate_rc_file/generate_rc_file.rb, line 91
def parse_commandline(i = @commandline)
  @commandline.each {|entry|
    case entry # case tag
    # ===================================================================== #
    # === gcfile --dir=~/AUTOGENERATED
    # ===================================================================== #
    when /-?-?dir=(.+)/ # See: http://rubular.com/r/kF3uKcOoL9
      set_base_dir($1.to_s.dup)
      try_to_create_the_base_directory_if_it_does_not_exist(base_dir?) # Also ensure that the directory exists.
    # ===================================================================== #
    # === --help
    # ===================================================================== #
    when /-?-?help/i
      show_help
      exit
    end
  }
  purge_entries_with_two_hyphens
end
Also aliased as: menu
purge_entries_with_two_hyphens() click to toggle source
#

purge_entries_with_two_hyphens

#
# File lib/generate_rc_file/generate_rc_file.rb, line 114
def purge_entries_with_two_hyphens
  @commandline.reject! {|entry| entry.to_s.start_with? '--' }
end
reset() click to toggle source
#

reset

#
# File lib/generate_rc_file/generate_rc_file.rb, line 47
def reset
  set_base_dir(BASE_DIR)
  @use_putty_style_aliases = false # if true we use putty-style aliases.
end
run() click to toggle source
#

run

#
# File lib/generate_rc_file/generate_rc_file.rb, line 264
def run
  do_generate_the_rc_file
end
set_base_dir(i = BASE_DIR) click to toggle source
#

set_base_dir

#
# File lib/generate_rc_file/generate_rc_file.rb, line 228
def set_base_dir(i = BASE_DIR)
  i = i.to_s.dup # Work on a "fresh" copy.
  # ======================================================================= #
  # Must check if the input contains a ~ character, which is then assumed
  # to refer to the home dir of the user.
  # ======================================================================= #
  if i.include? '~'
    i = File.expand_path(i)
  end
  i << '/' unless i.end_with? '/'
  @base_dir = i
end
set_commandline(i) click to toggle source
#

set_commandline

#
# File lib/generate_rc_file/generate_rc_file.rb, line 121
def set_commandline(i)
  i = [i] unless i.is_a? Array
  @commandline = i
end
set_store_here(i) click to toggle source
#

set_store_here

The argument to this method shall always be the absolute path.

#
# File lib/generate_rc_file/generate_rc_file.rb, line 131
def set_store_here(i)
  @store_here = i
end
show_help() click to toggle source
#

show_help (help tag)

#
# File lib/generate_rc_file/generate_rc_file.rb, line 78
def show_help
  e
  e 'You can use another target directory.'
  e
  e 'To do this, do:'
  e
  e '  generate_rc_file --dir=~/AUTOGENERATED'
  e
end
store_what_where( what = @_, where = base_directory?+'aliases_rc' ) click to toggle source
#

store_what_where

#
# File lib/generate_rc_file/generate_rc_file.rb, line 253
def store_what_where(
    what  = @_,
    where = base_directory?+'aliases_rc'
  ) # store what, where.
  opn; e 'Now storing at `'+sfile(where)+'`.'
  File.open(where, 'w+') {|f| f.puts what }
end
try_to_create_the_base_directory_if_it_does_not_exist(i = @base_dir) click to toggle source
#

try_to_create_the_base_directory_if_it_does_not_exist

#
# File lib/generate_rc_file/generate_rc_file.rb, line 244
def try_to_create_the_base_directory_if_it_does_not_exist(i = @base_dir)
  unless File.directory? i
    FileUtils.mkdir_p(i) if File.writable?(File.dirname(i))
  end
end