Module: Bridgetown::Utils::Aux

Defined in:
lib/bridgetown-core/utils/aux.rb

Class Method Summary collapse

Class Method Details

.add_pid(pid) ⇒ Object



16
17
18
# File 'lib/bridgetown-core/utils/aux.rb', line 16

def self.add_pid(pid)
  running_pids << pid
end

.group(&block) ⇒ Object



46
47
48
49
50
# File 'lib/bridgetown-core/utils/aux.rb', line 46

def self.group(&block)
  Bridgetown::Deprecator.deprecation_message "Bridgetown::Aux.group method will be removed" \
                                             "in a future version, use run_process"
  instance_exec(&block)
end

.kill_processesObject



52
53
54
55
56
57
58
# File 'lib/bridgetown-core/utils/aux.rb', line 52

def self.kill_processes
  Bridgetown.logger.info "Stopping auxiliary processes..."
  running_pids.each do |pid|
    Process.kill("SIGTERM", -Process.getpgid(pid))
  rescue Errno::ESRCH, Errno::EPERM, Errno::ECHILD # rubocop:disable Lint/SuppressedException
  end
end

.run_process(name, color, cmd, env: {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bridgetown-core/utils/aux.rb', line 20

def self.run_process(name, color, cmd, env: {})
  @mutex ||= Thread::Mutex.new

  Thread.new do
    rd, wr = IO.pipe("BINARY")
    pid = Process.spawn({ "BRIDGETOWN_NO_BUNDLER_REQUIRE" => nil }.merge(env),
                        cmd, out: wr, err: wr, pgroup: true)
    @mutex.synchronize { add_pid(pid) }

    loop do
      line = rd.gets
      line.to_s.lines.map(&:chomp).each do |message|
        next if name == "Frontend" && %r{ELIFECYCLE.*?Command failed}.match?(message)

        output = +""
        output << with_color(color, "[#{name}] ") if color
        output << message
        @mutex.synchronize do
          $stdout.puts output
          $stdout.flush
        end
      end
    end
  end
end

.running_pidsObject



12
13
14
# File 'lib/bridgetown-core/utils/aux.rb', line 12

def self.running_pids
  @running_pids ||= []
end

.with_color(name, message) ⇒ Object



6
7
8
9
10
# File 'lib/bridgetown-core/utils/aux.rb', line 6

def self.with_color(name, message)
  return message unless !name.nil? && Bridgetown::Utils::Ansi::COLORS[name.to_sym]

  Bridgetown::Utils::Ansi.send(name, message)
end