Class: WielderOfAnor::WielderOfAnor

Inherits:
Object
  • Object
show all
Includes:
WielderOfAnorVersion
Defined in:
lib/wielder_of_anor.rb

Constant Summary

Constants included from WielderOfAnorVersion

WielderOfAnorVersion::VERSION

Instance Method Summary collapse

Constructor Details

#initializeWielderOfAnor

Returns a new instance of WielderOfAnor.



12
13
14
15
16
# File 'lib/wielder_of_anor.rb', line 12

def initialize
  set_app_directory

  @forbidden_words = []
end

Instance Method Details

#add_forbidden_word(word) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/wielder_of_anor.rb', line 250

def add_forbidden_word(word)
  set_app_directory
  config_yaml = YAML.load_file("#{@app_directory}/lib/config.yaml")
  config_file = File.open("#{@app_directory}/lib/config.yaml", 'w')

  if word.nil?
    lines_pretty_print Rainbow('Please submit your word as a second parameter.').red

    abort
  end

  if config_yaml['forbidden_words'].include?(word)
    lines_pretty_print Rainbow("''#{word}'' is already a forbidden word!").red

    abort
  end

  config_yaml['forbidden_words'] << word

  YAML.dump(config_yaml, config_file)

  lines_pretty_print 'Added!'

  abort
end

#bash(command) ⇒ Object



444
445
446
447
448
# File 'lib/wielder_of_anor.rb', line 444

def bash(command)
  # Dir.chdir ensures all bash commands are being run from the correct
  # directory.
  Dir.chdir(@current_directory) { system "#{command}" }
end

#commitObject



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/wielder_of_anor.rb', line 362

def commit
  if @check_branch
    bash("git rev-parse --abbrev-ref HEAD > #{@current_branch_file_location}")
    current_branch = File.open(@current_branch_file_location, "r").read.strip!

    if @branches_to_check.include?(current_branch)
      lines_pretty_print Rainbow('I\'m sorry, Dave, but I can\'t allow you to do that.').red
      lines_pretty_print Rainbow('You have tried committing to a forbidden branch. Danger, Will Robinson! Danger! '\
                                 'Aborting!').red

      abort
    end
  end

  if @force_commit
    lines_pretty_print 'Skipped checking for forbidden words. Ready to commit now?'
    lines_pretty_print Rainbow('**WARNING: YOU ARE FORCING THE COMMIT WITHOUT CHECKING FOR FORBIDDEN WORDS.**').red

    single_space
  else
    lines_pretty_print 'Okay to commit! Should I go ahead and run the actual commit now?'
  end

  lines_pretty_print 'Please type \'yes\' OR \'y\' to continue. Any other input will be treated as a \'no\'.'

  input = STDIN.gets.chomp.downcase

  single_space

  if input == 'yes' || input == 'y'
    bash(%Q[git commit -m "#{@commit_message}"])
    single_space
    lines_pretty_print 'Committed.'
    single_space
  end
end

#double_spaceObject



460
461
462
# File 'lib/wielder_of_anor.rb', line 460

def double_space
  puts "\n\n"
end

#first_runObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/wielder_of_anor.rb', line 103

def first_run
  lines_pretty_print 'Thanks for downloading Wielder of Anor! Let\'s run through the '\
       'initial setup!'

  STDIN.gets

  files_changed_file_location = "#{@app_directory}/lib/files_changed"

  commit_for_user = set_commit_for_user

  check_branch = set_check_branch

  branches_to_check = set_branches_to_check if check_branch == 'yes' || check_branch == 'y'

  single_space

  forbidden_words = set_forbidden_words

  set_configs(files_changed_file_location, forbidden_words, commit_for_user, check_branch, branches_to_check)
end

#git_diffObject



246
247
248
# File 'lib/wielder_of_anor.rb', line 246

def git_diff
  bash("git diff HEAD --name-only --staged > #{@files_changed_file_location}")
end

#helpObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/wielder_of_anor.rb', line 56

def help
  lines_pretty_print 'Wielder of Anor can accept up to two parameters on run. The first parameter can be:'

  single_space

  lines_pretty_print '- Your eventual commit message (in quotes). This is ignored if you have not allowed '\
       'Wielder of Anor to commit for you.'
  lines_pretty_print '- help - You should already know what this does :).'
  lines_pretty_print '- config - Re-runs the initial set up.'

  single_space

  lines_pretty_print 'And your second parameter can only be \'1\', and it\'ll be ignored unless all of the '\
                     'following is true:'

  single_space

  lines_pretty_print '- You have allowed Wielder of Anor to run commits for you.'
  lines_pretty_print '- Your first parameter is your commit message.'

  abort
end

#lines_pretty_print(string) ⇒ Object



450
451
452
453
454
# File 'lib/wielder_of_anor.rb', line 450

def lines_pretty_print(string)
  lines = string.scan(/\S.{0,70}\S(?=\s|$)|\S+/)

  lines.each { |line| puts line }
end

#output_forbidden_wordsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/wielder_of_anor.rb', line 79

def output_forbidden_words
  set_app_directory
  config = YAML.load_file("#{@app_directory}/lib/config.yaml")

  unless config && config['forbidden_words']
    lines_pretty_print Rainbow('You have yet to set your forbidden words! Please run the app with the parameter '\
                               '\'config\' to set up your configurations and forbidden words.').red

    abort
  end

  lines_pretty_print Rainbow('Your forbidden words are:').yellow

  single_space

  config['forbidden_words'].each do |word|
    lines_pretty_print Rainbow(word).yellow
  end

  single_space

  abort
end

#prepare(commit_message, force_commit) ⇒ Object

## commit_message is the git commit message the user would like to send ## once we have verified that there are no forbidden words present in their ## code. ##

##

force_commit is either ‘1’ or nil. If it’s 1, we will commit later even ## if there are forbidden words present. ##

##


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/wielder_of_anor.rb', line 31

def prepare(commit_message, force_commit)
  # If there's just one, it's the current version. Don't run if the current config is present.
  restore_woa_settings if Dir.glob("#{@app_directory.chomp("/wielder_of_anor-#{VERSION}")}/wielder_of_anor*").length > 1 &&
                          !(File.exists?("#{@app_directory}/lib/config.yaml"))
  first_run unless File.exists?("#{@app_directory}/lib/config.yaml")

  config = YAML.load_file("#{@app_directory}/lib/config.yaml")
  @commit_message = commit_message
  @force_commit = force_commit
  @current_directory = Dir.pwd
  @files_changed_file_location = config['files_changed_file_location']
  @commit_for_user = config['commit_for_user']
  @check_branch = config['check_branch']
  @branches_to_check = config['branches_to_check']
  @current_branch_file_location = "#{@app_directory}/lib/current_branch"

  # Don't want to use a previous version.
  File.delete(@files_changed_file_location) if File.exists?(@files_changed_file_location)

  git_diff

  @files_changed_file = File.open(@files_changed_file_location, "r")
  @forbidden_words = config['forbidden_words']
end


358
359
360
# File 'lib/wielder_of_anor.rb', line 358

def print_header_footer
  puts '***************************************************************************'
end

#restore_woa_settingsObject



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/wielder_of_anor.rb', line 403

def restore_woa_settings
  lines_pretty_print 'I see that you have a previous wielder_of_anor installation on this machine.'
  lines_pretty_print Rainbow('Would you like to restore its settings?').yellow

  answered = false

  until answered
    answer = STDIN.gets.strip!

    single_space

    if answer == 'yes' || answer == 'y' || answer == 'no' || answer == 'n'
      answered = true
    else
      lines_pretty_print Rainbow('Please input either \'yes\' or \'no\'.').yellow
    end
  end

  return if answer == 'no' || answer == 'n'

  lines_pretty_print 'One moment, please.'

  single_space

  all_gems = Dir.glob("#{@app_directory.chomp("/wielder_of_anor-#{VERSION}")}/wielder_of_anor*")

  # glob orders things in the array alphabetically, so the second-to-last one in the array is the
  # most recent version that is not the current version.
  previous_config_file = "#{all_gems[-2]}/lib/config.yaml"
  config = YAML.load_file(previous_config_file)
  config['files_changed_file_location'] = "#{@app_directory}/lib/files_changed"
  new_config_file = File.open("#{@app_directory}/lib/config.yaml", 'w')

  YAML.dump(config, new_config_file)
  new_config_file.close

  lines_pretty_print 'Done! Please run me again when you\'re ready.'

  abort
end

#results(found_forbidden) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/wielder_of_anor.rb', line 335

def results(found_forbidden)
  if found_forbidden
    single_space
    lines_pretty_print 'Remove offending line(s) and re-run commit statement or run this '\
                       'app again with \'1\' as your second argument to force the commit.'

    single_space
    lines_pretty_print Rainbow('**ONLY FORCE THE COMMIT IF YOU ARE SURE YOU ARE 100% SURE YOU WANT '\
                               'TO COMMIT THE ABOVE LINES TO YOUR BRANCH!!**').red
    File.delete(@files_changed_file)
    abort
  else
    unless @force_commit
      lines_pretty_print Rainbow('Found 0 forbidden words!').green

      single_space
    end

    File.delete(@files_changed_file)
    commit if @commit_for_user
  end
end

#set_app_directoryObject



399
400
401
# File 'lib/wielder_of_anor.rb', line 399

def set_app_directory
  @app_directory = File.expand_path(File.dirname(__FILE__)).chomp('/lib')
end

#set_branches_to_checkObject



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
# File 'lib/wielder_of_anor.rb', line 159

def set_branches_to_check
  branches_to_check = []

  done = false

  lines_pretty_print 'Sounds good! What branches should I block you from committing to?'
  single_space

  until done do
    lines_pretty_print Rainbow('Enter a forbidden branch and hit enter. If you are done entering forbidden '\
                               'branches, just hit enter instead.').yellow unless branches_to_check.count > 0

    lines_pretty_print Rainbow('Added! Enter another forbidden branch and hit enter. If you are done '\
                               'entering forbidden branches, just hit enter instead.').yellow unless branches_to_check.count == 0
    branch = STDIN.gets.strip!

    single_space

    if branch == ''
      done = true
    else
      branches_to_check << branch
    end
  end

  branches_to_check
end

#set_check_branchObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/wielder_of_anor.rb', line 142

def set_check_branch
  lines_pretty_print 'Would you like Wielder of Anor to stop you from committing to a certain branch or branches?'
  lines_pretty_print Rainbow('(Type \'yes\' or \'no\'. Just hitting enter defaults to no.)').yellow

  check_branch = STDIN.gets.strip!.downcase
  check_branch = 'no' if check_branch == ''

  until check_branch == 'yes' || check_branch =='y' || check_branch == 'no' || check_branch == 'n' do
    lines_pretty_print Rainbow('Please type either \'yes\' or \'no\'.').yellow
    check_branch = STDIN.gets.strip!.downcase
  end

  single_space

  check_branch
end

#set_commit_for_userObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/wielder_of_anor.rb', line 124

def set_commit_for_user
  lines_pretty_print 'Would you like Wielder of Anor to run your commits for you once you'\
       ' have verified that your code is free of forbidden words?'
  lines_pretty_print Rainbow('(Type \'yes\' or \'no\'. Just hitting enter defaults to no.)').yellow

  commit_for_user = STDIN.gets.strip!.downcase
  commit_for_user = 'no' if commit_for_user == ''

  until commit_for_user == 'yes' || commit_for_user =='y' || commit_for_user == 'no' || commit_for_user == 'n' do
    lines_pretty_print Rainbow('Please type either \'yes\' or \'no\'.').yellow
    commit_for_user = STDIN.gets.strip!.downcase
  end

  single_space

  commit_for_user
end

#set_configs(files_changed_file_location, forbidden_words, commit_for_user, check_branch, branches_to_check) ⇒ Object



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
# File 'lib/wielder_of_anor.rb', line 187

def set_configs(files_changed_file_location, forbidden_words, commit_for_user, check_branch, branches_to_check)
  config = {}
  config['files_changed_file_location'] = files_changed_file_location

  if commit_for_user == 'yes' || commit_for_user == 'y'
    config['commit_for_user'] = true
  else
    config['commit_for_user'] = false
  end

  if check_branch == 'yes' || check_branch == 'y'
    config['check_branch'] = true
    config['branches_to_check'] = branches_to_check
  else
    config['check_branch'] = false
    config['branches_to_check'] = []
  end

  config['forbidden_words'] = forbidden_words

  file = File.open("#{@app_directory}/lib/config.yaml", 'w')
  YAML.dump(config, file)
  file.close

  lines_pretty_print 'And with that, we\'re done! Feel free to run Wielder of Anor again if'\
       ' you\'d like to check your code now!'

  abort
end

#set_forbidden_wordsObject



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
# File 'lib/wielder_of_anor.rb', line 217

def set_forbidden_words
  forbidden_words = []

  done = false

  lines_pretty_print 'Great! Now that we\'re done with that, let\'s add your forbidden '\
                     'words from here!'
  single_space

  until done do
    lines_pretty_print Rainbow('Enter a forbidden word and hit enter. If you are done entering '\
                       'forbidden words just hit enter instead.').yellow unless forbidden_words.count > 0

    lines_pretty_print Rainbow('Added! Enter another forbidden word and hit enter. If you are done '\
                               'entering forbidden words, just hit enter instead.').yellow unless forbidden_words.count == 0
    word = STDIN.gets.strip!

    single_space

    if word == ''
      done = true
    else
      forbidden_words << word
    end
  end

  forbidden_words
end

#single_spaceObject



456
457
458
# File 'lib/wielder_of_anor.rb', line 456

def single_space
  puts ''
end

#wielder_of_anorObject



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/wielder_of_anor.rb', line 276

def wielder_of_anor
  found_forbidden = false
  count = File.foreach(@files_changed_file).inject(0) {|c, line| c+1}

  if count == 0
    single_space

    lines_pretty_print Rainbow('No files have been added. Please use the git add command to add files to your '\
                         'commit.').red

    single_space

    abort
  end

  # If we're forcing the commit, don't bother checking for forbidden words.
  unless @force_commit
    single_space

    print_header_footer

    single_space

    @files_changed_file.each_line do |files_changed_line|
      file_path = "#{@current_directory}/#{files_changed_line.strip}"
      code_file = File.open(file_path, "r") if File.exists?(file_path)
      index = 0
      next unless code_file

      code_file.each_line do |line|
        index += 1
        @forbidden_words.each do |word|
          if line.include?(word)
            found_forbidden = true
            lines_pretty_print Rainbow("-- FORBIDDEN WORD FOUND ON LINE #{index} IN #{files_changed_line.strip}: --").red
            lines_pretty_print Rainbow("   #{line}").yellow
            double_space
          end
        end
      end

      code_file.close
    end

    single_space

    print_header_footer

    single_space
  else
    lines_pretty_print Rainbow('NOT SEARCHING FOR FORBIDDEN WORDS, PER USER INPUT.').red
    single_space
  end

  @files_changed_file.close

  results(found_forbidden)
end