Class: ToTarXz

Inherits:
Object
  • Object
show all
Includes:
Colours
Defined in:
lib/totarxz/totarxz.rb,
lib/totarxz/constants.rb,
lib/totarxz/version/version.rb

Overview

#

require ‘totarxz/version/version.rb’

#

Constant Summary collapse

TAR_XZ_EXTENSION =
#

TAR_XZ_EXTENSION

#
'.tar.xz'
TAR_BZ2_EXTENSION =
#

TAR_BZ2_EXTENSION

#
'.tar.bz2'
BE_VERBOSE =
#

BE_VERBOSE

Whether to be verbose by default or whether we will not be verbose.

#
true
VERSION =
#

ToTarXz

#
'0.0.21'
LAST_UPDATE =
#

LAST_UPDATE

#
'16.03.2024'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package_this_target = nil, be_verbose = BE_VERBOSE, run_already = true, &block) ⇒ ToTarXz

#

initialize

The first argument is the target to become a .tar.xz archive.

#


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/totarxz/totarxz.rb', line 30

def initialize(
    package_this_target = nil,
    be_verbose          = BE_VERBOSE,
    run_already         = true,
    &block
  )
  @be_verbose = be_verbose # default
  reset # This must come after @be_verbose was set.
  be_verbose if be_verbose # Be silent not before calling sanitize_data().
  set_package_this_target(
    package_this_target
  )
  case run_already
  # ======================================================================= #
  # === --help
  # ======================================================================= #
  when /^-?-?help$/i
    show_help_then_exit
  # ======================================================================= #
  # === --date
  # ======================================================================= #
  when /^-?-?date$/i,
       'TODAY','WITH_DATE'
    @repackage_archive_to_todays_date = true
    run_already = true
  end
  # ======================================================================= #
  # === Handle blocks next
  #
  # Intercept blocks here. Handle :be_verbose as argument, but also
  # certain keys in a Hash, since as of June 2020.
  # ======================================================================= #
  if block_given?
    yielded = yield
    case yielded
    when :be_silent
      @be_verbose = false
    when :be_verbose
      @be_verbose = true
    else
      if yielded.is_a? Hash
        # ================================================================= #
        # === :use_this_archive_name => "htop-2.2.0"
        # ================================================================= #
        if yielded.has_key? :use_this_archive_name
          set_tar_xz_name(yielded.delete(:use_this_archive_name))
        end
      end
    end
  end
  run if run_already
end

Class Method Details

.[](i) ⇒ Object

#

ToTarXz[]

#


327
328
329
# File 'lib/totarxz/totarxz.rb', line 327

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

Instance Method Details

#abort_then_exitObject

#

abort_then_exit

#


142
143
144
145
146
# File 'lib/totarxz/totarxz.rb', line 142

def abort_then_exit
  abort "class ToTarXz: No file was provided or could be found. Please \n"\
        "supply a valid argument (an existing file) and try again."
  exit
end

#be_silent?Boolean

#

be_silent?

#

Returns:

  • (Boolean)


113
114
115
# File 'lib/totarxz/totarxz.rb', line 113

def be_silent?
  !@be_verbose
end

#be_verbose?Boolean

#

be_verbose?

#

Returns:

  • (Boolean)


120
121
122
# File 'lib/totarxz/totarxz.rb', line 120

def be_verbose?
  @be_verbose
end

#build_cmd_stringObject

#

build_cmd_string

#


313
314
315
# File 'lib/totarxz/totarxz.rb', line 313

def build_cmd_string
  @cmd << @tar_xz_name << ' ' << input? # Simply append.
end

#determine_tar_xz_name(i = input?.delete('/')+ TAR_XZ_EXTENSION) ⇒ Object Also known as: determine_the_name_of_the_archive

#

determine_tar_xz_name

Note that this method will always append .tar.xz to the name. This is fine for .tar.xz archives but not for .tar.bz2 archives.

#


292
293
294
295
296
297
# File 'lib/totarxz/totarxz.rb', line 292

def determine_tar_xz_name(
    i = input?.delete('/')+
        TAR_XZ_EXTENSION
  )
  @tar_xz_name = i
end

#extract_to_this_directory?Boolean Also known as: directory?, dir?, tar_xz_name?, archive_location?, result?

#

extract_to_this_directory?

#

Returns:

  • (Boolean)


198
199
200
# File 'lib/totarxz/totarxz.rb', line 198

def extract_to_this_directory?
  @tar_xz_name
end

#location?Boolean

#

location?

#

Returns:

  • (Boolean)


209
210
211
# File 'lib/totarxz/totarxz.rb', line 209

def location?
  determine_tar_xz_name # Yes, we will determine anew.
end

#mv(old, new) ⇒ Object

#

mv

#


225
226
227
# File 'lib/totarxz/totarxz.rb', line 225

def mv(old, new)
  FileUtils.mv(old, new)
end

#package_this_target?Boolean Also known as: input?, archive_filename?, archive?

#

package_this_target?

#

Returns:

  • (Boolean)


216
217
218
# File 'lib/totarxz/totarxz.rb', line 216

def package_this_target?
  @package_this_target
end

#rename?Boolean

#

rename?

Whether we will rename our archive or whether we will not.

#

Returns:

  • (Boolean)


282
283
284
# File 'lib/totarxz/totarxz.rb', line 282

def rename?
  @repackage_archive_to_todays_date
end

#resetObject

#

reset (reset tag)

#


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/totarxz/totarxz.rb', line 86

def reset
  if @be_verbose
    @cmd = 'tar cJvf ' # v stands for verbosity here.
  else
    @cmd = 'tar cJf '  # v stands for verbosity here.
  end
  # ======================================================================= #
  # === @cmd
  # ======================================================================= #
  @cmd = @cmd.dup
  # ======================================================================= #
  # === @archive_type
  # ======================================================================= #
  @archive_type = '.tar.xz'
  # ======================================================================= #
  # === @repackage_archive_to_todays_date
  # ======================================================================= #
  @repackage_archive_to_todays_date = false
  # ======================================================================= #
  # === @orig_stdout
  # ======================================================================= #
  @orig_stdout = nil
end

#runObject

#

run (run tag)

#


320
321
322
# File 'lib/totarxz/totarxz.rb', line 320

def run
  create_tarxz_package
end

#run_cmdObject Also known as: create_tarxz_package

#

run_cmd

#


250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/totarxz/totarxz.rb', line 250

def run_cmd
  e @cmd
  system @cmd
  # ======================================================================= #
  # Next, we will apply a mv-action if we repackage to another name.
  # ======================================================================= #
  if rename?
    new_name = File.basename(archive?).sub(/\.tar\.xz$/, '')+
               '-'+
               today?+
               @archive_type
    opn; e 'Next renaming the archive to `'+sfancy(new_name)+'`.'
    mv(archive?, new_name)
  end
  if @orig_stdout
    $stdout = @orig_stdout unless be_verbose? # Restore stdout again.
  end
end

#set_cmd(i) ⇒ Object

#

set_cmd

#


239
240
241
242
243
244
245
# File 'lib/totarxz/totarxz.rb', line 239

def set_cmd(i)
  unless i.end_with? ' '
    i = i.dup if i.frozen?
    i << ' '
  end
  @cmd = i
end

#set_package_this_target(i = nil) ⇒ Object Also known as: set_archive_filename, directory=, extract_to_this_directory=

#

set_package_this_target

This method will be called from within initialize().

#


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
# File 'lib/totarxz/totarxz.rb', line 153

def set_package_this_target(
    i = nil
  )
  if i.is_a? Array and !i.empty?
    i = i.join(' ').strip
  end
  if i and i.end_with?('*')
    # In this case continue.
  elsif i.nil? or !File.exist?(i.to_s)
    abort_then_exit
  end
  case i
  # ======================================================================= #
  # === --help
  # ======================================================================= #
  when /^-?-?help$/i
    show_help_then_exit
  end 
  i = i.to_s.dup
  unless i.end_with? '*'
    i << '/' unless i.end_with? '/'
  end
  i.delete_suffix!('.tar.xz')  if i.include?('.tar.xz')
  i.delete_suffix!('.tar.bz2') if i.include?('.tar.bz2')
  @package_this_target = i
  # ======================================================================= #
  # Now we can determine the .tar.xz name.
  # ======================================================================= #
  determine_tar_xz_name
end

#set_tar_xz_name(i) ⇒ Object

#

set_tar_xz_name

#


302
303
304
305
306
307
308
# File 'lib/totarxz/totarxz.rb', line 302

def set_tar_xz_name(i)
  if i.frozen?
    i = i.dup
  end
  i << TAR_XZ_EXTENSION unless i.end_with? TAR_XZ_EXTENSION
  @tar_xz_name = i
end

#show_helpObject

#

show_help

#


127
128
129
# File 'lib/totarxz/totarxz.rb', line 127

def show_help
  e 'To package up with the date, use DATE or TODAY.'
end

#show_help_then_exitObject

#

show_help_then_exit

#


134
135
136
137
# File 'lib/totarxz/totarxz.rb', line 134

def show_help_then_exit
  show_help
  exit
end

#silenceObject

#

silence

#


189
190
191
192
193
# File 'lib/totarxz/totarxz.rb', line 189

def silence
  @be_verbose = false
  @orig_stdout = $stdout # Be silent.
  $stdout = File.new('/dev/null', 'w')
end

#today?Boolean

#

today?

#

Returns:

  • (Boolean)


232
233
234
# File 'lib/totarxz/totarxz.rb', line 232

def today?
  Time.now.strftime '%d.%m.%Y'
end