-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodechallenger
More file actions
executable file
·264 lines (206 loc) · 7.08 KB
/
codechallenger
File metadata and controls
executable file
·264 lines (206 loc) · 7.08 KB
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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env ruby
##
#
# @project CodeChallenger CLI
# @author Matti van de Weem <mvdweem@gmail.com>, Koen Vendrik <k.vendrik@gmail.com>
# @date January 2015
# @url http://github.com/CodeChallenger
#
##
require 'rubygems'
require 'bundler/setup'
require 'optparse'
require 'json'
require_relative 'inc/challenger.class.rb'
# read a file
def read_file(file_name)
file = File.open(file_name, 'r')
data = file.read
file.close
return data
end
# check if codechallenger directory
def check_if_challenger_dir(pathPrefix='')
if not File.exists?(pathPrefix+'.codechallenger.json')
puts 'No .codechallenger.json file found. Run the init command first.'
exit 1
end
end
def get_settings(pathPrefix='')
check_if_challenger_dir(pathPrefix)
# get path to settings
settingsPath = pathPrefix+'.codechallenger.json'
# get settings
settings = File.read(settingsPath)
return JSON.parse(settings)
end
def check_repository_field(pathPrefix='', settings, repoUrl)
# if no repository in settings
# ask url and add to both Git and the settings file
if not settings['repository']
if repoUrl == nil
loop do
print "\nEnter the URL of the repository for your challenge: "
repoUrl = gets.chomp
break if repoUrl != ''
end
end
# add url to git
if not system('git remote add codechallenger '+repoUrl)
exit 1
end
# add url to settings
settings['repository'] = repoUrl
# write settings back to file
File.open(pathPrefix+'.codechallenger.json', 'w') {|f| f.write(JSON.pretty_generate(settings)) }
end
end
# hash to store command line options
options = {}
helpText = <<HELP
Usage: codechallenger <command> [options...]
Commands:
init generate a new challenge in the directory
uninit remove CodeChallenger from the directory
deploy deploys your challenge code to Github
See 'codechallenger <command> --help' for help on a specific command.
HELP
global = OptionParser.new do |opts|
opts.on('-h', '--help', 'Show help') do |language|
puts helpText
exit 0
end
end
subCommands = {
'init' => {
:optionParser => OptionParser.new do |opts|
opts.banner = 'Usage: codechallenger init [options...]'
opts.on('--language [LANGUAGE]', 'Give a challenge for given language') do |language|
options[:language] = language.downcase
end
opts.on('--framework [FRAMEWORK]', 'Give a challenge with the given framework') do |framework|
options[:framework] = framework.downcase
end
opts.on('--path [PATH]', 'Path to folder you\'d like to create the challenge in') do |path|
options[:path] = path
end
end,
:handler => lambda {
path = options[:path] != nil ? options[:path]+'/.codechallenger.json' : '.codechallenger.json'
if File.file?(path)
loop do
print "\nThere is already a .codechallenger.json in this directory.\nWould you like to continue? [y/N] "
answer = gets.chomp
return if answer.downcase != 'y'
break if answer.downcase == 'y'
end
end
challenger = Challenger.new(options)
loop do
challenger.new_challenge
puts "\n"+challenger.construct_str
print "\nUse this challenge? [Y/n] "
answer = gets.chomp
break if answer.downcase != 'n'
end
details = challenger.get_details
# write details to .codechallenger.json file
File.open(path, 'w') {|f| f.write(JSON.pretty_generate(details)) }
puts "\nStart Coding!"
}
},
'deploy' => {
:optionParser => OptionParser.new do |opts|
opts.banner = 'Usage: codechallenger deploy [options...]'
opts.on('--path [PATH]', 'Path to the challenge folder') do |path|
options[:path] = path
end
opts.on('--repository [URL]', 'URL of the repository you would like to use. Will be saved.') do |repo|
options[:repo] = repo
end
opts.on('-m [MESSAGE]', '--message [MESSAGE]', 'The commit message you would like to use for the commit') do |msg|
options[:message] = msg
end
opts.on('-p', '--pull', 'Pull before pushing to the repo') do |pullFirst|
options[:pull] = pullFirst
end
opts.on('--istest', 'Specifies if running the deploy command is part of a unit test') do |isTest|
options[:isTest] = isTest
end
end,
:handler => lambda {
path = options[:path] != nil ? options[:path]+'/' : ''
# check if directory is codechallenger directory
check_if_challenger_dir(path)
# check if folder has Git
if not File.directory?(path+'.git')
if not system('git init')
exit 1
end
end
# get settings
settings = get_settings(path)
# check repo field
check_repository_field(path, settings, options[:repo])
# git commit message and repo
msgFlag = options[:message] != nil ? "-m '"+options[:message]+"'" : ''
# add and commit
if not system('git add -A') or not system("git commit #{msgFlag}")
exit 1
end
# pull first if needed
if options[:pull] != nil
if not system('git pull codechallenger master')
exit 1
end
end
# deploy to repo
# if not test
if not options[:isTest]
if not system('git push codechallenger master')
exit 1
end
end
puts "\nChallenge deployed to: "+settings['repository']
}
},
'uninit' => {
:optionParser => OptionParser.new do |opts|
opts.banner = 'Usage: codechallenger uninit [options...]'
opts.on('--path [PATH]', 'Path to the challenge folder') do |path|
options[:path] = path
end
end,
:handler => lambda {
path = options[:path] != nil ? options[:path]+'/' : ''
settings = get_settings(path)
loop do
print "\nThis will permanently remove CodeChallenger from this directory.\nWould you like to continue? [y/N] "
answer = gets.chomp
return if answer.downcase != 'y'
break if answer.downcase == 'y'
end
# remove settings file
File.delete(path+'.codechallenger.json')
# remove git url
if File.directory?(path+'.git') && settings['repository']
if not system('git remote remove codechallenger')
exit 1
end
end
puts "\nCodeChallenger has been removed from this directory."
}
}
}
# parse global flags
global.order!
# if subcommand, parse flags
command = ARGV.shift
if command != nil && subCommands[command] != nil
details = subCommands[command]
details[:optionParser].order!
details[:handler].call
else
puts helpText
end
exit 0