<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
#!/usr/bin/env ruby
source_root = File.expand_path("../..", File.dirname(__FILE__))
$LOAD_PATH.unshift("#{source_root}/src/ruby_supportlib")
begin
  require 'rubygems'
rescue LoadError
end
require 'phusion_passenger'

PhusionPassenger.locate_directories
PhusionPassenger.require_passenger_lib  'utils/json'

SimpleJSON = PhusionPassenger::Utils::JSON

FILES = Dir['src/**/*.{h,cpp}']
SCHEMAS = SimpleJSON.parse(File.read('dev/configkit-schemas/index.json'))

def main
  FILES.each do |path|
    update_file(path)
  end
end

def update_file(path)
  content = File.open(path, 'r:utf-8') do |f|
    f.read
  end
  orig_content = content.dup

  pos = 0

  while match_begin = /^(.*?)BEGIN ConfigKit schema: (.+)/.match(content, pos)
    if match_end = /END/.match(content, match_begin.end(0))
      prefix = match_begin.captures[0]
      class_name = match_begin.captures[1]
      replacement = format_inline_comment_for_schema(prefix, class_name)

      content[match_begin.begin(0) .. match_end.end(0) - 1] = replacement
      pos = match_begin.begin(0) + replacement.size
    else
      break
    end
  end

  if content != orig_content
    puts "Updating #{path}"
    File.open(path, 'w:utf-8') do |f|
      f.write(content)
    end
  end
end

def format_inline_comment_for_schema(prefix, class_name)
  lines = [
    "BEGIN ConfigKit schema: #{class_name}",
    "(do not edit: following text is automatically generated",
    "by 'rake configkit_schemas_inline_comments')",
    ""
  ]

  if schema = SCHEMAS[class_name]
    table = []
    schema.each_pair do |key, info|
      table << [
        key,
        info['type'],
        info['required'] ? 'required' : '-',
        format_option_names(info)
      ]
    end

    column_lengths = find_max_column_lengths(table)
    table.each do |row|
      fmt = "  %-#{column_lengths[0]}s   %-#{column_lengths[1]}s   %-#{column_lengths[2]}s   %-#{column_lengths[3]}s"
      lines << sprintf(fmt, *row)
    end
  else
    lines << "(unknown: #{class_name} not in dev/configkit-schemas/index.json"
    lines << " Please run:"
    lines << "   touch src/schema_printer/SchemaPrinterMain.cpp.cxxcodebuilder"
    lines << "   rake configkit_schemas_inline_comments"
    lines << ")"
  end

  lines << ""
  lines << "END"
  lines.map{ |x| "#{prefix}#{x}".rstrip }.join("\n")
end

def format_option_names(schema_entry)
  options = []
  if schema_entry['has_default_value']
    if schema_entry['has_default_value'] == 'static'
      desc = format_default_value_desc(schema_entry['default_value'])
      options << "default(#{desc})"
    else
      options << 'default'
    end
  end
  options << 'secret' if schema_entry['secret']
  options << 'read_only' if schema_entry['read_only']
  result = options.join(',')
  result = '-' if result.empty?
  result
end

def format_default_value_desc(value)
  if value.is_a?(Array) || value.is_a?(Hash)
    SimpleJSON.generate(value)
  else
    SimpleJSON.generate([value]).sub(/\A\[/, '').sub(/\]\Z/, '')
  end
end

def find_max_column_lengths(table)
  lengths = []
  table.each do |row|
    row.each_with_index do |col, i|
      if lengths[i].nil? || lengths[i] < col.size
        lengths[i] = col.size
      end
    end
  end
  lengths
end

main
