Class: Zubat::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/zubat/generator.rb

Constant Summary collapse

FILE =
'tmp/zubat/index.html'
TEMPLATE =
'templates/chart.html.erb'

Instance Method Summary collapse

Instance Method Details

#generate(results:, site_url:) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/zubat/generator.rb', line 9

def generate(results:, site_url:)
  erb = Zubat.root.join(TEMPLATE).read

  ylabels = []

  results.each do |result|
    result.stat.each do |ylabel, _|
      ylabels << ylabel unless ylabels.include?(ylabel)
    end
  end

  ylabels.sort!

  datasets = []

  results.each do |result|
    xlabel = result.label

    ylabels.each do |ylabel|
      dataset = datasets.find { |ds| ds[:label] == ylabel }

      unless dataset
        dataset = { label: ylabel, data: [] }

        datasets << dataset
      end

      dataset[:data] << {
        label: xlabel,
        commit_sha: result.commit.sha,
        commit_diff: result.stat.diff(ylabel),
        complexity_total: result.stat.complexity_total(ylabel),
        complexity_average: result.stat.complexity_average(ylabel),
        smells_scores: result.stat.smell_scores(ylabel).map { |type, value| { type:, value: } },
      }
    end
  end

  html = ERB.new(erb).result_with_hash(datasets:, site_url:)

  file = File.expand_path(FILE)

  FileUtils.mkdir_p(File.dirname(file))

  IO.write(file, html)

  file
end