Class: Erlgen::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/erlgen/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Generator

Returns a new instance of Generator.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/erlgen/generator.rb', line 10

def initialize(*args)
  @options = {}
  @opts = OptionParser.new do |o|
    o.banner = "Usage: #{File.basename($0)} [options] appname\ne.g. #{File.basename($0)} superapp"
    o.on('--with-git', 'initialize git repository') do
      @options[:with_git] = true
    end

    o.on('--gen_server', 'create a skeleton gen_server module') do
      @options[:file_only] = true
      @options[:gen] = "gen_server"
    end

    o.on('--gen_fsm', 'create a skeleton gen_fsm module') do
      @options[:file_only] = true
      @options[:gen] = "gen_fsm"
    end

    o.on('--gen_event', 'create a skeleton gen_event module') do
      @options[:file_only] = true
      @options[:gen] = "gen_event"
    end


    o.on_tail('-h', '--help', 'display this help and exit') do
      @options[:show_help] = true
    end
  end
  @opts.parse!(args)
  @project_name = args.shift
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/erlgen/generator.rb', line 9

def options
  @options
end

#project_nameObject

Returns the value of attribute project_name.



9
10
11
# File 'lib/erlgen/generator.rb', line 9

def project_name
  @project_name
end

Instance Method Details

#create_filesObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/erlgen/generator.rb', line 64

def create_files
  unless File.exists?(target_dir) || File.directory?(target_dir)
    FileUtils.mkdir target_dir
  else
    raise "The directory #{target_dir} already exists, aborting. Maybe move it out of the way before continuing?"
  end
  mkdir_in_target 'src'
  mkdir_in_target 'ebin'
  mkdir_in_target 'priv'
  mkdir_in_target 'include'
  output_template_in_target 'application.app', File.join('ebin', "#{@project_name}.app")
  output_template_in_target 'application.erl', File.join('src', "#{@project_name}.erl")
  output_template_in_target 'application_sup.erl', File.join('src', "#{@project_name}_sup.erl") 
  output_template_in_target 'Rakefile'
  touch_in_target File.join('include', "#{@project_name}.hrl")
end

#create_genObject



81
82
83
# File 'lib/erlgen/generator.rb', line 81

def create_gen
   output_template_in_target "#{@options[:gen]}.erl", "#{@project_name}.erl"
end

#create_version_controlObject



122
123
124
125
126
127
128
129
130
# File 'lib/erlgen/generator.rb', line 122

def create_version_control
  Dir.chdir(target_dir) do
    begin
      @repo = Git.init()
    rescue Git::GitExecuteError => e
      raise GitInitFailed, "Encountered an error during gitification. Maybe the repo already exists, or has already been pushed to?"
    end
  end
end

#mkdir_in_target(directory) ⇒ Object



89
90
91
92
93
# File 'lib/erlgen/generator.rb', line 89

def mkdir_in_target(directory)
  final_destination = File.join(target_dir, directory)
  FileUtils.mkdir final_destination
  $stdout.puts "\tcreate\t#{directory}"
end

#output_template_in_target(source, destination = source) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/erlgen/generator.rb', line 103

def output_template_in_target(source, destination = source)
  final_destination = File.join(target_dir, destination)
  template_result   = render_template(source)

  File.open(final_destination, 'w') {|file| file.write(template_result)}

  $stdout.puts "\tcreate\t#{destination}"
end

#render_template(source) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/erlgen/generator.rb', line 95

def render_template(source)
  template_contents = File.read(File.join(template_dir, source))
  template          = ERB.new(template_contents, nil, '<>')

  # squish extraneous whitespace from some of the conditionals
  template.result(binding).gsub(/\n\n\n+/, "\n\n")
end

#runObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/erlgen/generator.rb', line 42

def run
  if @options[:show_help]
    $stderr.puts @opts
    return 1
  end

  if @project_name.nil? || @project_name.squeeze.strip == ""
    $stderr.puts @opts
    return 1
  end
  
  if @options[:file_only]
    create_gen
  else
    create_files
  end

  if @options[:with_git]
    create_version_control
  end
end

#target_dirObject



85
86
87
# File 'lib/erlgen/generator.rb', line 85

def target_dir
  @options[:file_only] ? (Dir['*'].include?('src') ? 'src' : Dir.pwd) : @project_name
end

#template_dirObject



118
119
120
# File 'lib/erlgen/generator.rb', line 118

def template_dir
  File.join(File.dirname(__FILE__), 'templates')
end

#touch_in_target(destination) ⇒ Object



112
113
114
115
116
# File 'lib/erlgen/generator.rb', line 112

def touch_in_target(destination)
  final_destination = File.join(target_dir, destination)
  FileUtils.touch  final_destination
  $stdout.puts "\tcreate\t#{destination}"
end