|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "json" |
| 5 | +require "pathname" |
| 6 | +require "set" |
| 7 | + |
| 8 | +ROOT = Pathname(__dir__).join("..").expand_path |
| 9 | +API_JSON = ROOT.join("api.json") |
| 10 | +APIIGNORE_JSON = ROOT.join("apiignore.json") |
| 11 | +TOKENIZER_TYPE_KEY_PREFIX = "PDB_TYPE_TOKENIZER_" |
| 12 | + |
| 13 | +def load_json(path) |
| 14 | + JSON.parse(path.read) |
| 15 | +rescue Errno::ENOENT |
| 16 | + raise "#{path} not found" |
| 17 | +rescue JSON::ParserError => e |
| 18 | + raise "invalid JSON in #{path}: #{e.message}" |
| 19 | +end |
| 20 | + |
| 21 | +def flatten_ignore(section, kind:) |
| 22 | + case section |
| 23 | + when nil |
| 24 | + Set.new |
| 25 | + when Array |
| 26 | + Set.new(section.map(&:to_s)) |
| 27 | + when Hash |
| 28 | + Set.new(section.values.flatten.map(&:to_s)) |
| 29 | + else |
| 30 | + raise "apiignore #{kind} section must be an Array or object of Arrays" |
| 31 | + end |
| 32 | +end |
| 33 | + |
| 34 | +def source_paths |
| 35 | + files = Dir.glob(ROOT.join("lib/**/*.rb").to_s).sort |
| 36 | + rakefile = ROOT.join("Rakefile") |
| 37 | + files << rakefile.to_s if rakefile.file? |
| 38 | + files |
| 39 | +end |
| 40 | + |
| 41 | +def read_sources |
| 42 | + source_paths.to_h { |path| [path, File.read(path)] } |
| 43 | +end |
| 44 | + |
| 45 | +def missing_quoted_tokens(expected_tokens, source_text) |
| 46 | + expected_tokens.select do |token| |
| 47 | + !source_text.match?(/["']#{Regexp.escape(token)}["']/) |
| 48 | + end.sort |
| 49 | +end |
| 50 | + |
| 51 | +def missing_symbols(expected_symbols, source_text) |
| 52 | + expected_symbols.select { |symbol| !source_text.include?(symbol) }.sort |
| 53 | +end |
| 54 | + |
| 55 | +def main |
| 56 | + api = load_json(API_JSON) |
| 57 | + apiignore = APIIGNORE_JSON.file? ? load_json(APIIGNORE_JSON) : {} |
| 58 | + |
| 59 | + operators = Set.new(api.fetch("operators").values.map(&:to_s)) |
| 60 | + functions = Set.new(api.fetch("functions").values.map(&:to_s)) |
| 61 | + all_types_by_key = api.fetch("types").transform_keys(&:to_s).transform_values(&:to_s) |
| 62 | + |
| 63 | + tokenizer_types = Set.new( |
| 64 | + all_types_by_key |
| 65 | + .select { |name, _| name.start_with?(TOKENIZER_TYPE_KEY_PREFIX) } |
| 66 | + .values |
| 67 | + ) |
| 68 | + static_types = Set.new( |
| 69 | + all_types_by_key |
| 70 | + .reject { |name, _| name.start_with?(TOKENIZER_TYPE_KEY_PREFIX) } |
| 71 | + .values |
| 72 | + ) |
| 73 | + |
| 74 | + ignored_operators = flatten_ignore(apiignore["operators"], kind: "operators") |
| 75 | + ignored_functions = flatten_ignore(apiignore["functions"], kind: "functions") |
| 76 | + ignored_types = flatten_ignore(apiignore["types"], kind: "types") |
| 77 | + |
| 78 | + sources = read_sources |
| 79 | + source_text = sources.values.join("\n") |
| 80 | + |
| 81 | + missing_ops = missing_quoted_tokens(operators, source_text) |
| 82 | + missing_functions = missing_symbols(functions, source_text) |
| 83 | + missing_static_types = missing_symbols(static_types, source_text) |
| 84 | + |
| 85 | + tokenizer_sql = sources.find { |path, _| path.end_with?("lib/parade_db/tokenizer_sql.rb") }&.last.to_s |
| 86 | + dynamic_tokenizer_supported = tokenizer_sql.include?('"pdb.#{function_name}"') |
| 87 | + missing_tokenizer_types = dynamic_tokenizer_supported ? [] : tokenizer_types.to_a.sort |
| 88 | + |
| 89 | + referenced_symbols = Set.new(source_text.scan(/\bpdb\.[a-zA-Z_][a-zA-Z0-9_]*\b/)) |
| 90 | + allowed_symbols = functions | static_types | tokenizer_types | ignored_functions | ignored_types |
| 91 | + untracked_symbols = (referenced_symbols - allowed_symbols).to_a.sort |
| 92 | + |
| 93 | + issues = [] |
| 94 | + |
| 95 | + unless missing_ops.empty? |
| 96 | + issues << "operators declared in api.json but not found in Ruby wrappers: #{missing_ops.join(', ')}" |
| 97 | + end |
| 98 | + |
| 99 | + unless missing_functions.empty? |
| 100 | + issues << "functions declared in api.json but not found in Ruby wrappers: #{missing_functions.join(', ')}" |
| 101 | + end |
| 102 | + |
| 103 | + unless missing_static_types.empty? |
| 104 | + issues << "types declared in api.json but not found in Ruby wrappers: #{missing_static_types.join(', ')}" |
| 105 | + end |
| 106 | + |
| 107 | + unless missing_tokenizer_types.empty? |
| 108 | + issues << "tokenizer types require dynamic tokenizer qualification, but support was not detected: #{missing_tokenizer_types.join(', ')}" |
| 109 | + end |
| 110 | + |
| 111 | + unless untracked_symbols.empty? |
| 112 | + issues << "pdb.* symbols used in code but missing from api.json/apiignore.json: #{untracked_symbols.join(', ')}" |
| 113 | + end |
| 114 | + |
| 115 | + if issues.empty? |
| 116 | + puts "✅ API coverage check passed." |
| 117 | + puts " operators: #{operators.size}, functions: #{functions.size}, types: #{all_types_by_key.size}, referenced symbols: #{referenced_symbols.size}" |
| 118 | + return 0 |
| 119 | + end |
| 120 | + |
| 121 | + warn "❌ API coverage check failed:" |
| 122 | + issues.each { |issue| warn " - #{issue}" } |
| 123 | + warn "\nUpdate api.json, apiignore.json, or wrapper usage so they stay in sync." |
| 124 | + 1 |
| 125 | +end |
| 126 | + |
| 127 | +exit(main) |
0 commit comments