class ToTarXz

#

require 'totarxz/constants.rb'

#
#

require 'totarxz/version/version.rb'

#

Constants

BE_VERBOSE
#

BE_VERBOSE

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

#
TAR_BZ2_EXTENSION
#

TAR_BZ2_EXTENSION

#
TAR_XZ_EXTENSION
#

TAR_XZ_EXTENSION

#
VERSION
#

ToTarXz

#

Public Class Methods

[](i) click to toggle source
#

ToTarXz[]

#
# File lib/totarxz/totarxz.rb, line 322
def self.[](i)
  new(i)
end
new( package_this_target = nil, be_verbose = BE_VERBOSE, run_already = true ) { || ... } click to toggle source
#

initialize

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

#
# 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$/,
       'TODAY','DATE','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

Public Instance Methods

abort_then_exit() click to toggle source
#

abort_then_exit

#
# 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
archive?()
archive_filename?()
archive_location?()
be_silent?() click to toggle source
#

be_silent?

#
# File lib/totarxz/totarxz.rb, line 113
def be_silent?
  !@be_verbose
end
be_verbose?() click to toggle source
#

be_verbose?

#
# File lib/totarxz/totarxz.rb, line 120
def be_verbose?
  @be_verbose
end
build_cmd_string() click to toggle source
#

build_cmd_string

#
# File lib/totarxz/totarxz.rb, line 308
def build_cmd_string
  @cmd << @tar_xz_name << ' ' << input? # Simply append.
end
create_tarxz_package()
Alias for: run_cmd
determine_tar_xz_name( i = input?.delete('/')+ TAR_XZ_EXTENSION ) click to toggle source
#

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.

#
# File lib/totarxz/totarxz.rb, line 287
def determine_tar_xz_name(
    i = input?.delete('/')+
        TAR_XZ_EXTENSION
  )
  @tar_xz_name = i
end
dir?()
directory=( i = nil )
directory?()
extract_to_this_directory=( i = nil )
extract_to_this_directory?() click to toggle source
#

extract_to_this_directory?

#
# File lib/totarxz/totarxz.rb, line 198
def extract_to_this_directory?
  @tar_xz_name
end
input?()
location?() click to toggle source
#

location?

#
# File lib/totarxz/totarxz.rb, line 208
def location?
  determine_tar_xz_name # Yes, we will determine anew.
end
mv(old, new) click to toggle source
#

mv

#
# File lib/totarxz/totarxz.rb, line 224
def mv(old, new)
  FileUtils.mv(old, new)
end
package_this_target?() click to toggle source
#

package_this_target?

#
# File lib/totarxz/totarxz.rb, line 215
def package_this_target?
  @package_this_target
end
Also aliased as: input?, archive_filename?, archive?
rename?() click to toggle source
#

rename?

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

#
# File lib/totarxz/totarxz.rb, line 277
def rename?
  @repackage_archive_to_todays_date
end
reset() click to toggle source
#

reset (reset tag)

#
# 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
run() click to toggle source
#

run (run tag)

#
# File lib/totarxz/totarxz.rb, line 315
def run
  create_tarxz_package
end
run_cmd() click to toggle source
#

run_cmd

#
# File lib/totarxz/totarxz.rb, line 249
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
  $stdout = @orig_stdout unless be_verbose? # Restore stdout again.
end
Also aliased as: create_tarxz_package
set_archive_filename( i = nil )
set_cmd(i) click to toggle source
#

set_cmd

#
# File lib/totarxz/totarxz.rb, line 238
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 ) click to toggle source
#

set_package_this_target

This method will be called from within initialize().

#
# 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.gsub!(/\.tar\.xz$/, '')  if i.include? '.tar.xz'
  i.gsub!(/\.tar\.bz2$/, '') if i.include? '.tar.bt2'
  @package_this_target = i
  # ======================================================================= #
  # Now we can determine the .tar.xz name.
  # ======================================================================= #
  determine_tar_xz_name
end
set_tar_xz_name(i) click to toggle source
#

set_tar_xz_name

#
# File lib/totarxz/totarxz.rb, line 297
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_help() click to toggle source
#

show_help

#
# 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_exit() click to toggle source
#

show_help_then_exit

#
# File lib/totarxz/totarxz.rb, line 134
def show_help_then_exit
  show_help
  exit
end
silence() click to toggle source
#

silence

#
# File lib/totarxz/totarxz.rb, line 189
def silence
  @be_verbose = false
  @orig_stdout = $stdout # Be silent.
  $stdout = File.new('/dev/null', 'w')
end
tar_xz_name?()
today?() click to toggle source
#

today?

#
# File lib/totarxz/totarxz.rb, line 231
def today?
  Time.now.strftime '%d.%m.%Y'
end