lib/cobra_commander/executor.rb in cobra_commander-1.0.1 vs lib/cobra_commander/executor.rb in cobra_commander-1.1.0
- old
+ new
@@ -1,45 +1,47 @@
# frozen_string_literal: true
-require_relative "executor/execution"
-require_relative "executor/job"
-require_relative "executor/script"
-require_relative "executor/command"
-require_relative "executor/spinners"
-require_relative "executor/interactive_printer"
-require_relative "executor/markdown_printer"
+require "pastel"
+require "tty-command"
+require "tty-prompt"
module CobraCommander
# Execute a command on all given packages
module Executor
+ autoload :BufferedPrinter, "cobra_commander/executor/buffered_printer"
+ autoload :Command, "cobra_commander/executor/command"
+ autoload :OutputPrompt, "cobra_commander/executor/output_prompt"
+ autoload :IsolatedPTY, "cobra_commander/executor/isolated_pty"
+ autoload :PackageCriteria, "cobra_commander/executor/package_criteria"
+ autoload :Printers, "cobra_commander/executor/printers"
+ autoload :RunScript, "cobra_commander/executor/run_script"
+ autoload :Script, "cobra_commander/executor/script"
+ autoload :WorkerPool, "cobra_commander/executor/worker_pool"
+
module_function
- # Executes the given jobs in an CobraCommander::Executor::Execution.
+ # Executes the given jobs in an CobraCommander::Executor::WorkerPool.
#
- # This facade also allows to execute the jobs with a spinner (@see CobraCommander::Executor::Spinners) to display
- # the execution status of each job.
+ # When only one job is queued, it choses the :quiet printer, to print the
+ # output as it happens.
+ # When more than one job is queued in interactive mode, it uses the :progress
+ # printer, which will display a green dot or a red F depending on the result
+ # of each script execution.
+ # If not in interactive mode, it will print the output in a buffered way to
+ # make it easier to read each output.
#
- # You can also determine how to display the execution once it's completed, by setting `output_mode` to either
- # :interactive or :markdown. When using :interactive, a menu with each job will be displayed allowing the user
- # to select a job and see its output. When using :markdown, a markdown will be printed to `output` with the
- # output of each job.
- #
# @param jobs [Enumerable<CobraCommander::Executor::Job>] the jobs to run
- # @param status_output [IO,nil] if not nil, will print the spinners for each job in this output
- # @param workers [Integer] number of workers processing the jobs queue (see CobraCommander::Executor::Execution)
- # @param output_mode [:interactive,:markdown,nil] how the output will be printed after execution
- # @param workers [Integer] number of workers processing the jobs queue (see CobraCommander::Executor::Execution)
- # @return [CobraCommander::Executor::Execution]
- # @see CobraCommander::Executor::Execution
- # @see CobraCommander::Executor::Spinners
- # @see CobraCommander::Executor::InterativePrinter
- # @see CobraCommander::Executor::MarkdownPrinter
- def execute(jobs:, status_output: nil, output_mode: nil, output: nil, **kwargs)
- Execution.new(jobs, **kwargs).tap do |execution|
- Spinners.start(execution, output: status_output) if status_output
- execution.wait
- InteractivePrinter.run(execution, output) if output_mode == :interactive
- MarkdownPrinter.run(execution, output) if output_mode == :markdown
- end
+ # @param interactive [Boolean] prefer interactive output
+ # @see CobraCommander::Executor::WorkerPool for more options
+ def execute_and_handle_exit(jobs:, interactive: false, **kwargs, &name_f)
+ printer = if jobs.size == 1 then :quiet
+ elsif interactive then :progress
+ else
+ ::CobraCommander::Executor::BufferedPrinter
+ end
+ pool = WorkerPool.new(jobs: jobs, printer: printer, **kwargs, &name_f).tap(&:start)
+ return CobraCommander::Executor::OutputPrompt.run(pool) if interactive && jobs.size > 1
+
+ exit(1) if pool.error?
end
end
end