Class: Roebe::IncrementApplicationVersion

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

Overview

Roebe::IncrementApplicationVersion

Constant Summary collapse

VERSION_STRING =
#

VERSION_STRING

#
'  VERSION = '
GREP_FOR_THIS_VERSION_STRING =

GREP_FOR_THIS_VERSION_STRING

VERSION_STRING
DEPOT_TEMP_DIR =
#

DEPOT_TEMP_DIR

#
Roebe.temp_dir?

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) ⇒ IncrementApplicationVersion

#

initialize

#


37
38
39
40
41
42
43
44
# File 'lib/roebe/classes/increment_application_version.rb', line 37

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

Class Method Details

.[](i = '') ⇒ Object

#

Roebe::IncrementApplicationVersion[]

#


298
299
300
# File 'lib/roebe/classes/increment_application_version.rb', line 298

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

Instance Method Details

#consider_changing_directoryObject

#

consider_changing_directory

#


172
173
174
175
176
177
178
179
180
181
# File 'lib/roebe/classes/increment_application_version.rb', line 172

def consider_changing_directory
  if has_an_argument_been_passed? and is_on_roebe?
    target = RUBY_SRC+first_argument?.to_s
    if File.directory? target
      cd(target)
    else
      opne steelblue(target)+' is not a directory - can not cd into this target.'
    end
  end
end

#create_backup_file(this_file = version_file? ) ⇒ Object Also known as: copy_old_file

#

create_backup_file

This method will create a backup-file.

#


118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/roebe/classes/increment_application_version.rb', line 118

def create_backup_file(
    this_file = version_file?
  )
  e
  e steelblue(' === Backup up the old file now ...')
  e
  opne "A #{tomato('backup')} file will be created before any modification"
  opne 'will be done, though.'
  target_directory = DEPOT_TEMP_DIR+'incremented_versions/'
  mkdir_p(target_directory) unless File.directory? target_directory
  new_target = target_directory+
               File.basename(this_file)
  opnn; copy_file(this_file, new_target)
end

#create_the_modified_file(what, into = version_file? ) ⇒ Object

#

create_the_modified_file

The first argument to this method should be the dataset that we wish to store.

#


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/roebe/classes/increment_application_version.rb', line 93

def create_the_modified_file(
    what,
    into = version_file?
  )
  if what.is_a? Array
    what.map! {|line|
      "#{line.chomp}\n"
    }
  end
  # ======================================================================= #
  # Last line newline will be removed.
  # ======================================================================= #
  what[-1] = what[-1].chomp
  opne "Storing the new content into `#{sfile(into)}`."
  if what.is_a? Array
    what = what.join
  end
  write_what_into(what, into)
end

#do_we_have_a_version_file?Boolean

#

do_we_have_a_version_file?

#

Returns:

  • (Boolean)


63
64
65
# File 'lib/roebe/classes/increment_application_version.rb', line 63

def do_we_have_a_version_file?
  @internal_hash[:use_this_version_file]
end

#increment_this_version(i) ⇒ Object

#

increment_this_version

This method will increment a program version, such as ‘0.0.1’ towards ‘0.0.2’.

#


139
140
141
142
143
# File 'lib/roebe/classes/increment_application_version.rb', line 139

def increment_this_version(i)
  splitted = i.split('.')
  splitted[-1] = (splitted[-1].to_i + 1).to_s
  splitted.join('.')
end

#new_version?Boolean

#

new_version?

#

Returns:

  • (Boolean)


155
156
157
# File 'lib/roebe/classes/increment_application_version.rb', line 155

def new_version?
  @internal_hash[:new_version_number]
end

#notify_the_user_that_everything_is_doneObject

#

notify_the_user_that_everything_is_done

#


162
163
164
165
166
167
# File 'lib/roebe/classes/increment_application_version.rb', line 162

def notify_the_user_that_everything_is_done
  opne "All done - we now have #{rosybrown('version')}"\
       " #{emphasis(new_version?)} "\
       "for the file"
  opne "  `#{sfile(main_file?)}`."
end

#report_that_no_version_substring_could_be_found_in_the_designated_version_fileObject

#

report_that_no_version_substring_could_be_found_in_the_designated_version_file

#


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

def report_that_no_version_substring_could_be_found_in_the_designated_version_file
  opne 'No '+steelblue('VERSION =')+' substring could be '\
       'found in '+sfile(version_file?)+'.'
end

#resetObject

#

reset (reset tag)

#


49
50
51
52
53
54
55
56
57
58
# File 'lib/roebe/classes/increment_application_version.rb', line 49

def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # This variable will ultimately keep track of where the version file
  # can be found. For instance, it may point towards the path at
  # "/Depot/j/roebe/lib/roebe/version/version.rb".
  # ======================================================================= #
  @internal_hash[:use_this_version_file] = nil
end

#runObject

#

run (run tag)

#


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
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
290
291
292
293
# File 'lib/roebe/classes/increment_application_version.rb', line 234

def run
  consider_changing_directory
  scan_for_possible_version_files
  if do_we_have_a_version_file?
    # ===================================================================== #
    # Only continue if we have found a 'version.rb' file. We still have
    # to ensure that this file has a 'VERSION' string, though.
    # ===================================================================== #
    dataset = File.read(version_file?)
    if dataset.include? VERSION_STRING
      # =================================================================== #
      # Ok, VERSION exists. Obtain the version number thus. For this to
      # work, we will use a specific version.
      #
      # The regex that is used can be seen here:
      #
      #   https://rubular.com/r/z16xUFvRm31wTZ
      #
      # =================================================================== #
      use_this_regex = /  VERSION = '(\d{0,3}.\d{0,3}.\d{0,4})'/
      dataset =~ use_this_regex
      version_number = $1.to_s.dup
      @internal_hash[:original_version_number] = version_number
      new_version_number = increment_this_version(version_number) # This also works for 0.15.3 to 0.15.4.
      set_new_version_number(new_version_number)
      opne 'Going to assume that the new version number '\
           'will be '+steelblue(new_version_number)+'.'
      # =================================================================== #
      # Now we have the new program version - we must update the
      # original dataset with this new information.
      # =================================================================== #
      dataset = readlines_with_main_encoding(version_file?)
      this_index = nil
      dataset.each.with_index {|line, index|
        if line.include? VERSION_STRING # e. g. '  VERSION = '
          this_index = index
        end
      }
      if this_index
        target_line = dataset[this_index].chomp 
        opne "Found a version string at:     "\
             "#{emphasis(target_line)}"
        new_line = target_line.sub(
          /#{use_this_regex}/,
          "  VERSION = '#{new_version_number}'"
        ).chomp
        opne "Will modify that line towards: #{emphasis(new_line)}"
        dataset[this_index] = new_line
        create_backup_file(version_file?)
        # ================================================================= #
        # Next, we will create the new, modified file next:
        # ================================================================= #
        create_the_modified_file(dataset)
        notify_the_user_that_everything_is_done
      end
    else
      report_that_no_version_substring_could_be_found_in_the_designated_version_file
    end
  end
end

#scan_for_possible_version_filesObject

#

scan_for_possible_version_files

This method will scan for a possible version.rb file. Note that this can be problematic if we have more than one entry, aka more than one file called version.rb. In that case we have to decide which one is to be picked.

#


199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/roebe/classes/increment_application_version.rb', line 199

def scan_for_possible_version_files
  possible_files = Dir['**/**/version.rb']
  if possible_files.size > 1
    # ===================================================================== #
    # Ok - we have found more than one version.rb file, so we have to
    # determine which one to pick. Evidently we ought to pick the
    # entry that is "the most logical one", aka the best entry.
    #
    # Let's consider the following result, for the rbt/ project:
    #
    #   ["lib/rbt/version/version.rb", "lib/rbt/compile/version.rb"]
    #
    # Evidently the first variant is better. So we will check whether
    # there is any entry having two "version" substrings, and this
    # will then chose. Otherwise we will default to sorting based on
    # size/length, and pick the shortest in that event.
    # ===================================================================== #
    if possible_files.any? {|entry| entry.scan(/version/).size > 1 }
      possible_files = possible_files.sort {|entry|
        entry.scan(/version/).size
      }.reverse # reverse is necessary.
    else
      possible_files = possible_files.sort {|entry|
        entry.scan(/version/).size
      }
    end
  end
  unless possible_files.empty?
    set_use_this_version_file(possible_files.first) # Select the first entry then.
  end
end

#set_new_version_number(i) ⇒ Object

#

set_new_version_number

#


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

def set_new_version_number(i)
  @internal_hash[:new_version_number] = i
end

#set_use_this_version_file(i) ⇒ Object

#

set_use_this_version_file

We will use the full path to the ‘version.rb’ file.

#


80
81
82
83
84
85
# File 'lib/roebe/classes/increment_application_version.rb', line 80

def set_use_this_version_file(i)
  i = rds(
    return_pwd+i
  )
  @internal_hash[:use_this_version_file] = i
end

#use_this_version_file?Boolean Also known as: version_file?, main_file?

#

use_this_version_file?

#

Returns:

  • (Boolean)


70
71
72
# File 'lib/roebe/classes/increment_application_version.rb', line 70

def use_this_version_file?
  @internal_hash[:use_this_version_file]
end