Class: Workbush::CommandRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/workbush/command_runner.rb

Overview

Handles executing post-creation commands in the new worktree

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worktree_path:, verbose: false) ⇒ CommandRunner

Initialize a new CommandRunner instance

Parameters:

  • worktree_path (String)

    Path to the worktree where commands will run

  • verbose (Boolean) (defaults to: false)

    Enable verbose output



15
16
17
18
19
20
# File 'lib/workbush/command_runner.rb', line 15

def initialize(worktree_path:, verbose: false)
  @worktree_path = File.expand_path(worktree_path)
  @verbose = verbose
  @success_count = 0
  @failed_count = 0
end

Instance Attribute Details

#verboseObject (readonly)

Returns the value of attribute verbose.



9
10
11
# File 'lib/workbush/command_runner.rb', line 9

def verbose
  @verbose
end

#worktree_pathObject (readonly)

Returns the value of attribute worktree_path.



9
10
11
# File 'lib/workbush/command_runner.rb', line 9

def worktree_path
  @worktree_path
end

Instance Method Details

#run_command(command) ⇒ Boolean

Run a single command

Parameters:

  • command (String)

    Command to execute

Returns:

  • (Boolean)

    True if successful



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
# File 'lib/workbush/command_runner.rb', line 48

def run_command(command)
  log "Executing: #{command}", :info

  stdout, stderr, status = Open3.capture3(
    command,
    chdir: @worktree_path,
    unsetenv_others: false
  )

  if status.success?
    log "  ✓ Success", :success
    log_output(stdout) if @verbose && !stdout.strip.empty?
    @success_count += 1
    true
  else
    log "  ✗ Failed (exit #{status.exitstatus})", :error
    log_output(stderr) unless stderr.strip.empty?
    @failed_count += 1
    false
  end
rescue StandardError => e
  log "  ✗ Failed: #{e.message}", :error
  @failed_count += 1
  false
end

#run_command_streaming(command) ⇒ Boolean

Run commands with streaming output (for interactive commands)

Parameters:

  • command (String)

    Command to execute

Returns:

  • (Boolean)

    True if successful



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
# File 'lib/workbush/command_runner.rb', line 78

def run_command_streaming(command)
  log "Executing: #{command}", :info

  Open3.popen3(command, chdir: @worktree_path) do |_stdin, stdout, stderr, wait_thr|
    # Stream stdout
    Thread.new do
      stdout.each_line { |line| puts line }
    end

    # Stream stderr
    Thread.new do
      stderr.each_line { |line| warn line }
    end

    status = wait_thr.value

    if status.success?
      log "  ✓ Success", :success
      @success_count += 1
      true
    else
      log "  ✗ Failed (exit #{status.exitstatus})", :error
      @failed_count += 1
      false
    end
  end
rescue StandardError => e
  log "  ✗ Failed: #{e.message}", :error
  @failed_count += 1
  false
end

#run_from_config(config) ⇒ Hash

Run commands from configuration

Parameters:

  • config (Config)

    Configuration object with commands

Returns:

  • (Hash)

    Statistics about command execution



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/workbush/command_runner.rb', line 26

def run_from_config(config)
  commands = config.post_create_commands
  return { success: 0, failed: 0 } if commands.empty?

  log "Running post-creation commands..."

  commands.each do |command|
    run_command(command)
  end

  log_summary

  {
    success: @success_count,
    failed: @failed_count
  }
end