# frozen_string_literal: true module Jive class Shell COMMAND_MAP = { cd: "/usr/bin/cd", echo: "/usr/bin/echo", git: "/usr/bin/git", mkdir: "/usr/bin/mkdir" }.freeze def run_each(tasks) tasks.each do |task| break unless execute(task) end end def execute(command, env: {}) system(env, expand(command)) end def after_run(tasks) finalizer_fd = 42 pipe = IO.new(finalizer_fd) pipe.puts(tasks.map { |x| x.join(":") }.join("\n")) rescue Errno::EBADF => e puts e exit 1 end def expand(command) Array(command) .flatten .map { |x| COMMAND_MAP.fetch(x, x).to_s } .join(" ") end def run_safely yield rescue StandardError => e puts e after_run([%w[noop noop]]) end end end