Module: ShellHelpers::PathnameExt::ActionHandler

Extended by:
FUClass
Included in:
ShellHelpers::PathnameExt
Defined in:
lib/shell_helpers/pathname.rb

Defined Under Namespace

Classes: PathnameError

Constant Summary collapse

RemoveError =
Class.new(PathnameError)
FSError =
Class.new(PathnameError)

Instance Attribute Summary

Attributes included from FUClass

#fu_class

Instance Method Summary collapse

Instance Method Details

#do_action?(mode: :all, dereference: false, **others) ⇒ Boolean (protected)

Returns:

  • (Boolean)


453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/shell_helpers/pathname.rb', line 453

protected def do_action?(mode: :all, dereference: false, **others)
  path=self.dereference(dereference)
  case mode
  when :none, false
    return false
  when :noclobber
    return false if path.may_exist?
  when :symlink
    return false unless path.symlink?
  when :dangling_symlink
    return false unless path.symlink? && ! self.exist?
  when :file
    return false if path.directory?
  when :dir
    return false unless path.directory?
  end
  true
end

#on_rm(recursive: false, mode: :all, dereference: false, rescue_error: true, **others) ⇒ Object



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/shell_helpers/pathname.rb', line 473

def on_rm(recursive: false, mode: :all, dereference: false, rescue_error: true, **others)
  path=self.dereference(dereference)
  return nil unless path.may_exist?
  if path.do_action?(mode: mode)
    fuopts=others.select {|k,v| [:verbose,:noop,:force].include?(k)}
    if recursive
      #this is only called if both recursive=true and mode=:all or :dir
      logger.debug("rm_r #{self} (#{path}) #{fuopts}") if respond_to?(:logger)
      self.class.fu_class.rm_r(path, **fuopts)
    else
      logger.debug("rm #{self} (#{path}) #{fuopts}") if respond_to?(:logger)
      self.class.fu_class.rm(path, **fuopts)
    end
  else
    puts "\# #{__method__}: Skip #{self} [mode=#{mode}]" if others[:verbose]
  end
rescue => e
  warn "Error in #{path}.#{__method__}: #{e}"
  raise RemoveError.new(e) unless rescue_error
end

#on_rm_r(**opts) ⇒ Object



493
494
495
# File 'lib/shell_helpers/pathname.rb', line 493

def on_rm_r(**opts)
  on_rm(recursive:true,**opts)
end

#on_rm_rf(**opts) ⇒ Object



496
497
498
# File 'lib/shell_helpers/pathname.rb', line 496

def on_rm_rf(**opts)
  on_rm(recursive:true,force:true,**opts)
end

#rel_ln_s(target) ⇒ Object

add the relative path to target in the symlink Pathname.new("foo/bar").rel_ln_s(Pathname.new("baz/toto")) makes a symlink foo/bar -> ../baz/toto this is similar to 'ln -rs ...' from coreutils



568
569
570
# File 'lib/shell_helpers/pathname.rb', line 568

def rel_ln_s(target)
  on_ln_s(rel_path_to(target))
end

#squel(target, base: self.class.pwd, action: nil, rel_path_opts: {}, mkpath: false, **opts) {|out, rel_path, target: target, orig: self, **opts| ... } ⇒ Object

Pathname.new("foo").squel("bar/baz", action: :on_ln_s) will create a symlink foo/bar/baz -> ../../bar/baz

Yields:

  • (out, rel_path, target: target, orig: self, **opts)


539
540
541
542
543
544
545
546
547
# File 'lib/shell_helpers/pathname.rb', line 539

def squel(target, base: self.class.pwd, action: nil, rel_path_opts: {}, mkpath: false, **opts)
  target=self.class.new(target)
  out=self+base.rel_path_to(target, inside: true)
  out.dirname.mkpath if mkpath
  rel_path=out.rel_path_to(target, **rel_path_opts)
  #rel_path is the path from out to target
  out.public_send(action, rel_path,**opts) if action
  yield(out,rel_path, target: target, orig: self, **opts) if block_given?
end

#squel_dir(target, action: nil, **opts) ⇒ Object



549
550
551
552
553
554
555
556
557
# File 'lib/shell_helpers/pathname.rb', line 549

def squel_dir(target, action: nil, **opts)
  target=self.class.new(target)
  target.find do |file|
    squel(file,mkpath: opts.fetch(:mkpath,!!action), **opts) do |out,rel_path|
      out.public_send(action, rel_path,**opts) if action and !file.directory?
      yield(out,rel_path, target: file, squel_target: target, orig: self, **opts) if block_given?
    end
  end
end