Class: Pathname
- Inherits:
-
Object
- Object
- Pathname
- Includes:
- DiskUsageExtension, ELFShim, MachOShim
- Defined in:
- extend/pathname.rb,
extend/os/mac/extend/pathname.rb,
extend/os/linux/extend/pathname.rb
Overview
Homebrew extends Ruby’s Pathname
to make our code more readable.
Instance Method Summary collapse
-
#append_lines(content, **open_args) ⇒ void
Only appends to a file that is already created.
-
#atomic_write(content) ⇒ void
-
#binary_executable? ⇒ Boolean
-
#cd(&_block) ⇒ T.type_parameter(:U)
-
#ds_store? ⇒ Boolean
-
#dylib? ⇒ Boolean
-
#env_script_all_files(dst, env) ⇒ Object
Writes a wrapper env script and moves all files to the dst.
-
#extname ⇒ String
Extended to support common double extensions.
-
#install(*sources) ⇒ void
Moves a file from the original location to the Pathname’s.
-
#install_metafiles(from = Pathname.pwd) ⇒ Object
-
#install_symlink(*sources) ⇒ void
Creates symlinks to sources in this folder.
-
#mach_o_bundle? ⇒ Boolean
-
#rpaths ⇒ Array<String>
-
#sha256 ⇒ String
-
#stem ⇒ String
For filetypes we support, returns basename without extension.
-
#subdirs ⇒ Array<Pathname>
-
#verify_checksum(expected) ⇒ void
-
#write_env_script(target, args, env = nil) ⇒ Object
Writes an exec script that sets environment variables.
-
#write_exec_script(*targets) ⇒ Object
Writes an exec script in this folder for each target pathname.
-
#write_jar_script(target_jar, script_name, java_opts = "", java_version: nil) ⇒ Integer
Writes an exec script that invokes a Java jar.
Methods included from DiskUsageExtension
#abv, #disk_usage, #file_count
Methods included from MachOShim
#arch, #archs, #change_dylib_id, #change_install_name, #change_rpath, #delete_rpath, #dynamically_linked_libraries, #i386?, #mach_o_executable?, #ppc64?, #ppc7400?, #universal?, #x86_64?
Methods included from ELFShim
#arch, #dylib_id, #dynamic_elf?, #dynamically_linked_libraries, #elf?, #elf_type, #interpreter, #patch!, #patchelf_patcher, #read_uint16, #read_uint8, #rpath, #rpath_using_patchelf_rb, #save_using_patchelf_rb
Instance Method Details
#append_lines(content, **open_args) ⇒ void
This method returns an undefined value.
Only appends to a file that is already created.
170 171 172 173 174 |
# File 'extend/pathname.rb', line 170 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) ⇒ void
This always overwrites.
This method returns an undefined value.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'extend/pathname.rb', line 178 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
441 442 443 |
# File 'extend/pathname.rb', line 441 def binary_executable? false end |
#cd(&_block) ⇒ T.type_parameter(:U)
302 303 304 |
# File 'extend/pathname.rb', line 302 def cd(&_block) Dir.chdir(self) { yield self } end |
#ds_store? ⇒ Boolean
436 437 438 |
# File 'extend/pathname.rb', line 436 def ds_store? basename.to_s == ".DS_Store" end |
#dylib? ⇒ Boolean
451 452 453 |
# File 'extend/pathname.rb', line 451 def dylib? false end |
#env_script_all_files(dst, env) ⇒ Object
Writes a wrapper env script and moves all files to the dst.
389 390 391 392 393 394 395 396 397 398 |
# File 'extend/pathname.rb', line 389 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 ⇒ String
Extended to support common double extensions.
228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'extend/pathname.rb', line 228 def extname basename = File.basename(self) bottle_ext, = HOMEBREW_BOTTLES_EXTNAME_REGEX.match(basename).to_a return bottle_ext if bottle_ext archive_ext = basename[/(\.(tar|cpio|pax)\.(gz|bz2|lz|xz|zst|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) ⇒ void
This method returns an undefined value.
Moves a file from the original location to the Pathname’s.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'extend/pathname.rb', line 92 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
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'extend/pathname.rb', line 418 def (from = Pathname.pwd) Pathname(from).children.each do |p| next if p.directory? next if File.zero?(p) 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 |
#install_symlink(*sources) ⇒ void
This method returns an undefined value.
Creates symlinks to sources in this folder.
146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'extend/pathname.rb', line 146 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
446 447 448 |
# File 'extend/pathname.rb', line 446 def mach_o_bundle? false end |
#sha256 ⇒ String
282 283 284 285 |
# File 'extend/pathname.rb', line 282 def sha256 require "digest/sha2" Digest::SHA256.file(self).hexdigest end |
#stem ⇒ String
For filetypes we support, returns basename without extension.
245 246 247 |
# File 'extend/pathname.rb', line 245 def stem File.basename(self, extname) end |
#subdirs ⇒ Array<Pathname>
307 308 309 |
# File 'extend/pathname.rb', line 307 def subdirs children.select(&:directory?) end |
#verify_checksum(expected) ⇒ void
This method returns an undefined value.
288 289 290 291 292 293 |
# File 'extend/pathname.rb', line 288 def verify_checksum(expected) raise ChecksumMissingError if expected.blank? actual = Checksum.new(sha256.downcase) raise ChecksumMismatchError.new(self, expected, actual) unless expected == actual end |
#write_env_script(target, args, env = nil) ⇒ Object
Writes an exec script that sets environment variables.
374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'extend/pathname.rb', line 374 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} "$@" SH end |
#write_exec_script(*targets) ⇒ Object
Writes an exec script in this folder for each target pathname.
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'extend/pathname.rb', line 357 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) ⇒ Integer
Writes an exec script that invokes a Java jar.
409 410 411 412 413 414 415 416 |
# File 'extend/pathname.rb', line 409 def write_jar_script(target_jar, script_name, java_opts = "", java_version: nil) mkpath (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}" "$@" EOS end |