Module: Squared::Common::System

Defined in:
lib/squared/common/system.rb

Class Method Summary collapse

Class Method Details

.copy_d(src, dest, glob: ['**/*'], pass: nil, create: false, verbose: true) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/squared/common/system.rb', line 23

def copy_d(src, dest, glob: ['**/*'], pass: nil, create: false, verbose: true)
  src = ::Pathname.new(src)
  dest = ::Pathname.new(dest)
  raise "#{dest} (not found)" if !create && !dest.parent.exist?

  subdir = []
  files = 0
  dest.mkpath if create
  if pass
    exclude = []
    (pass.is_a?(::Array) ? pass : [pass]).each { |val| exclude += ::Dir.glob(src.join(val)) }
  end
  (glob.is_a?(::Array) ? glob : [glob]).each do |val|
    ::Dir.glob(src.join(val)) do |path|
      next if exclude&.include?(path) || (path = ::Pathname.new(path)).directory?

      target = dest.join(path.relative_path_from(src))
      dir = target.dirname
      unless subdir.include?(dir.to_s)
        dir.mkpath
        subdir << dir.to_s
      end
      copy_f path, target
      files += 1
    end
  end
  puts [dest.realpath, subdir.size.to_s, files.to_s].join(' => ') if verbose
end

.copy_f(src, dest, overwrite: true, verbose: false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/squared/common/system.rb', line 52

def copy_f(src, dest, overwrite: true, verbose: false)
  unless overwrite
    if (path = ::Pathname.new(dest)).directory?
      src = (src.is_a?(::Array) ? src : [src]).reject { |val| path.join(::File.basename(val)).exist? }
    elsif path.exist?
      return
    end
  end
  ::FileUtils.cp(src, dest, verbose: verbose)
end

.shell(*args, **kwargs) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/squared/common/system.rb', line 11

def shell(*args, **kwargs)
  if RUBY_VERSION =~ /^2\.[0-5]\./
    exception = kwargs.delete(:exception)
    ret = system(*args, **kwargs)
    return ret if ret || !exception

    raise $?.to_s
  else
    system(*args, **kwargs)
  end
end