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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/comet/runner.rb', line 12
def self.go(args, cwd)
program_desc 'Test your Ruby skills! Download Ruby exercises and submit your solutions for grading.'
desc 'Initialize the current directory as a comet project directory'
skips_pre
command :init do |c|
c.action do |global_options, options, args|
answers = Comet::Init.find_config(cwd) || {}
['email', 'token', 'server'].each do |setting|
answers[setting] = prompt_for_setting(setting, answers)
end
Comet::Init.init_project_dir(cwd, answers)
end
end
desc 'List the available katas'
command :list do |c|
c.action do |global_options,options,args|
katas = Comet::Kata.list(@config)
if katas.empty?
puts "No katas available."
else
katas.each do |kata|
printf("(%4d) \e[34m%s\e[0m: %s (%s)\n",
kata[:id],
kata[:topic_name],
kata[:name],
difficulty_to_string(kata[:difficulty]))
end
end
end
end
desc 'Download a kata'
command :fetch do |c|
c.action do |global_options, options, args|
kata_id = args.first
kata = Comet::Kata.find(@config, kata_id)
directory = kata.download
info_file = File.join(directory, '.kata')
info = YAML.load(File.read(info_file))
info['id'] = kata_id.to_i
File.write(info_file, info.to_yaml)
puts "Downloaded kata to #{directory}."
end
end
desc 'Run test suite'
command :test do |c|
c.action do |global_options, options, args|
kata_file = Comet::Kata.find_kata_dir(cwd)
if !kata_file.nil?
Comet::Tester.run_test_suite(kata_file)
else
$stderr.puts "\e[31mNot a kata directory.\e[0m"
end
end
end
desc 'Submit kata'
command :submit do |c|
c.action do |global_options, options, args|
require 'rest_client'
require 'tmpdir'
current_dir = cwd
kata_file = File.join(current_dir, '.kata')
if File.exists?(kata_file)
kata_info = YAML.load(File.read(kata_file))
lib_dir = File.join(current_dir, 'lib')
slug = File.basename(current_dir)
Dir.mktmpdir do |tmpdir|
submission_file = File.join(tmpdir, 'submission.tar.gz')
if system("tar zcf #{Shellwords.escape(submission_file)} -C #{Shellwords.escape(lib_dir)} .")
payload = {
submission: {
challenge_id: kata_info['id'],
archive: File.new(submission_file)
}
}
= { 'Authorization' => "Token #{@config['token']}" }
RestClient.post("#{@config['server']}/api/v1/submissions.json", payload, )
puts "Submitted solution for #{slug}."
else
puts "Unable to create submission archive."
exit 1
end
end
else
puts "Not a kata directory."
exit 1
end
end
end
pre do |global,command,options,args|
if [:submit, :fetch, :list].include?(command.name)
latest_version = Comet::API.latest_gem_version
if Comet::Version.is_more_recent(latest_version)
$stderr.puts "\e[33mNOTICE: An updated version of comet exists. " +
"Run `gem update comet` to upgrade.\e[0m"
end
end
@config = Comet::Init.find_config(cwd)
!@config.nil?
end
on_error do |exception|
case exception
when Comet::UnauthorizedError
$stderr.puts "\e[31mERROR: Invalid credentials. " +
"Verify that the e-mail, token, and server are correctly " +
"configured in #{File.join(@config['basedir'], '.comet')}\e[0m"
else
raise exception
end
end
run(args)
end
|