Class: Roebe::SetNormalAliasTo

Inherits:
Base show all
Defined in:
lib/roebe/classes/set_normal_alias_to.rb

Overview

Roebe::SetNormalAliasTo

Constant Summary

Constants inherited from Base

Base::COLOURS, Base::HOME_DIRECTORY_OF_USER_X, Base::N, Base::NAMESPACE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#actions, #append_this_onto_that_file, #append_what_into, #be_silent, #be_verbose?, #beautiful_url, #burlywood, #call_next, #chdir, #cheerful_guy, #cliner, #complex_esystem, #convert_global_env, #copy_directory, #copy_file, #cornflowerblue, #create_directory, #crimson, #current_month?, #current_time?, #current_year?, #cyan, #darkcyan, #darkgreen, #darkkhaki, #darkslateblue, #deeppink, #delete_symlink, #do_not_use_the_base_colours, #do_use_the_base_colours, #e, #ecomment, #editor_to_use?, #efancy, #eimp, #emphasis, #ensure_main_encoding, #ensure_utf_encoding, #eparse, #erev, #esteelblue, #etomato, #ewarn, #exit_program, #firebrick, #get_current_day, #get_current_month, #get_files_and_directories, #get_files_from, #get_german_name_for_this_weekday, #gold, #green, #grey, #hh_mm_ss, #hh_mm_ss_day_month_year, #home_dir?, #home_directory_of_user_x?, #infer_the_namespace, #internal_hash?, #is_a_directory?, #is_a_file?, #is_a_jpg_file?, #is_an_image_file?, #is_archive?, #is_audio_file?, #is_in_studium_dir?, #is_multimedia_file?, #is_on_roebe?, #is_on_windows?, #is_studium_available?, #is_symlink?, #is_this_a_ruby_file?, #is_video_file?, #iso_encoding?, #le, #left_colour, #lightblue, #lightgreen, #lightseagreen, #lightsteelblue, #lime, #limegreen, #localhost_to_data, #log_directory?, #main_encoding?, #mediumorchid, #mediumpurple, #mediumseagreen, #mediumslateblue, #mediumspringgreen, #mediumturquoise, #mkdir_p, #mv, #n_days_in_this_month, #n_pages_in_this_pdf_file?, #namespace?, #no_file_exists_at, #ogrey, #olive, #olivedrab, #open_in_browser, #opne, #opnesystem, #opnn, #orange, #orchid, #orev, #palegoldenrod, #palevioletred, #pink, #powderblue, #programs_dir?, #project_base_dir?, #random_html_colour, #rds, #read_file_in_iso_encoding, #read_file_via_the_default_encoding, #read_lines_via_iso_encoding, #readlines_with_main_encoding, #red, #register_sigint, #remove, #remove_directory, #remove_file, #rename_kde_konsole_tab, #replace_localhost_with_data, #report_pwd, #require_rescue, #reset_the_internal_hash, #return_all_directories_from_this_directory, #return_all_files_from_this_directory, #return_current_directory, #return_dd_mm_yyyy, #return_file_or_directory_of, #return_files_from_pwd, #return_last_part_of_the_current_directory, #return_utc, #rev, #right_arrow?, #right_colour, #roebe_log_directory?, #rosybrown, #royalblue, #ruby_base_directory?, #run_in_background, #run_rcfiles_then_run_ata_via_qdbus, #sandybrown, #sdir, #seagreen, #set_be_verbose, #set_xorg_buffer, #sfancy, #sfile, #show_help, #silent_redirection?, #simp, #simple_esystem, #skyblue, #slateblue, #slategray, #springgreen, #steelblue, #string_italic, #swarn, #symlink, #teal, #temp_dir?, #to_camelcase, #to_counted_hash, #tomato, #touch, #try_to_require_the_beautiful_url_gem, #try_to_require_the_html_template, #try_to_require_the_open_gem, #try_to_require_the_program_information_gem, #try_to_require_the_xorg_buffer, #use_colours?, #verbose_truth, #weekday?, #word_wrap, #write_what_into, #yellow

Methods included from Base::CommandlineArguments

#append_onto_the_commandline_arguments, #clear_commandline_arguments, #commandline_arguments?, #commandline_arguments_as_string?, #commandline_arguments_without_leading_hyphens?, #first_argument?, #first_argument_without_leading_hyphens?, #has_an_argument_been_passed?, #remove_hyphened_arguments_from_the_commandline_arguments, #return_commandline_arguments_with_leading_hyphens, #second_argument?, #set_commandline_arguments

Constructor Details

#initialize(i = nil, run_already = true) ⇒ SetNormalAliasTo

#

initialize

Ideally an Array should be put as the first argument to the initialize() method.

#


33
34
35
36
37
38
39
40
# File 'lib/roebe/classes/set_normal_alias_to.rb', line 33

def initialize(
    i           = nil,
    run_already = true
  )
  reset
  set_commandline_arguments(i)
  run if run_already
end

Class Method Details

.[](i = '') ⇒ Object

#

[]

#


263
264
265
# File 'lib/roebe/classes/set_normal_alias_to.rb', line 263

def self.[](i = '')
  new(i)
end

Instance Method Details

#do_make_a_backup_file(from = @read_from_this_file, to = location_of_the_backup_file? ) ⇒ Object

#

do_make_a_backup_file

#


96
97
98
99
100
101
102
# File 'lib/roebe/classes/set_normal_alias_to.rb', line 96

def do_make_a_backup_file(
    from = @read_from_this_file,
    to   = location_of_the_backup_file?
  ) 
  opne 'Copying `'+sfile(from)+'` to `'+sfile(to)+'`'
  copy_file(from, to) { :be_quiet }
end

#do_modify_the_original_alias_file(what = main_dataset, into = @read_from_this_file) ⇒ Object

#

do_modify_the_original_alias_file (save tag)

#


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/roebe/classes/set_normal_alias_to.rb', line 187

def do_modify_the_original_alias_file(
    what = main_dataset,
    into = @read_from_this_file
  )
  if what.is_a? Array
    what = what.join(N)
  end
  opne 'Now storing into `'+sfile(into)+'`.'
  write_what_into(what, into)
  if is_on_roebe?
    opne 'Storing into `'+sfile(into)+'` as well.'
    into = RUBY_SRC+
           'rcfiles/lib/rcfiles/yaml/aliases.yml'
    write_what_into(what, into)
  end
end

#location_of_the_backup_file?Boolean

#

location_of_the_backup_file?

#

Returns:

  • (Boolean)


107
108
109
110
111
112
113
# File 'lib/roebe/classes/set_normal_alias_to.rb', line 107

def location_of_the_backup_file?
  if File.directory? '/Depot/Temp/'
    '/Depot/Temp/BACKUP_'+File.basename(@read_from_this_file)
  else
    '/tmp/BACKUP_'+File.basename(@read_from_this_file)
  end
end

#main_dataset?Boolean

#

main_dataset?

#

Returns:

  • (Boolean)


64
65
66
# File 'lib/roebe/classes/set_normal_alias_to.rb', line 64

def main_dataset?
  @dataset_from_file_aliases
end

#parse_the_commandlineObject

#

parse_the_commandline

The primary purpose of this method is to determine two instance variables.

#


128
129
130
131
132
133
134
135
# File 'lib/roebe/classes/set_normal_alias_to.rb', line 128

def parse_the_commandline
  # ======================================================================= #
  # We will assume that the commandline has two inputs.
  # ======================================================================= #
  _ = commandline_arguments?
  set_modify_this_alias(_.first)
  set_new_value_of_this_alias(_.last)
end

#resetObject

#

reset (reset tag)

#


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/roebe/classes/set_normal_alias_to.rb', line 45

def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # === @read_from_this_file
  # ======================================================================= #
  @read_from_this_file = FILE_ALIASES # We will store into this file - or read from it.
  # ======================================================================= #
  # === @dataset_from_file_aliases
  # ======================================================================= #
  @dataset_from_file_aliases = nil
  if File.exist? @read_from_this_file
    @dataset_from_file_aliases = file_read_with_proper_encoding(@read_from_this_file).split(N)
  end
end

#return_position_of_the_most_important_space_character(i) ⇒ Object

#

return_position_of_the_most_important_space_character

#


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/roebe/classes/set_normal_alias_to.rb', line 157

def return_position_of_the_most_important_space_character(i)
  index_position = 0
  already_seen_at_the_least_one_space_character = false
  found_the_real_position = false
  i.each_char {|this_char|
    if found_the_real_position
      # In this case do nothing anymore.
    else
      if (this_char == ' ')
        # ================================================================= #
        # Ok, now we have seen at the least one space character.
        # ================================================================= #
        already_seen_at_the_least_one_space_character = true
      else
        if already_seen_at_the_least_one_space_character
          found_the_real_position = true
        end
      end
      # =================================================================== #
      # Else we must count up.
      # =================================================================== #
      index_position += 1 unless found_the_real_position
    end
  }
  index_position - 1
end

#runObject

#

run (run tag)

#


207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/roebe/classes/set_normal_alias_to.rb', line 207

def run
  parse_the_commandline
  main_dataset = main_dataset?
  selection = main_dataset.select {|line|
    line =~ /^(#{@modify_this_alias}: .+)$/
  }
  if selection.empty?
    opne 'We did not find any match.'
  else
    full_line = selection.first.to_s # Obtain the full line here.
    # ===================================================================== #
    # Next determine the "right index" of the most important
    # space character in the line. In the past we used .rindex(' ')
    # but this won't work very well when there are more than one
    # ' ' space characters in the line, hence the custom method.
    # ===================================================================== #
    rindex_of_the_last_space_character =
      return_position_of_the_most_important_space_character(full_line)
    new_modified_line = full_line[0 .. rindex_of_the_last_space_character]+@new_value_of_this_alias
    # ===================================================================== #
    # Next, obtain the line number that we wish to modify.
    # ===================================================================== #
    @line_number = main_dataset.map.with_index {|line, index|
      index if line.include?(full_line)
    }.compact.first # For now, we only care about the first entry.
    # ===================================================================== #
    # We must obtain the line number as well.
    # ===================================================================== #
    opne 'We will modify alias number '+sfancy(@modify_this_alias)+'.'
    opne 'The '+steelblue('old line')+' is:                       '+
         orange(full_line)
    opne 'The '+steelblue('new, modified line')+rev+' will be:        '+
         slateblue(new_modified_line)
    opne 'The line number that will be modified: '+
         lightgreen(@line_number.to_s)
    # ===================================================================== #
    # Ok, modify the line next.
    # ===================================================================== #
    main_dataset[@line_number] = new_modified_line
    opne "A backup will be kept, at `"\
            "#{sfile(location_of_the_backup_file?)}`"
    do_make_a_backup_file # Backup the old FILE_ALIASES
    do_modify_the_original_alias_file(main_dataset)
    update_rcfiles
    update_aliases
    if is_on_roebe?
      # Since as of 26.04.2019 we may also update a file in the
      # Studium project, if it is available.
      opne 'Also consider using the alias "'+orange('ata')+'".'
    end
  end
end

#set_modify_this_alias(i) ⇒ Object

#

set_modify_this_alias

#


118
119
120
# File 'lib/roebe/classes/set_normal_alias_to.rb', line 118

def set_modify_this_alias(i)
  @modify_this_alias = i.to_s
end

#set_new_value_of_this_alias(i) ⇒ Object

#

set_new_value_of_this_alias

#


140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/roebe/classes/set_normal_alias_to.rb', line 140

def set_new_value_of_this_alias(i)
  if i.is_a? Array
    i.flatten!
    i.compact!
  end
  if i.is_a? Array
    # ===================================================================== #
    # In this case we will only use the first entry.
    # ===================================================================== #
    i = i.first
  end
  @new_value_of_this_alias = i
end

#update_aliasesObject

#

update_aliases

#


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/roebe/classes/set_normal_alias_to.rb', line 71

def update_aliases
  begin
    require 'rcfiles/toplevel_methods/run_it.rb'
    Rcfiles.run_it { :be_quiet }
  rescue LoadError
    if is_on_roebe?
      opne rev+'In the method '+orange('update_aliases()')+
           ', there was a problem:'
      opne 'We could not load the project called Rcfiles.'
    end
  end
end

#update_rcfilesObject

#

update_rcfiles

#


87
88
89
90
91
# File 'lib/roebe/classes/set_normal_alias_to.rb', line 87

def update_rcfiles
  require 'roebe/classes/install_ruby_project.rb'
  _ = HOME_DIRECTORY_OF_USER_X+'DATA/PROGRAMMING_LANGUAGES/RUBY/src/rcfiles/'
  Roebe::InstallRubyProject.new(_)
end