Class: Pathname

Inherits:
Object
  • Object
show all
Includes:
DiskUsageExtension
Defined in:
brew/Library/Homebrew/extend/pathname.rb,
brew/Library/Homebrew/extend/os/mac/extend/pathname.rb,
brew/Library/Homebrew/extend/os/linux/extend/pathname.rb

Overview

Homebrew extends Ruby’s Pathname to make our code more readable.

Instance Method Summary collapse

Methods included from DiskUsageExtension

#abv, #disk_usage, #file_count

Instance Method Details

#append_lines(content, *open_args) ⇒ Object

Only appends to a file that is already created.



161
162
163
164
165
# File 'brew/Library/Homebrew/extend/pathname.rb', line 161

def append_lines(content, *open_args)
  raise "Cannot append file that doesn't exist: #{self}" unless exist?

  open("a", *open_args) { |f| f.puts(content) }
end

#atomic_write(content) ⇒ Object

NOTE: This always overwrites.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'brew/Library/Homebrew/extend/pathname.rb', line 168

def atomic_write(content)
  old_stat = stat if exist?
  File.atomic_write(self) do |file|
    file.write(content)
  end

  return unless old_stat

  # Try to restore original file's permissions separately
  # atomic_write does it itself, but it actually erases
  # them if chown fails
  begin
    # Set correct permissions on new file
    chown(old_stat.uid, nil)
    chown(nil, old_stat.gid)
  rescue Errno::EPERM, Errno::EACCES
    # Changing file ownership failed, moving on.
    nil
  end
  begin
    # This operation will affect filesystem ACL's
    chmod(old_stat.mode)
  rescue Errno::EPERM, Errno::EACCES
    # Changing file permissions failed, moving on.
    nil
  end
end

#binary_executable?Boolean

Returns:

  • (Boolean)


410
411
412
# File 'brew/Library/Homebrew/extend/pathname.rb', line 410

def binary_executable?
  false
end

#cdObject



280
281
282
# File 'brew/Library/Homebrew/extend/pathname.rb', line 280

def cd
  Dir.chdir(self) { yield self }
end

#ds_store?Boolean

Returns:

  • (Boolean)


397
398
399
# File 'brew/Library/Homebrew/extend/pathname.rb', line 397

def ds_store?
  basename.to_s == ".DS_Store"
end

#dylib?Boolean

Returns:

  • (Boolean)


418
419
420
# File 'brew/Library/Homebrew/extend/pathname.rb', line 418

def dylib?
  false
end

#env_script_all_files(dst, env) ⇒ Object

Writes a wrapper env script and moves all files to the dst



360
361
362
363
364
365
366
367
368
369
# File 'brew/Library/Homebrew/extend/pathname.rb', line 360

def env_script_all_files(dst, env)
  dst.mkpath
  Pathname.glob("#{self}/*") do |file|
    next if file.directory?

    dst.install(file)
    new_file = dst.join(file.basename)
    file.write_env_script(new_file, env)
  end
end

#extname(path = to_s) ⇒ Object

Extended to support common double extensions



217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'brew/Library/Homebrew/extend/pathname.rb', line 217

def extname(path = to_s)
  basename = File.basename(path)

  bottle_ext = basename[BOTTLE_EXTNAME_RX, 1]
  return bottle_ext if bottle_ext

  archive_ext = basename[/(\.(tar|cpio|pax)\.(gz|bz2|lz|xz|Z))\Z/, 1]
  return archive_ext if archive_ext

  # Don't treat version numbers as extname.
  return "" if basename.match?(/\b\d+\.\d+[^\.]*\Z/) && !basename.end_with?(".7z")

  File.extname(basename)
end

#install(*sources) ⇒ Object

Moves a file from the original location to the Pathname’s.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'brew/Library/Homebrew/extend/pathname.rb', line 79

def install(*sources)
  sources.each do |src|
    case src
    when Resource
      src.stage(self)
    when Resource::Partial
      src.resource.stage { install(*src.files) }
    when Array
      if src.empty?
        opoo "tried to install empty array to #{self}"
        break
      end
      src.each { |s| install_p(s, File.basename(s)) }
    when Hash
      if src.empty?
        opoo "tried to install empty hash to #{self}"
        break
      end
      src.each { |s, new_basename| install_p(s, new_basename) }
    else
      install_p(src, File.basename(src))
    end
  end
end

#install_metafiles(from = Pathname.pwd) ⇒ Object



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'brew/Library/Homebrew/extend/pathname.rb', line 381

def install_metafiles(from = Pathname.pwd)
  Pathname(from).children.each do |p|
    next if p.directory?
    next unless Metafiles.copy?(p.basename.to_s)

    # Some software symlinks these files (see help2man.rb)
    filename = p.resolved_path
    # Some software links metafiles together, so by the time we iterate to one of them
    # we may have already moved it. libxml2's COPYING and Copyright are affected by this.
    next unless filename.exist?

    filename.chmod 0644
    install(filename)
  end
end

Creates symlinks to sources in this folder.



127
128
129
130
131
132
133
134
135
136
137
138
# File 'brew/Library/Homebrew/extend/pathname.rb', line 127

def install_symlink(*sources)
  sources.each do |src|
    case src
    when Array
      src.each { |s| install_symlink_p(s, File.basename(s)) }
    when Hash
      src.each { |s, new_basename| install_symlink_p(s, new_basename) }
    else
      install_symlink_p(src, File.basename(src))
    end
  end
end

#mach_o_bundle?Boolean

Returns:

  • (Boolean)


414
415
416
# File 'brew/Library/Homebrew/extend/pathname.rb', line 414

def mach_o_bundle?
  false
end

#sha256Object



266
267
268
269
# File 'brew/Library/Homebrew/extend/pathname.rb', line 266

def sha256
  require "digest/sha2"
  Digest::SHA256.file(self).hexdigest
end

#stemObject

For filetypes we support, basename without extension



233
234
235
# File 'brew/Library/Homebrew/extend/pathname.rb', line 233

def stem
  File.basename((path = to_s), extname(path))
end

#subdirsObject



284
285
286
# File 'brew/Library/Homebrew/extend/pathname.rb', line 284

def subdirs
  children.select(&:directory?)
end

#verify_checksum(expected) ⇒ Object



271
272
273
274
275
276
# File 'brew/Library/Homebrew/extend/pathname.rb', line 271

def verify_checksum(expected)
  raise ChecksumMissingError if expected.nil? || expected.empty?

  actual = Checksum.new(expected.hash_type, send(expected.hash_type).downcase)
  raise ChecksumMismatchError.new(self, expected, actual) unless expected == actual
end

#write(content, *open_args) ⇒ Object

We assume this pathname object is a file, obviously



153
154
155
156
157
158
# File 'brew/Library/Homebrew/extend/pathname.rb', line 153

def write(content, *open_args)
  raise "Will not overwrite #{self}" if exist?

  dirname.mkpath
  open("w", *open_args) { |f| f.write(content) }
end

#write_env_script(target, env) ⇒ Object

Writes an exec script that sets environment variables



349
350
351
352
353
354
355
356
357
# File 'brew/Library/Homebrew/extend/pathname.rb', line 349

def write_env_script(target, env)
  env_export = +""
  env.each { |key, value| env_export << "#{key}=\"#{value}\" " }
  dirname.mkpath
  write <<~SH
    #!/bin/bash
    #{env_export}exec "#{target}" "$@"
  SH
end

#write_exec_script(*targets) ⇒ Object

Writes an exec script in this folder for each target pathname



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'brew/Library/Homebrew/extend/pathname.rb', line 332

def write_exec_script(*targets)
  targets.flatten!
  if targets.empty?
    opoo "tried to write exec scripts to #{self} for an empty list of targets"
    return
  end
  mkpath
  targets.each do |target|
    target = Pathname.new(target) # allow pathnames or strings
    join(target.basename).write <<~SH
      #!/bin/bash
      exec "#{target}" "$@"
    SH
  end
end

#write_jar_script(target_jar, script_name, java_opts = "", java_version: nil) ⇒ Object

Writes an exec script that invokes a Java jar



372
373
374
375
376
377
378
379
# File 'brew/Library/Homebrew/extend/pathname.rb', line 372

def write_jar_script(target_jar, script_name, java_opts = "", java_version: nil)
  mkpath
  java_home = ("JAVA_HOME=\"#{Language::Java.java_home_shell(java_version)}\" " if java_version)
  join(script_name).write <<~SH
    #!/bin/bash
    #{java_home}exec java #{java_opts} -jar #{target_jar} "$@"
  SH
end