Sha256: b5a8a77cf100e8ce3d1347360a4968ca4c81718d7e1c27b4ab3115fcf8205f1b

Contents?: true

Size: 1.09 KB

Versions: 6

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

require 'tempfile'

module Attr
  module Gather
    module Workflow
      # @api private
      class DotSerializer
        def initialize(task_graph)
          @task_graph = task_graph
        end

        def to_dot
          lines = @task_graph.tsort.map { |t| serialize_row(t) }
          joined_lines = lines.flatten.map { |l| "  #{l}" }.join("\n").strip

          <<~DOT
            digraph TaskGraph {
              #{joined_lines}
            }
          DOT
        end

        def preview
          Tempfile.open(['task-graph-preview', '.svg']) do |tf|
            IO.popen("dot -Tsvg -o #{tf.path}", 'w') { |p| p.write(to_dot) }
            `xdg-open #{tf.path}`
          end
        end

        private

        def serialize_row(task)
          row = all_dependants_for_task(task).map { |dt| [task, dt] }
          lines = row.map { |item| item.map(&:name).join(' -> ') + ';' }
          lines
        end

        def all_dependants_for_task(input_task)
          @task_graph.to_h.keys.select { |task| task.depends_on?(input_task) }
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
attr-gather-1.2.0 lib/attr/gather/workflow/dot_serializer.rb
attr-gather-1.1.3 lib/attr/gather/workflow/dot_serializer.rb
attr-gather-1.1.2 lib/attr/gather/workflow/dot_serializer.rb
attr-gather-1.1.1 lib/attr/gather/workflow/dot_serializer.rb
attr-gather-1.1.0 lib/attr/gather/workflow/dot_serializer.rb
attr-gather-1.0.0 lib/attr/gather/workflow/dot_serializer.rb