Module: Origen::CodeGenerators::Actions::Helpers
- Included in:
- Origen::CodeGenerators::Actions
- Defined in:
- lib/origen/code_generators/actions.rb
Overview
Should probably move to its own file, these are general helpers rather than actions
Instance Method Summary collapse
-
#add_type_to_namespaces(namespaces) ⇒ Object
Adds :class and :module identifiers to an array of namespaces.
-
#class_name_to_blocks_dir(name) ⇒ Object
Returns a Pathname to the blocks directory that should contain the given class name.
-
#class_name_to_lib_file(name) ⇒ Object
Returns a Pathname to the lib directory file that should contain the given class name.
-
#internal_depth(file) ⇒ Object
Returns the depth of the given file, where depth is the number of modules and classes it contains.
-
#resource_path(path) ⇒ Object
Converts a path to a resource identifier, by performing the following operations on the given path: 1) Convert any absolute paths to relative 2) Removes any leading blocks/, lib/ or application namespaces 3) Remove any derivatives directories from the path 3) Removes any trailing .rb.
- #resource_path_to_blocks_dir(path) ⇒ Object
- #resource_path_to_class(path) ⇒ Object
- #resource_path_to_lib_file(path) ⇒ Object
-
#unless_has_method(filepath, name) ⇒ Object
Only executes the given block if the given file does not already define the given method, where the block would normally go on to insert the method.
-
#unless_valid_underscored_identifier(str) ⇒ Object
Executes the given block unless the given string is lower cased and underscored and doesn't start with a number of contain any special characters.
- #validate_resource_path(name) ⇒ Object (also: #validate_resource_name)
Instance Method Details
#add_type_to_namespaces(namespaces) ⇒ Object
Adds :class and :module identifiers to an array of namespaces
["my_app", "models", "bist"] => [[:module, "my_app"], [:module, "models"], [:class, "bist"]]
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/origen/code_generators/actions.rb', line 416 def add_type_to_namespaces(namespaces) identifier = nil namespaces.map do |namespace| if identifier identifier += "::#{camelcase(namespace)}" else identifier = camelcase(namespace) end begin const = identifier.constantize [const.is_a?(Class) ? :class : :module, namespace] rescue NameError [:module, namespace] end end end |
#class_name_to_blocks_dir(name) ⇒ Object
Returns a Pathname to the blocks directory that should contain the given class name. No checking is done of the name and it is assumed that it is a valid class name including the application namespace.
355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/origen/code_generators/actions.rb', line 355 def class_name_to_blocks_dir(name) name = name.split('::') name.shift # Drop the application name dir = Origen.root.join('app', 'blocks') name.each_with_index do |n, i| if i == 0 dir = dir.join(n.underscore) else dir = dir.join('derivatives', n.underscore) end end dir end |
#class_name_to_lib_file(name) ⇒ Object
Returns a Pathname to the lib directory file that should contain the given class name. No checking is done of the name and it is assumed that it is a valid class name including the application namespace.
371 372 373 374 375 376 377 378 |
# File 'lib/origen/code_generators/actions.rb', line 371 def class_name_to_lib_file(name) name = name.split('::') dir = Origen.root.join('app', 'lib') name.each_with_index do |n, i| dir = dir.join(i == name.size - 1 ? "#{n.underscore}.rb" : n.underscore) end dir end |
#internal_depth(file) ⇒ Object
Returns the depth of the given file, where depth is the number of modules and classes it contains
276 277 278 279 280 281 282 283 284 285 |
# File 'lib/origen/code_generators/actions.rb', line 276 def internal_depth(file) depth = 0 File.readlines(file).each do |line| if line =~ /^\s*(end|def)/ return depth elsif line =~ /^\s*(module|class)/ depth += 1 end end end |
#resource_path(path) ⇒ Object
Converts a path to a resource identifier, by performing the following operations on the given path:
1) Convert any absolute paths to relative
2) Removes any leading blocks/, lib/ or application namespaces
3) Remove any derivatives directories from the path
3) Removes any trailing .rb
Examples:
/my/code/my_app/app/blocks/dut/derivatives/falcon => dut/falcon
app/lib/my_app/eagle.rb => eagle
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/origen/code_generators/actions.rb', line 326 def resource_path(path) path = Pathname.new(path)..relative_path_from(Pathname.pwd).to_s path = path.sub('.rb', '') path = path.split('/') from_block_dir_path = false path.shift if path.first == 'app' path.shift if path.first == 'lib' if path.first == 'blocks' path.shift from_block_dir_path = true end path.shift if path.first == underscored_app_namespace if path.include?('derivatives') path.delete('derivatives') from_block_dir_path = true end if from_block_dir_path path.delete('sub_blocks') path.pop if path.last == 'model' if path.last == 'controller' path.pop path << "#{path.pop}_controller" end end path.join('/') end |
#resource_path_to_blocks_dir(path) ⇒ Object
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/origen/code_generators/actions.rb', line 380 def resource_path_to_blocks_dir(path) name = resource_path(path).split('/') # Ensure this is clean, don't care about performance here dir = Origen.root.join('app', 'blocks') name.each_with_index do |n, i| if i == 0 dir = dir.join(n.underscore) else if dir.join('sub_blocks', n.underscore).exist? dir = dir.join('sub_blocks', n.underscore) else dir = dir.join('derivatives', n.underscore) end end end dir end |
#resource_path_to_class(path) ⇒ Object
406 407 408 409 410 |
# File 'lib/origen/code_generators/actions.rb', line 406 def resource_path_to_class(path) name = resource_path(path).split('/') # Ensure this is clean, don't care about performance here name.unshift(underscored_app_namespace) name.map { |n| camelcase(n) }.join('::') end |
#resource_path_to_lib_file(path) ⇒ Object
397 398 399 400 401 402 403 404 |
# File 'lib/origen/code_generators/actions.rb', line 397 def resource_path_to_lib_file(path) name = resource_path(path).split('/') # Ensure this is clean, don't care about performance here dir = Origen.root.join('app', 'lib', underscored_app_namespace) name.each_with_index do |n, i| dir = dir.join(i == name.size - 1 ? "#{n.underscore}.rb" : n.underscore) end dir end |
#unless_has_method(filepath, name) ⇒ Object
Only executes the given block if the given file does not already define the given method, where the block would normally go on to insert the method.
See the ensure_define_sub_blocks method in the sub_blocks.rb generator for a usage example.
291 292 293 294 295 |
# File 'lib/origen/code_generators/actions.rb', line 291 def unless_has_method(filepath, name) unless File.read(filepath) =~ /^\s*def #{name}(\(|\s|\n)/ yield end end |
#unless_valid_underscored_identifier(str) ⇒ Object
Executes the given block unless the given string is lower cased and underscored and doesn't start with a number of contain any special characters
299 300 301 302 303 |
# File 'lib/origen/code_generators/actions.rb', line 299 def unless_valid_underscored_identifier(str) if str =~ /[^0-9a-z_]/ || str =~ /^[0-9]/ yield end end |
#validate_resource_path(name) ⇒ Object Also known as: validate_resource_name
305 306 307 308 309 310 311 312 313 |
# File 'lib/origen/code_generators/actions.rb', line 305 def validate_resource_path(name) name.split('/').each do |n| unless_valid_underscored_identifier(n) do Origen.log.error "All parts of a resource name must be lower-cased, underscored and start with letter, '#{n}' is invalid" exit 1 end end name end |