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