lib/makit/markdown.rb



# frozen_string_literal: true


require_relative "humanize"

# This module provides classes for the Makit gem.

module Makit
  class Markdown
    def self.get_commands_markdown(commands)
      summary = ""
      commands.each do |command|
        md = Makit::Markdown.get_command_markdown(command)
        summary += md
      end
      summary
    end

    def self.get_command_markdown(command)
      md = "<details>\n"
      md += "<summary>#{Makit::Humanize::get_command_summary(command)}</summary>\n\n"

      if command.output.length > 0
        md += "<table><tr><th>output</th></tr><tr><td>\n\n" #<pre>\n"

        md += "```shell\n"
        md += "#{command.output}\n"
        md += "```\n\n"
        md += "</td></tr></table>\n\n"
        #md += "output:\n"

        #md += "```shell\n"

        #md += "#{command.output}\n"

        #md += "```\n\n"

      end
      if command.error.length > 0
        md += "error:\n"
        md += "```shell\n"
        md += "#{command.error}\n"
        md += "```\n\n"
      end
      md += "| exit code | started at | duration | user | device | os | directory |\n"
      md += "| --- | --- | --- | --- | --- | --- | --- |\n"
      md += "| #{command.exit_code} | #{Makit::Humanize::get_protobuf_timestamp(command.started_at)} | #{Makit::Humanize::get_protobuf_duration(command.duration)} | #{command.user} | #{command.device} | #{command.os} | #{command.directory} |\n"

      #md += "| Name | Value |\n"

      #md += "| --- | --- |\n"

      #md += "| exit code | #{command.exit_code} |\n"

      #md += "| started at | #{Makit::Humanize::get_protobuf_timestamp(command.started_at)} |\n"

      #md += "| duration | #{Makit::Humanize::get_protobuf_duration(command.duration)} |\n"

      #md += "| user | #{command.user} |\n"

      #md += "| device | #{command.device} |\n"

      #md += "| os | #{command.os} |\n"

      #md += "| directory | #{command.directory} |\n"

      md += "</details>\n"
      md
    end

    def self.get_make_result_markdown(make_result)
      md = "# Make Result\n"
      # display allow of the fields of the MakeResult message

      md += "| Name | Value |\n"
      md += "| --- | --- |\n"
      md += "| repository | #{make_result.repository} |\n"
      md += "| commit | #{make_result.commit} |\n"
      md += "| branch | #{make_result.branch} |\n"
      md += "| tag | #{make_result.tag} |\n"
      md += "| device | #{make_result.device} |\n"
      md += "| runtime identifier | #{make_result.runtime_identifier} |\n"
      md += "| initial size | #{make_result.initial_size} |\n"
      md += "| final size | #{make_result.final_size} |\n"
      md += "\n"
      md += "## Commands\n"
      make_result.commands.each do |command|
        md += Makit::Markdown.get_command_markdown(command)
      end
    end
  end
end