Class: Ace::Git::Worktree::Molecules::HookExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/git/worktree/molecules/hook_executor.rb

Overview

Hook executor molecule

Executes after-create hooks defined in YAML configuration. Supports sequential command execution with timeout, error handling, and environment variable interpolation.

Examples:

Execute hooks from configuration

executor = HookExecutor.new
hooks = [
  { "command" => "mise trust mise.toml", "timeout" => 10 },
  { "command" => "echo 'Setup complete'" }
]
result = executor.execute_hooks(hooks, worktree_path: "/path/to/worktree")

Hook configuration format

hooks:
  after_create:
    - command: "mise trust mise*.toml"
      working_dir: "."
      timeout: 30
      continue_on_error: true
      env:
        CUSTOM_VAR: "value"

Constant Summary collapse

FALLBACK_DEFAULT_TIMEOUT =

Fallback timeout for hook execution (seconds) Used only when config is unavailable

30
FALLBACK_MAX_TIMEOUT =

Fallback maximum timeout allowed (seconds) Used only when config is unavailable

300

Instance Method Summary collapse

Constructor Details

#initializeHookExecutor

Initialize a new HookExecutor



43
44
45
# File 'lib/ace/git/worktree/molecules/hook_executor.rb', line 43

def initialize
  @results = []
end

Instance Method Details

#default_timeoutInteger

Get default timeout from config or fallback

Returns:

  • (Integer)

    Default timeout in seconds



49
50
51
52
53
# File 'lib/ace/git/worktree/molecules/hook_executor.rb', line 49

def default_timeout
  Ace::Git::Worktree.hook_timeout
rescue
  FALLBACK_DEFAULT_TIMEOUT
end

#execute_hooks(hooks, worktree_path:, project_root: Dir.pwd, task_data: nil) ⇒ Hash

Execute a list of hooks

Examples:

result = executor.execute_hooks(
  [{ "command" => "mise trust" }],
  worktree_path: "/path/to/worktree",
  project_root: "/path/to/project",
  task_data: { task_id: "081", title: "Fix bug" }
)
# => { success: true, results: [...], errors: [] }

Parameters:

  • hooks (Array<Hash>)

    Array of hook definitions

  • worktree_path (String)

    Path to the worktree

  • task_data (Hash, nil) (defaults to: nil)

    Optional task data for variable interpolation

  • project_root (String) (defaults to: Dir.pwd)

    Project root directory (default working dir)

Returns:

  • (Hash)

    Execution result with :success, :results, :errors



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
# File 'lib/ace/git/worktree/molecules/hook_executor.rb', line 79

def execute_hooks(hooks, worktree_path:, project_root: Dir.pwd, task_data: nil)
  return success_result if hooks.nil? || hooks.empty?

  @worktree_path = worktree_path
  @project_root = project_root
  @task_data = task_data || {}
  @results = []
  errors = []

  hooks.each_with_index do |hook_config, index|
    result = execute_hook(hook_config, index)
    @results << result

    unless result[:success]
      errors << "Hook #{index + 1}: #{result[:error]}"
      # Stop execution unless continue_on_error is true
      break unless hook_config["continue_on_error"]
    end
  end

  {
    success: errors.empty?,
    results: @results,
    errors: errors
  }
rescue => e
  {
    success: false,
    results: @results,
    errors: ["Unexpected error: #{e.message}"]
  }
end

#max_timeoutInteger

Get maximum timeout from config or fallback

Returns:

  • (Integer)

    Maximum timeout in seconds



57
58
59
60
61
# File 'lib/ace/git/worktree/molecules/hook_executor.rb', line 57

def max_timeout
  Ace::Git::Worktree.max_timeout
rescue
  FALLBACK_MAX_TIMEOUT
end