2024-05-21 20:18:58 +09:00
|
|
|
# typed: true
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-06-11 07:53:47 +09:00
|
|
|
require "extend/os/copy"
|
2024-06-08 19:39:56 +09:00
|
|
|
require "fileutils"
|
2024-05-21 20:18:58 +09:00
|
|
|
require "system_command"
|
|
|
|
|
|
|
|
module Utils
|
2024-06-08 19:39:56 +09:00
|
|
|
# Helper functions for copying files.
|
2024-06-11 07:53:47 +09:00
|
|
|
module Copy
|
2024-05-21 20:18:58 +09:00
|
|
|
class << self
|
2024-06-09 12:36:04 +09:00
|
|
|
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
|
|
|
|
}
|
2024-06-09 05:54:36 +09:00
|
|
|
def with_attributes(source, target, force_command: false, sudo: false, verbose: false, command: SystemCommand)
|
2024-06-09 12:36:04 +09:00
|
|
|
if force_command || sudo || (flags = extra_flags)
|
|
|
|
command.run! "cp", args: ["-p", *flags, *source, target], sudo:, verbose:
|
2024-06-09 05:54:36 +09:00
|
|
|
else
|
|
|
|
FileUtils.cp source, target, preserve: true, verbose:
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
2024-05-21 20:18:58 +09:00
|
|
|
end
|
|
|
|
|
2024-06-09 12:36:04 +09:00
|
|
|
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
|
|
|
|
}
|
2024-06-09 05:54:36 +09:00
|
|
|
def recursive_with_attributes(source, target, force_command: false, sudo: false, verbose: false,
|
|
|
|
command: SystemCommand)
|
2024-06-09 12:36:04 +09:00
|
|
|
if force_command || sudo || (flags = extra_flags)
|
|
|
|
command.run! "cp", args: ["-pR", *flags, *source, target], sudo:, verbose:
|
2024-06-09 05:54:36 +09:00
|
|
|
else
|
|
|
|
FileUtils.cp_r source, target, preserve: true, verbose:
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
2024-05-21 20:18:58 +09:00
|
|
|
end
|
2024-06-09 12:36:04 +09:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def extra_flags; end
|
2024-05-21 20:18:58 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|