Class: CommiterRules::CommiterRulesStarter

Inherits:
Object
  • Object
show all
Defined in:
lib/commiter_rules_generator.rb

Class Method Summary collapse

Class Method Details

.start(save_path) ⇒ Object

Start Position To Create The Ruby Executable File



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

def self.start(save_path)

  begin
    File.open(save_path,"w") do |f|
      f.write("#!/usr/bin/env ruby")
      f.write("")
      f.write("require 'json'")
      f.write("target_message_line = 0")
      f.write("target_text_message = \" \"")
      f.write("current_directory = __dir__")
      f.write("data_hash = \"\"")
      f.write("check_type = \"\"")
      f.write("")
      f.write("# --------------------- Step Read Commiter Configuration ----------------- #")
      f.write("text = File.open(ARGV[0]).read")
      f.write("     text.gsub!(/\r\n?/,\" \\n \")")
      f.write("     text.each_line do |line|")
      f.write("     target_text_message = line")
      f.write("end")
      f.write("")
      f.write("file = File.open current_directory + \"/commiterConfig.json\"")
      f.write("    data = JSON.load file")
      f.write("file.close")
      f.write("")
      f.write("json_from_file = File.read(current_directory + \"/commiterConfig.json\")")
      f.write("hash = JSON.parse(json_from_file)")
      f.write("check_type = hash[\"generated_style\"]")
      f.write("# --------------------- End Step Read Commiter Configuration ----------------- #")
      f.write("")
      f.write("")
      f.write("# --------------------- Step Check Commiter Configuration Enabled ----------------- #")
      f.write("if hash[\"is_enabled\"]")
      f.write("  # Hooks Enabled Start Validation")
      f.write("  # Will Continue To Check Based on Json Config")
      f.write("else")
      f.write("  # Hooks Disabled Continue")
      f.write("  exit 0")
      f.write("end")
      f.write("# --------------------- End Step Check Commiter Configuration Enabled ----------------- #")
      f.write("")
      f.write("# --------------------- Step Check if Ticket Number Exists ----------------- #")
      f.write("if check_type == \"t\"")
      f.write("   validation_message = hash[\"ticket_number_example\"]")
      f.write("   validation_message[\"{#}\"] = \"\"")
      f.write("   if target_text_message.include? validation_message")
      f.write("    puts \"Ticket Number Key Found, Good Commit Based on Configured Rules\"")
      f.write("    exit 0")
      f.write("  else")
      f.write("    puts \"Bad Commit Message (Ticket Number Not Found) Commit Rejected\"")
      f.write("    exit 1")
      f.write("  end")
      f.write("end")
      f.write("# --------------------- End Step Check if Ticket Number Exists ----------------- #")
      f.write("")
      f.write("# --------------------- Step Check if Commit Message Equals Black List Array ----------------- #")
      f.write("")
      f.write("if check_type == \"b\"")
      f.write("  black_list_words = hash[\"black_list_words\"]")
      f.write("  filtered_message = target_text_message.downcase.strip")
      f.write("  black_list_words.each { |value|")
      f.write("    if filtered_message == value.downcase.strip")
      f.write("      puts \"Bad Commit Message (\" " + value + " Message Not Acceptable) Commit Rejected\"")
      f.write("      exit 1")
      f.write("    end")
      f.write("  }")
      f.write("end")
      f.write("# --------------------- End Step Check if Commit Message Equals Black List Array ----------------- #")
      f.write("")
      f.write("# --------------------- Step Check Commit Message Equals Regex ----------------- #")
      f.write("if check_type == \"r\"")
      f.write("  if target_text_message.match(hash[\"regex_input\"])")
      f.write("    # Input Regex Matched")
      f.write("    exit 0")
      f.write("  else")
      f.write("    puts \"Bad Commit Message (Message Not Match Regex) Commit Rejected\"")
      f.write("    exit 1")
      f.write("  end")
      f.write("end")
      f.write("# --------------------- End Step Check Commit Message Equals Regex ----------------- #")
      f.write("")
      f.write("")
    end
  rescue => error
    puts "Error While Executing Git Rules : #{error.message}"
    puts error.backtrace
  end

end