Class: TDX::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/tdx/base.rb

Overview

Base class

Instance Method Summary collapse

Constructor Details

#initialize(uri, opts) ⇒ Base

Returns a new instance of Base.



38
39
40
41
42
43
44
45
46
# File 'lib/tdx/base.rb', line 38

def initialize(uri, opts)
  @uri = uri
  @opts = opts
  @issues = nil
  @pure = nil
  @hoc = nil
  @logopts = '--ignore-space-change --no-color --find-copies-harder \
--ignore-all-space --ignore-submodules -M --diff-filter=ACDM'
end

Instance Method Details

#svgObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/tdx/base.rb', line 48

def svg
  version = Exec.new('git --version').stdout.split(/ /)[2]
  raise "git version #{version} is too old, upgrade it to 2.0+" unless
    Gem::Version.new(version) >= Gem::Version.new('2.0')
  path = checkout
  commits = Exec.new(
    "git log '--pretty=format:%H %cI' #{@logopts} " +
    (@opts[:sha] ? @opts[:sha] : 'HEAD'),
    path
  ).stdout.split(/\n/).map { |c| c.split(' ') }
  puts "Date\t\t\tCode\tTests\tIssues\tSHA\tIdx"
  metrics = commits.each_with_index.map do |c, i|
    Exec.new("git checkout --quiet --force #{c[0]}", path).stdout
    pure = pure(path, c[0])
    m = {
      date: c[1],
      code: pure,
      tests: hoc(path, c[0]) - pure,
      issues: issues(commits)[c[0]],
      sha: c[0]
    }
    puts "#{m[:date][0, 16]}\t#{m[:code]}\t#{m[:tests]}\t\
#{m[:issues]}\t#{m[:sha][0, 7]}\t#{i}/#{commits.size}"
    m
  end
  dat = if @opts[:data]
    File.new(@opts[:data], 'w+')
  else
    Tempfile.new('tdx')
  end
  metrics.select { |m| m[:code] > 0 }.each do |m|
    dat << [
      m[:date],
      m[:code],
      m[:tests],
      m[:issues],
      m[:sha]
    ].join(' ') + "\n"
  end
  dat.close
  svg = Tempfile.new('tdx')
  gpi = [
    "set output \"#{svg.path}\"",
    'set terminal svg size 700, 260',
    'set termoption font "monospace,10"',
    'set xdata time',
    'set timefmt "%Y-%m"',
    'set ytics format "%.0f" textcolor rgb "black"',
    'set y2tics format "%.0f" textcolor rgb "orange"',
    'set grid linecolor rgb "gray"',
    'set xtics format "%b/%y" font "monospace,8" textcolor rgb "black"',
    'set autoscale y',
    'set autoscale y2',
    'set style fill solid',
    'set boxwidth 0.75 relative',
    [
      "plot \"#{dat.path}\" u 1:2 w l t \"code\" lc rgb \"#81b341\"",
      ', "" u 1:3 w l t "tests" lc rgb "red"',
      ', "" u 1:4 w l t "Issues" lc rgb "orange" axes x1y2'
    ].join(' ')
  ]
  Exec.new("gnuplot -e '#{gpi.join('; ')}'").stdout
  FileUtils.rm_rf(path)
  File.delete(dat)
  xml = File.read(svg)
  File.delete(svg)
  xml
end