Commit 3cb9cdb
Changed files (1)
lib
elelem
lib/elelem/toolbox.rb
@@ -30,6 +30,7 @@ module Elelem
def initialize(tools = TOOLS.dup)
@tools = tools
@hooks = { before: Hash.new { |h, k| h[k] = [] }, after: Hash.new { |h, k| h[k] = [] } }
+ setup_default_hooks
end
def add(name, tool)
@@ -79,12 +80,43 @@ module Elelem
end
def format_result(name, result)
- return if result[:exit_status]
+ return if result[:exit_status] && !result[:verify]
+
+ parts = []
+ if result[:verify]
+ result[:verify].each do |cmd, v|
+ status = v[:exit_status] == 0 ? "✓" : "✗"
+ parts << " #{status} #{cmd}"
+ parts << v[:content].lines.first(5).map { |l| " #{l}" }.join if v[:exit_status] != 0
+ end
+ end
text = result[:content] || result[:error] || ""
- return if text.strip.empty?
+ parts << (result[:error] ? " ! #{text.lines.first&.strip}" : text) unless text.strip.empty?
+ parts.join("\n") unless parts.empty?
+ end
+
+ private
- result[:error] ? " ! #{text.lines.first&.strip}" : text
+ def test_commands_for(path)
+ commands = []
+ commands << "ruby -c #{path}" if path&.end_with?(".rb")
+ commands << %w[script/test bin/test].find { |s| File.executable?(s) }
+ commands.compact
+ end
+
+ def setup_default_hooks
+ after("write") do |args, result|
+ next if result[:error]
+
+ result[:verify] = {}
+ test_commands_for(result[:path]).each do |cmd|
+ $stdout.puts "\n → verify: #{cmd}"
+ v = Elelem.sh("bash", args: ["-c", cmd]) { |x| $stdout.print(x) }
+ result[:verify][cmd] = v
+ break if v[:exit_status] != 0
+ end
+ end
end
end
end