main
 1# frozen_string_literal: true
 2
 3module Elelem
 4  module Verifiers
 5    SYNTAX = {
 6      ".rb" => "ruby -c %{path}",
 7      ".erb" => "erb -x %{path} | ruby -c",
 8      ".py" => "python -m py_compile %{path}",
 9      ".go" => "go vet %{path}",
10      ".rs" => "cargo check --quiet",
11      ".ts" => "npx tsc --noEmit %{path}",
12      ".js" => "node --check %{path}",
13    }.freeze
14
15    def self.for(path)
16      return [] unless path
17
18      cmds = []
19      ext = File.extname(path)
20      cmds << (SYNTAX[ext] % { path: path }) if SYNTAX[ext]
21      cmds << test_runner
22      cmds.compact
23    end
24
25    def self.test_runner
26      %w[bin/test script/test].find { |s| File.executable?(s) }
27    end
28  end
29
30  Plugins.register(:verify) do |agent|
31    agent.toolbox.add("verify",
32      description: "Verify file syntax and run tests",
33      params: { path: { type: "string" } },
34      required: ["path"]
35    ) do |a|
36      path = a["path"]
37      Verifiers.for(path).inject({verified: []}) do |memo, cmd|
38        agent.terminal.say agent.toolbox.header("execute", { "command" => cmd })
39        v = agent.toolbox.run("execute", { "command" => cmd })
40        break v.merge(path: path, command: cmd) if v[:exit_status] != 0
41
42        memo[:verified] << cmd
43        memo
44      end
45    end
46  end
47end