makit 0.0.168 → 0.0.169

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +41 -41
  3. data/exe/makit +5 -5
  4. data/lib/makit/apache.rb +28 -28
  5. data/lib/makit/auto.rb +48 -48
  6. data/lib/makit/azure/blob_storage.rb +257 -257
  7. data/lib/makit/azure/cli.rb +284 -284
  8. data/lib/makit/azure-pipelines.rb +187 -187
  9. data/lib/makit/cli/base.rb +17 -17
  10. data/lib/makit/cli/build_commands.rb +500 -500
  11. data/lib/makit/cli/generators/base_generator.rb +74 -74
  12. data/lib/makit/cli/generators/dotnet_generator.rb +50 -50
  13. data/lib/makit/cli/generators/generator_factory.rb +49 -49
  14. data/lib/makit/cli/generators/node_generator.rb +50 -50
  15. data/lib/makit/cli/generators/ruby_generator.rb +77 -77
  16. data/lib/makit/cli/generators/rust_generator.rb +50 -50
  17. data/lib/makit/cli/generators/templates/dotnet_templates.rb +167 -167
  18. data/lib/makit/cli/generators/templates/node_templates.rb +161 -161
  19. data/lib/makit/cli/generators/templates/ruby/gemfile.rb +26 -26
  20. data/lib/makit/cli/generators/templates/ruby/gemspec.rb +41 -41
  21. data/lib/makit/cli/generators/templates/ruby/main_lib.rb +33 -33
  22. data/lib/makit/cli/generators/templates/ruby/rakefile.rb +35 -35
  23. data/lib/makit/cli/generators/templates/ruby/readme.rb +63 -63
  24. data/lib/makit/cli/generators/templates/ruby/test.rb +39 -39
  25. data/lib/makit/cli/generators/templates/ruby/test_helper.rb +29 -29
  26. data/lib/makit/cli/generators/templates/ruby/version.rb +29 -29
  27. data/lib/makit/cli/generators/templates/rust_templates.rb +128 -128
  28. data/lib/makit/cli/main.rb +78 -78
  29. data/lib/makit/cli/pipeline_commands.rb +311 -311
  30. data/lib/makit/cli/project_commands.rb +868 -868
  31. data/lib/makit/cli/repository_commands.rb +661 -661
  32. data/lib/makit/cli/strategy_commands.rb +207 -207
  33. data/lib/makit/cli/utility_commands.rb +521 -521
  34. data/lib/makit/commands/factory.rb +359 -359
  35. data/lib/makit/commands/middleware/base.rb +73 -73
  36. data/lib/makit/commands/middleware/cache.rb +248 -248
  37. data/lib/makit/commands/middleware/command_logger.rb +312 -312
  38. data/lib/makit/commands/middleware/validator.rb +269 -269
  39. data/lib/makit/commands/request.rb +316 -316
  40. data/lib/makit/commands/result.rb +323 -323
  41. data/lib/makit/commands/runner.rb +386 -386
  42. data/lib/makit/commands/strategies/base.rb +171 -171
  43. data/lib/makit/commands/strategies/child_process.rb +162 -162
  44. data/lib/makit/commands/strategies/factory.rb +136 -136
  45. data/lib/makit/commands/strategies/synchronous.rb +139 -139
  46. data/lib/makit/commands.rb +50 -50
  47. data/lib/makit/configuration/dotnet_project.rb +48 -48
  48. data/lib/makit/configuration/gitlab_helper.rb +61 -61
  49. data/lib/makit/configuration/project.rb +292 -292
  50. data/lib/makit/configuration/rakefile_helper.rb +43 -43
  51. data/lib/makit/configuration/step.rb +34 -34
  52. data/lib/makit/configuration/timeout.rb +74 -74
  53. data/lib/makit/configuration.rb +21 -21
  54. data/lib/makit/content/default_gitignore.rb +7 -7
  55. data/lib/makit/content/default_gitignore.txt +225 -225
  56. data/lib/makit/content/default_rakefile.rb +13 -13
  57. data/lib/makit/content/gem_rakefile.rb +16 -16
  58. data/lib/makit/context.rb +1 -1
  59. data/lib/makit/data.rb +49 -49
  60. data/lib/makit/directories.rb +170 -170
  61. data/lib/makit/directory.rb +262 -262
  62. data/lib/makit/docs/files.rb +89 -89
  63. data/lib/makit/docs/rake.rb +102 -102
  64. data/lib/makit/dotnet/cli.rb +224 -224
  65. data/lib/makit/dotnet/project.rb +217 -217
  66. data/lib/makit/dotnet/solution.rb +38 -38
  67. data/lib/makit/dotnet/solution_classlib.rb +239 -239
  68. data/lib/makit/dotnet/solution_console.rb +264 -264
  69. data/lib/makit/dotnet/solution_maui.rb +354 -354
  70. data/lib/makit/dotnet/solution_wasm.rb +275 -275
  71. data/lib/makit/dotnet/solution_wpf.rb +304 -304
  72. data/lib/makit/dotnet.rb +110 -110
  73. data/lib/makit/email.rb +90 -90
  74. data/lib/makit/environment.rb +142 -142
  75. data/lib/makit/examples/runner.rb +370 -370
  76. data/lib/makit/exceptions.rb +45 -45
  77. data/lib/makit/fileinfo.rb +32 -32
  78. data/lib/makit/files.rb +43 -43
  79. data/lib/makit/gems.rb +49 -49
  80. data/lib/makit/git/cli.rb +103 -103
  81. data/lib/makit/git/repository.rb +100 -100
  82. data/lib/makit/git.rb +104 -104
  83. data/lib/makit/github_actions.rb +202 -202
  84. data/lib/makit/gitlab/pipeline.rb +857 -857
  85. data/lib/makit/gitlab/pipeline_service_impl.rb +1535 -1535
  86. data/lib/makit/gitlab_runner.rb +59 -59
  87. data/lib/makit/humanize.rb +218 -218
  88. data/lib/makit/indexer.rb +47 -47
  89. data/lib/makit/io/filesystem.rb +111 -111
  90. data/lib/makit/io/filesystem_service_impl.rb +337 -337
  91. data/lib/makit/lint.rb +212 -212
  92. data/lib/makit/logging/configuration.rb +309 -309
  93. data/lib/makit/logging/format_registry.rb +84 -84
  94. data/lib/makit/logging/formatters/base.rb +39 -39
  95. data/lib/makit/logging/formatters/console_formatter.rb +140 -140
  96. data/lib/makit/logging/formatters/json_formatter.rb +65 -65
  97. data/lib/makit/logging/formatters/plain_text_formatter.rb +71 -71
  98. data/lib/makit/logging/formatters/text_formatter.rb +64 -64
  99. data/lib/makit/logging/log_request.rb +119 -119
  100. data/lib/makit/logging/logger.rb +199 -199
  101. data/lib/makit/logging/sinks/base.rb +91 -91
  102. data/lib/makit/logging/sinks/console.rb +72 -72
  103. data/lib/makit/logging/sinks/file_sink.rb +92 -92
  104. data/lib/makit/logging/sinks/structured.rb +123 -123
  105. data/lib/makit/logging/sinks/unified_file_sink.rb +296 -296
  106. data/lib/makit/logging.rb +578 -578
  107. data/lib/makit/markdown.rb +75 -75
  108. data/lib/makit/mp/basic_object_mp.rb +17 -17
  109. data/lib/makit/mp/command_mp.rb +13 -13
  110. data/lib/makit/mp/command_request.mp.rb +17 -17
  111. data/lib/makit/mp/project_mp.rb +199 -199
  112. data/lib/makit/mp/string_mp.rb +205 -205
  113. data/lib/makit/nuget.rb +460 -458
  114. data/lib/makit/podman/podman.rb +458 -458
  115. data/lib/makit/podman/podman_service_impl.rb +1081 -1081
  116. data/lib/makit/port.rb +32 -32
  117. data/lib/makit/process.rb +377 -377
  118. data/lib/makit/protoc.rb +112 -112
  119. data/lib/makit/rake/cli.rb +196 -196
  120. data/lib/makit/rake/trace_controller.rb +174 -174
  121. data/lib/makit/rake.rb +81 -81
  122. data/lib/makit/ruby/cli.rb +185 -185
  123. data/lib/makit/ruby.rb +25 -25
  124. data/lib/makit/rubygems.rb +137 -137
  125. data/lib/makit/secrets/azure_key_vault.rb +322 -322
  126. data/lib/makit/secrets/azure_secrets.rb +221 -221
  127. data/lib/makit/secrets/local_secrets.rb +72 -72
  128. data/lib/makit/secrets/secrets_manager.rb +105 -105
  129. data/lib/makit/secrets.rb +96 -96
  130. data/lib/makit/serializer.rb +130 -130
  131. data/lib/makit/services/builder.rb +186 -186
  132. data/lib/makit/services/error_handler.rb +226 -226
  133. data/lib/makit/services/repository_manager.rb +367 -367
  134. data/lib/makit/services/validator.rb +112 -112
  135. data/lib/makit/setup/classlib.rb +101 -101
  136. data/lib/makit/setup/gem.rb +268 -268
  137. data/lib/makit/setup/pages.rb +11 -11
  138. data/lib/makit/setup/razorclasslib.rb +101 -101
  139. data/lib/makit/setup/runner.rb +54 -54
  140. data/lib/makit/setup.rb +5 -5
  141. data/lib/makit/show.rb +110 -110
  142. data/lib/makit/storage.rb +126 -126
  143. data/lib/makit/symbols.rb +175 -175
  144. data/lib/makit/task_info.rb +130 -130
  145. data/lib/makit/tasks/at_exit.rb +15 -15
  146. data/lib/makit/tasks/build.rb +22 -22
  147. data/lib/makit/tasks/bump.rb +7 -7
  148. data/lib/makit/tasks/clean.rb +13 -13
  149. data/lib/makit/tasks/configure.rb +10 -10
  150. data/lib/makit/tasks/format.rb +10 -10
  151. data/lib/makit/tasks/hook_manager.rb +443 -443
  152. data/lib/makit/tasks/info.rb +368 -368
  153. data/lib/makit/tasks/init.rb +49 -49
  154. data/lib/makit/tasks/integrate.rb +60 -60
  155. data/lib/makit/tasks/pull_incoming.rb +13 -13
  156. data/lib/makit/tasks/secrets.rb +7 -7
  157. data/lib/makit/tasks/setup.rb +16 -16
  158. data/lib/makit/tasks/sync.rb +14 -14
  159. data/lib/makit/tasks/tag.rb +27 -27
  160. data/lib/makit/tasks/task_monkey_patch.rb +81 -81
  161. data/lib/makit/tasks/test.rb +22 -22
  162. data/lib/makit/tasks/update.rb +21 -21
  163. data/lib/makit/tasks/version.rb +6 -6
  164. data/lib/makit/tasks.rb +24 -24
  165. data/lib/makit/test_cache.rb +239 -239
  166. data/lib/makit/tree.rb +37 -37
  167. data/lib/makit/v1/configuration/project_service_impl.rb +370 -370
  168. data/lib/makit/v1/git/git_repository_service_impl.rb +295 -295
  169. data/lib/makit/v1/makit.v1_pb.rb +35 -35
  170. data/lib/makit/v1/makit.v1_services_pb.rb +27 -27
  171. data/lib/makit/v1/services/repository_manager_service_impl.rb +572 -572
  172. data/lib/makit/version.rb +661 -661
  173. data/lib/makit/version_util.rb +21 -21
  174. data/lib/makit/wix.rb +95 -95
  175. data/lib/makit/yaml.rb +29 -29
  176. data/lib/makit/zip.rb +17 -17
  177. data/lib/makit copy.rb +44 -44
  178. data/lib/makit.rb +121 -121
  179. metadata +2 -2
data/lib/makit/process.rb CHANGED
@@ -1,377 +1,377 @@
1
- # frozen_string_literal: true
2
-
3
- # This module provides classes for the Makit gem.
4
- module Makit
5
- # Cross-platform process management utilities
6
- #
7
- # This class provides methods for checking, listing, and terminating processes
8
- # across different operating systems (Windows, Linux, macOS). It abstracts
9
- # platform-specific commands into a unified interface.
10
- class Process
11
- # Check if a process with the given name is currently running
12
- #
13
- # Uses platform-specific commands:
14
- # - Windows: tasklist command
15
- # - Unix: ps command with grep filtering
16
- #
17
- # @param name [String] Name of the process to check (without .exe extension)
18
- # @return [Boolean] True if the process is running, false otherwise
19
- def self.is_running?(name)
20
- return false if name.nil? || name.strip.empty?
21
-
22
- # provide a cross-platform way to check if a process is running
23
- results = if Makit::Environment.is_windows?
24
- # on windows, use the tasklist command
25
- `tasklist /FI "imagename eq #{name}.exe" 2>nul`
26
- else
27
- # on linux/mac, use the ps command
28
- `ps aux | grep "#{name}" | grep -v grep 2>/dev/null`
29
- end
30
- results.include?(name)
31
- end
32
-
33
- # Terminate all processes that match a given name
34
- #
35
- # Uses platform-specific termination commands:
36
- # - Windows: taskkill command
37
- # - Unix: kill command with SIGKILL (-9)
38
- #
39
- # @param name [String] Name of the process(es) to terminate
40
- # @return [Array<String>] Array of killed process IDs (empty if none found/killed)
41
- def self.kill(name)
42
- return [] if name.nil? || name.strip.empty?
43
-
44
- killed_pids = []
45
-
46
- if Makit::Environment.is_windows?
47
- # Windows: Use tasklist to find PIDs first, then kill them
48
- begin
49
- # Find processes by name
50
- tasklist_output = `tasklist /FI "imagename eq #{name}.exe" /FO CSV 2>nul`
51
-
52
- # Parse CSV output to extract PIDs
53
- lines = tasklist_output.split("\n")
54
- if lines.length > 1 # Skip header line
55
- lines[1..].each do |line|
56
- parts = line.split(",")
57
- next unless parts.length >= 2
58
-
59
- pid = parts[1]&.gsub('"', "")&.strip
60
- next unless pid && !pid.empty? && pid.match?(/^\d+$/)
61
-
62
- begin
63
- `taskkill /PID #{pid} /F 2>nul`
64
- killed_pids << pid
65
- rescue StandardError => e
66
- # Log error but continue with other processes
67
- puts "Failed to kill process #{pid}: #{e.message}"
68
- end
69
- end
70
- end
71
- rescue StandardError => e
72
- # If tasklist fails, try direct taskkill by name
73
- begin
74
- `taskkill /IM "#{name}.exe" /F 2>nul`
75
- # Since we can't get PIDs from direct kill, return empty array
76
- # but don't raise error
77
- rescue StandardError => e2
78
- # Both methods failed, return empty array
79
- puts "Failed to kill processes by name #{name}: #{e2.message}"
80
- end
81
- end
82
- else
83
- # Unix (Linux/Mac): Use pgrep to find PIDs, then kill them
84
- begin
85
- # Use pgrep to find PIDs, handle both Linux and Mac
86
- pids = if Makit::Environment.is_mac?
87
- # Mac: pgrep -f might not work the same way
88
- `pgrep -x "#{name}" 2>/dev/null`.split("\n").reject(&:empty?)
89
- else
90
- # Linux: pgrep -f works well
91
- `pgrep -f "#{name}" 2>/dev/null`.split("\n").reject(&:empty?)
92
- end
93
-
94
- pids.each do |pid|
95
- next unless pid.match?(/^\d+$/)
96
-
97
- begin
98
- `kill -9 #{pid} 2>/dev/null`
99
- killed_pids << pid
100
- rescue StandardError => e
101
- puts "Failed to kill process #{pid}: #{e.message}"
102
- end
103
- end
104
- rescue StandardError => e
105
- # If pgrep fails, return empty array but don't raise error
106
- puts "Failed to find processes by name #{name}: #{e.message}"
107
- end
108
- end
109
-
110
- killed_pids
111
- end
112
-
113
- # List all running processes on the system
114
- #
115
- # Uses platform-specific commands to get a complete process listing:
116
- # - Windows: tasklist command
117
- # - Unix: ps aux command
118
- #
119
- # @return [String] Raw output from the system process listing command
120
- def self.list
121
- if Makit::Environment.is_windows?
122
- `tasklist 2>nul`
123
- else
124
- `ps aux 2>/dev/null`
125
- end
126
- end
127
-
128
- # List all running processes for the current user
129
- #
130
- # Filters the process list to show only processes owned by the current user.
131
- # Uses platform-specific filtering:
132
- # - Windows: tasklist with USERNAME filter
133
- # - Unix: ps aux with grep filtering
134
- #
135
- # @return [String] Raw output from the filtered process listing command
136
- def self.list_current_user
137
- if Makit::Environment.is_windows?
138
- `tasklist /FI "STATUS eq running and USERNAME eq #{Makit::Environment.current_user}" 2>nul`
139
- else
140
- `ps aux | grep "#{Makit::Environment.current_user}" | grep -v grep 2>/dev/null`
141
- end
142
- end
143
-
144
- # List processes for the current user filtered by name
145
- #
146
- # Returns an array of process lines for consistency across platforms.
147
- # Filters by both current user and process name.
148
- #
149
- # @param name [String] Process name to filter by
150
- # @return [Array<String>] Array of process lines matching the criteria
151
- def self.list_current_user_processes(name)
152
- if Makit::Environment.is_windows?
153
- # filter the results to only include the current user
154
- results = `tasklist /FI "STATUS eq running and USERNAME eq #{Makit::Environment.current_user}" 2>nul`
155
- results.split("\n").select { |line| line.include?(Makit::Environment.current_user) }
156
- else
157
- # Unix (Linux/Mac): return as array for consistency
158
- results = `ps aux | grep "#{name}" | grep -v grep 2>/dev/null`
159
- results.split("\n").reject(&:empty?)
160
- end
161
- end
162
-
163
- # Run a command in the background and track it with a PID file
164
- #
165
- # This method starts a process in the background and stores its PID in a file
166
- # for later management. It handles cross-platform differences in process spawning.
167
- #
168
- # @param command [String] The command to run in the background
169
- # @param pid_file [String] Path to the PID file to store the process ID
170
- # @param working_dir [String, nil] Working directory for the process (optional)
171
- # @param environment [Hash, nil] Environment variables to set (optional)
172
- # @return [Integer] The process ID of the started process
173
- # @raise [StandardError] If the process fails to start
174
- def self.run_background(command, pid_file, working_dir: nil, environment: {})
175
- # Ensure the command is not nil or empty
176
- raise ArgumentError, "Command cannot be nil or empty" if command.nil? || command.strip.empty?
177
- raise ArgumentError, "PID file path cannot be nil or empty" if pid_file.nil? || pid_file.strip.empty?
178
-
179
- # Convert command to array for Process.spawn
180
- cmd_array = if Makit::Environment.is_windows?
181
- # Windows: Use cmd /c for complex commands
182
- ["cmd", "/c", command]
183
- else
184
- # Unix-like: Use shell for command parsing
185
- ["sh", "-c", command]
186
- end
187
-
188
- # Prepare spawn options
189
- spawn_options = {}
190
- spawn_options[:chdir] = working_dir if working_dir
191
- spawn_options[:pgroup] = true unless Makit::Environment.is_windows?
192
-
193
- # Set environment variables
194
- env = ENV.to_h.merge(environment)
195
- spawn_options[:unsetenv_others] = false
196
-
197
- begin
198
- # Start the process
199
- pid = Process.spawn(env, *cmd_array, spawn_options)
200
-
201
- # Write PID to file
202
- File.write(pid_file, pid.to_s)
203
-
204
- # Give the process a moment to start
205
- sleep(0.5)
206
-
207
- # Verify the process is still running
208
- begin
209
- Process.getpgid(pid) if Makit::Environment.is_windows?
210
- Process.getpgid(pid) unless Makit::Environment.is_windows?
211
- rescue Errno::ESRCH
212
- raise StandardError, "Process failed to start or exited immediately"
213
- end
214
-
215
- pid
216
- rescue => e
217
- # Clean up PID file if process failed to start
218
- File.delete(pid_file) if File.exist?(pid_file)
219
- raise StandardError, "Failed to start background process: #{e.message}"
220
- end
221
- end
222
-
223
- # Stop a background process using its PID file
224
- #
225
- # This method reads the PID from a file and terminates the process gracefully.
226
- # It handles cross-platform differences in process termination.
227
- #
228
- # @param pid_file [String] Path to the PID file containing the process ID
229
- # @param signal [String] Signal to send (default: "TERM" for graceful shutdown)
230
- # @return [Boolean] True if the process was stopped, false if it wasn't running
231
- def self.stop_background(pid_file, signal: "TERM")
232
- return false unless File.exist?(pid_file)
233
-
234
- begin
235
- pid = File.read(pid_file).strip.to_i
236
- return false if pid <= 0
237
-
238
- # Check if process is still running
239
- begin
240
- if Makit::Environment.is_windows?
241
- # Windows: Use taskkill
242
- if signal == "TERM"
243
- system("taskkill /PID #{pid} /F >nul 2>&1")
244
- else
245
- system("taskkill /PID #{pid} /F >nul 2>&1")
246
- end
247
- else
248
- # Unix-like: Use kill with specified signal
249
- Process.kill(signal, pid)
250
- end
251
-
252
- # Wait a moment for the process to terminate
253
- sleep(0.5)
254
-
255
- # Verify the process is gone
256
- begin
257
- Process.getpgid(pid)
258
- # Process still running, force kill if TERM didn't work
259
- if signal == "TERM"
260
- return stop_background(pid_file, signal: "KILL")
261
- end
262
- return false
263
- rescue Errno::ESRCH
264
- # Process is gone, success
265
- true
266
- end
267
- rescue Errno::ESRCH, Errno::EPERM
268
- # Process was not running or permission denied
269
- false
270
- end
271
- rescue => e
272
- # Log error but don't raise
273
- puts "Error stopping background process: #{e.message}" if defined?(puts)
274
- false
275
- ensure
276
- # Clean up PID file
277
- File.delete(pid_file) if File.exist?(pid_file)
278
- end
279
- end
280
-
281
- # Check if a background process is still running using its PID file
282
- #
283
- # @param pid_file [String] Path to the PID file containing the process ID
284
- # @return [Boolean] True if the process is running, false otherwise
285
- def self.background_running?(pid_file)
286
- return false unless File.exist?(pid_file)
287
-
288
- begin
289
- pid = File.read(pid_file).strip.to_i
290
- return false if pid <= 0
291
-
292
- # Check if process is still running
293
- Process.getpgid(pid)
294
- true
295
- rescue Errno::ESRCH, Errno::EPERM
296
- false
297
- rescue => e
298
- # Log error but return false
299
- puts "Error checking background process: #{e.message}" if defined?(puts)
300
- false
301
- end
302
- end
303
-
304
- # Get the PID of a background process from its PID file
305
- #
306
- # @param pid_file [String] Path to the PID file containing the process ID
307
- # @return [Integer, nil] The process ID, or nil if not found or invalid
308
- def self.get_background_pid(pid_file)
309
- return nil unless File.exist?(pid_file)
310
-
311
- begin
312
- pid = File.read(pid_file).strip.to_i
313
- return nil if pid <= 0
314
- pid
315
- rescue => e
316
- puts "Error reading PID file: #{e.message}" if defined?(puts)
317
- nil
318
- end
319
- end
320
-
321
- # Clean up stale PID files (processes that are no longer running)
322
- #
323
- # @param pid_files [Array<String>] Array of PID file paths to check
324
- # @return [Array<String>] Array of cleaned up PID file paths
325
- def self.cleanup_stale_pids(pid_files)
326
- cleaned = []
327
-
328
- pid_files.each do |pid_file|
329
- next unless File.exist?(pid_file)
330
-
331
- unless background_running?(pid_file)
332
- File.delete(pid_file)
333
- cleaned << pid_file
334
- end
335
- end
336
-
337
- cleaned
338
- end
339
-
340
- # Health check for a service running on a specific port
341
- #
342
- # @param port [Integer] Port number to check
343
- # @param host [String] Host to check (default: "localhost")
344
- # @param timeout [Integer] Timeout in seconds (default: 5)
345
- # @return [Boolean] True if the service is responding, false otherwise
346
- def self.health_check_port(port, host: "localhost", timeout: 5)
347
- require 'net/http'
348
- require 'timeout'
349
-
350
- begin
351
- uri = URI("http://#{host}:#{port}")
352
- Timeout.timeout(timeout) do
353
- response = Net::HTTP.get_response(uri)
354
- response.code.to_i < 500 # Consider 4xx as "responding" but 5xx as "unhealthy"
355
- end
356
- rescue => e
357
- false
358
- end
359
- end
360
-
361
- # Health check for multiple ports
362
- #
363
- # @param ports [Array<Integer>] Array of port numbers to check
364
- # @param host [String] Host to check (default: "localhost")
365
- # @param timeout [Integer] Timeout in seconds (default: 5)
366
- # @return [Hash] Hash with port numbers as keys and health status as values
367
- def self.health_check_ports(ports, host: "localhost", timeout: 5)
368
- results = {}
369
-
370
- ports.each do |port|
371
- results[port] = health_check_port(port, host: host, timeout: timeout)
372
- end
373
-
374
- results
375
- end
376
- end
377
- end
1
+ # frozen_string_literal: true
2
+
3
+ # This module provides classes for the Makit gem.
4
+ module Makit
5
+ # Cross-platform process management utilities
6
+ #
7
+ # This class provides methods for checking, listing, and terminating processes
8
+ # across different operating systems (Windows, Linux, macOS). It abstracts
9
+ # platform-specific commands into a unified interface.
10
+ class Process
11
+ # Check if a process with the given name is currently running
12
+ #
13
+ # Uses platform-specific commands:
14
+ # - Windows: tasklist command
15
+ # - Unix: ps command with grep filtering
16
+ #
17
+ # @param name [String] Name of the process to check (without .exe extension)
18
+ # @return [Boolean] True if the process is running, false otherwise
19
+ def self.is_running?(name)
20
+ return false if name.nil? || name.strip.empty?
21
+
22
+ # provide a cross-platform way to check if a process is running
23
+ results = if Makit::Environment.is_windows?
24
+ # on windows, use the tasklist command
25
+ `tasklist /FI "imagename eq #{name}.exe" 2>nul`
26
+ else
27
+ # on linux/mac, use the ps command
28
+ `ps aux | grep "#{name}" | grep -v grep 2>/dev/null`
29
+ end
30
+ results.include?(name)
31
+ end
32
+
33
+ # Terminate all processes that match a given name
34
+ #
35
+ # Uses platform-specific termination commands:
36
+ # - Windows: taskkill command
37
+ # - Unix: kill command with SIGKILL (-9)
38
+ #
39
+ # @param name [String] Name of the process(es) to terminate
40
+ # @return [Array<String>] Array of killed process IDs (empty if none found/killed)
41
+ def self.kill(name)
42
+ return [] if name.nil? || name.strip.empty?
43
+
44
+ killed_pids = []
45
+
46
+ if Makit::Environment.is_windows?
47
+ # Windows: Use tasklist to find PIDs first, then kill them
48
+ begin
49
+ # Find processes by name
50
+ tasklist_output = `tasklist /FI "imagename eq #{name}.exe" /FO CSV 2>nul`
51
+
52
+ # Parse CSV output to extract PIDs
53
+ lines = tasklist_output.split("\n")
54
+ if lines.length > 1 # Skip header line
55
+ lines[1..].each do |line|
56
+ parts = line.split(",")
57
+ next unless parts.length >= 2
58
+
59
+ pid = parts[1]&.gsub('"', "")&.strip
60
+ next unless pid && !pid.empty? && pid.match?(/^\d+$/)
61
+
62
+ begin
63
+ `taskkill /PID #{pid} /F 2>nul`
64
+ killed_pids << pid
65
+ rescue StandardError => e
66
+ # Log error but continue with other processes
67
+ puts "Failed to kill process #{pid}: #{e.message}"
68
+ end
69
+ end
70
+ end
71
+ rescue StandardError => e
72
+ # If tasklist fails, try direct taskkill by name
73
+ begin
74
+ `taskkill /IM "#{name}.exe" /F 2>nul`
75
+ # Since we can't get PIDs from direct kill, return empty array
76
+ # but don't raise error
77
+ rescue StandardError => e2
78
+ # Both methods failed, return empty array
79
+ puts "Failed to kill processes by name #{name}: #{e2.message}"
80
+ end
81
+ end
82
+ else
83
+ # Unix (Linux/Mac): Use pgrep to find PIDs, then kill them
84
+ begin
85
+ # Use pgrep to find PIDs, handle both Linux and Mac
86
+ pids = if Makit::Environment.is_mac?
87
+ # Mac: pgrep -f might not work the same way
88
+ `pgrep -x "#{name}" 2>/dev/null`.split("\n").reject(&:empty?)
89
+ else
90
+ # Linux: pgrep -f works well
91
+ `pgrep -f "#{name}" 2>/dev/null`.split("\n").reject(&:empty?)
92
+ end
93
+
94
+ pids.each do |pid|
95
+ next unless pid.match?(/^\d+$/)
96
+
97
+ begin
98
+ `kill -9 #{pid} 2>/dev/null`
99
+ killed_pids << pid
100
+ rescue StandardError => e
101
+ puts "Failed to kill process #{pid}: #{e.message}"
102
+ end
103
+ end
104
+ rescue StandardError => e
105
+ # If pgrep fails, return empty array but don't raise error
106
+ puts "Failed to find processes by name #{name}: #{e.message}"
107
+ end
108
+ end
109
+
110
+ killed_pids
111
+ end
112
+
113
+ # List all running processes on the system
114
+ #
115
+ # Uses platform-specific commands to get a complete process listing:
116
+ # - Windows: tasklist command
117
+ # - Unix: ps aux command
118
+ #
119
+ # @return [String] Raw output from the system process listing command
120
+ def self.list
121
+ if Makit::Environment.is_windows?
122
+ `tasklist 2>nul`
123
+ else
124
+ `ps aux 2>/dev/null`
125
+ end
126
+ end
127
+
128
+ # List all running processes for the current user
129
+ #
130
+ # Filters the process list to show only processes owned by the current user.
131
+ # Uses platform-specific filtering:
132
+ # - Windows: tasklist with USERNAME filter
133
+ # - Unix: ps aux with grep filtering
134
+ #
135
+ # @return [String] Raw output from the filtered process listing command
136
+ def self.list_current_user
137
+ if Makit::Environment.is_windows?
138
+ `tasklist /FI "STATUS eq running and USERNAME eq #{Makit::Environment.current_user}" 2>nul`
139
+ else
140
+ `ps aux | grep "#{Makit::Environment.current_user}" | grep -v grep 2>/dev/null`
141
+ end
142
+ end
143
+
144
+ # List processes for the current user filtered by name
145
+ #
146
+ # Returns an array of process lines for consistency across platforms.
147
+ # Filters by both current user and process name.
148
+ #
149
+ # @param name [String] Process name to filter by
150
+ # @return [Array<String>] Array of process lines matching the criteria
151
+ def self.list_current_user_processes(name)
152
+ if Makit::Environment.is_windows?
153
+ # filter the results to only include the current user
154
+ results = `tasklist /FI "STATUS eq running and USERNAME eq #{Makit::Environment.current_user}" 2>nul`
155
+ results.split("\n").select { |line| line.include?(Makit::Environment.current_user) }
156
+ else
157
+ # Unix (Linux/Mac): return as array for consistency
158
+ results = `ps aux | grep "#{name}" | grep -v grep 2>/dev/null`
159
+ results.split("\n").reject(&:empty?)
160
+ end
161
+ end
162
+
163
+ # Run a command in the background and track it with a PID file
164
+ #
165
+ # This method starts a process in the background and stores its PID in a file
166
+ # for later management. It handles cross-platform differences in process spawning.
167
+ #
168
+ # @param command [String] The command to run in the background
169
+ # @param pid_file [String] Path to the PID file to store the process ID
170
+ # @param working_dir [String, nil] Working directory for the process (optional)
171
+ # @param environment [Hash, nil] Environment variables to set (optional)
172
+ # @return [Integer] The process ID of the started process
173
+ # @raise [StandardError] If the process fails to start
174
+ def self.run_background(command, pid_file, working_dir: nil, environment: {})
175
+ # Ensure the command is not nil or empty
176
+ raise ArgumentError, "Command cannot be nil or empty" if command.nil? || command.strip.empty?
177
+ raise ArgumentError, "PID file path cannot be nil or empty" if pid_file.nil? || pid_file.strip.empty?
178
+
179
+ # Convert command to array for Process.spawn
180
+ cmd_array = if Makit::Environment.is_windows?
181
+ # Windows: Use cmd /c for complex commands
182
+ ["cmd", "/c", command]
183
+ else
184
+ # Unix-like: Use shell for command parsing
185
+ ["sh", "-c", command]
186
+ end
187
+
188
+ # Prepare spawn options
189
+ spawn_options = {}
190
+ spawn_options[:chdir] = working_dir if working_dir
191
+ spawn_options[:pgroup] = true unless Makit::Environment.is_windows?
192
+
193
+ # Set environment variables
194
+ env = ENV.to_h.merge(environment)
195
+ spawn_options[:unsetenv_others] = false
196
+
197
+ begin
198
+ # Start the process
199
+ pid = Process.spawn(env, *cmd_array, spawn_options)
200
+
201
+ # Write PID to file
202
+ File.write(pid_file, pid.to_s)
203
+
204
+ # Give the process a moment to start
205
+ sleep(0.5)
206
+
207
+ # Verify the process is still running
208
+ begin
209
+ Process.getpgid(pid) if Makit::Environment.is_windows?
210
+ Process.getpgid(pid) unless Makit::Environment.is_windows?
211
+ rescue Errno::ESRCH
212
+ raise StandardError, "Process failed to start or exited immediately"
213
+ end
214
+
215
+ pid
216
+ rescue => e
217
+ # Clean up PID file if process failed to start
218
+ File.delete(pid_file) if File.exist?(pid_file)
219
+ raise StandardError, "Failed to start background process: #{e.message}"
220
+ end
221
+ end
222
+
223
+ # Stop a background process using its PID file
224
+ #
225
+ # This method reads the PID from a file and terminates the process gracefully.
226
+ # It handles cross-platform differences in process termination.
227
+ #
228
+ # @param pid_file [String] Path to the PID file containing the process ID
229
+ # @param signal [String] Signal to send (default: "TERM" for graceful shutdown)
230
+ # @return [Boolean] True if the process was stopped, false if it wasn't running
231
+ def self.stop_background(pid_file, signal: "TERM")
232
+ return false unless File.exist?(pid_file)
233
+
234
+ begin
235
+ pid = File.read(pid_file).strip.to_i
236
+ return false if pid <= 0
237
+
238
+ # Check if process is still running
239
+ begin
240
+ if Makit::Environment.is_windows?
241
+ # Windows: Use taskkill
242
+ if signal == "TERM"
243
+ system("taskkill /PID #{pid} /F >nul 2>&1")
244
+ else
245
+ system("taskkill /PID #{pid} /F >nul 2>&1")
246
+ end
247
+ else
248
+ # Unix-like: Use kill with specified signal
249
+ Process.kill(signal, pid)
250
+ end
251
+
252
+ # Wait a moment for the process to terminate
253
+ sleep(0.5)
254
+
255
+ # Verify the process is gone
256
+ begin
257
+ Process.getpgid(pid)
258
+ # Process still running, force kill if TERM didn't work
259
+ if signal == "TERM"
260
+ return stop_background(pid_file, signal: "KILL")
261
+ end
262
+ return false
263
+ rescue Errno::ESRCH
264
+ # Process is gone, success
265
+ true
266
+ end
267
+ rescue Errno::ESRCH, Errno::EPERM
268
+ # Process was not running or permission denied
269
+ false
270
+ end
271
+ rescue => e
272
+ # Log error but don't raise
273
+ puts "Error stopping background process: #{e.message}" if defined?(puts)
274
+ false
275
+ ensure
276
+ # Clean up PID file
277
+ File.delete(pid_file) if File.exist?(pid_file)
278
+ end
279
+ end
280
+
281
+ # Check if a background process is still running using its PID file
282
+ #
283
+ # @param pid_file [String] Path to the PID file containing the process ID
284
+ # @return [Boolean] True if the process is running, false otherwise
285
+ def self.background_running?(pid_file)
286
+ return false unless File.exist?(pid_file)
287
+
288
+ begin
289
+ pid = File.read(pid_file).strip.to_i
290
+ return false if pid <= 0
291
+
292
+ # Check if process is still running
293
+ Process.getpgid(pid)
294
+ true
295
+ rescue Errno::ESRCH, Errno::EPERM
296
+ false
297
+ rescue => e
298
+ # Log error but return false
299
+ puts "Error checking background process: #{e.message}" if defined?(puts)
300
+ false
301
+ end
302
+ end
303
+
304
+ # Get the PID of a background process from its PID file
305
+ #
306
+ # @param pid_file [String] Path to the PID file containing the process ID
307
+ # @return [Integer, nil] The process ID, or nil if not found or invalid
308
+ def self.get_background_pid(pid_file)
309
+ return nil unless File.exist?(pid_file)
310
+
311
+ begin
312
+ pid = File.read(pid_file).strip.to_i
313
+ return nil if pid <= 0
314
+ pid
315
+ rescue => e
316
+ puts "Error reading PID file: #{e.message}" if defined?(puts)
317
+ nil
318
+ end
319
+ end
320
+
321
+ # Clean up stale PID files (processes that are no longer running)
322
+ #
323
+ # @param pid_files [Array<String>] Array of PID file paths to check
324
+ # @return [Array<String>] Array of cleaned up PID file paths
325
+ def self.cleanup_stale_pids(pid_files)
326
+ cleaned = []
327
+
328
+ pid_files.each do |pid_file|
329
+ next unless File.exist?(pid_file)
330
+
331
+ unless background_running?(pid_file)
332
+ File.delete(pid_file)
333
+ cleaned << pid_file
334
+ end
335
+ end
336
+
337
+ cleaned
338
+ end
339
+
340
+ # Health check for a service running on a specific port
341
+ #
342
+ # @param port [Integer] Port number to check
343
+ # @param host [String] Host to check (default: "localhost")
344
+ # @param timeout [Integer] Timeout in seconds (default: 5)
345
+ # @return [Boolean] True if the service is responding, false otherwise
346
+ def self.health_check_port(port, host: "localhost", timeout: 5)
347
+ require 'net/http'
348
+ require 'timeout'
349
+
350
+ begin
351
+ uri = URI("http://#{host}:#{port}")
352
+ Timeout.timeout(timeout) do
353
+ response = Net::HTTP.get_response(uri)
354
+ response.code.to_i < 500 # Consider 4xx as "responding" but 5xx as "unhealthy"
355
+ end
356
+ rescue => e
357
+ false
358
+ end
359
+ end
360
+
361
+ # Health check for multiple ports
362
+ #
363
+ # @param ports [Array<Integer>] Array of port numbers to check
364
+ # @param host [String] Host to check (default: "localhost")
365
+ # @param timeout [Integer] Timeout in seconds (default: 5)
366
+ # @return [Hash] Hash with port numbers as keys and health status as values
367
+ def self.health_check_ports(ports, host: "localhost", timeout: 5)
368
+ results = {}
369
+
370
+ ports.each do |port|
371
+ results[port] = health_check_port(port, host: host, timeout: timeout)
372
+ end
373
+
374
+ results
375
+ end
376
+ end
377
+ end