Class: Roebe::ModifyShebangHeader

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

Overview

Roebe::ModifyShebangHeader

Constant Summary collapse

DEFAULT_SHEBANG_TO_USE_ON_WINDOWS =
#

DEFAULT_SHEBANG_TO_USE_ON_WINDOWS

#
'#!/ruby/bin/ruby -w'
DEFAULT_SHEBANG_TO_USE_ON_LINUX =
#

DEFAULT_SHEBANG_TO_USE_ON_LINUX

The following constant does not require of us to specify a leading ‘#!’ variant.

On windows you may want to use another shebang line, such as “#!/ruby/bin/ruby -w”.

#

DEFAULT_SHEBANG_TO_USE_ON_LINUX = ‘/System/Index/bin/ruby -w’ # The old shebang that was in use.

'#!/usr/bin/ruby -w'
MAY_WE_OVERWRITE_EXISTING_FILES =
#

MAY_WE_OVERWRITE_EXISTING_FILES

If the following constant is true then we may overwrite existing files. This is the default behaviour.

#
true
THIS_IS_THE_PRIMARY_ENCODING =
#

THIS_IS_THE_PRIMARY_ENCODING

The following constant will specify which encoding we have to use.

#
'# Encoding: UTF-8'
DEFAULT_FILE_TO_USE =
#

DEFAULT_FILE_TO_USE

#
'foobar.rb'
WILL_WE_TRY_TO_CHECK_FOR_THE_CORRECT_ENCODING =
#

WILL_WE_TRY_TO_CHECK_FOR_THE_CORRECT_ENCODING

#
true

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, #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(commandline_arguments = nil, run_already = true) ⇒ ModifyShebangHeader

#

initialize

#


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/roebe/classes/modify_shebang_header/modify_shebang_header.rb', line 76

def initialize(
    commandline_arguments = nil,
    run_already           = true
  )
  reset
  set_commandline_arguments(
    commandline_arguments
  )
  case commandline_arguments
  # ======================================================================= #
  # === :do_not_run_yet
  # ======================================================================= #
  when :do_not_run_yet,
       :dont_run_yet
    run_already = false
  end
  case run_already
  # ======================================================================= #
  # === :do_not_run_yet
  # ======================================================================= #
  when :do_not_run_yet
    run_already = false
  end
  run if run_already
end

Class Method Details

.[](i = ARGV) ⇒ Object

#

Roebe::ModifyShebangHeader[]

#


427
428
429
# File 'lib/roebe/classes/modify_shebang_header/modify_shebang_header.rb', line 427

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

Instance Method Details

#append_work_on_these_files(i) ⇒ Object

#

append_work_on_these_files

#


205
206
207
208
209
210
211
# File 'lib/roebe/classes/modify_shebang_header/modify_shebang_header.rb', line 205

def append_work_on_these_files(i)
  unless @internal_hash[:work_on_these_files].include?(i)
    @internal_hash[:work_on_these_files] << i
    @internal_hash[:work_on_these_files].flatten!
    @internal_hash[:work_on_these_files].compact!
  end
end

#do_work_on_the_files(i = work_on_these_files?, , be_verbose = be_verbose? ) ⇒ Object Also known as: do_modify_the_files

#

do_work_on_the_files

This is the method-action of this class, the main powerhorse. This method will try to modify the files passed to it.

#


321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/roebe/classes/modify_shebang_header/modify_shebang_header.rb', line 321

def do_work_on_the_files(
    i          = work_on_these_files?,
    be_verbose = be_verbose?
  )
  # ======================================================================= #
  # Iterate over the given files next:
  # ======================================================================= #
  i.each {|this_file|
    # ===================================================================== #
    # Check for empty and notify the user about this.
    # ===================================================================== #
    if File.empty? this_file
      opne "The file at #{sfile(this_file)} is empty. "\
           "Skipping empty files."
      next
    end
    # ===================================================================== #
    # Next, keep a reference to the current file that may be modified.
    # ===================================================================== #
    set_store_into_this_file(this_file) # Keep a reference here, .
    if File.exist? this_file
      if be_verbose
        opne "Now working on the file `#{sfile(this_file)}`."
      end
      old_content = File.readlines(this_file)
      if ensure_proper_encoding?
        encoding_line = old_content[1] # Second line may be the encoding line.
        # =================================================================== #
        # We only consider lines that begin with '# ' and also include
        # an Encoding entry.
        # =================================================================== #
        if encoding_line.start_with?('# ') and
           encoding_line.include?('# Encoding: ')
          unless encoding_line.include? THIS_IS_THE_PRIMARY_ENCODING
            old_content[1] = THIS_IS_THE_PRIMARY_ENCODING
            unless old_content[1].end_with? N
              old_content[1] = old_content[1].dup if old_content[1].frozen?
              old_content[1] << N
            end
          end
        end
      end  
      old_content[0] = "#{use_which_shebang?}\n"
      into = rds(this_file)
      opne "Storing into the file #{sfile(into)} next."
      store_this_file_content_into_that_file(
        old_content.join,
        into
      )
    end
  }
end

#ensure_proper_encoding?Boolean

#

ensure_proper_encoding?

#

Returns:

  • (Boolean)


141
142
143
# File 'lib/roebe/classes/modify_shebang_header/modify_shebang_header.rb', line 141

def ensure_proper_encoding?
  @internal_hash[:will_we_try_to_check_for_the_correct_encoding]
end

#is_on_windowsObject

#

is_on_windows

#


148
149
150
# File 'lib/roebe/classes/modify_shebang_header/modify_shebang_header.rb', line 148

def is_on_windows
  set_new_shebang_to_use(:windows)
end
#

menu (menu tag)

#


235
236
237
238
239
240
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/roebe/classes/modify_shebang_header/modify_shebang_header.rb', line 235

def menu(
    i = commandline_arguments?
  )
  if i.is_a? Array
    i.each {|entry| menu(entry) }
  else
    if i.start_with?('--')
      case i
      # =================================================================== #
      # === modify_shebang --help
      # =================================================================== #
      when /^-?-?help$/
        show_help
        exit
      # =================================================================== #
      # === modify_shebang --on_windows
      #
      # This entry point allows us to assume the default shebang that I
      # use on windows.
      # =================================================================== #
      when /^-?-?on(-|_)?windows$/,
           /^-?-?is(-|_)?on(-|_)?windows$/
        is_on_windows
      # =================================================================== #
      # === modify_shebang --file-type=php
      #
      # This entry point allows us to set to another file type, such
      # as .php files.
      #
      # A combined example would be this:
      #
      #   modify_shebang --file-type=cgi /Depot/jjjj/ANOTHER_TEST/
      #
      # =================================================================== #
      when /^-?-?file(-|_)?type=(.+)$/ # === $2
        set_filetype(
          $2.to_s.dup
        )
      end
    elsif i and File.file?(i)
      append_work_on_these_files(i)
    elsif i and File.directory?(i)
      _ = @internal_hash[:work_on_this_file_type_only]
      append_work_on_these_files(
        Dir["#{i}/**/**"].select {|entry|
          entry.end_with?(_)
        }
      )
    else
      unless i.empty?
        opne "Unsure what to do with #{i}."
      end
    end
  end
end

#new_shebang_to_use?Boolean Also known as: use_which_shebang?, return_new_shebang, we_want_to_have_this_new_shebang_lines

#

new_shebang_to_use?

#

Returns:

  • (Boolean)


187
188
189
# File 'lib/roebe/classes/modify_shebang_header/modify_shebang_header.rb', line 187

def new_shebang_to_use?
  @internal_hash[:new_shebang_to_use]
end

#resetObject

#

reset (reset tag)

#


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
# File 'lib/roebe/classes/modify_shebang_header/modify_shebang_header.rb', line 105

def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # === @internal_hash
  # ======================================================================= #
  @internal_hash = {}
  # ======================================================================= #
  # === :work_on_these_files
  #
  # Empty Array by default.
  # ======================================================================= #
  @internal_hash[:work_on_these_files] = []
  # ======================================================================= #
  # === :new_shebang_to_use
  # ======================================================================= #
  set_new_shebang_to_use(:infer_automatically)
  # ======================================================================= #
  # === :work_on_this_file_type_only
  #
  # This variable will determine which files are to be selected.
  # By default, the class will work on .rb files, as this is
  # the most common use case I have had.
  # ======================================================================= #
  @internal_hash[:work_on_this_file_type_only] = '.rb'
  # ======================================================================= #
  # === :will_we_try_to_check_for_the_correct_encoding
  # ======================================================================= #
  @internal_hash[:will_we_try_to_check_for_the_correct_encoding] =
    WILL_WE_TRY_TO_CHECK_FOR_THE_CORRECT_ENCODING
  set_be_verbose
end

#runObject

#

run (run tag)

#


419
420
421
422
# File 'lib/roebe/classes/modify_shebang_header/modify_shebang_header.rb', line 419

def run
  menu # Must come after reset().
  do_work_on_the_files
end

#set_new_shebang_to_use(i) ⇒ Object

#

set_new_shebang_to_use

#


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
# File 'lib/roebe/classes/modify_shebang_header/modify_shebang_header.rb', line 155

def set_new_shebang_to_use(i)
  if i.is_a? Array
    i = i.first
  end
  case i # case tag
  # ======================================================================= #
  # === :infer_automatically
  # ======================================================================= #
  when :infer_automatically
    if is_on_windows?
      i = DEFAULT_SHEBANG_TO_USE_ON_WINDOWS
    else
      i = DEFAULT_SHEBANG_TO_USE_ON_LINUX
    end
  # ======================================================================= #
  # === :default
  # ======================================================================= #
  when :default,
       :linux
    i = DEFAULT_SHEBANG_TO_USE_ON_LINUX
  # ======================================================================= #
  # === :windows
  # ======================================================================= #
  when :windows
    i = DEFAULT_SHEBANG_TO_USE_ON_WINDOWS
  end
  @internal_hash[:new_shebang_to_use] = i
end

#set_store_into_this_file(i = DEFAULT_FILE_TO_USE) ⇒ Object Also known as: set_file_name, set_filename

#

set_store_into_this_file

This method will keep track of which file we may overwrite.

#


379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/roebe/classes/modify_shebang_header/modify_shebang_header.rb', line 379

def set_store_into_this_file(
    i = DEFAULT_FILE_TO_USE
  )
  i = DEFAULT_FILE_TO_USE if i.nil?
  i = i.dup if i.frozen?
  # ======================================================================= #
  # Sanitize the given input a little bit.
  # ======================================================================= #
  i.chop! if i.end_with? '#'
  i.chop! if i.end_with? ':'
  @internal_hash[:store_into_this_file] = i
end

#set_work_on_these_files(i) ⇒ Object Also known as: set_modify_these_files, set_fix_this_file

#

set_work_on_these_files

#


196
197
198
199
# File 'lib/roebe/classes/modify_shebang_header/modify_shebang_header.rb', line 196

def set_work_on_these_files(i)
  i = [i].flatten.compact
  @internal_hash[:work_on_these_files] = i
end

#set_work_on_this_file_type_only(i = '.rb') ⇒ Object Also known as: set_filetype, set_use_this_filetype, use_this_filetype=

#

set_work_on_this_file_type_only

Note that we will keep the ‘.’, thus the filetype may be ‘.rb’.

#


303
304
305
306
307
308
309
310
311
# File 'lib/roebe/classes/modify_shebang_header/modify_shebang_header.rb', line 303

def set_work_on_this_file_type_only(
    i = '.rb'
  )
  i = i.dup if i.frozen?
  unless i.start_with?('.')
    i.prepend('.')
  end
  @internal_hash[:work_on_this_file_type_only] = i
end

#show_helpObject

#

show_help (help tag)

#


223
224
225
226
227
228
229
230
# File 'lib/roebe/classes/modify_shebang_header/modify_shebang_header.rb', line 223

def show_help
  e
  e 'These are the documented (and thus available) options:'
  e
  e '  --file-type=php # specify another file type suffix'
  e '  --on_windows    # designate that we are on windows'
  e
end

#store_into_this_file?Boolean Also known as: store_into_which_file?

#

store_into_this_file?

#

Returns:

  • (Boolean)


396
397
398
# File 'lib/roebe/classes/modify_shebang_header/modify_shebang_header.rb', line 396

def store_into_this_file?
  @internal_hash[:store_into_this_file]
end

#store_this_file_content_into_that_file(what, into = store_into_which_file? ) ⇒ Object Also known as: store_dataset, save_result

#

store_this_file_content_into_that_file

#


403
404
405
406
407
408
409
410
411
412
413
# File 'lib/roebe/classes/modify_shebang_header/modify_shebang_header.rb', line 403

def store_this_file_content_into_that_file(
    what,
    into = store_into_which_file?
  )
  if MAY_WE_OVERWRITE_EXISTING_FILES
    if be_verbose?
      opnn; e "Storing into the file `#{sfile(into)}`."
    end
    write_what_into(what, into)
  end
end

#work_on_these_files?Boolean

#

work_on_these_files?

#

Returns:

  • (Boolean)


216
217
218
# File 'lib/roebe/classes/modify_shebang_header/modify_shebang_header.rb', line 216

def work_on_these_files?
  @internal_hash[:work_on_these_files]
end

#work_on_this_file_type_only?Boolean Also known as: use_this_filetype?

#

work_on_this_file_type_only?

#

Returns:

  • (Boolean)


294
295
296
# File 'lib/roebe/classes/modify_shebang_header/modify_shebang_header.rb', line 294

def work_on_this_file_type_only?
  @internal_hash[:work_on_this_file_type_only]
end