Class: Makit::Lint
- Inherits:
-
Object
- Object
- Makit::Lint
- Defined in:
- lib/makit/lint.rb
Overview
Lint utilities for validating JSON and YAML files
This class provides methods for linting JSON and YAML files, helping to catch syntax errors and validation issues early.
Constant Summary collapse
- DEFAULT_EXCLUDE_PATTERNS =
Default directories to exclude from linting
[ "artifacts/", "bin/", "obj/", ".nuget/", ".git/", ".specify/" ].freeze
Class Method Summary collapse
-
.lint_json(pattern, exclude_patterns: DEFAULT_EXCLUDE_PATTERNS) ⇒ Hash
Lint JSON files based on a glob pattern or filename.
-
.lint_json!(pattern, exclude_patterns: DEFAULT_EXCLUDE_PATTERNS, verbose: true) ⇒ Object
Lint JSON files and print results.
-
.lint_yaml(pattern, exclude_patterns: DEFAULT_EXCLUDE_PATTERNS) ⇒ Hash
Lint YAML files based on a glob pattern or filename.
-
.lint_yaml!(pattern, exclude_patterns: DEFAULT_EXCLUDE_PATTERNS, verbose: true) ⇒ Object
Lint YAML files and print results.
Class Method Details
.lint_json(pattern, exclude_patterns: DEFAULT_EXCLUDE_PATTERNS) ⇒ Hash
Lint JSON files based on a glob pattern or filename
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 |
# File 'lib/makit/lint.rb', line 28 def self.lint_json(pattern, exclude_patterns: DEFAULT_EXCLUDE_PATTERNS) # Determine if pattern is a glob or specific file if File.file?(pattern) json_files = [pattern] else json_files = Dir.glob(pattern) .reject { |f| exclude_patterns.any? { |exclude| f.start_with?(exclude) } } .select { |f| File.file?(f) } end if json_files.empty? return { valid: true, errors: [], file_count: 0, message: "No JSON files found to validate" } end errors = [] json_files.each do |file| begin content = File.read(file) # Remove BOM if present (UTF-8 BOM: EF BB BF) content = content.force_encoding("UTF-8").sub(/\A\xEF\xBB\xBF/, "") JSON.parse(content) rescue JSON::ParserError => e errors << { file: file, error: e. } end end { valid: errors.empty?, errors: errors, file_count: json_files.length } end |
.lint_json!(pattern, exclude_patterns: DEFAULT_EXCLUDE_PATTERNS, verbose: true) ⇒ Object
Lint JSON files and print results
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 |
# File 'lib/makit/lint.rb', line 72 def self.lint_json!(pattern, exclude_patterns: DEFAULT_EXCLUDE_PATTERNS, verbose: true) result = lint_json(pattern, exclude_patterns: exclude_patterns) if result[:file_count] == 0 puts result[:message] if verbose return result end if verbose puts "Validating #{result[:file_count]} JSON file(s)..." end # Re-run to show progress dots if verbose && result[:file_count] > 0 json_files = File.file?(pattern) ? [pattern] : Dir.glob(pattern) .reject { |f| exclude_patterns.any? { |exclude| f.start_with?(exclude) } } .select { |f| File.file?(f) } json_files.each do |file| begin content = File.read(file) content = content.force_encoding("UTF-8").sub(/\A\xEF\xBB\xBF/, "") JSON.parse(content) print "." rescue JSON::ParserError print "F" end end puts "\n" end if result[:valid] puts "✓ All JSON files are valid" if verbose else puts "\n✗ Found #{result[:errors].length} invalid JSON file(s):" if verbose result[:errors].each do |err| puts " - #{err[:file]}: #{err[:error]}" if verbose end raise "JSON validation failed" end result end |
.lint_yaml(pattern, exclude_patterns: DEFAULT_EXCLUDE_PATTERNS) ⇒ Hash
Lint YAML files based on a glob pattern or filename
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 |
# File 'lib/makit/lint.rb', line 121 def self.lint_yaml(pattern, exclude_patterns: DEFAULT_EXCLUDE_PATTERNS) # Determine if pattern is a glob or specific file if File.file?(pattern) yaml_files = [pattern] else yaml_files = Dir.glob(pattern) .reject { |f| exclude_patterns.any? { |exclude| f.start_with?(exclude) } } .select { |f| File.file?(f) } end if yaml_files.empty? return { valid: true, errors: [], file_count: 0, message: "No YAML files found to validate" } end errors = [] yaml_files.each do |file| begin content = File.read(file) # Remove BOM if present (UTF-8 BOM: EF BB BF) content = content.force_encoding("UTF-8").sub(/\A\xEF\xBB\xBF/, "") YAML.load(content, permitted_classes: [Symbol, Date, Time], aliases: true) rescue Psych::SyntaxError => e errors << { file: file, error: e. } rescue => e errors << { file: file, error: e. } end end { valid: errors.empty?, errors: errors, file_count: yaml_files.length } end |
.lint_yaml!(pattern, exclude_patterns: DEFAULT_EXCLUDE_PATTERNS, verbose: true) ⇒ Object
Lint YAML files and print results
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 |
# File 'lib/makit/lint.rb', line 167 def self.lint_yaml!(pattern, exclude_patterns: DEFAULT_EXCLUDE_PATTERNS, verbose: true) result = lint_yaml(pattern, exclude_patterns: exclude_patterns) if result[:file_count] == 0 puts result[:message] if verbose return result end if verbose puts "Validating #{result[:file_count]} YAML file(s)..." end # Re-run to show progress dots if verbose && result[:file_count] > 0 yaml_files = File.file?(pattern) ? [pattern] : Dir.glob(pattern) .reject { |f| exclude_patterns.any? { |exclude| f.start_with?(exclude) } } .select { |f| File.file?(f) } yaml_files.each do |file| begin content = File.read(file) content = content.force_encoding("UTF-8").sub(/\A\xEF\xBB\xBF/, "") YAML.load(content, permitted_classes: [Symbol, Date, Time], aliases: true) print "." rescue print "F" end end puts "\n" end if result[:valid] puts "✓ All YAML files are valid" if verbose else puts "\n✗ Found #{result[:errors].length} invalid YAML file(s):" if verbose result[:errors].each do |err| puts " - #{err[:file]}: #{err[:error]}" if verbose end raise "YAML validation failed" end result end |