bin/cobench in cobench-0.0.18 vs bin/cobench in cobench-0.0.19

- old
+ new

@@ -33,11 +33,11 @@ loog = Loog::REGULAR def config(path) f = File.expand_path(path) args = [] - args += File.readlines(f).map(&:strip) if File.exist?(f) + args += File.readlines(f).map(&:strip).reject { |a| a.empty? } if File.exist?(f) args end args = config('~/.cobench') + config('.cobench') + ARGV @@ -45,10 +45,11 @@ o.banner = "Usage (#{Cobench::VERSION}): cobench [options]" o.bool '-h', '--help', 'Show these instructions' o.bool '--version', 'Show current version' o.bool '--verbose', 'Print as much log messages as possible' o.bool '--dry', 'Make no real round trips to GitHub' + o.bool '--reuse', 'Don\'t fetch from GitHub, reuse the existing XML file' o.integer '--days', 'How many days to measure', default: 7 o.string '--to', 'Directory where to save all files to', default: './cobench' o.string '--token', 'GitHub authentication token' o.array '--coder', 'GitHub nickname of a coder to track' o.array '--metrics', 'Names of metrics to use (all by default)' @@ -71,31 +72,22 @@ end Encoding.default_external = Encoding::UTF_8 Encoding.default_internal = Encoding::UTF_8 -data = {} - -begin - home = File.absolute_path(opts[:to]) - loog.debug("All files generated will be saved to #{home}") - if File.exist?(home) - loog.debug("Directory #{home} exists") - else - FileUtils.mkdir_p(home) - loog.debug("Directory #{home} created") - end +def build_xml(opts, loog) if opts.token? api = Octokit::Client.new(:access_token => opts[:token]) else api = Octokit::Client.new loog.warn("Connecting to GitHub without a token, this may lead to errors, use --token") end api.auto_paginate = true api = Obk.new(api, pause: 2000) loog.info("Reading GitHub data for the last #{opts[:days]} days") titles = {} + data = {} opts[:coder].each do |u| user = u.downcase loog.info("Scanning #{user}...") data[user] = {} Dir[File.join(__dir__, '../lib/cobench/metrics/*.rb')].each do |f| @@ -109,13 +101,14 @@ require_relative f m = type.split('::').reduce(Module, :const_get).new(api, user, opts) if opts.dry? measures = [ { title: 'Issues', total: Random.new.rand(100), href: 'https://github.com/' }, - { title: 'Pulls', total: Random.new.rand(100), href: '' }, - { title: 'HoC', total: Random.new.rand(100), href: '' }, - { title: 'HoC', total: Random.new.rand(100), href: '' } + { title: 'Pulls', total: Random.new.rand(100) }, + { title: 'Commits', total: Random.new.rand(100) }, + { title: 'HoC', total: Random.new.rand(100) }, + { title: 'HoC', total: Random.new.rand(100) } ] else measures = m.take(loog) end measures.each do |d| @@ -125,10 +118,24 @@ titles[d[:title]] = d[:title] loog.info("The value of #{user}/#{d[:title]} is #{d[:total]}") end end end + caps = { + 'HoC' => lambda { |ms| ms['Commits'][:total] * 1000 }, + } + data.each do |u, ms| + ms.map do |t, h| + next unless caps.key?(t) + cap = caps[t].call(ms) + if h[:total] > cap + data[u][t][:actual] = h[:total] + data[u][t][:total] = cap + end + end + data[u] = ms + end weights = { 'HoC' => 1, 'Pulls' => 200, 'Issues' => 50, 'Commits' => 5, @@ -155,27 +162,46 @@ end end end xml.coders do data.each do |u, ms| - xml.coder(id: u, details: api.user(u).name) do + xml.coder(id: u) do + xml.parent.set_attribute('details', api.user(u).name) xml.metrics do ms.each do |k, v| xml.m(id: k, href: v[:href]) do + xml.parent.set_attribute('actual', v[:actual]) unless v[:actual].nil? + xml.parent.set_attribute('href', v[:href]) unless v[:href].nil? xml.text v[:total] end end end end end end end end - index = File.join(home, 'index.xml') xml = builder.to_xml loog.debug(xml) - File.write(index, xml) - loog.debug("XML saved to #{index} (#{File.size(index)} bytes)") + xml +end +begin + home = File.absolute_path(opts[:to]) + loog.debug("All files generated will be saved to #{home}") + if File.exist?(home) + loog.debug("Directory #{home} exists") + else + FileUtils.mkdir_p(home) + loog.debug("Directory #{home} created") + end + index = File.join(home, 'index.xml') + if opts[:reuse] + xml = File.read(index) + else + xml = build_xml(opts, loog) + File.write(index, xml) + loog.debug("XML saved to #{index} (#{File.size(index)} bytes)") + end xslt = Nokogiri::XSLT(File.read(File.join(__dir__, '../assets/index.xsl'))) html = xslt.transform(Nokogiri::XML(xml), 'version' => "'#{Cobench::VERSION}'") loog.debug(html) front = File.join(home, 'index.html') File.write(front, html)