Class: Pathname

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
DiskUsageExtension
Defined in:
Library/Homebrew/extend/pathname.rb,
Library/Homebrew/extend/os/mac/extend/pathname.rb,
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


185
186
187
188
189
# File 'Library/Homebrew/extend/pathname.rb', line 185

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

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

#atomic_write(content) ⇒ Object


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'Library/Homebrew/extend/pathname.rb', line 193

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)

454
455
456
# File 'Library/Homebrew/extend/pathname.rb', line 454

def binary_executable?
  false
end

#cd(&_block) ⇒ Object


317
318
319
# File 'Library/Homebrew/extend/pathname.rb', line 317

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

#ds_store?Boolean

Returns:

  • (Boolean)

449
450
451
# File 'Library/Homebrew/extend/pathname.rb', line 449

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

#dylib?Boolean

Returns:

  • (Boolean)

464
465
466
# File 'Library/Homebrew/extend/pathname.rb', line 464

def dylib?
  false
end

#env_script_all_files(dst, env) ⇒ Object

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


404
405
406
407
408
409
410
411
412
413
# File 'Library/Homebrew/extend/pathname.rb', line 404

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

#extnameObject


243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'Library/Homebrew/extend/pathname.rb', line 243

def extname
  basename = File.basename(self)

  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


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'Library/Homebrew/extend/pathname.rb', line 94

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


432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'Library/Homebrew/extend/pathname.rb', line 432

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

148
149
150
151
152
153
154
155
156
157
158
159
# File 'Library/Homebrew/extend/pathname.rb', line 148

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)

459
460
461
# File 'Library/Homebrew/extend/pathname.rb', line 459

def mach_o_bundle?
  false
end

#resolved_pathObject


328
329
330
# File 'Library/Homebrew/extend/pathname.rb', line 328

def resolved_path
  symlink? ? dirname.join(readlink) : self
end

#resolved_path_exists?Boolean

Returns:

  • (Boolean)

334
335
336
337
338
339
340
341
# File 'Library/Homebrew/extend/pathname.rb', line 334

def resolved_path_exists?
  link = readlink
rescue ArgumentError
  # The link target contains NUL bytes
  false
else
  dirname.join(link).exist?
end

#rmdir_if_possibleObject


269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'Library/Homebrew/extend/pathname.rb', line 269

def rmdir_if_possible
  rmdir
  true
rescue Errno::ENOTEMPTY
  if (ds_store = join(".DS_Store")).exist? && children.length == 1
    ds_store.unlink
    retry
  else
    false
  end
rescue Errno::EACCES, Errno::ENOENT, Errno::EBUSY
  false
end

#sha256Object


297
298
299
300
# File 'Library/Homebrew/extend/pathname.rb', line 297

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

#stemObject


260
261
262
# File 'Library/Homebrew/extend/pathname.rb', line 260

def stem
  File.basename(self, extname)
end

#subdirsObject


322
323
324
# File 'Library/Homebrew/extend/pathname.rb', line 322

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

#text_executable?Boolean

Returns:

  • (Boolean)

292
293
294
# File 'Library/Homebrew/extend/pathname.rb', line 292

def text_executable?
  /^#!\s*\S+/.match?(open("r") { |f| f.read(1024) })
end

#verify_checksum(expected) ⇒ Object


303
304
305
306
307
308
# File 'Library/Homebrew/extend/pathname.rb', line 303

def verify_checksum(expected)
  raise ChecksumMissingError if expected.blank?

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

#versionObject


285
286
287
288
# File 'Library/Homebrew/extend/pathname.rb', line 285

def version
  require "version"
  Version.parse(basename)
end

#write(content, offset = 0, open_args = {}) ⇒ Object


175
176
177
178
179
180
181
# File 'Library/Homebrew/extend/pathname.rb', line 175

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

  dirname.mkpath

  old_write(content, offset, open_args)
end

#write_env_script(target, args, env = nil) ⇒ Object

Writes an exec script that sets environment variables.


389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'Library/Homebrew/extend/pathname.rb', line 389

def write_env_script(target, args, env = nil)
  unless env
    env = args
    args = nil
  end
  env_export = +""
  env.each { |key, value| env_export << "#{key}=\"#{value}\" " }
  dirname.mkpath
  write <<~SH
    #!/bin/bash
    #{env_export}exec "#{target}" #{args} "[email protected]"
  SH
end

#write_exec_script(*targets) ⇒ Object

Writes an exec script in this folder for each target pathname.


372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'Library/Homebrew/extend/pathname.rb', line 372

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}" "[email protected]"
    SH
  end
end

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


424
425
426
427
428
429
430
# File 'Library/Homebrew/extend/pathname.rb', line 424

def write_jar_script(target_jar, script_name, java_opts = "", java_version: nil)
  (self/script_name).write <<~EOS
    #!/bin/bash
    export JAVA_HOME="#{Language::Java.overridable_java_home_env(java_version)[:JAVA_HOME]}"
    exec "${JAVA_HOME}/bin/java" #{java_opts} -jar "#{target_jar}" "[email protected]"
  EOS
end