Class: ExtractIt

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

Overview

#

require 'extract_it/version/version.rb'

#

Constant Summary collapse

NAMESPACE =
#

NAMESPACE

#
inspect
ARRAY_ARCHIVE_TYPES =
#

ExtractIt::ARRAY_ARCHIVE_TYPES

Register the available (and handled) archive types here.

#
%w(
  .xz
  .zip
  .tar
  .bz2
  .7z
)
VERSION =
#

ExtractIt::VERSION

#
'0.1.1'

Instance Method Summary collapse

Constructor Details

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

#

initialize

#

48
49
50
51
52
53
54
55
# File 'lib/extract_it/extract_it.rb', line 48

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

Instance Method Details

#be_silentObject

#

be_silent

#

107
108
109
# File 'lib/extract_it/extract_it.rb', line 107

def be_silent
  @be_silent = true
end

#be_verboseObject Also known as: show_commands_used

#

be_verbose

#

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

def be_verbose
  @be_silent = false
end

#extract_inputObject

#

extract_input

#

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/extract_it/extract_it.rb', line 134

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

#

is_archive?

#

Returns:

  • (Boolean)

180
181
182
# File 'lib/extract_it/extract_it.rb', line 180

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

#

121
122
123
124
125
126
127
128
129
# File 'lib/extract_it/extract_it.rb', line 121

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)

#

60
61
62
# File 'lib/extract_it/extract_it.rb', line 60

def reset
  @debug = false
end

#runObject

#

run (run tag)

#

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

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.

#

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/extract_it/extract_it.rb', line 70

def set_input(i = N)
  case i
  # ======================================================================= #
  # === extract_it --help
  # ======================================================================= #
  when /-?-?help/,'HELP' # Show some help stuff here.
    e 'We will show a little bit help, then exit.'
    e 'To extract .tar.xz, do:'
    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

#try_to_glob_on(i) ⇒ Object

#

try_to_glob_on

#

187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/extract_it/extract_it.rb', line 187

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