Module: Pkg::Util::File
- Defined in:
- ext/packaging/lib/packaging/util/file.rb
Overview
Utility methods for handling files and directories
Class Method Summary collapse
- .empty_dir?(dir) ⇒ Boolean
- .erb_file(erbfile, outfile = nil, remove_orig = false, opts = { :binding => binding }) ⇒ Object
- .erb_string(erbfile, b = binding) ⇒ Object
- .file_exists?(file, args = {:required => false}) ⇒ Boolean
- .file_writable?(file, args = {:required => false}) ⇒ Boolean
- .mktemp ⇒ Object (also: get_temp)
- .untar_into(source, target = nil, options = "") ⇒ Object
Class Method Details
.empty_dir?(dir) ⇒ Boolean
11 12 13 |
# File 'ext/packaging/lib/packaging/util/file.rb', line 11 def empty_dir?(dir) File.exist?(dir) and File.directory?(dir) and Dir["#{dir}/**/*"].empty? end |
.erb_file(erbfile, outfile = nil, remove_orig = false, opts = { :binding => binding }) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'ext/packaging/lib/packaging/util/file.rb', line 39 def erb_file(erbfile, outfile=nil, remove_orig = false, opts = { :binding => binding }) outfile ||= File.join(mktemp, File.basename(erbfile).sub(File.extname(erbfile),"")) output = erb_string(erbfile, opts[:binding]) File.open(outfile, 'w') { |f| f.write output } puts "Generated: #{outfile}" FileUtils.rm_rf erbfile if remove_orig outfile end |
.erb_string(erbfile, b = binding) ⇒ Object
33 34 35 36 37 |
# File 'ext/packaging/lib/packaging/util/file.rb', line 33 def erb_string(erbfile, b = binding) template = File.read(erbfile) = ERB.new(template, nil, "-") .result(b) end |
.file_exists?(file, args = {:required => false}) ⇒ Boolean
15 16 17 18 19 20 21 |
# File 'ext/packaging/lib/packaging/util/file.rb', line 15 def file_exists?(file, args={:required => false}) exists = File.exist? file if !exists and args[:required] fail "Required file #{file} could not be found" end exists end |
.file_writable?(file, args = {:required => false}) ⇒ Boolean
23 24 25 26 27 28 29 |
# File 'ext/packaging/lib/packaging/util/file.rb', line 23 def file_writable?(file, args={:required => false}) writable = File.writable? file if !writable and args[:required] fail "File #{file} is not writable" end writable end |
.mktemp ⇒ Object Also known as: get_temp
6 7 8 9 |
# File 'ext/packaging/lib/packaging/util/file.rb', line 6 def mktemp mktemp = Pkg::Util::Tool.find_tool('mktemp', :required => true) `#{mktemp} -d -t pkgXXXXXX`.strip end |
.untar_into(source, target = nil, options = "") ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'ext/packaging/lib/packaging/util/file.rb', line 48 def untar_into(source, target = nil, = "") tar = Pkg::Util::Tool.find_tool('tar', :required => true) # We only accept a writable directory as a target if target and !target.empty? and file_writable?(target) and File.directory?(target) target_opts = "-C #{target}" end if file_exists?(source, :required => true) ex(%Q[#{tar} #{options} #{target_opts} -xf #{source}]) end end |