module Origen::CodeGenerators::Actions::Helpers
Should probably move to its own file, these are general helpers rather than actions
Public Instance Methods
Source
# 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
Adds :class and :module identifiers to an array of namespaces
["my_app", "models", "bist"] => [[:module, "my_app"], [:module, "models"], [:class, "bist"]]
Source
# 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
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.
Source
# 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
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.
Source
# 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
Returns the depth of the given file, where depth is the number of modules and classes it contains
Source
# File lib/origen/code_generators/actions.rb, line 326 def resource_path(path) path = Pathname.new(path).expand_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
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
Source
# 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
Source
# 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
Source
# 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
Source
# 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
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.
Source
# 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
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
Source
# 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