This repository has been archived by the owner on Dec 23, 2017. It is now read-only.
/
avatar.rb
119 lines (95 loc) · 2.67 KB
/
avatar.rb
1
2
3
4
5
6
7
8
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
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
116
117
require 'Files'
require 'Locking'
require 'make_time_helper'
class Avatar
include MakeTimeHelper
def self.names
%w(
alligator buffalo cheetah deer
elephant frog gorilla hippo
koala lion moose panda
raccoon snake wolf zebra
)
end
def initialize(kata, name)
@kata = kata
@name = name
if !File.exists? dir
Dir::mkdir(dir)
Dir::mkdir(sandbox)
Files::file_write(pathed(Manifest_filename), @kata.visible_files)
Files::file_write(pathed(Increments_filename), [ ])
command = "git init --quiet;" +
"git add '#{Manifest_filename}';" +
"git add '#{Increments_filename}';"
system(cd_dir(command))
git_commit_tag(@kata.visible_files, tag = 0)
end
end
def kata
@kata
end
def name
@name
end
def save_run_tests(visible_files, output, inc)
inc[:time] = make_time(Time.now)
visible_files['output'] = output
Locking::io_lock(dir) do
increments = locked_read(Increments_filename)
tag = increments.length + 1
inc[:number] = tag
Files::file_write(pathed(Increments_filename), increments << inc)
Files::file_write(pathed(Manifest_filename), visible_files)
git_commit_tag(visible_files, tag)
end
end
def visible_files(tag = nil)
unlocked_read(Manifest_filename, tag)
end
def increments(tag = nil)
unlocked_read(Increments_filename, tag)
end
def diff_lines(tag)
command = cd_dir("git diff --ignore-space-at-eol --find-copies-harder #{tag-1} #{tag} sandbox;")
Files::popen_read(command)
end
private
def sandbox
pathed('sandbox')
end
def pathed(filename)
dir + '/' + filename
end
def dir
@kata.dir + '/' + name
end
def git_commit_tag(visible_files, tag)
command = ""
visible_files.each do |filename,content|
pathed_filename = sandbox + '/' + filename
File.open(pathed_filename, 'w') { |file| file.write content }
command += "git add '#{pathed_filename}';"
end
command += "git commit -a -m '#{tag}' --quiet;"
command += "git tag -m '#{tag}' #{tag} HEAD;"
system(cd_dir(command))
end
def unlocked_read(filename, tag)
Locking::io_lock(dir) { locked_read(filename, tag) }
end
def locked_read(filename, tag = nil)
tag ||= most_recent_tag
command = cd_dir("git show #{tag}:#{filename}")
eval Files::popen_read(command)
end
def most_recent_tag
command = cd_dir("git tag|sort -g")
eval Files::popen_read(command)
end
def cd_dir(command)
"cd #{dir};" + command
end
Increments_filename = 'increments.rb'
Manifest_filename = 'manifest.rb'
end