Class: Roebe::PathGenerator

Inherits:
Base show all
Defined in:
lib/roebe/classes/path_generator/misc.rb,
lib/roebe/classes/path_generator/reset.rb,
lib/roebe/classes/path_generator/constants.rb

Overview

Roebe::PathGenerator

Constant Summary collapse

DEBUG =
#

DEBUG

If debug is true then we will show more information. Otherwise, we will run fairly quietly.

#
false
FILE_PATH_VARIABLE_RC =
#

FILE_PATH_VARIABLE_RC

#
'/AUTOGENERATED/path_variable_rc'
LINUX_YAML_DIR =
#

LINUX_YAML_DIR

#
"#{HOME_DIRECTORY_OF_USER_X}DATA/PC/OS/LINUX/YAML/"
FILE_PATH_VARIABLE =
nil
SYSBIN =
#

SYSBIN

#
'/usr/bin/'
UBIN =
#

UBIN

#
'/usr/bin/'
DATASET =
nil
PATH =
#

PATH

#
ENV['PATH'].to_s

Constants inherited from Base

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

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, #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) ⇒ PathGenerator

#

initialize

#


14
15
16
17
18
19
20
21
# File 'lib/roebe/classes/path_generator/misc.rb', line 14

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

Instance Method Details

#dataset?Boolean

#

dataset?

#

Returns:

  • (Boolean)


316
317
318
# File 'lib/roebe/classes/path_generator/misc.rb', line 316

def dataset?
  @dataset.uniq
end

#debug?Boolean

#

debug?

#

Returns:

  • (Boolean)


309
310
311
# File 'lib/roebe/classes/path_generator/misc.rb', line 309

def debug?
  DEBUG
end

#generate_pathrc_file(into_these_files = [ FILE_PATH_VARIABLE_RC ]) ⇒ Object

#

generate_pathrc_file

#


241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/roebe/classes/path_generator/misc.rb', line 241

def generate_pathrc_file(
    into_these_files = [ FILE_PATH_VARIABLE_RC ]
  )
  if @dataset
    into_these_files.each {|file|
      opne 'We will now generate the pathrc file '\
              'at `'+sfile(file)+'`.'
      what = 'export PATH="'.dup
      add_this = dataset?.join(':')
      add_this[0,1] = '' if add_this.start_with? ':'
      what << add_this
      what << '"'
      write_what_into(what, file)
      opne
      opne "The #{gold('new path')} is:"
      opne
      opne '  '+sfancy(
        what.gsub(/export/,'').
             gsub(/PATH/,'').
             strip.
             delete('"').
             delete('=')
      )
      opne
    }
  else
    opne "No dataset has been assigned to #{slateblue('@dataset')}."
    @dataset_has_been_found = false
  end
end

#input?Boolean

#

input?

#

Returns:

  • (Boolean)


53
54
55
# File 'lib/roebe/classes/path_generator/misc.rb', line 53

def input?
  @input
end

#notify_roebeObject

#

notify_roebe

#


275
276
277
278
279
280
281
# File 'lib/roebe/classes/path_generator/misc.rb', line 275

def notify_roebe
  if @dataset_has_been_found
    opne 'Do not forget to source the path_variable '\
            'via `'+sfancy('ata')+'` to make'
    opne 'it known to your shell.'
  end
end

#remove_this_path_entry(i) ⇒ Object

#

remove_this_path_entry (remove tag)

Usage example:

pathgen --remove /usr/bin
#


77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/roebe/classes/path_generator/misc.rb', line 77

def remove_this_path_entry(i)
  splitted = i.split(' ')
  path = rds(splitted.last+'/')
  if dataset? and dataset?.include?(path)
    set_dataset(dataset? - [path])
    store_dataset
  else
    opne 'The input '+simp(path)+' is not included.'
    opne 'We will next display the dataset.'
    pp dataset?
  end
end

#resetObject

#

reset (reset tag)

#


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/roebe/classes/path_generator/reset.rb', line 14

def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # === @dataset_has_been_found
  # ======================================================================= #
  @dataset_has_been_found = true
  # ======================================================================= #
  # === @dataset
  # ======================================================================= #
  @dataset = DATASET
  if debug?
    opne "Loading dataset from `#{sfile(FILE_PATH_VARIABLE)}`."
    if File.exist? FILE_PATH_VARIABLE
      e File.read(FILE_PATH_VARIABLE)
    end
  end
end

#reshuffle_inputObject

#

reshuffle_input

#


60
61
62
63
64
65
66
67
# File 'lib/roebe/classes/path_generator/misc.rb', line 60

def reshuffle_input
  if @dataset
    if @dataset.include? input?
      @dataset.delete(input?) # We must get rid of the input in this case.
    end
    @dataset[0,0] = input? unless input?.is_a? Symbol
  end
end

#runObject

#

run (run tag)

#


323
324
325
326
327
328
329
330
# File 'lib/roebe/classes/path_generator/misc.rb', line 323

def run
  sanitize_dataset
  reshuffle_input
  generate_pathrc_file
  notify_roebe if is_on_roebe?
  copy_dataset_into_yaml_file if is_on_roebe?
  pp dataset? if debug?
end

#sanitize_datasetObject

#

sanitize_dataset

We must turn the dataset into an Array.

#


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/roebe/classes/path_generator/misc.rb', line 216

def sanitize_dataset
  if @dataset
    unless @dataset.is_a? Array # This is required since CLEAR instruction can lead to empty Array.
      @dataset = @dataset.split(N).reject {|entry| entry.empty? }
      # ===================================================================== #
      # Handle :exclude_cwd instructions next:
      # ===================================================================== #
      if input? == :exclude_cwd
        cwd = Dir.pwd
        opne 'Rejecting the current working directory ('+sdir(cwd)+').'
        @dataset.reject! {|entry|
          entry.start_with? cwd
        }
      end
      @dataset.map! {|entry|
        entry << '/' unless entry.end_with? '/'
        entry
      }
    end
  end
end

#set_dataset(i) ⇒ Object

#

set_dataset

#


207
208
209
# File 'lib/roebe/classes/path_generator/misc.rb', line 207

def set_dataset(i)
  @dataset = i
end

#set_input(i = '') ⇒ Object

#

set_input

#


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/roebe/classes/path_generator/misc.rb', line 101

def set_input(i = '')
  i = i.join(' ').strip if i.is_a? Array
  i = i.to_s.dup.squeeze('/')
  case i # case tag, menu tag
  # ======================================================================= #
  # === pathgen --open
  # ======================================================================= #
  when /^-?-?open$/i
    open_in_editor(FILE_PATH_VARIABLE)
    exit
  # ======================================================================= #
  # === pathgen toolchain
  # === pathgen --toolchain
  # ======================================================================= #
  when /^-?-?toolchain$/i
    i = '/home/Programs/Toolchain/bin'
  # ======================================================================= #
  # === pathgen SYSBIN
  # ======================================================================= #
  when 'SYSBIN','1'
    i = SYSBIN
  # ======================================================================= #
  # === pathgen UBIN
  #
  # This entry point is a shortcut for /usr/bin/ as path.
  # ======================================================================= #
  when 'UBIN','trad'
    i = UBIN
  # ======================================================================= #
  # === pathgen --file?
  # ======================================================================= #
  when /^-?-?file\??$/
    show_main_file
    exit
  # ======================================================================= #
  # === pathgen --show
  # ======================================================================= #
  when /^-?-?show$/
    show_path
    exit
  # ======================================================================= #
  # === pathgen --remove
  # ======================================================================= #
  when /--remove/
    remove_this_path_entry(i)
    exit
  when '' # Pass through in this case.
  # ======================================================================= #
  # === pathgen --update
  # ======================================================================= #
  when /^-?-?updated?$/
    update_path_variable_yaml_file
  # ======================================================================= #
  # === pathgen --help
  # ======================================================================= #
  when /^-?-?help$/i,'?'
    show_help
    exit
  # ======================================================================= #
  # === pathgen --pwd
  # ======================================================================= #
  when /^-?-?pwd$/i,
       'this_dir',
       'Dir.pwd'
    i = (Dir.pwd+'/').squeeze '/'
  # ======================================================================= #
  # === pathgen --not-pwd
  # ======================================================================= #
  when '--not-pwd' # Get rid of the current working directory.
    i = :exclude_cwd
  # ======================================================================= #
  # === pathgen --clear
  # ======================================================================= #
  when /^-?-?clear$/,'CLEAR'
    @dataset = []
    i = ''
  # ======================================================================= #
  # === pathgen --minimal
  # ======================================================================= #
  when 'minimal'
    @dataset = %w( /usr/bin )
    i = ''
  else
    if File.exist? i
      # Is ok.
    elsif i.is_a?(String) and i.include?(':') # Assume a path here.
      # Is ok as well.
    elsif i.is_a?(String)
      # Is ok too. 
    else
      opne 'An unknown command (`'+steelblue(i)+'`) was given.'
      e
      opne 'Please clarify or use no command.'
      exit
    end
  end
  unless i.empty?
    i = i.dup if i.frozen?
    i << '/' unless i.end_with? '/'
  end if i.is_a? String
  @input = i
end

#show_helpObject

#

show_help (help tag)

To invoke this method, do:

pathgen --help
#


31
32
33
34
35
36
37
38
39
40
41
# File 'lib/roebe/classes/path_generator/misc.rb', line 31

def show_help
  opne 'The following help options are available:'
  result = <<EOF

--remove path # will remove the specific path from the list
--update      # will update the path_variable.yml file
--file?       # show the location of the main file (path_variable.yml)

EOF
  e result
end

#show_main_fileObject

#

show_main_file

#


46
47
48
# File 'lib/roebe/classes/path_generator/misc.rb', line 46

def show_main_file
  e sfile(FILE_PATH_VARIABLE)
end

#show_pathObject

#

show_path

#


93
94
95
96
# File 'lib/roebe/classes/path_generator/misc.rb', line 93

def show_path
  opne 'The path is:'
  opne simp("  #{PATH}")
end

#store_datasetObject

#

store_dataset

#


286
287
288
289
# File 'lib/roebe/classes/path_generator/misc.rb', line 286

def store_dataset
  generate_pathrc_file
  update_path_variable_yaml_file
end

#update_path_variable_yaml_fileObject Also known as: copy_dataset_into_yaml_file

#

update_path_variable_yaml_file

#


294
295
296
297
298
299
300
301
302
303
304
# File 'lib/roebe/classes/path_generator/misc.rb', line 294

def update_path_variable_yaml_file
  into = FILE_PATH_VARIABLE
  if @dataset
    what = YAML.dump(dataset?)
    begin
      write_what_into(what, into)
    rescue Exception => error
      pp error
    end
  end
end