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



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

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



365
366
367
368
369
# File 'lib/wielder_of_anor.rb', line 365

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

#commitObject



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/wielder_of_anor.rb', line 341

def commit
  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



385
386
387
# File 'lib/wielder_of_anor.rb', line 385

def double_space
  puts "\n\n"
end

#first_runObject



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

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

  single_space

  forbidden_words = set_forbidden_words

  set_configs(files_changed_file_location, forbidden_words, commit_for_user)
end

#git_diffObject



225
226
227
# File 'lib/wielder_of_anor.rb', line 225

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

#helpObject



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

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



375
376
377
378
379
# File 'lib/wielder_of_anor.rb', line 375

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

  lines.each { |line| puts line }
end

#output_forbidden_wordsObject



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

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
# 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_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']

  # 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


337
338
339
# File 'lib/wielder_of_anor.rb', line 337

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

#restore_settingsObject

Attempt to restore settings from previous version.



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

def restore_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)
  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



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/wielder_of_anor.rb', line 314

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



371
372
373
# File 'lib/wielder_of_anor.rb', line 371

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

#set_commit_for_userObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/wielder_of_anor.rb', line 158

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

  commit_for_user
end

#set_configs(files_changed_file_location, forbidden_words, commit_for_user) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/wielder_of_anor.rb', line 174

def set_configs(files_changed_file_location, forbidden_words, commit_for_user)
  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

  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



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

def set_forbidden_words
  forbidden_words = []

  done = false

  lines_pretty_print 'Great! Now that we\'re done with the files, 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, type \'x211\' and 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, type \'x211\' and hit enter instead.').yellow unless forbidden_words.count == 0
    word = STDIN.gets.strip!

    single_space

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

  forbidden_words
end

#single_spaceObject



381
382
383
# File 'lib/wielder_of_anor.rb', line 381

def single_space
  puts ''
end

#wielder_of_anorObject



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
# File 'lib/wielder_of_anor.rb', line 255

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