Download lib/forge/cli/cli.ex from Snapkitty/forge-code: direct link, hf CLI and curl.
- Browser
- Download file 23.5 kB
-
https://huggingface.co/datasets/Snapkitty/forge-code/resolve/main/lib/forge/cli/cli.ex
- Command line
-
hf download hf://datasets/Snapkitty/forge-code/lib/forge/cli/cli.ex
-
curl -L -o cli.ex https://huggingface.co/datasets/Snapkitty/forge-code/resolve/main/lib/forge/cli/cli.ex
23.5 kB
| defmodule Forge.CLI do | |
| @moduledoc """ | |
| CLI - Command-line interface for Forge Code tournament system. | |
| This module provides the main command-line interface for the Forge Code | |
| reverse-engineering and benchmarking framework. It supports multiple | |
| output formats and comprehensive error handling. | |
| ## Usage | |
| # Run the CLI | |
| mix escript.build | |
| ./forge compile path/to/module.ex | |
| # Or via mix | |
| mix forge.compile path/to/module.ex | |
| ## Commands | |
| | Command | Description | | |
| |---------|-------------| | |
| | compile | Compile Elixir source with artifact capture | | |
| | inspect | Display BEAM metadata | | |
| | disassemble | Show BEAM instructions | | |
| | binary | Parse binary structure | | |
| | ir | Generate normalized IR | | |
| | hash | Compute SHA-256 hash | | |
| | tournament | Manage tournaments | | |
| | challenge | Manage challenges | | |
| | version | Display version information | | |
| | help | Display help | | |
| """ | |
| alias Forge.Compiler | |
| alias Forge.Beam | |
| alias Forge.BinaryIR | |
| alias Forge.Hash | |
| alias Forge.Provenance | |
| alias Forge.Tournament | |
| @type output_format :: :text | :json | :jsonl | :markdown | |
| @type cli_options :: %{ | |
| format: output_format(), | |
| verbose: boolean(), | |
| debug: boolean(), | |
| output: String.t() | nil, | |
| config: String.t() | nil | |
| } | |
| @default_options %{ | |
| format: :text, | |
| verbose: false, | |
| debug: false, | |
| output: nil, | |
| config: nil | |
| } | |
| @commands %{ | |
| "compile" => &handle_compile/2, | |
| "inspect" => &handle_inspect/2, | |
| "disassemble" => &handle_disassemble/2, | |
| "binary" => &handle_binary/2, | |
| "ir" => &handle_ir/2, | |
| "hash" => &handle_hash/2, | |
| "tournament" => &handle_tournament/2, | |
| "challenge" => &handle_challenge/2, | |
| "version" => &handle_version/2, | |
| "help" => &handle_help/2 | |
| } | |
| @doc """ | |
| Main entry point for the CLI. | |
| """ | |
| def main(args) do | |
| # Parse command-line arguments | |
| {command, args, options} = parse_args(args) | |
| # Look up command handler | |
| handler = Map.get(@commands, command) | |
| if handler do | |
| case handler.(args, options) do | |
| {:ok, output} -> | |
| case options.format do | |
| :text -> IO.puts(output) | |
| :json -> IO.puts(to_json(output)) | |
| :jsonl -> IO.puts(to_jsonl(output)) | |
| :markdown -> IO.puts(to_markdown(output)) | |
| end | |
| :ok | |
| {:error, message} -> | |
| print_error(message, options) | |
| :error | |
| end | |
| else | |
| print_error("Unknown command: #{command}", options) | |
| print_help() | |
| :error | |
| end | |
| end | |
| @doc """ | |
| Parses command-line arguments. | |
| """ | |
| defp parse_args(args) do | |
| # Handle help flag | |
| if "--help" in args || "-h" in args do | |
| {"help", [], @default_options} | |
| else | |
| parse_args_impl(args, @default_options, []) | |
| end | |
| end | |
| defp parse_args_impl([], options, args), do: {nil, Enum.reverse(args), options} | |
| defp parse_args_impl([arg | rest], options, acc) do | |
| case arg do | |
| "--format" -> parse_format_option(rest, options, acc) | |
| "--verbose" -> parse_args_impl(rest, %{options | verbose: true}, acc) | |
| "--debug" -> parse_args_impl(rest, %{options | debug: true}, acc) | |
| "--output" -> parse_output_option(rest, options, acc) | |
| "--config" -> parse_config_option(rest, options, acc) | |
| "--json" -> parse_args_impl(rest, %{options | format: :json}, acc) | |
| "-f" -> parse_format_option(rest, options, acc) | |
| "-v" -> parse_args_impl(rest, %{options | verbose: true}, acc) | |
| "-d" -> parse_args_impl(rest, %{options | debug: true}, acc) | |
| "-o" -> parse_output_option(rest, options, acc) | |
| _ when String.starts_with?(arg, "--") -> | |
| # Unknown option | |
| {nil, Enum.reverse(acc ++ [arg | rest]), options} | |
| _ -> | |
| # Regular argument | |
| parse_args_impl(rest, options, [arg | acc]) | |
| end | |
| end | |
| defp parse_format_option([value | rest], options, acc) do | |
| format = String.to_atom(value) | |
| parse_args_impl(rest, %{options | format: format}, acc) | |
| end | |
| defp parse_format_option(_rest, options, acc), do: parse_args_impl(_rest, options, acc) | |
| defp parse_output_option([value | rest], options, acc) do | |
| parse_args_impl(rest, %{options | output: value}, acc) | |
| end | |
| defp parse_output_option(_rest, options, acc), do: parse_args_impl(_rest, options, acc) | |
| defp parse_config_option([value | rest], options, acc) do | |
| parse_args_impl(rest, %{options | config: value}, acc) | |
| end | |
| defp parse_config_option(_rest, options, acc), do: parse_args_impl(_rest, options, acc) | |
| @doc """ | |
| Handles the compile command. | |
| """ | |
| defp handle_compile(args, options) do | |
| if length(args) == 0 do | |
| {:error, "Usage: forge compile <file.ex> [--format json|text]"} | |
| else | |
| file_path = List.first(args) | |
| unless File.exists?(file_path) do | |
| {:error, "File not found: #{file_path}"} | |
| else | |
| case File.read(file_path) do | |
| {:ok, source} -> | |
| case Compiler.compile(source, %{ | |
| compiler_flags: [], | |
| sandbox: false | |
| }) do | |
| {:ok, result} -> | |
| output = format_compile_result(result, options) | |
| {:ok, output} | |
| {:error, reason, details} -> | |
| {:error, "Compilation failed: #{reason}"} | |
| end | |
| {:error, reason} -> | |
| {:error, "Failed to read file: #{reason}"} | |
| end | |
| end | |
| end | |
| end | |
| @doc """ | |
| Handles the inspect command. | |
| """ | |
| defp handle_inspect(args, options) do | |
| if length(args) == 0 do | |
| {:error, "Usage: forge inspect <file.beam> [--format json|text]"} | |
| else | |
| file_path = List.first(args) | |
| unless File.exists?(file_path) do | |
| {:error, "File not found: #{file_path}"} | |
| else | |
| case Beam.read(file_path) do | |
| {:ok, metadata} -> | |
| output = format_inspect_result(metadata, options) | |
| {:ok, output} | |
| {:error, reason} -> | |
| {:error, "Failed to read BEAM file: #{reason}"} | |
| end | |
| end | |
| end | |
| end | |
| @doc """ | |
| Handles the disassemble command. | |
| """ | |
| defp handle_disassemble(args, options) do | |
| if length(args) == 0 do | |
| {:error, "Usage: forge disassemble <file.beam> [--format json|text]"} | |
| else | |
| file_path = List.first(args) | |
| unless File.exists?(file_path) do | |
| {:error, "File not found: #{file_path}"} | |
| else | |
| case Beam.read(file_path) do | |
| {:ok, metadata} -> | |
| output = format_disassemble_result(metadata, options) | |
| {:ok, output} | |
| {:error, reason} -> | |
| {:error, "Failed to read BEAM file: #{reason}"} | |
| end | |
| end | |
| end | |
| end | |
| @doc """ | |
| Handles the binary command. | |
| """ | |
| defp handle_binary(args, options) do | |
| if length(args) == 0 do | |
| {:error, "Usage: forge binary <file.beam> [--format json|text]"} | |
| else | |
| file_path = List.first(args) | |
| unless File.exists?(file_path) do | |
| {:error, "File not found: #{file_path}"} | |
| else | |
| case BinaryIR.from_beam(file_path) do | |
| {:ok, ir} -> | |
| output = format_binary_ir(ir, options) | |
| {:ok, output} | |
| {:error, reason} -> | |
| {:error, "Failed to parse binary: #{reason}"} | |
| end | |
| end | |
| end | |
| end | |
| @doc """ | |
| Handles the ir command. | |
| """ | |
| def handle_ir(args, options), do: handle_binary(args, options) | |
| @doc """ | |
| Handles the hash command. | |
| """ | |
| defp handle_hash(args, options) do | |
| if length(args) == 0 do | |
| {:error, "Usage: forge hash <file> [--format json|text]"} | |
| else | |
| file_path = List.first(args) | |
| unless File.exists?(file_path) do | |
| {:error, "File not found: #{file_path}"} | |
| else | |
| case Hash.file(file_path) do | |
| {:ok, hash, size} -> | |
| output = format_hash_result(file_path, hash, size, options) | |
| {:ok, output} | |
| {:error, reason} -> | |
| {:error, "Failed to hash file: #{reason}"} | |
| end | |
| end | |
| end | |
| end | |
| @doc """ | |
| Handles the tournament command. | |
| """ | |
| defp handle_tournament(args, options) do | |
| if length(args) == 0 do | |
| {:error, "Usage: forge tournament <run|create|score|report> [args]"} | |
| else | |
| subcommand = List.first(args) | |
| sub_args = Enum.drop(args, 1) | |
| case subcommand do | |
| "run" -> handle_tournament_run(sub_args, options) | |
| "create" -> handle_tournament_create(sub_args, options) | |
| "score" -> handle_tournament_score(sub_args, options) | |
| "report" -> handle_tournament_report(sub_args, options) | |
| _ -> {:error, "Unknown tournament subcommand: #{subcommand}"} | |
| end | |
| end | |
| end | |
| defp handle_tournament_run(args, options) do | |
| if length(args) == 0 do | |
| {:error, "Usage: forge tournament run <tournament.exs>"} | |
| else | |
| tournament_file = List.first(args) | |
| unless File.exists?(tournament_file) do | |
| {:error, "Tournament file not found: #{tournament_file}"} | |
| else | |
| case Tournament.run(tournament_file, options) do | |
| {:ok, result} -> | |
| output = format_tournament_result(result, options) | |
| {:ok, output} | |
| {:error, reason} -> | |
| {:error, "Tournament failed: #{reason}"} | |
| end | |
| end | |
| end | |
| end | |
| defp handle_tournament_create(args, options) do | |
| {:error, "Tournament create: Not yet implemented"} | |
| end | |
| defp handle_tournament_score(args, options) do | |
| {:error, "Tournament score: Not yet implemented"} | |
| end | |
| defp handle_tournament_report(args, options) do | |
| {:error, "Tournament report: Not yet implemented"} | |
| end | |
| @doc """ | |
| Handles the challenge command. | |
| """ | |
| defp handle_challenge(args, options) do | |
| if length(args) == 0 do | |
| {:error, "Usage: forge challenge <list|create> [args]"} | |
| else | |
| subcommand = List.first(args) | |
| case subcommand do | |
| "list" -> | |
| # List available challenges | |
| {:ok, "Available challenges:\n (none defined - check examples/challenges)"} | |
| _ -> | |
| {:error, "Unknown challenge subcommand: #{subcommand}"} | |
| end | |
| end | |
| end | |
| @doc """ | |
| Handles the version command. | |
| """ | |
| defp handle_version(args, options) do | |
| version = System.get_env("FORGE_VERSION") || "1.0.0" | |
| elixir = System.get_env("ELIXIR_VERSION") || to_string(:elixir) | |
| otp = System.get_env("OTP_VERSION") || System.version() | |
| output = [ | |
| "FORGE CODE: ELIXIR TOURNAMENT", | |
| "Version: #{version}", | |
| "Elixir: #{elixir}", | |
| "OTP: #{otp}", | |
| "" | |
| ] |> Enum.join("\n") | |
| {:ok, output} | |
| end | |
| @doc """ | |
| Handles the help command. | |
| """ | |
| defp handle_help(args, options) do | |
| {:ok, print_help()} | |
| end | |
| @doc """ | |
| Prints the help message. | |
| """ | |
| defp print_help() do | |
| """ | |
| FORGE CODE: ELIXIR TOURNAMENT | |
| Reverse-Engineering and Benchmarking Laboratory for Elixir Programs | |
| USAGE: | |
| forge <command> [options] [args] | |
| COMMANDS: | |
| compile <file.ex> Compile Elixir source with artifact capture | |
| inspect <file.beam> Display BEAM metadata | |
| disassemble <file.beam> Show BEAM instructions | |
| binary <file.beam> Parse binary structure | |
| ir <file.beam> Generate normalized IR | |
| hash <file> Compute SHA-256 hash | |
| tournament run <file.exs> Execute tournament | |
| challenge list List available challenges | |
| version Display version information | |
| help Display this help | |
| OPTIONS: | |
| --format <fmt> - Output format: text, json, jsonl, markdown (default: text) | |
| --verbose - Verbose output | |
| --debug - Debug output | |
| --output <file> - Write output to file | |
| --config <file> - Configuration file | |
| -f <fmt> - Same as --format | |
| -v - Same as --verbose | |
| -d - Same as --debug | |
| -o <file> - Same as --output | |
| EXAMPLES: | |
| # Compile and inspect | |
| forge compile my_module.ex | |
| forge inspect my_module.beam --format json | |
| # Disassemble BEAM file | |
| forge disassemble my_module.beam | |
| # Run a tournament | |
| forge tournament run my_tournament.exs --format json | |
| # Hash a file | |
| forge hash my_module.ex | |
| """ | |
| end | |
| @doc """ | |
| Prints an error message. | |
| """ | |
| defp print_error(message, options) do | |
| if options.verbose do | |
| IO.puts(:stderr, "ERROR: #{message}") | |
| else | |
| IO.puts(:stderr, message) | |
| end | |
| end | |
| @doc """ | |
| Formats a compile result for output. | |
| """ | |
| defp format_compile_result(result, options) do | |
| case options.format do | |
| :json -> to_json(result) | |
| :jsonl -> to_jsonl(result) | |
| :markdown -> to_markdown_compile(result) | |
| _ -> to_text_compile(result) | |
| end | |
| end | |
| defp to_text_compile(result) do | |
| lines = [ | |
| "Compilation Result:", | |
| " Artifact ID: #{result.artifact_id}", | |
| " Status: #{if result.errors == [], do: "SUCCESS", else: "ERRORS"}", | |
| " Source Size: #{result.metrics.source_size} bytes", | |
| "" | |
| ] | |
| if beam_path = result.beam_path do | |
| lines = lines ++ [ | |
| " BEAM File: #{beam_path}", | |
| " BEAM Size: #{result.metrics.beam_size} bytes", | |
| " Function Count: #{result.metrics.function_count}", | |
| " Module Count: #{result.metrics.module_count}", | |
| " Instruction Count: #{result.metrics.instruction_count}" | |
| ] | |
| end | |
| if result.errors != [] do | |
| lines = lines ++ ["\nErrors:"] ++ Enum.map(result.errors, &" - #{&1}") | |
| end | |
| if result.warnings != [] do | |
| lines = lines ++ ["\nWarnings:"] ++ Enum.map(result.warnings, &" - #{&1}") | |
| end | |
| if provenance = result.provenance do | |
| lines = lines ++ ["\n" <> Provenance.summary(provenance)] | |
| end | |
| Enum.join(lines, "\n") | |
| end | |
| defp to_json(result) do | |
| # Convert to JSON-serializable structure | |
| json_result = %{ | |
| artifact_id: result.artifact_id, | |
| status: if result.errors == [], do: "success", else: "error" | |
| source_size: result.metrics.source_size, | |
| beam_path: result.beam_path, | |
| beam_size: result.metrics.beam_size, | |
| function_count: result.metrics.function_count, | |
| module_count: result.metrics.module_count, | |
| instruction_count: result.metrics.instruction_count, | |
| errors: result.errors, | |
| warnings: result.warnings, | |
| provenance: result.provenance | |
| } | |
| Jason.encode!(json_result) | |
| rescue | |
| _ -> to_string(result) | |
| end | |
| defp to_jsonl(result) do | |
| to_json(result) | |
| end | |
| defp to_markdown_compile(result) do | |
| lines = [ | |
| "# Compilation Result", | |
| "", | |
| "## Summary", | |
| "", | |
| "| Property | Value |", | |
| "|----------|-------|", | |
| "| Artifact ID | #{result.artifact_id} |", | |
| "| Status | #{if result.errors == [], do: "SUCCESS", else: "ERROR"} |", | |
| "| Source Size | #{result.metrics.source_size} bytes |", | |
| "| BEAM Size | #{result.metrics.beam_size} bytes |", | |
| "| Functions | #{result.metrics.function_count} |", | |
| "| Modules | #{result.metrics.module_count} |", | |
| "| Instructions | #{result.metrics.instruction_count} |", | |
| "" | |
| ] | |
| if result.errors != [] do | |
| lines = lines ++ ["## Errors", ""] ++ Enum.map(result.errors, &"- #{&1}") | |
| end | |
| if result.warnings != [] do | |
| lines = lines ++ ["\n## Warnings", ""] ++ Enum.map(result.warnings, &"- #{&1}") | |
| end | |
| Enum.join(lines, "\n") | |
| end | |
| @doc """ | |
| Formats a BEAM inspect result for output. | |
| """ | |
| defp format_inspect_result(metadata, options) do | |
| case options.format do | |
| :json -> to_json(metadata) | |
| :jsonl -> to_jsonl(metadata) | |
| :markdown -> to_markdown_inspect(metadata) | |
| _ -> to_text_inspect(metadata) | |
| end | |
| end | |
| defp to_text_inspect(metadata) do | |
| lines = [ | |
| "BEAM Module: #{inspect(metadata.module)}", | |
| "Version: #{metadata.version || 'unknown'}", | |
| "Compiler: #{metadata.compiler || 'unknown'}", | |
| "Source: #{metadata.source || 'unknown'}", | |
| "MD5: #{metadata.md5 || 'unknown'}", | |
| "Size: #{metadata.raw_size} bytes", | |
| "Debug Info: #{metadata.debug_info}", | |
| "Line Info: #{metadata.line_info}", | |
| "" | |
| ] | |
| lines = lines ++ ["Exports: #{length(metadata.exports || [])}"] | |
| Enum.each(metadata.exports || [], &(&1 |> to_string() |> IO.puts())) | |
| lines = lines ++ ["\nImports:"] | |
| Enum.each(metadata.imports || [], &(&1 |> inspect() |> IO.puts())) | |
| lines = lines ++ ["\nFunctions: #{length(metadata.functions || [])}"] | |
| Enum.each(metadata.functions || [], fn func -> | |
| lines = lines ++ [" #{inspect(func.name)}/#{func.arity} - #{length(func.instructions || [])} instructions"] | |
| end) | |
| lines = lines ++ ["\nAtoms: #{length(metadata.atoms || [])}"] | |
| lines = lines ++ ["Strings: #{length(metadata.strings || [])}"] | |
| lines = lines ++ ["Literals: #{length(metadata.literals || [])}"] | |
| Enum.join(lines, "\n") | |
| end | |
| defp to_markdown_inspect(metadata) do | |
| lines = [ | |
| "# BEAM Module Inspection", | |
| "", | |
| "## Module Information", | |
| "", | |
| "| Property | Value |", | |
| "|----------|-------|", | |
| "| Module | #{inspect(metadata.module)} |", | |
| "| Version | #{metadata.version || 'unknown'} |", | |
| "| Compiler | #{metadata.compiler || 'unknown'} |", | |
| "| Source | #{metadata.source || 'unknown'} |", | |
| "| MD5 | #{metadata.md5 || 'unknown'} |", | |
| "| Size | #{metadata.raw_size} bytes |", | |
| "| Debug Info | #{metadata.debug_info} |", | |
| "| Line Info | #{metadata.line_info} |", | |
| "", | |
| "## Exports (#{length(metadata.exports || [])})", | |
| "" | |
| ] | |
| Enum.each(metadata.exports || [], &(&1 |> to_string() |> IO.puts())) | |
| lines = lines ++ ["\n## Functions (#{length(metadata.functions || [])})", ""] | |
| Enum.each(metadata.functions || [], fn func -> | |
| lines = lines ++ ["- **#{inspect(func.name)}/#{func.arity}**: #{length(func.instructions || [])} instructions"] | |
| end) | |
| lines = lines ++ [ | |
| "\n## Constants", | |
| "", | |
| "- Atoms: #{length(metadata.atoms || [])}", | |
| "- Strings: #{length(metadata.strings || [])}", | |
| "- Literals: #{length(metadata.literals || [])}" | |
| ] | |
| Enum.join(lines, "\n") | |
| end | |
| @doc """ | |
| Formats a disassemble result for output. | |
| """ | |
| defp format_disassemble_result(metadata, options) do | |
| lines = [ | |
| "BEAM Disassembly: #{inspect(metadata.module)}", | |
| "", | |
| "Functions:", | |
| "" | |
| ] | |
| Enum.each(metadata.functions || [], fn func -> | |
| lines = lines ++ [ | |
| "\nFunction: #{inspect(func.name)}/#{func.arity}", | |
| " Labels: #{inspect(func.labels)}", | |
| " Instructions:" | |
| ] | |
| Enum.each(func.instructions || [], fn instr -> | |
| lines = lines ++ [ | |
| " #{instr.offset}: #{Atom.to_string(instr.opcode)} #{format_args(instr.args)}" | |
| ] | |
| end) | |
| end) | |
| Enum.join(lines, "\n") | |
| end | |
| defp format_args(args) do | |
| Enum.map(args, &inspect/1) |> Enum.join(", ") | |
| end | |
| @doc """ | |
| Formats a binary IR for output. | |
| """ | |
| defp format_binary_ir(ir, options) do | |
| case options.format do | |
| :json -> to_json(ir) | |
| :jsonl -> to_jsonl(ir) | |
| :markdown -> to_markdown_ir(ir) | |
| _ -> to_text_ir(ir) | |
| end | |
| end | |
| defp to_text_ir(ir) do | |
| lines = [ | |
| "Binary IR: #{inspect(ir.module)}", | |
| "Version: #{ir.version || 'unknown'}", | |
| "Hash: #{if hash = ir.hashes[:beam_file], do: Hash.to_hex(hash), else: 'N/A'}", | |
| "" | |
| ] | |
| lines = lines ++ ["Sections (#{length(ir.sections)}):"] | |
| Enum.each(ir.sections, fn section -> | |
| lines = lines ++ [ | |
| " - #{Atom.to_string(section.name)}: offset=#{section.offset}, length=#{section.length}, fields=#{length(section.fields)}" | |
| ] | |
| end) | |
| lines = lines ++ ["\nConstants (#{length(ir.constants)}):"] | |
| Enum.each(ir.constants, fn const -> | |
| lines = lines ++ [" - #{const.type}: #{inspect(const.value)} at offset #{const.offset}"] | |
| end) | |
| lines = lines ++ ["\nFunctions (#{length(ir.functions)}):"] | |
| Enum.each(ir.functions, fn func -> | |
| lines = lines ++ [ | |
| " - #{func.name}/#{func.arity}: #{length(func.instructions)} instructions" | |
| ] | |
| end) | |
| Enum.join(lines, "\n") | |
| end | |
| defp to_markdown_ir(ir) do | |
| lines = [ | |
| "# Binary IR", | |
| "", | |
| "## Module", | |
| "", | |
| "- Module: #{inspect(ir.module)}", | |
| "- Version: #{ir.version || 'unknown'}", | |
| "- Hash: #{if hash = ir.hashes[:beam_file], do: Hash.to_hex(hash), else: 'N/A'}", | |
| "", | |
| "## Sections (#{length(ir.sections)})", | |
| "" | |
| ] | |
| Enum.each(ir.sections, fn section -> | |
| lines = lines ++ [ | |
| "### #{Atom.to_string(section.name)}", | |
| "", | |
| "- Offset: #{section.offset}", | |
| "- Length: #{section.length}", | |
| "- Fields: #{length(section.fields)}" | |
| ] | |
| end) | |
| lines = lines ++ [ | |
| "\n## Constants (#{length(ir.constants)})", | |
| "" | |
| ] | |
| Enum.each(ir.constants, fn const -> | |
| lines = lines ++ ["- **#{const.type}**: #{inspect(const.value)} at offset #{const.offset}"] | |
| end) | |
| lines = lines ++ [ | |
| "\n## Functions (#{length(ir.functions)})", | |
| "" | |
| ] | |
| Enum.each(ir.functions, fn func -> | |
| lines = lines ++ [ | |
| "### #{func.name}/#{func.arity}", | |
| "", | |
| "- Instructions: #{length(func.instructions)}", | |
| "- Offsets: #{inspect(func.offsets)}" | |
| ] | |
| end) | |
| Enum.join(lines, "\n") | |
| end | |
| @doc """ | |
| Formats a hash result for output. | |
| """ | |
| defp format_hash_result(path, hash, size, options) do | |
| case options.format do | |
| :json -> Jason.encode!(%{path: path, hash: Hash.to_hex(hash), size: size}) | |
| :jsonl -> Jason.encode!(%{path: path, hash: Hash.to_hex(hash), size: size}) | |
| _ -> "#{path}: #{Hash.to_hex(hash)} (#{size} bytes)" | |
| end | |
| rescue | |
| _ -> "#{path}: #{Hash.to_hex(hash)} (#{size} bytes)" | |
| end | |
| @doc """ | |
| Formats a tournament result for output. | |
| """ | |
| defp format_tournament_result(result, options) do | |
| case options.format do | |
| :json -> Tournament.to_json(result) | |
| :jsonl -> Tournament.to_json(result) | |
| :markdown -> to_markdown_tournament(result) | |
| _ -> Tournament.summary(result) | |
| end | |
| end | |
| defp to_markdown_tournament(result) do | |
| lines = [ | |
| "# Tournament Results: #{result.tournament_id}", | |
| "", | |
| "## Summary", | |
| "", | |
| "| Property | Value |", | |
| "|----------|-------|", | |
| "| Contestants | #{length(result.contestants)} |", | |
| "| Challenges | #{length(result.challenges)} |", | |
| "| Timestamp | #{result.metadata.timestamp} |", | |
| "", | |
| "## Overall Ranking", | |
| "" | |
| ] | |
| Enum.each_with_index(result.overall_ranking, fn ranking, idx -> | |
| position = idx + 1 | |
| lines = lines ++ [ | |
| "#{position}. **#{ranking.contestant_id}** - Score: #{Float.round(ranking.average_score, 4)}" | |
| ] | |
| end) | |
| Enum.join(lines, "\n") | |
| end | |
| @doc """ | |
| Converts any term to JSON. | |
| """ | |
| defp to_json(term) do | |
| if Code.ensure_loaded?(Jason) do | |
| Jason.encode!(term) | |
| else | |
| :erlang.term_to_binary(term) |> to_string() | |
| end | |
| end | |
| @doc """ | |
| Converts any term to JSONL (one JSON object per line). | |
| """ | |
| defp to_jsonl(term) do | |
| to_json(term) | |
| end | |
| end | |