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, preserve: nil, force: false, verbose: true, pass: nil, hidden: false) ⇒ Object

Raises:

  • (Errno::ENOENT)


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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/squared/common/system.rb', line 64

def copy_dir(src, dest, glob = ['**/*'], create: false, link: nil, preserve: nil, force: false, verbose: true,
             pass: nil, hidden: false)
  base = Pathname.new(src)
  target = Pathname.new(dest)
  raise Errno::ENOENT, dest.cleanpath.to_s unless create || target.parent.exist?

  subdir = {}
  target.mkpath if create
  flags = hidden ? [File::FNM_DOTMATCH] : []
  exclude = Array(pass).each_with_object([]) { |val, out| out.concat(Dir.glob(val, *flags, base: base)) } if pass
  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
  type = System.send :parse_link, link
  subdir.each do |dir, files|
    unless type == 0
      items = files.dup
      files.clear
      items.each do |file|
        if file.exist?
          if !file.symlink?
            files << file
          elsif !force
            next
          end
        end
        case type
        when 1
          FileUtils.ln_s(file, dir, force: force, verbose: false)
        when 2
          FileUtils.ln_s(file.relative_path_from(dir), dir, force: force, verbose: false)
        else
          FileUtils.ln(file, dir, force: force, verbose: false)
        end
        soft += 1
      end
    end
    next if files.empty?

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

.copy_guard(*src, dest, base: '.', create: false, link: nil, preserve: nil, force: false, verbose: true) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/squared/common/system.rb', line 121

def copy_guard(*src, dest, base: '.', create: false, link: nil, preserve: nil, force: false, verbose: true)
  target = src.compact.flatten
  dest = Pathname.new(dest).realdirpath
  base = Pathname.new(base).realpath
  dir = if dest.directory?
          true
        elsif target.size > 1
          raise Errno::ENOENT, dest.cleanpath.to_s unless create && !dest.exist?

          dest.mkpath
          true
        end
  target.map! { |file| [base + file, dir ? dest + File.basename(file) : dest] }
  return if !force && (target = target.reject { |to| to[1].exist? }).empty?

  type = System.send :parse_link, link
  target.each do |file, to|
    case type
    when 0
      FileUtils.cp(file, to, preserve: preserve, verbose: verbose)
    when 1
      FileUtils.ln_s(file, to, force: force, verbose: verbose)
    when 2
      FileUtils.ln_s(file.relative_path_from(dir ? to.dirname : to), to, force: force, verbose: verbose)
    else
      FileUtils.ln(file, to, force: force, verbose: verbose)
    end
  end
  nil
end

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



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 31

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

  ret
end

.shell_t(*args, timeout: 0, **kwargs) ⇒ Object



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

def shell_t(*args, timeout: 0, **kwargs)
  return shell(*args, **kwargs) unless timeout > 0

  begin
    Timeout.timeout(timeout) do
      shell(*args, **kwargs)
    end
  rescue Interrupt
    exit 1
  end
end