Module: AgileUtils::FileUtil

Defined in:
lib/agile_utils/file_util.rb

Constant Summary collapse

CustomError =
Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.delete(files) ⇒ Object

Delete the files from the given list

Parameters:

  • files

    list of files to be deleted



57
58
59
60
61
# File 'lib/agile_utils/file_util.rb', line 57

def delete(files)
  files.each do |file|
    FileUtils.rm_rf(file)
  end
end

.find(base_dir, extension = "xhtml") ⇒ Array<String>

Find list of files based on certain extension

rubocop:disable CollectionMethods

Parameters:

  • base_dir (String)

    the starting directory

  • extension (String) (defaults to: "xhtml")

    the file extension to search for

Returns:

  • (Array<String>)

    list of matching files or empty list



19
20
21
22
23
24
25
# File 'lib/agile_utils/file_util.rb', line 19

def find(base_dir, extension = "xhtml")
  file_paths = []
  Find.find(base_dir) do |path|
    file_paths << path if path =~ /.*\.#{extension}$/
  end
  file_paths
end

.gunzip(filename, output_dir) ⇒ Object

Uncompress ‘input.tar.gz’ file

Parameters:

  • filename (String)

    input file in the ‘tar.gzip’ format

  • output_dir (String)

    the output directory



48
49
50
51
52
# File 'lib/agile_utils/file_util.rb', line 48

def gunzip(filename, output_dir)
  input_file = File.open(filename, "rb")
  tgz = Zlib::GzipReader.new(input_file)
  Archive::Tar::Minitar.unpack(tgz, output_dir)
end

.tar_gzip_files(files, output = "output.tar.gz") ⇒ Object

TODO:

rename to tar_gzip(..)

Tar and gzip list of files

Parameters:

  • files (Array<String>)

    list of input files

  • output (String) (defaults to: "output.tar.gz")

    the output file in .tar.gz format



33
34
35
36
37
38
39
40
41
42
# File 'lib/agile_utils/file_util.rb', line 33

def tar_gzip_files(files, output = "output.tar.gz")
  sgz = Zlib::GzipWriter.new(File.open(output, "wb"))
  tar = Archive::Tar::Minitar::Output.new(sgz)
  files.each do |file|
    Archive::Tar::Minitar.pack_file(file, tar)
  end
ensure
  tar.close unless tar.nil?
  tar = nil
end

.timeObject

Time the operation before and after the operation for tuning purpose



64
65
66
67
68
69
# File 'lib/agile_utils/file_util.rb', line 64

def time
  beg_time = Time.now
  yield
  end_time = Time.now
  end_time - beg_time
end