Utils::Cp: Deduplicate SystemCommand invocations

This commit is contained in:
Daiki Mizukami 2024-06-09 12:36:04 +09:00
parent 67f280eb53
commit eab1e87726
No known key found for this signature in database
GPG Key ID: 10478E598B944AA2
3 changed files with 48 additions and 43 deletions

View File

@ -5,45 +5,6 @@ module Utils
module Cp
class << self
module MacOSOverride
sig {
params(
source: T.any(String, Pathname, T::Array[T.any(String, Pathname)]),
target: T.any(String, Pathname),
force_command: T::Boolean,
sudo: T::Boolean,
verbose: T::Boolean,
command: T.class_of(SystemCommand),
).void
}
def with_attributes(source, target, force_command: false, sudo: false, verbose: false, command: SystemCommand)
if (flags = extra_flags)
command.run! "cp", args: ["-p", *flags, *source, target], sudo:, verbose:
nil
else
super
end
end
sig {
params(
source: T.any(String, Pathname, T::Array[T.any(String, Pathname)]),
target: T.any(String, Pathname),
force_command: T::Boolean,
sudo: T::Boolean,
verbose: T::Boolean,
command: T.class_of(SystemCommand),
).void
}
def recursive_with_attributes(source, target, force_command: false, sudo: false, verbose: false,
command: SystemCommand)
if (flags = extra_flags)
command.run! "cp", args: ["-pR", *flags, *source, target], sudo:, verbose:
nil
else
super
end
end
private
# Use the lightweight `clonefile(2)` syscall if applicable.

View File

@ -2,3 +2,23 @@
# This file contains temporary definitions for fixes that have
# been submitted upstream to https://github.com/sorbet/sorbet.
# https://github.com/sorbet/sorbet/pull/7959
module FileUtils
sig {
params(
src: T.any(File, String, Pathname, T::Array[T.any(File, String, Pathname)]),
dest: T.any(String, Pathname),
preserve: T.nilable(T::Boolean),
noop: T.nilable(T::Boolean),
verbose: T.nilable(T::Boolean),
dereference_root: T::Boolean,
remove_destination: T.nilable(T::Boolean),
).returns(T.nilable(T::Array[String]))
}
def self.cp_r(src, dest, preserve: nil, noop: nil, verbose: nil, dereference_root: true, remove_destination: nil)
# XXX: This comment is a placeholder to suppress `Style/EmptyMethod` lint.
# Simply compacting the method definition in a single line would in turn trigger
# `Layout/LineLength`, driving `brew style --fix` to an infinite loop.
end
end

View File

@ -9,9 +9,19 @@ module Utils
# Helper functions for copying files.
module Cp
class << self
sig {
params(
source: T.any(String, Pathname, T::Array[T.any(String, Pathname)]),
target: T.any(String, Pathname),
force_command: T::Boolean,
sudo: T::Boolean,
verbose: T::Boolean,
command: T.class_of(SystemCommand),
).void
}
def with_attributes(source, target, force_command: false, sudo: false, verbose: false, command: SystemCommand)
if force_command || sudo
command.run! "cp", args: ["-p", *source, target], sudo:, verbose:
if force_command || sudo || (flags = extra_flags)
command.run! "cp", args: ["-p", *flags, *source, target], sudo:, verbose:
else
FileUtils.cp source, target, preserve: true, verbose:
end
@ -19,16 +29,30 @@ module Utils
nil
end
sig {
params(
source: T.any(String, Pathname, T::Array[T.any(String, Pathname)]),
target: T.any(String, Pathname),
force_command: T::Boolean,
sudo: T::Boolean,
verbose: T::Boolean,
command: T.class_of(SystemCommand),
).void
}
def recursive_with_attributes(source, target, force_command: false, sudo: false, verbose: false,
command: SystemCommand)
if force_command || sudo
command.run! "cp", args: ["-pR", *source, target], sudo:, verbose:
if force_command || sudo || (flags = extra_flags)
command.run! "cp", args: ["-pR", *flags, *source, target], sudo:, verbose:
else
FileUtils.cp_r source, target, preserve: true, verbose:
end
nil
end
private
def extra_flags; end
end
end
end