Class: Makit::Tasks::HookManager

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/tasks/hook_manager.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.excluded_tasksSet<String> (readonly)

Returns Tasks excluded from hook execution.

Returns:

  • (Set<String>)

    Tasks excluded from hook execution



65
66
67
# File 'lib/makit/tasks/hook_manager.rb', line 65

def excluded_tasks
  @excluded_tasks
end

.performance_logString (readonly)

Returns Path to performance log file.

Returns:

  • (String)

    Path to performance log file



74
75
76
# File 'lib/makit/tasks/hook_manager.rb', line 74

def performance_log
  @performance_log
end

.post_hooksArray<Proc> (readonly)

Returns Global post-task hooks.

Returns:

  • (Array<Proc>)

    Global post-task hooks



56
57
58
# File 'lib/makit/tasks/hook_manager.rb', line 56

def post_hooks
  @post_hooks
end

.pre_hooksArray<Proc> (readonly)

Returns Global pre-task hooks.

Returns:

  • (Array<Proc>)

    Global pre-task hooks



53
54
55
# File 'lib/makit/tasks/hook_manager.rb', line 53

def pre_hooks
  @pre_hooks
end

.task_specific_post_hooksHash<String, Array<Proc>> (readonly)

Returns Task-specific post-task hooks.

Returns:

  • (Hash<String, Array<Proc>>)

    Task-specific post-task hooks



62
63
64
# File 'lib/makit/tasks/hook_manager.rb', line 62

def task_specific_post_hooks
  @task_specific_post_hooks
end

.task_specific_pre_hooksHash<String, Array<Proc>> (readonly)

Returns Task-specific pre-task hooks.

Returns:

  • (Hash<String, Array<Proc>>)

    Task-specific pre-task hooks



59
60
61
# File 'lib/makit/tasks/hook_manager.rb', line 59

def task_specific_pre_hooks
  @task_specific_pre_hooks
end

.task_stackArray<Hash> (readonly)

Returns Stack of currently executing tasks.

Returns:

  • (Array<Hash>)

    Stack of currently executing tasks



71
72
73
# File 'lib/makit/tasks/hook_manager.rb', line 71

def task_stack
  @task_stack
end

.timing_enabledBoolean (readonly)

Returns Whether timing is enabled.

Returns:

  • (Boolean)

    Whether timing is enabled



68
69
70
# File 'lib/makit/tasks/hook_manager.rb', line 68

def timing_enabled
  @timing_enabled
end

Class Method Details

.add_post_hook {|task_name, duration, result, error| ... } ⇒ void

This method returns an undefined value.

Add a global post-task hook

The hook will be executed after every task completes (success or failure).

Yields:

  • (task_name, duration, result, error)

    hook parameters

Yield Parameters:

  • task_name (String)

    the name of the task

  • duration (Float)

    execution duration in seconds

  • result (Object, nil)

    the result of the task execution

  • error (Exception, nil)

    any error that occurred during execution



97
98
99
# File 'lib/makit/tasks/hook_manager.rb', line 97

def add_post_hook(&block)
  @post_hooks << block
end

.add_post_hook_for(task_name) {|task_name, duration, result, error| ... } ⇒ void

This method returns an undefined value.

Add a post-task hook for a specific task

The hook will only be executed after the specified task completes.

Parameters:

  • task_name (String, Symbol)

    the name of the task

Yields:

  • (task_name, duration, result, error)

    hook parameters

Yield Parameters:

  • task_name (String)

    the name of the task

  • duration (Float)

    execution duration in seconds

  • result (Object, nil)

    the result of the task execution

  • error (Exception, nil)

    any error that occurred during execution



125
126
127
128
# File 'lib/makit/tasks/hook_manager.rb', line 125

def add_post_hook_for(task_name, &block)
  @task_specific_post_hooks[task_name.to_s] ||= []
  @task_specific_post_hooks[task_name.to_s] << block
end

.add_pre_hook {|task_name| ... } ⇒ void

This method returns an undefined value.

Add a global pre-task hook

The hook will be executed before every task runs.

Yields:

  • (task_name)

    the name of the task being executed

Yield Parameters:

  • task_name (String)

    the name of the task



83
84
85
# File 'lib/makit/tasks/hook_manager.rb', line 83

def add_pre_hook(&block)
  @pre_hooks << block
end

.add_pre_hook_for(task_name) {|task_name| ... } ⇒ void

This method returns an undefined value.

Add a pre-task hook for a specific task

The hook will only be executed before the specified task runs.

Parameters:

  • task_name (String, Symbol)

    the name of the task

Yields:

  • (task_name)

    the name of the task being executed

Yield Parameters:

  • task_name (String)

    the name of the task



109
110
111
112
# File 'lib/makit/tasks/hook_manager.rb', line 109

def add_pre_hook_for(task_name, &block)
  @task_specific_pre_hooks[task_name.to_s] ||= []
  @task_specific_pre_hooks[task_name.to_s] << block
end

.clear_all_hooksvoid

This method returns an undefined value.

Clear all hooks



187
188
189
190
191
192
# File 'lib/makit/tasks/hook_manager.rb', line 187

def clear_all_hooks
  @pre_hooks.clear
  @post_hooks.clear
  @task_specific_pre_hooks.clear
  @task_specific_post_hooks.clear
end

.clear_performance_logsvoid

This method returns an undefined value.

Clear performance logs



259
260
261
262
# File 'lib/makit/tasks/hook_manager.rb', line 259

def clear_performance_logs
  FileUtils.rm_f(@performance_log)
  FileUtils.rm_f("artifacts/task_failures.log")
end

.disable_timing!void

This method returns an undefined value.

Disable task timing and performance logging



245
246
247
# File 'lib/makit/tasks/hook_manager.rb', line 245

def disable_timing!
  @timing_enabled = false
end

.enable_timing!void

This method returns an undefined value.

Enable task timing and performance logging



237
238
239
240
# File 'lib/makit/tasks/hook_manager.rb', line 237

def enable_timing!
  @timing_enabled = true
  puts "🔧 Task timing enabled".colorize(:grey) if defined?(String.instance_method(:colorize))
end

.exclude_task(task_name) ⇒ void

This method returns an undefined value.

Exclude a task from hook execution

Parameters:

  • task_name (String, Symbol)

    the name of the task to exclude



198
199
200
# File 'lib/makit/tasks/hook_manager.rb', line 198

def exclude_task(task_name)
  @excluded_tasks.add(task_name.to_s)
end

.excluded?(task_name) ⇒ Boolean

Check if a task is excluded from hook execution

Parameters:

  • task_name (String, Symbol)

    the name of the task to check

Returns:

  • (Boolean)

    true if the task is excluded



214
215
216
# File 'lib/makit/tasks/hook_manager.rb', line 214

def excluded?(task_name)
  @excluded_tasks.include?(task_name.to_s)
end

.execute_post_hooks(task_name, duration, result, error) ⇒ void

This method returns an undefined value.

Execute all post-task hooks for a given task

Parameters:

  • task_name (String)

    the name of the task

  • duration (Float)

    execution duration in seconds

  • result (Object, nil)

    the result of the task execution

  • error (Exception, nil)

    any error that occurred during execution



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/makit/tasks/hook_manager.rb', line 161

def execute_post_hooks(task_name, duration, result, error)
  return if excluded?(task_name)

  # Handle timing if enabled
  if timing_enabled?
    task_info = @task_stack.pop
    if task_info && task_info[:name] == task_name && (duration > 0.1)
      # Log performance for tasks that took longer than 0.1 seconds
      log_task_performance(task_name, duration, task_info[:level])
    end

    # Log task failure if there was an error
    log_task_failure(task_name, duration, error, task_info&.dig(:level) || 0) if error
  end

  # Execute global post-hooks
  @post_hooks.each { |hook| hook.call(task_name, duration, result, error) }

  # Execute task-specific post-hooks
  task_hooks = @task_specific_post_hooks[task_name.to_s] || []
  task_hooks.each { |hook| hook.call(task_name, duration, result, error) }
end

.execute_pre_hooks(task_name) ⇒ void

This method returns an undefined value.

Execute all pre-task hooks for a given task

Parameters:

  • task_name (String)

    the name of the task



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/makit/tasks/hook_manager.rb', line 134

def execute_pre_hooks(task_name)
  return if excluded?(task_name)

  # Track timing if enabled
  if timing_enabled?
    @task_stack.push({
                       name: task_name,
                       start_time: Time.now,
                       level: @task_stack.length,
                     })
  end

  # Execute global pre-hooks
  @pre_hooks.each { |hook| hook.call(task_name) }

  # Execute task-specific pre-hooks
  task_hooks = @task_specific_pre_hooks[task_name.to_s] || []
  task_hooks.each { |hook| hook.call(task_name) }
end

.include_task(task_name) ⇒ void

This method returns an undefined value.

Include a task in hook execution (remove from exclusions)

Parameters:

  • task_name (String, Symbol)

    the name of the task to include



206
207
208
# File 'lib/makit/tasks/hook_manager.rb', line 206

def include_task(task_name)
  @excluded_tasks.delete(task_name.to_s)
end

.log_task_completion(task_name, duration, result, error) ⇒ Object



391
392
393
394
395
396
397
# File 'lib/makit/tasks/hook_manager.rb', line 391

def log_task_completion(task_name, duration, result, error)
  # Makit::Logging.info("#{task_name}".colorize(:white).bold)
  # Makit::Logging.info("Task completed".colorize(:green).bold)
  # Makit::Logging.info("Duration: #{duration.round(2)}s".colorize(:green).bold)
  # Makit::Logging.info("Result: #{result}".colorize(:green).bold)
  # Makit::Logging.info("Error: #{error}".colorize(:red).bold)
end

.log_task_start(task_name) ⇒ Object



385
386
387
388
389
# File 'lib/makit/tasks/hook_manager.rb', line 385

def log_task_start(task_name)
  # return if @logged_tasks.include?(task_name)
  # @logged_tasks.add(task_name)
  # Makit::Logging.info("#{task_name}".colorize(:white).bold)
end

.setup!void

This method returns an undefined value.

Setup task hooks by monkey patching Rake::Task

This method patches the Rake::Task#execute method to automatically call pre and post hooks for all task executions.



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/makit/tasks/hook_manager.rb', line 270

def setup!
  return if @hooks_installed

  ::Rake::Task.class_eval do
    alias_method :makit_original_execute, :execute

    # Check if this task should have enhanced makit tracing
    #
    # @return [Boolean] true if task should be traced
    def should_trace_makit_task?
      # Check if trace is enabled
      return false unless ENV['RAKE_TRACE'] || ARGV.include?('--trace') || ENV['MAKIT_TRACE'] == 'true'
      
      # Check if task is makit-related
      makit_related_task?
    end
    
    # Check if task is makit-related
    #
    # @return [Boolean] true if task is makit-related
    def makit_related_task?
      name.downcase.include?('makit') ||
      name.include?('bundle') ||
      name.include?('gem') ||
      name.include?('dotnet') ||
      name.include?('protoc') ||
      name.include?('git') ||
      name.include?('setup') ||
      name.include?('build') ||
      name.include?('test')
    end
    
    # Get makit strategy information safely
    #
    # @return [String] strategy information
    def get_makit_strategy_info
      begin
        if defined?(Makit::Commands::Runner)
          runner = Makit::Commands::Runner.default
          info = runner.strategy_info
          "#{info[:type]} (#{info[:class]})"
        else
          "not available"
        end
      rescue => e
        "error: #{e.message}"
      end
    end

    def execute(args = nil)
      start_time = Time.now
      error = nil
      result = nil
      
      # Add enhanced trace output for makit-related tasks
      if should_trace_makit_task?
        puts "  [MAKIT] Executing task: #{name}"
        puts "  [MAKIT] Strategy: #{get_makit_strategy_info}"
        puts "  [MAKIT] Environment: #{ENV['MAKIT_STRATEGY'] || 'auto'}"
        puts "  [MAKIT] Working Directory: #{Dir.pwd}"
        puts "  [MAKIT] Timestamp: #{Time.now.iso8601}"
      end

      begin
        # Execute pre-hooks
        Makit::Tasks::HookManager.execute_pre_hooks(name)

        # Execute the original task
        result = makit_original_execute(args)
      rescue StandardError => e
        error = e
        raise
      ensure
        # Calculate duration
        duration = Time.now - start_time

        # Execute post-hooks
        Makit::Tasks::HookManager.execute_post_hooks(name, duration, result, error)
      end

      result
    end
  end

  @hooks_installed = true
end

.setup_default_hooksvoid

This method returns an undefined value.

Setup default task hooks

This method sets up the default task hooks that provide basic task execution feedback. It should be called during initialization.



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/makit/tasks/hook_manager.rb', line 363

def setup_default_hooks
  # Setup the monkey patching first
  setup!

  # Only setup default hooks if no hooks are already registered
  return unless @pre_hooks.empty? && @post_hooks.empty?

  # Exclude the init task from hooks to prevent duplication
  exclude_task(:init)
  exclude_task(:default)

  # Default pre-task hook: log task start to default logger
  add_pre_hook do |task_name|
    log_task_start(task_name)
  end

  # Default post-task hook: log task completion
  add_post_hook do |task_name, duration, result, error|
    log_task_completion(task_name, duration, result, error)
  end
end

.statsHash

Get statistics about registered hooks

Returns:

  • (Hash)

    hook statistics



221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/makit/tasks/hook_manager.rb', line 221

def stats
  {
    global_pre_hooks: @pre_hooks.size,
    global_post_hooks: @post_hooks.size,
    task_specific_pre_hooks: @task_specific_pre_hooks.values.flatten.size,
    task_specific_post_hooks: @task_specific_post_hooks.values.flatten.size,
    tasks_with_pre_hooks: @task_specific_pre_hooks.keys,
    tasks_with_post_hooks: @task_specific_post_hooks.keys,
    timing_enabled: @timing_enabled,
    task_stack_depth: @task_stack.size,
  }
end

.timing_enabled?Boolean

Check if timing is enabled

Returns:

  • (Boolean)

    true if timing is enabled



252
253
254
# File 'lib/makit/tasks/hook_manager.rb', line 252

def timing_enabled?
  @timing_enabled == true
end