Top Level Namespace
Defined Under Namespace
Modules: BakerActions Classes: Baker, BakerConfig, Plugins, Recipe, RecipeStep
Instance Method Summary collapse
Instance Method Details
#validate_rails_command(command_line) ⇒ Object
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 99 100 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 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/baker/plugins/rails_generate_preflight.rb', line 27 def validate_rails_command(command_line) require 'active_support/core_ext/string/inflections' require 'shellwords' warnings = [] = [] # Normalize and split the command line command_line = command_line.strip tokens = Shellwords.shellsplit(command_line) raw_tokens = command_line.split(/\s+/) raw_tokens.each { |token| if token =~ /\{\d+\s*,\s*\d+\}/ && !(token.strip.start_with?('\'') && token.strip.end_with?('\'')) warnings << "Need to escape {x,y} to avoid conflict with shell." end } # Remove prefixes prefixes = ['rails', 'g', 'generate', 'bin/rails'] tokens.shift while prefixes.include?(tokens.first) if tokens.empty? warnings << "No generator type specified." return warnings end generator_type = tokens.shift unless %w[model scaffold controller migration].include?(generator_type) # We only handle the known generator type return warnings end if tokens.empty? warnings << "No name specified for the generator." return warnings end name = tokens.shift reserved_model_fields = %w[ type id attributes position parent_id lft rgt created_at created_on updated_at updated_on deleted_at lock_version accept action attributes callback category connection database dispatcher drive errors format host key layout load link new notify open public quote render request records responses save scope send session system template test timeout to_s type visits lock_version quote_value and not or alias begin break case class def else do elsif end ensure for if module next redo rescue retry return self false true nil super then undef unless until when while yield ] # Check naming conventions case generator_type when 'model', 'scaffold' expected_name = name.camelize.singularize unless name == expected_name warnings << "Model name '#{name}' should be singular and in CamelCase (e.g., '#{expected_name}')." end when 'controller' expected_name = name.camelize unless name == expected_name warnings << "Controller name '#{name}' should be in CamelCase (e.g., '#{expected_name}')." end if Dir.glob("app/models/*.rb").any? { |filename| File.basename(filename, ".*").camelize == name } warnings << "Controller name matches an existing model '#{name}'. Did you want to create a controller for this model? Use plural form '#{name.pluralize}'." end # For controller, the remaining tokens are action names action_names = tokens action_names.each do |action| if action.start_with?('--') << action else reserved_actions = %w[ send object_id class method initialize freeze exit loop raise fork eval alias break begin end save valid invalid errors reload attributes all head render redirect_to params session request response controller action ] # Optionally validate action names if reserved_actions.include?(action.downcase) warnings << "Action name '#{action}' might conflict with existing rails controller methods." end end end # Skip attribute validation for controllers return warnings end # Separate attributes and options attributes = [] tokens.each do |token| if token.start_with?('--') << token else attributes << token end end # Supported types supported_types = %w[ binary boolean date datetime decimal float integer string text time timestamp references ] # Validate attributes attributes.each do |attr| # Split attribute into name and type with modifiers attr_parts = attr.split(':', 2) attr_name = attr_parts[0] attr_type_and_modifiers = attr_parts[1] if attr_name.nil? || attr_name.empty? warnings << "Invalid attribute format '#{attr}'." next end if reserved_model_fields.include?(attr_name.downcase) warnings << "Attribute name '#{attr_name}' is a reserved word." end unless attr_type_and_modifiers warnings << "No type specified for attribute '#{attr_name}'." next end # Extract type and modifiers if attr_type_and_modifiers =~ /^(\w+)(\{[^}]*\})?$/ attr_type = $1 modifiers = $2 else warnings << "Invalid attribute format '#{attr}'." next end unless supported_types.include?(attr_type) warnings << "Attribute type '#{attr_type}' is not supported." end if attr_type == 'references' && attr_name != attr_name.singularize warnings << "Reference attribute '#{attr_name}' should be singular (e.g., '#{attr_name.singularize}')." end if attr_type == 'decimal' && modifiers && !(modifiers =~ /\{(\d+)\s*[,-]\s*\d+\}/) warnings << "Decimal modifier should be {precision,scale}." end if modifiers if !modifiers.match(/^\{[\w,]+\}$/) warnings << "Modifiers '#{modifiers}' for attribute '#{attr_name}' are not properly formatted. Use '{...}'." end end end # Allowed options = %w[ --skip-routes --skip-assets --skip-helper --skip-test-framework --skip-template-engine --skip-stylesheets --skip-migration --skip-timestamps --skip-namespace # Include all other allowed options ] .each do |option| unless .include?(option) warnings << "Option '#{option}' is not allowed." end end warnings end |