class Extracter::ExtractIt

Constants

ARRAY_ARCHIVE_TYPES
#

ExtractIt::ARRAY_ARCHIVE_TYPES

Register the available (and handled) archive types here.

#
NAMESPACE
#

NAMESPACE

#

Public Class Methods

[](i = '') click to toggle source
#

Extracter::ExtractIt[]

#
# File lib/extracter/extract_it/extract_it.rb, line 229
def self.[](i = '')
  new(i)
end
new( optional_set_input = nil, run_already = true ) click to toggle source
#

initialize

#
# File lib/extracter/extract_it/extract_it.rb, line 58
def initialize(
    optional_set_input = nil,
    run_already        = true
  )
  reset
  set_input(optional_set_input)
  run if run_already
end

Public Instance Methods

be_silent() click to toggle source
#

be_silent

#
# File lib/extracter/extract_it/extract_it.rb, line 119
def be_silent
  @be_silent = true
end
be_verbose() click to toggle source
#

be_verbose

#
# File lib/extracter/extract_it/extract_it.rb, line 126
def be_verbose
  @be_silent = false
end
Also aliased as: show_commands_used
extract_input() click to toggle source
#

extract_input

#
# File lib/extracter/extract_it/extract_it.rb, line 146
def extract_input
  pp @input if @debug
  @input.each {|entry|
    to_this_dir = Dir.pwd
    to_this_dir << '/' unless to_this_dir.end_with? '/'
    unless File.exist? entry
      entry = try_to_glob_on(entry)
    end
    # ===================================================================== #
    # Delegate to class Extracter next.
    # ===================================================================== #
    Extracter.extract_what_to(
      entry,
      to_this_dir,
      @be_silent
    )
    _ = RemoveFileSuffix[entry]
    if File.exist? entry
      # =================================================================== #
      # Must also check whether the extracted directory exists.
      # =================================================================== #
      name_of_the_extracted_archive = to_this_dir+_
      ARRAY_ARCHIVE_TYPES.each {|extension_name|
        if name_of_the_extracted_archive.include? extension_name
          quoted = Regexp.quote(extension_name)
          name_of_the_extracted_archive.sub!(/#{quoted}$/,'')
        end
      }
      if File.exist?(name_of_the_extracted_archive) and
        # ================================================================= #
        # The following check ensures that we really have another name
        # for the extracted directory.
        # ================================================================= #
        !(entry == name_of_the_extracted_archive)
        opn; e "Finished extracting #{sfile(_)} to `#{sdir(to_this_dir)}`."
      else
        opn; e "No file called `#{sfile(name_of_the_extracted_archive)}"\
               "` appears to exist."
      end
    end
  }
end
is_archive?(i) click to toggle source
#

is_archive?

#
# File lib/extracter/extract_it/extract_it.rb, line 192
def is_archive?(i)
  ARRAY_ARCHIVE_TYPES.include?(
    File.extname(File.basename(i))
  )
end
notify_the_user_that_no_input_was_given_but_this_file_was_found( this_file ) click to toggle source
#

notify_the_user_that_no_input_was_given_but_this_file_was_found

#
# File lib/extracter/extract_it/extract_it.rb, line 133
def notify_the_user_that_no_input_was_given_but_this_file_was_found(
    this_file
  )
  opn; e 'No input was given to '+sfancy(NAMESPACE)+' but a '\
         '.zip file was'
  opn; e 'found in this directory ('+sdir(Dir.pwd)+'): '+
          sfancy(this_file)
  opn; e 'We will use this zip file.'
end
reset() click to toggle source
#

reset (reset tag)

#
# File lib/extracter/extract_it/extract_it.rb, line 70
def reset
  @debug = false
end
run() click to toggle source
#

run (run tag)

#
# File lib/extracter/extract_it/extract_it.rb, line 222
def run
  extract_input
end
set_input(i = N) click to toggle source
#

set_input

We will work on an Array as value to @input, at the end of this method.

#
# File lib/extracter/extract_it/extract_it.rb, line 80
def set_input(i = N)
  case i
  # ======================================================================= #
  # === extract_it --help
  # ======================================================================= #
  when /-?-?help$/i # Show some help stuff here.
    e 'We will show a little bit help, then exit.'
    e
    e 'To extract .tar.xz, do:'
    e
    efancy '  -> tar -xJf *.tar.xz'
    exit
  end
  i = [i] if i.is_a? String # Need an Array.
  if @debug
    opn; e 'The input given to us is: `'+sfile(i)+'`'
  end
  if i.is_a?(Array) and i.empty?
    # ===================================================================== #
    # In this case, try to see if the current directory has a .zip
    # file. We will use this in that case.
    # ===================================================================== #
    is_there_a_zip_file = Dir['*.zip']
    unless is_there_a_zip_file.empty?
      use_this_zip_file = is_there_a_zip_file.first
      notify_the_user_that_no_input_was_given_but_this_file_was_found(use_this_zip_file)
      i << use_this_zip_file
    end
    is_there_at_the_least_one_tar_xz_file = Dir['*.tar.xz']
    unless is_there_at_the_least_one_tar_xz_file.empty?
      i << is_there_at_the_least_one_tar_xz_file.first
    end
  end
  @input = i # Should be an array, always.
end
show_commands_used()
Alias for: be_verbose
try_to_glob_on(i) click to toggle source
#

try_to_glob_on

#
# File lib/extracter/extract_it/extract_it.rb, line 201
def try_to_glob_on(i)
  result = Dir["#{i}*"]
  # ======================================================================= #
  # Next, sort this result to put archives on the beginning of the Array.
  # ======================================================================= #
  result = result.partition {|entry| is_archive?(entry) }
  result.flatten!
  unless result.empty?
    # ===================================================================== #
    # Ok, we grab the first entry next.
    # ===================================================================== #
    i = result.first
    opn; e "No result could be found for the given input, thus "\
           "using #{sfancy(i)} instead."
  end
  i
end