Class: Dir
- Inherits:
-
Object
- Object
- Dir
- Defined in:
- lib/base/dir.rb
Class Method Summary collapse
- .copy_files(src_dir, glob_pattern, exclude_patterns, target_dir) ⇒ Object
- .empty?(directory) ⇒ Boolean
- .get_latest_mtime(directory) ⇒ Object
- .get_project_name(directory) ⇒ Object
- .make(directory) ⇒ Object
- .remove(directory, remove_empty_parents = false) ⇒ Object
- .unzip(zipfilename, directory) ⇒ Object
- .zip(directory, files, zipfilename) ⇒ Object
Class Method Details
.copy_files(src_dir, glob_pattern, exclude_patterns, target_dir) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/base/dir.rb', line 92 def self.copy_files(src_dir, glob_pattern, exclude_patterns, target_dir) if Dir.exist?(src_dir) Dir.chdir(src_dir) do Dir.glob(glob_pattern).each do |f| next unless File.file?(f) exclude = false exclude_patterns&.each do |p| exclude = true if f.include?(p) end next if exclude dest = "#{target_dir}/#{f}" FileUtils.mkdir_p(File.dirname(dest)) unless Dir.exist?(File.dirname(dest)) FileUtils.cp(f, dest) end end end end |
.empty?(directory) ⇒ Boolean
20 21 22 23 24 |
# File 'lib/base/dir.rb', line 20 def self.empty?(directory) return true if (Dir.entries(directory) - %w[. ..]).empty? false end |
.get_latest_mtime(directory) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/base/dir.rb', line 26 def self.get_latest_mtime(directory) mtime = Time.new(1980) Dir.chdir(directory) do latest_filename = "" Dir.glob("**/*.*").each do |f| if mtime.nil? || File.mtime(f) > mtime mtime = File.mtime(f) latest_filename = f end end puts " latest_mtime #{mtime} #{latest_filename}" if Environment.default.debug? end mtime end |
.get_project_name(directory) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/base/dir.rb', line 41 def self.get_project_name(directory) name = directory.split("/").last rakefile = "#{directory}/rakefile.rb" if File.exist?(rakefile) txt = IO.read(rakefile) if txt.include?("NAME=") scan = txt.scan(/NAME=['"]([\w.]+)/) name = scan[0][0] if !scan.nil? && (scan.length.positive? && !scan[0].nil? && scan[0].length.positive?) end end name end |
.make(directory) ⇒ Object
6 7 8 |
# File 'lib/base/dir.rb', line 6 def self.make(directory) FileUtils.mkdir_p directory unless File.exist? directory end |
.remove(directory, remove_empty_parents = false) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/base/dir.rb', line 10 def self.remove(directory, remove_empty_parents = false) FileUtils.rm_rf directory unless Dir.empty?(directory) FileUtils.rm_r directory if File.exist?(directory) if remove_empty_parents parent_dir = File.dirname(directory) Dir.remove parent_dir, true if Dir.empty?(parent_dir) end rescue StandardError end |
.unzip(zipfilename, directory) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/base/dir.rb', line 73 def self.unzip(zipfilename, directory) if Gem::Specification.find_all_by_name("rubyzip").any? require "zip" count = 0 Zip::File.open(zipfilename) do |zip_file| zip_file.each do |entry| dest = "#{directory}/#{entry}" parent_dir = File.dirname(dest) FileUtils.mkdir_p parent_dir unless Dir.exist?(parent_dir) entry.extract("#{directory}/#{entry}") count += 1 end end puts "extracted #{count} files to #{directory}" else puts "rubyzip gem is not installed 'gem install rubyzip'" end end |
.zip(directory, files, zipfilename) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/base/dir.rb', line 54 def self.zip(directory, files, zipfilename) if Gem::Specification.find_all_by_name("rubyzip").any? require "zip" File.delete(zipfilename) if File.exist?(zipfilename) Zip::File.open(zipfilename, Zip::File::CREATE) do |zipfile| Dir.chdir(directory) do count = 0 files.each do |source_file| zipfile.add(source_file, "#{directory}/#{source_file}") count += 1 end puts "added #{count} files to #{zipfilename}" end end else puts "rubyzip gem is not installed 'gem install rubyzip'" end end |