Class: Origen::CodeGenerators::Mod

Inherits:
Base
  • Object
show all
Defined in:
lib/origen/code_generators/module.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

inherited, name, namespace

Methods included from Actions

#add_acronyms, #add_autoload, #add_config, #add_source, #camelcase, #comment_config, #config, #environment, #gem, #gem_group, #generate, #git, #initialize, #lib, #rakefile, #readme, #underscored_app_namespace

Methods included from Actions::Helpers

#add_type_to_namespaces, #class_name_to_blocks_dir, #class_name_to_lib_file, #internal_depth, #resource_path, #resource_path_to_blocks_dir, #resource_path_to_class, #resource_path_to_lib_file, #unless_has_method, #unless_valid_underscored_identifier, #validate_resource_path

Class Method Details



4
5
6
# File 'lib/origen/code_generators/module.rb', line 4

def self.banner
  'origen new module NAME [CLASS]'
end

Instance Method Details

#create_module_fileObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/origen/code_generators/module.rb', line 48

def create_module_file
  if @class_file
    @namespaces = resource_path_to_class(@class_file).split('::').map(&:underscore)
    paths = resource_path_to_class(@resource_path).split('::').map(&:underscore)
    @name = paths.pop
    paths.shift  # Lose the app namespace
    @namespaces += paths
    file = File.join(@class_file.sub('.rb', ''), "#{@name}.rb")
    @module_name = (@namespaces + [@name]).map { |n| camelcase(n) }.join('::')
  else
    @module_name = resource_path_to_class(@resource_path)
    @namespaces = @module_name.split('::').map(&:underscore)
    @name = @namespaces.pop
    file = class_name_to_lib_file(@module_name)
  end
  @namespaces = add_type_to_namespaces(@namespaces)
  template 'templates/code_generators/module.rb', file
end

#include_moduleObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/origen/code_generators/module.rb', line 67

def include_module
  if @class_file
    klass = resource_path_to_class(@class_file)

    # Does file have a nested namespace structure
    snippet = File.foreach(@class_file).first(50)
    if snippet.any? { |line| line =~ /\s*class #{klass.split('::').last}/ }
      indent = '  ' * klass.split('::').size
      lines = []
      lines << indent + "include #{@module_name}"
      lines << ''
      inject_into_class @class_file, klass.split('::').last, lines.join("\n") + "\n"

    # Else assume it is the compact style (class MyApp::DUT::Falcon)
    else
      lines = []
      lines << "  include #{@module_name}"
      lines << ''
      inject_into_class @class_file, klass, lines.join("\n") + "\n"
    end
  end
  add_acronyms
end

#validate_argsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/origen/code_generators/module.rb', line 29

def validate_args
  if args.size > 2 || args.size == 0
    msg = args.size == 0 ? 'At least one argument is' : 'No more than two arguments are'
    msg << " expected by the module generator, e.g. 'origen new module helpers', 'origen new module helpers app/lib/my_app/my_class.rb'"
    puts msg
    exit 1
  end

  if args.size == 2
    @class_file = args.first
    unless File.exist?(@class_file)
      puts "This class file does not exist: #{@class_file}"
      exit 1
    end
  end

  @resource_path = validate_resource_path(args.last)
end