Module: CheckFileSyntax
- Defined in:
- lib/check_file_syntax.rb
Constant Summary collapse
- ALL_CHECKS =
[:puppet, :ruby, :python, :perl, :bash, :erb, :yaml, :json]
Class Method Summary collapse
- .bash_file?(path) ⇒ Boolean
- .check_file_syntax(path, &block) ⇒ Object
- .erb_file?(path) ⇒ Boolean
- .json_file?(path) ⇒ Boolean
- .perl_file?(path) ⇒ Boolean
-
.puppet_file?(path) ⇒ Boolean
define a bunch of convenience functions.
- .python_file?(path) ⇒ Boolean
- .ruby_file?(path) ⇒ Boolean
- .search_all_files_for_errors(directory, excludes = [], checks = ALL_CHECKS, &block) ⇒ Object
- .show_status(name, success, errors) ⇒ Object
- .type_of_file(path, interpreter, extensions) ⇒ Object
- .yaml_file?(path) ⇒ Boolean
Class Method Details
.bash_file?(path) ⇒ Boolean
48 49 50 |
# File 'lib/check_file_syntax.rb', line 48 def bash_file?(path) type_of_file(path, :bash, ['.sh', '.bash', '.zsh', '.ksh']) end |
.check_file_syntax(path, &block) ⇒ Object
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 |
# File 'lib/check_file_syntax.rb', line 107 def check_file_syntax(path, &block) errors = '' status = nil if puppet_file? path if system('which puppet >/dev/null') errors = `puppet parser validate #{path} 2>&1` status = $?.success? ? :passed : :failed else puts 'Consider installing puppet so that syntax can be checked.'.colorize(:yellow) status = :skipped end end if erb_file? path errors = `cat #{path} | erb -x -T - | ruby -c 2>&1` status = $?.success? ? :passed : :failed end if python_file? path if system('which python >/dev/null') errors = `python -m py_compile #{path} 2>&1` status = $?.success? ? :passed : :failed else puts 'Consider installing python so that syntax can be checked.'.colorize(:yellow) status = :skipped end end if ruby_file? path errors = `ruby -c #{path} 2>&1` status = $?.success? ? :passed : :failed end if perl_file? path if system('which perl >/dev/null') errors = `perl -c #{path} 2>&1` status = $?.success? ? :passed : :failed else puts 'Consider installing perl so that syntax can be checked.'.colorize(:yellow) status = :skipped end end if bash_file? path errors = `bash -n #{path} 2>&1`.to_i status = $?.success? ? :passed : :failed end if json_file? path begin JSON.parse(File.read(path)) status = :passed rescue Exception => e errors = e. status = :failed end end if yaml_file? path begin YAML.parse(File.read(path)) status = :passed rescue Exception => e errors = e. status = :failed end end if block_given? yield path, status, errors else show_status(path, status, errors) error_count += 1 if status == :failed end end |
.erb_file?(path) ⇒ Boolean
32 33 34 |
# File 'lib/check_file_syntax.rb', line 32 def erb_file?(path) type_of_file(path, :erb, '.erb') end |
.json_file?(path) ⇒ Boolean
52 53 54 |
# File 'lib/check_file_syntax.rb', line 52 def json_file?(path) type_of_file(path, :json, '.json') end |
.perl_file?(path) ⇒ Boolean
44 45 46 |
# File 'lib/check_file_syntax.rb', line 44 def perl_file?(path) type_of_file(path, :perl, ['.pl', '.pm']) end |
.puppet_file?(path) ⇒ Boolean
define a bunch of convenience functions
28 29 30 |
# File 'lib/check_file_syntax.rb', line 28 def puppet_file?(path) type_of_file(path, :puppet, '.pp') end |
.python_file?(path) ⇒ Boolean
36 37 38 |
# File 'lib/check_file_syntax.rb', line 36 def python_file?(path) type_of_file(path, :python, '.py') end |
.ruby_file?(path) ⇒ Boolean
40 41 42 |
# File 'lib/check_file_syntax.rb', line 40 def ruby_file?(path) type_of_file(path, :ruby, '.rb') end |
.search_all_files_for_errors(directory, excludes = [], checks = ALL_CHECKS, &block) ⇒ Object
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 |
# File 'lib/check_file_syntax.rb', line 76 def search_all_files_for_errors(directory, excludes=[], checks=ALL_CHECKS, &block) error_count = 0 Find.find(directory) do |path| # prune the directory tree if we found a directory that should be excluded if File.directory? path if not (excludes.select { |d| path.end_with? d }).empty? Find.prune else # we don't do any thing with dirs, so go to next item next end end # TODO Need to look at checks to determine if we should do this check_file_syntax(path) do |path, status, errors| if status == :failed error_count += 1 end if block_given? yield path, status, errors else show_status(path, status, errors) end end end return error_count end |
.show_status(name, success, errors) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/check_file_syntax.rb', line 62 def show_status (name, success, errors) # Untested files return a nil success unless success.nil? if success == :passed puts ' OK '.colorize(:green) + " #{name}".colorize(:cyan) else puts ' FAIL '.colorize(:light_yellow).swap + " #{name}".colorize(:cyan) puts errors end end end |
.type_of_file(path, interpreter, extensions) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/check_file_syntax.rb', line 12 def type_of_file(path, interpreter, extensions) # check extensions first [extensions].flatten.each {|ext| return true if path.end_with? ext } # check only if really a file and is not 0 byte if File.file?(path) and File.size?(path) # Look for a she-bang line and check for interpreter shebang = File.open(path).first if shebang and shebang.start_with? '#!/' and shebang.include? interpreter.to_s return true end end return false end |
.yaml_file?(path) ⇒ Boolean
56 57 58 |
# File 'lib/check_file_syntax.rb', line 56 def yaml_file?(path) type_of_file(path, '---', ['.yaml', '.yml']) end |