Module: Squared::Common::System

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

Class Method Summary collapse

Class Method Details

.copy_dir(src, dest, glob = ['**/*'], create: false, link: nil, force: false, pass: nil, hidden: false, verbose: true) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/squared/common/system.rb', line 32

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

  subdir = {}
  target.mkpath if create
  flags = hidden ? [File::FNM_DOTMATCH] : []
  if pass
    exclude = []
    Array(pass).each { |val| exclude.concat(Dir.glob(val, *flags, base: base)) }
  end
  Array(glob).each do |val|
    Dir.glob(val, *flags, base: base) do |file|
      next if exclude&.include?(file) || (entry = base + file).directory?

      dir = target.join(file).dirname
      if (data = subdir[dir.to_s])
        data << entry
      else
        dir.mkpath
        subdir[dir.to_s] = [entry]
      end
    end
  end
  count = 0
  soft = 0
  subdir.each do |dir, files|
    if link
      files.dup.yield_self do |items|
        files.clear
        items.each do |file|
          if file.exist?
            if file.symlink?
              next unless force
            else
              files << file
              next
            end
          end
          if link == 'hard'
            FileUtils.ln(file, dir, force: force, verbose: false)
          else
            FileUtils.ln_s(file, dir, force: force, verbose: false)
          end
          soft += 1
        end
      end
    end
    next if files.empty?

    out = FileUtils.cp(files, dir, verbose: false)
    count += out.size
  end
  puts [target.realpath, subdir.size, soft > 0 ? "#{count}+#{soft}" : count].join(' => ') if verbose
end

.copy_guard(src, dest, link: nil, force: false, verbose: true) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/squared/common/system.rb', line 90

def copy_guard(src, dest, link: nil, force: false, verbose: true)
  unless force
    target = Pathname.new(dest)
    if target.directory?
      src = Array(src).reject { |val| target.join(File.basename(val)).exist? }
      return if src.empty?
    elsif target.exist?
      return
    end
  end
  case link
  when 'hard', 1
    FileUtils.ln(src, dest, force: force, verbose: verbose)
  when ::TrueClass, 'soft', 0
    FileUtils.ln_s(src, dest, force: force, verbose: verbose)
  else
    FileUtils.cp(src, dest, verbose: verbose)
  end
end

.shell(*args, name: :system, **kwargs) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/squared/common/system.rb', line 11

def shell(*args, name: :system, **kwargs)
  if RUBY_ENGINE == 'jruby' && Rake::Win32.windows?
    e = kwargs[:exception]
    if (dir = kwargs[:chdir]) && ((pwd = Dir.pwd) != dir)
      Dir.chdir(dir)
      ret = Kernel.send(name, *args)
      Dir.chdir(pwd)
    else
      ret = Kernel.send(name, *args)
    end
  elsif RUBY_VERSION < '2.6'
    e = kwargs.delete(:exception)
    ret = Kernel.send(name, *args, **kwargs)
  else
    return Kernel.send(name, *args, **kwargs)
  end
  return ret if ret || !e

  raise $?.to_s
end