Class: Hiiro::Invocation

Inherits:
Object
  • Object
show all
Defined in:
lib/hiiro/invocation.rb

Class Method Summary collapse

Class Method Details

.create_table!(db) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/hiiro/invocation.rb', line 9

def self.create_table!(db)
  db.create_table?(:invocations) do
    String :id, primary_key: true
    String :command
    String :pwd
    String :task
    String :worktree
    String :git_branch
    String :tmux_session
    String :tmux_window
    String :tmux_pane
    String :entered_at
  end
end

.detect_git_branchObject



85
86
87
88
89
# File 'lib/hiiro/invocation.rb', line 85

def self.detect_git_branch
  `git rev-parse --abbrev-ref HEAD 2>/dev/null`.chomp.then { |b| b.empty? ? nil : b }
rescue
  nil
end

.detect_tmux_sessionObject



97
98
99
100
101
102
# File 'lib/hiiro/invocation.rb', line 97

def self.detect_tmux_session
  return nil unless ENV['TMUX']
  `tmux display-message -p '#S' 2>/dev/null`.chomp.then { |s| s.empty? ? nil : s }
rescue
  nil
end

.detect_tmux_windowObject



104
105
106
107
108
109
# File 'lib/hiiro/invocation.rb', line 104

def self.detect_tmux_window
  return nil unless ENV['TMUX']
  `tmux display-message -p '#W' 2>/dev/null`.chomp.then { |w| w.empty? ? nil : w }
rescue
  nil
end

.detect_worktreeObject



91
92
93
94
95
# File 'lib/hiiro/invocation.rb', line 91

def self.detect_worktree
  `git rev-parse --show-toplevel 2>/dev/null`.chomp.then { |w| w.empty? ? nil : w }
rescue
  nil
end

.record!(bin:, subcmd: nil, args: []) ⇒ Object

Record this process’s context. Creates a new Invocation if none exists (no HIIRO_INVOCATION_ID env var), or appends a resolution if one does. Returns the invocation.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
73
74
75
76
77
78
79
80
81
# File 'lib/hiiro/invocation.rb', line 32

def self.record!(bin:, subcmd: nil, args: [])
  invocation_id = ENV['HIIRO_INVOCATION_ID']

  if invocation_id
    inv = where(id: invocation_id).first
    if inv
      depth = InvocationResolution.where(invocation_id: invocation_id).count
      InvocationResolution.create(
        invocation_id: invocation_id,
        depth: depth,
        bin: bin,
        subcmd: subcmd,
        args_json: Hiiro::DB::JSON.dump(Array(args)),
        created_at: Time.now.iso8601
      )
    end
    inv
  else
    id = "inv_#{Time.now.strftime('%Y%m%d%H%M%S%L')}_#{SecureRandom.hex(4)}"
    ENV['HIIRO_INVOCATION_ID'] = id

    command = ([bin] + Array(args)).join(' ')
    inv = create(
      id: id,
      command: command,
      pwd: Dir.pwd,
      task: ENV['HIIRO_TASK'],
      worktree: detect_worktree,
      git_branch: detect_git_branch,
      tmux_session: detect_tmux_session,
      tmux_window: detect_tmux_window,
      tmux_pane: ENV['TMUX_PANE'],
      entered_at: Time.now.iso8601
    )

    InvocationResolution.create(
      invocation_id: id,
      depth: 0,
      bin: bin,
      subcmd: subcmd,
      args_json: Hiiro::DB::JSON.dump(Array(args)),
      created_at: Time.now.iso8601
    )

    inv
  end
rescue => e
  warn "Invocation tracking error: #{e}" if ENV['HIIRO_DEBUG']
  nil
end