Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/mp/string_mp.rb

Overview

monkey patch String class with command execution methods

Instance Method Summary collapse

Instance Method Details

#cache_log(log_file) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/makit/mp/string_mp.rb', line 187

def cache_log(log_file)
  command = self
  request = Makit::Commands::Request.from_string(command)
  result = Makit::Commands::Runner.default.execute(request)

  # Write output to log file
  FileUtils.mkdir_p(File.dirname(log_file))
  File.write(log_file, result.stdout) if result.stdout && !result.stdout.empty?
  File.write(log_file, result.stderr, mode: "a") if result.stderr && !result.stderr.empty?

  result
end

#cache_run(timestamp = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/makit/mp/string_mp.rb', line 64

def cache_run(timestamp = nil)
  #puts "cache_run: #{self}"
  command = self
  request = Makit::Commands::Request.from_string(command)
  if timestamp
    # Add timestamp to metadata for cache key generation
    request = Makit::Commands::Request.new(
      command: request.command,
      arguments: request.arguments,
      directory: request.directory,
      timeout: request.timeout,
      environment: request.environment || {},
      metadata: (request. || {}).merge(timestamp: timestamp),
    )
    #puts "timestamp: #{timestamp}"
  else
    #puts "no timestamp"
  end
  Makit::Commands::Runner.default.execute(request)
end

#cache_run_with_timestamp(timestamp) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/makit/mp/string_mp.rb', line 159

def cache_run_with_timestamp(timestamp)
  command = self
  request = Makit::Commands::Request.from_string(command)
  request = Makit::Commands::Request.new(
    command: request.command,
    arguments: request.arguments,
    directory: request.directory,
    timeout: request.timeout,
    environment: request.environment || {},
    metadata: (request. || {}).merge(timestamp: timestamp),
  )
  Makit::Commands::Runner.default.execute(request)
end

#cache_try(timestamp = nil) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/makit/mp/string_mp.rb', line 120

def cache_try(timestamp = nil)
  command = self
  request = Makit::Commands::Request.from_string(command)
  if timestamp
    # Add timestamp to metadata for cache key generation
    request = Makit::Commands::Request.new(
      command: request.command,
      arguments: request.arguments,
      directory: request.directory,
      timeout: request.timeout,
      environment: request.environment || {},
      metadata: (request. || {}).merge(timestamp: timestamp),
    )
  end
  Makit::Commands::Runner.default.execute(request)
end

#cache_try_with_timestamp(timestamp) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/makit/mp/string_mp.rb', line 173

def cache_try_with_timestamp(timestamp)
  command = self
  request = Makit::Commands::Request.from_string(command)
  request = Makit::Commands::Request.new(
    command: request.command,
    arguments: request.arguments,
    directory: request.directory,
    timeout: request.timeout,
    environment: request.environment || {},
    metadata: (request. || {}).merge(timestamp: timestamp),
  )
  Makit::Commands::Runner.default.execute(request)
end

#log(log_file = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/makit/mp/string_mp.rb', line 97

def log(log_file = nil)
  command = self
  request = Makit::Commands::Request.from_string(command)
  result = Makit::Commands::Runner.default.execute(request)

  # If a log file is provided, write outputs to the file (maintains backward compatibility)
  if log_file
    FileUtils.mkdir_p(File.dirname(log_file))
    File.write(log_file, "") unless File.exist?(log_file)
    File.write(log_file, result.stdout, mode: "w") if result.stdout && !result.stdout.empty?
    File.write(log_file, result.stderr, mode: "a") if result.stderr && !result.stderr.empty?
  else
    # Otherwise, log to the Makit logger
    if result.success?
      Makit::Logging.info("Command completed successfully: #{command}")
    else
      Makit::Logging.error("Command failed: #{command} (exit code: #{result.exit_code})")
    end
  end

  result
end

#run(args = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
# File 'lib/makit/mp/string_mp.rb', line 8

def run(args = nil)
  command = self
  if args.nil?
    request = Makit::Commands::Request.from_string(command)
  else
    # Parse initial request
    base_request = Makit::Commands::Request.from_string(command)

    if args.is_a?(Hash)
      # Create new request with combined options
      request_options = {
        command: base_request.command,
        arguments: base_request.arguments || [],
        directory: base_request.directory,
        timeout: base_request.timeout,
        environment: base_request.environment || {},
        metadata: base_request. || {},
      }

      # Apply args to the request options
      args.each do |key, value|
        case key.to_s
        when "arguments"
          # Append additional arguments
          request_options[:arguments] = (request_options[:arguments] + Array(value)).flatten
        when "directory"
          request_options[:directory] = value
        when "timeout"
          request_options[:timeout] = value
        when "environment"
          request_options[:environment] = (request_options[:environment] || {}).merge(value)
        when "metadata"
          request_options[:metadata] = (request_options[:metadata] || {}).merge(value)
        else
          # Add unknown keys to metadata
          request_options[:metadata] = (request_options[:metadata] || {}).merge(key.to_s => value)
        end
      end

      # Create new request with combined options
      request = Makit::Commands::Request.new(**request_options)
    else
      # args is an array of additional arguments
      request = Makit::Commands::Request.new(
        command: base_request.command,
        arguments: (base_request.arguments || []) + Array(args),
        directory: base_request.directory,
        timeout: base_request.timeout,
        environment: base_request.environment || {},
        metadata: base_request. || {},
      )
    end
  end
  Makit::Commands::Runner.default.execute(request)
end

#showObject



91
92
93
94
95
# File 'lib/makit/mp/string_mp.rb', line 91

def show
  command = self
  request = Makit::Commands::Request.from_string(command)
  Makit::Commands::Runner.default.execute(request)
end

#show_command(cmd) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/makit/mp/string_mp.rb', line 137

def show_command(cmd)
  if cmd.success?
    puts "#{cmd.command} #{cmd.arguments&.join(" ")}".colorize(:green)
  else
    puts "#{cmd.command} #{cmd.arguments&.join(" ")}".colorize(:red)
    puts "   Exit code: #{cmd.exit_code}".colorize(:red)
    puts "   Error: #{cmd.stderr}".colorize(:red) if cmd.stderr && !cmd.stderr.empty?
  end
end

#to_markdownObject



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/makit/mp/string_mp.rb', line 147

def to_markdown
  command = self
  request = Makit::Commands::Request.from_string(command)
  result = Makit::Commands::Runner.default.execute(request)

  if result.success?
    "```\n#{result.stdout}\n```"
  else
    "```\nError: #{result.stderr}\n```"
  end
end

#tryObject



85
86
87
88
89
# File 'lib/makit/mp/string_mp.rb', line 85

def try
  command = self
  request = Makit::Commands::Request.from_string(command).exit_on_failure(false)
  Makit::Commands::Runner.default.execute(request)
end