Module: Pkg::Util::File

Defined in:
lib/packaging/util/file.rb

Class Method Summary collapse

Class Method Details

.directories(dir) ⇒ Object

Returns an array of all the directories at the top level of #dir



28
29
30
31
32
33
34
# File 'lib/packaging/util/file.rb', line 28

def directories(dir)
  if File.directory?(dir)
    Dir.chdir(dir) do
      Dir.glob("*").select { |entry| File.directory?(entry) }
    end
  end
end

.directory?(file) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/packaging/util/file.rb', line 12

def directory?(file)
  ::File.directory?(file)
end

.empty_dir?(dir) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/packaging/util/file.rb', line 22

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



66
67
68
69
70
71
72
73
# File 'lib/packaging/util/file.rb', line 66

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



60
61
62
63
64
# File 'lib/packaging/util/file.rb', line 60

def erb_string(erbfile, b = binding)
  template = File.read(erbfile)
  message  = ERB.new(template, nil, "-")
  message.result(b)
end

.exist?(file) ⇒ Boolean Also known as: exists?

Returns:

  • (Boolean)


7
8
9
# File 'lib/packaging/util/file.rb', line 7

def exist?(file)
  ::File.exist?(file)
end

.file_exists?(file, args = { :required => false }) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
# File 'lib/packaging/util/file.rb', line 42

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

Returns:

  • (Boolean)


50
51
52
53
54
55
56
# File 'lib/packaging/util/file.rb', line 50

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

.files_with_ext(dir, ext) ⇒ Object

Returns an array of all files with #ext inside #dir



37
38
39
# File 'lib/packaging/util/file.rb', line 37

def files_with_ext(dir, ext)
  Dir.glob("#{dir}/**/*#{ext}")
end

.install_files_into_dir(file_patterns, workdir) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/packaging/util/file.rb', line 87

def install_files_into_dir(file_patterns, workdir)
  install = []
  # We need to add our list of file patterns from the configuration; this
  # used to be a list of "things to copy recursively", which would install
  # editor backup files and other nasty things.
  #
  # This handles that case correctly, with a deprecation warning, to augment
  # our FileList with the right things to put in place.
  #
  # Eventually, when all our projects are migrated to the new standard, we
  # can drop this in favour of just pushing the patterns directly into the
  # FileList and eliminate many lines of code and comment.
  Dir.chdir(Pkg::Config.project_root) do
    file_patterns.each do |pattern|
      if File.directory?(pattern) and !Pkg::Util::File.empty_dir?(pattern)
        install << Dir[pattern + "/**/*"]
      else
        install << Dir[pattern]
      end
    end
    install.flatten!

    # Transfer all the files and symlinks into the working directory...
    install = install.select { |x| File.file?(x) or File.symlink?(x) or Pkg::Util::File.empty_dir?(x) }

    install.each do |file|
      if Pkg::Util::File.empty_dir?(file)
        FileUtils.mkpath(File.join(workdir, file), :verbose => false)
      else
        FileUtils.mkpath(File.dirname(File.join(workdir, file)), :verbose => false)
        FileUtils.cp(file, File.join(workdir, file), :verbose => false, :preserve => true)
      end
    end
  end
  Pkg::Util::Version.versionbump(workdir) if Pkg::Config.update_version_file
end

.mktempObject Also known as: get_temp



16
17
18
19
20
# File 'lib/packaging/util/file.rb', line 16

def mktemp
  mktemp = Pkg::Util::Tool.find_tool('mktemp', :required => true)
  stdout, _, _ = Pkg::Util::Execution.capture3("#{mktemp} -d -t pkgXXXXXX")
  stdout.strip
end

.untar_into(source, target = nil, options = "") ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/packaging/util/file.rb', line 75

def untar_into(source, target = nil, options = "")
  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)
    stdout, _, _ = Pkg::Util::Execution.capture3(%Q(#{tar} #{options} #{target_opts} -xf #{source}))
    stdout
  end
end