Class: Extracter::ExtractIt

Inherits:
Base
  • Object
show all
Defined in:
lib/extracter/extract_it/extract_it.rb

Overview

Extracter::ExtractIt

Constant Summary collapse

NAMESPACE =
#

ExtractIt::NAMESPACE

#
inspect
ARRAY_ARCHIVE_TYPES =
#

ExtractIt::ARRAY_ARCHIVE_TYPES

Register the available (and handled) archive types here.

#
::Extracter::ARRAY_REGISTERED_ARCHIVES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#change_directory, #check_whether_the_colours_gem_is_available, #cpr, #cyan?, #e, #ewarn, #move, #remove_this_directory, #rev, #sdir, #sfancy, #sfile, #simp, #steelblue, #tomato, #use_colours?

Constructor Details

#initialize(optional_set_input = nil, run_already = true) ⇒ ExtractIt

#

initialize

#


42
43
44
45
46
47
48
49
# File 'lib/extracter/extract_it/extract_it.rb', line 42

def initialize(
    optional_set_input = nil,
    run_already        = true
  )
  reset
  set_input(optional_set_input)
  run if run_already
end

Class Method Details

.[](i = '') ⇒ Object

#

Extracter::ExtractIt[]

#


224
225
226
# File 'lib/extracter/extract_it/extract_it.rb', line 224

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

Instance Method Details

#be_silentObject

#

be_silent

#


114
115
116
# File 'lib/extracter/extract_it/extract_it.rb', line 114

def be_silent
  @be_silent = true
end

#be_verboseObject Also known as: show_commands_used

#

be_verbose

#


121
122
123
# File 'lib/extracter/extract_it/extract_it.rb', line 121

def be_verbose
  @be_silent = false
end

#extract_inputObject

#

extract_input

#


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/extracter/extract_it/extract_it.rb', line 162

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_verbose: @be_silent }}
    _ = ::Extracter.remove_file_suffix(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 "#{rev}Finished extracting "\
               "#{sfile(_)}#{rev} to `#{sdir(to_this_dir)}#{rev}`."
      else
        opn; e "#{rev}No file called `#{sfile(name_of_the_extracted_archive)}"\
               "#{rev}` appears to exist."
      end
    end
  }
end

#is_archive?(i) ⇒ Boolean

#

is_archive?

#

Returns:

  • (Boolean)


208
209
210
211
212
# File 'lib/extracter/extract_it/extract_it.rb', line 208

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

#

notify_the_user_that_no_input_was_given_but_this_file_was_found

#


128
129
130
131
132
133
134
135
136
# File 'lib/extracter/extract_it/extract_it.rb', line 128

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

#resetObject

#

reset (reset tag)

#


54
55
56
57
58
59
60
# File 'lib/extracter/extract_it/extract_it.rb', line 54

def reset
  super()
  # ======================================================================= #
  # === @debug
  # ======================================================================= #
  @debug = false
end

#runObject

#

run (run tag)

#


217
218
219
# File 'lib/extracter/extract_it/extract_it.rb', line 217

def run
  extract_input
end

#set_input(i = N) ⇒ Object

#

set_input

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

#


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/extracter/extract_it/extract_it.rb', line 79

def set_input(i = N)
  case i
  # ======================================================================= #
  # === extract_it --help
  # ======================================================================= #
  when /-?-?help$/i # Show some help stuff here.
    show_help
    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_helpObject

#

show_help

#


65
66
67
68
69
70
71
# File 'lib/extracter/extract_it/extract_it.rb', line 65

def show_help
  e 'We will show a little bit help, then exit.'
  e
  e 'To extract .tar.xz, do:'
  e
  efancy '  → tar -xJf *.tar.xz'
end

#try_to_glob_on(i) ⇒ Object

#

try_to_glob_on

#


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/extracter/extract_it/extract_it.rb', line 141

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 "#{rev}No result could be found for the given input, "\
           "thus using #{sfancy(i)} #{rev}instead."
  end
  i
end