Skip to content

Commit

Permalink
#4 exec.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Mar 15, 2017
1 parent c5ce469 commit 0b1c87c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 23 deletions.
39 changes: 16 additions & 23 deletions lib/tdx/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
require 'octokit'
require 'fileutils'
require 'English'
require 'tdx/exec'

# TDX main module.
# Author:: Yegor Bugayenko (yegor256@gmail.com)
Expand All @@ -40,20 +41,18 @@ def initialize(uri, opts)

def svg
dat = Tempfile.new('tdx')
version = `git --version`.split(/ /)[2]
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 =
`cd "#{path}" && git log '--pretty=format:%H %cI' --reverse`
.split(/\n/)
.map { |c| c.split(' ') }

commits = Exec.new('git log "--pretty=format:%H %cI" --reverse', path)
.stdout.split(/\n/).map { |c| c.split(' ') }
issues = issues(commits)
puts "Date\t\tTest\tHoC\tFiles\tLoC\tIssues\tSHA"
puts "Date\t\t\tTest\tHoC\tFiles\tLoC\tIssues\tSHA"
commits.each do |sha, date|
`cd "#{path}" && git checkout --quiet #{sha}`
raise 'Failed to checkout' unless $CHILD_STATUS.exitstatus == 0
line = "#{date[0, 10]}\t#{tests(path)}\t#{hoc(path)}\t#{files(path)}\t\
Exec.new("git checkout --quiet #{sha}", path).stdout
line = "#{date[0, 16]}\t#{tests(path)}\t#{hoc(path)}\t#{files(path)}\t\
#{loc(path)}\t#{issues[sha]}\t#{sha[0, 7]}"
dat << "#{line}\n"
puts line
Expand Down Expand Up @@ -81,8 +80,7 @@ def svg
', "" using 1:6 with boxes title "Issues" linecolor rgb "orange"'
].join(' ')
]
`gnuplot -e '#{gpi.join('; ')}'`
raise 'Failed to run Gnuplot' unless $CHILD_STATUS.exitstatus == 0
Exec.new("gnuplot -e '#{gpi.join('; ')}'").stdout
FileUtils.rm_rf(path)
File.delete(dat)
xml = File.read(svg)
Expand All @@ -94,8 +92,7 @@ def svg

def checkout
dir = Dir.mktmpdir
`cd #{dir} && git clone --quiet #{@uri} .`
raise 'Failed to clone repository' unless $CHILD_STATUS.exitstatus == 0
Exec.new("git clone --quiet #{@uri} .", dir).stdout
size = Dir.glob(File.join(dir, '**/*'))
.map(&:size)
.inject(0) { |a, e| a + e }
Expand All @@ -108,9 +105,7 @@ def files(path)
end

def loc(path)
cloc = `cd "#{path}" && cloc . --yaml --quiet`
raise 'Failed to run cloc' unless $CHILD_STATUS.exitstatus == 0
yaml = YAML.load(cloc)
yaml = YAML.load(Exec.new('cloc . --yaml --quiet', path).stdout)
if yaml
yaml['SUM']['code']
else
Expand All @@ -119,15 +114,11 @@ def loc(path)
end

def hoc(path)
hoc = `cd "#{path}" && hoc`
raise 'Failed to run hoc' unless $CHILD_STATUS.exitstatus == 0
hoc.strip
Exec.new('hoc', path).stdout.strip
end

def tests(path)
hoc = `cd "#{path}" && hoc`
raise 'Failed to run hoc' unless $CHILD_STATUS.exitstatus == 0
hoc.strip
Exec.new('hoc', path).stdout.strip
end

def issues(commits)
Expand All @@ -142,7 +133,9 @@ def issues(commits)
else
@uri.gsub(%r{^https://github\.com/|\.git$}, '')
end
client.list_issues(repo, state: :all).map(&:created_at)
list = client.list_issues(repo, state: :all).map(&:created_at)
puts "Loaded #{list.length} issues from GitHub repo '#{repo}'"
list
else
[]
end
Expand Down
47 changes: 47 additions & 0 deletions lib/tdx/exec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# encoding: utf-8
#
# Copyright (c) 2017 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require 'date'
require 'yaml'
require 'octokit'
require 'fileutils'
require 'English'

# TDX main module.
# Author:: Yegor Bugayenko (yegor256@gmail.com)
# Copyright:: Copyright (c) 2017 Yegor Bugayenko
# License:: MIT
module TDX
# Command line executor
class Exec
def initialize(cmd, dir = '.')
@cmd = cmd
@dir = dir
end

def stdout
out = `cd #{@dir} && #{@cmd} 2>/dev/null`
raise 'Previous command failed' unless $CHILD_STATUS.exitstatus == 0
out
end
end
end

0 comments on commit 0b1c87c

Please sign in to comment.