ronin-gen 1.0.1 → 1.1.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -32,48 +32,47 @@ module Ronin
32
32
  #
33
33
  class Library < DirGenerator
34
34
 
35
- # Default version of the library
36
- DEFAULT_VERSION = '0.1.0'
37
-
38
35
  # Default license of the library
39
36
  DEFAULT_LICENSE = 'GPL-2'
40
37
 
41
- # Default author of the library
42
- DEFAULT_AUTHOR = 'Author'
43
-
44
- # Default email of the library
45
- DEFAULT_EMAIL = 'name@host.com'
46
-
47
- # Default homepage for the library
48
- DEFAULT_HOMEPAGE = 'http://ronin-ruby.github.com/'
49
-
50
38
  # Directory to store command classes in
51
39
  COMMANDS_DIR = File.join('lib',UI::CLI::Commands.namespace_root)
52
40
 
53
41
  # Directory to store generator classes in
54
42
  GENERATORS_DIR = File.join('lib',Generators.namespace_root)
55
43
 
56
- desc 'Generates a new Ronin library'
57
- class_option :name, :type => :string
58
- class_option :version, :type => :string, :default => DEFAULT_VERSION
59
- class_option :author, :type => :string, :default => DEFAULT_AUTHOR
60
- class_option :email, :type => :string, :default => DEFAULT_EMAIL
61
- class_option :homepage, :type => :string,
62
- :default => DEFAULT_HOMEPAGE
44
+ data_dir File.join('ronin','gen','library')
45
+
46
+ parameter :name, :type => String
47
+
48
+ parameter :version, :type => String,
49
+ :default => '0.1.0'
50
+
51
+ parameter :author, :type => String,
52
+ :default => 'Author'
53
+
54
+ parameter :email, :type => String,
55
+ :default => 'name@host.com'
56
+
57
+ parameter :homepage, :type => String,
58
+ :default => 'http://ronin-ruby.github.com/'
63
59
 
64
- class_option :commands, :type => :array,
65
- :default => [],
66
- :banner => 'NAME [...]'
60
+ parameter :commands, :type => Array[String],
61
+ :default => []
67
62
 
68
- class_option :generators, :type => :array,
69
- :default => [],
70
- :banner => 'NAME [...]'
63
+ parameter :generators, :type => Array[String],
64
+ :default => []
71
65
 
72
- class_option :no_git, :type => :boolean
66
+ parameter :no_git, :type => true
73
67
 
68
+ #
69
+ # Sets up the library generator.
70
+ #
74
71
  def setup
75
- @name = (options[:name] || File.basename(self.path))
76
- @dir_name = @name.gsub(/^ronin[-_]/,'')
72
+ super
73
+
74
+ @name ||= File.basename(@path)
75
+ @dir_name = @name.gsub(/^ronin[-_]/,'')
77
76
  @module_name = @dir_name.capitalize
78
77
 
79
78
  @title = @name.split(/[\s_-]+/).map { |word|
@@ -82,133 +81,100 @@ module Ronin
82
81
 
83
82
  @license = DEFAULT_LICENSE
84
83
 
85
- @author = options[:author]
86
- @email = options[:email]
87
84
  @safe_email = @email.gsub(/\s*@\s*/,' at ')
88
- @homepage = options[:homepage]
85
+
86
+ @bin_script = File.join('bin',"ronin-#{@dir_name}")
89
87
  end
90
88
 
91
89
  #
92
90
  # Generates top-level files.
93
91
  #
94
92
  def generate
95
- unless options[:no_git]
96
- inside { run "git init" }
93
+ unless no_git?
94
+ run "git init"
97
95
  end
98
96
 
99
- erb File.join('ronin','gen','library','Gemfile.erb'), 'Gemfile'
100
- cp File.join('ronin','gen','library','Rakefile'), 'Rakefile'
97
+ template 'Gemfile.erb', 'Gemfile'
98
+ cp 'Rakefile'
101
99
 
102
- erb File.join('ronin','gen','library','name.gemspec.erb'),
103
- "#{@name}.gemspec"
104
- erb File.join('ronin','gen','library','gemspec.yml.erb'),
105
- 'gemspec.yml'
100
+ template 'name.gemspec.erb', "#{@name}.gemspec"
101
+ template 'gemspec.yml.erb', 'gemspec.yml'
106
102
 
107
- unless options[:no_git]
108
- cp File.join('ronin','gen','library','.gitignore'), '.gitignore'
103
+ unless no_git?
104
+ cp '.gitignore'
109
105
  end
110
106
 
111
- cp File.join('ronin','gen','library','.rspec'), '.rspec'
112
- cp File.join('ronin','gen','library','.document'), '.document'
113
- erb File.join('ronin','gen','library','.yardopts.erb'),
114
- '.yardopts'
107
+ cp '.rspec'
108
+ cp '.document'
109
+ template '.yardopts.erb', '.yardopts'
115
110
 
116
- cp File.join('ronin','gen','library','COPYING.txt'), 'COPYING.txt'
111
+ cp 'COPYING.txt'
117
112
 
118
- erb File.join('ronin','gen','library','ChangeLog.md.erb'),
119
- 'ChangeLog.md'
120
-
121
- erb File.join('ronin','gen','library','README.md.erb'),
122
- 'README.md'
123
-
124
- mkdir 'data'
125
- end
113
+ template 'ChangeLog.md.erb', 'ChangeLog.md'
114
+ template 'README.md.erb', 'README.md'
126
115
 
127
- #
128
- # Generates the contents of the `bin` directory.
129
- #
130
- def bin
131
116
  mkdir 'bin'
132
117
 
133
- bin_script = File.join('bin',"ronin-#{@dir_name}")
118
+ template File.join('bin','ronin-name.erb'), @bin_script
119
+ chmod 0755, @bin_script
134
120
 
135
- erb File.join('ronin','gen','library','bin','ronin-name.erb'),
136
- bin_script
137
- chmod bin_script, 0755
138
- end
121
+ mkdir_p File.join('lib','ronin',@dir_name)
139
122
 
140
- #
141
- # Generates the contents of the `lib` directory.
142
- #
143
- def lib
144
- mkdir File.join('lib','ronin',@dir_name)
123
+ template File.join('lib','ronin','name.rb.erb'),
124
+ File.join('lib','ronin',"#{@dir_name}.rb")
145
125
 
146
- erb File.join('ronin','gen','library','lib','ronin','name.rb.erb'),
147
- File.join('lib','ronin',"#{@dir_name}.rb")
126
+ template File.join('lib','ronin','name','version.rb.erb'),
127
+ File.join('lib','ronin',@dir_name,'version.rb')
148
128
 
149
- erb File.join('ronin','gen','library','lib','ronin','name','version.rb.erb'),
150
- File.join('lib','ronin',@dir_name,'version.rb')
151
- end
129
+ mkdir 'data'
152
130
 
153
- #
154
- # Generates the test suite.
155
- #
156
- def test_suite
157
131
  mkdir 'spec'
158
- erb File.join('ronin','gen','library','spec','spec_helper.rb.erb'),
159
- File.join('spec','spec_helper.rb')
132
+ template File.join('spec','spec_helper.rb.erb'),
133
+ File.join('spec','spec_helper.rb')
160
134
 
161
135
  mkdir File.join('spec',@dir_name)
162
- erb File.join('ronin','gen','library','spec','name','name_spec.rb.erb'),
163
- File.join('spec',@dir_name,"#{@dir_name}_spec.rb")
136
+ template File.join('spec','name','name_spec.rb.erb'),
137
+ File.join('spec',@dir_name,"#{@dir_name}_spec.rb")
138
+
139
+ generate_commands unless @commands.empty?
140
+ generate_generators unless @generators.empty?
141
+
142
+ unless no_git?
143
+ run 'git add .'
144
+ run 'git commit -m "Initial commit."'
145
+ end
164
146
  end
165
147
 
166
148
  #
167
149
  # Generates any optional commands for the library.
168
150
  #
169
- def commands
170
- unless options[:commands].empty?
171
- mkdir COMMANDS_DIR
151
+ def generate_commands
152
+ mkdir COMMANDS_DIR
172
153
 
173
- options[:commands].each do |name|
174
- @command_file = name.downcase.gsub(/[_-]+/,'_')
175
- @command_class = @command_file.to_const_string
154
+ @commands.each do |name|
155
+ @command_file = name.downcase.gsub(/[_-]+/,'_')
156
+ @command_class = @command_file.to_const_string
176
157
 
177
- erb File.join('ronin','gen','library','bin','ronin-command.erb'),
178
- File.join('bin','ronin-' + @command_file.gsub('_','-'))
158
+ template File.join('bin','ronin-command.erb'),
159
+ File.join('bin','ronin-' + @command_file.gsub('_','-'))
179
160
 
180
- erb File.join('ronin','gen','library',COMMANDS_DIR,'command.rb.erb'),
181
- File.join(COMMANDS_DIR,"#{@command_file}.rb")
182
- end
161
+ template File.join(COMMANDS_DIR,'command.rb.erb'),
162
+ File.join(COMMANDS_DIR,"#{@command_file}.rb")
183
163
  end
184
164
  end
185
165
 
186
166
  #
187
167
  # Generates any optional generators for the library.
188
168
  #
189
- def gen
190
- unless options[:generators].empty?
191
- mkdir GENERATORS_DIR
192
-
193
- options[:generators].each do |name|
194
- @generator_file = name.downcase.gsub(/[_-]+/,'_')
195
- @generator_class = @generator_file.to_const_string
169
+ def generate_generators
170
+ mkdir GENERATORS_DIR
196
171
 
197
- erb File.join('ronin','gen','library',GENERATORS_DIR,'generator.rb.erb'),
198
- File.join(GENERATORS_DIR,"#{@generator_file}.rb")
199
- end
200
- end
201
- end
172
+ @generators.each do |name|
173
+ @generator_file = name.downcase.gsub(/[_-]+/,'_')
174
+ @generator_class = @generator_file.to_const_string
202
175
 
203
- #
204
- # Finalizes the generated library.
205
- #
206
- def finalize
207
- unless options[:no_git]
208
- inside do
209
- run('git add .')
210
- run('git commit -m "Initial commit."')
211
- end
176
+ template File.join(GENERATORS_DIR,'generator.rb.erb'),
177
+ File.join(GENERATORS_DIR,"#{@generator_file}.rb")
212
178
  end
213
179
  end
214
180
 
@@ -22,8 +22,6 @@ require 'ronin/gen/dir_generator'
22
22
  require 'ronin/repository'
23
23
  require 'ronin/version'
24
24
 
25
- require 'set'
26
-
27
25
  module Ronin
28
26
  module Gen
29
27
  module Generators
@@ -41,103 +39,66 @@ module Ronin
41
39
  # The primary script directory
42
40
  SCRIPT_DIR = Ronin::Repository::SCRIPT_DIRS.first
43
41
 
44
- # Default license to use
45
- DEFAULT_LICENSE = 'CC-by'
46
-
47
42
  # Default authors to use
48
- DEFAULT_AUTHORS = ['Anonymous']
43
+ DEFAULT_AUTHOR = 'Anonymous'
49
44
 
50
- # Default description to use
51
- DEFAULT_DESCRIPTION = 'This is a Ronin Repository'
45
+ data_dir File.join('ronin','gen','repository')
52
46
 
53
- desc 'Generates a new Ronin Repository'
54
- class_option :title, :type => :string
55
- class_option :uri, :type => :string
56
- class_option :source, :type => :string
57
- class_option :website, :type => :string
47
+ parameter :title, :type => String
48
+ parameter :uri, :type => String
49
+ parameter :source, :type => String
50
+ parameter :website, :type => String
58
51
 
59
- class_option :license, :type => :string,
60
- :default => DEFAULT_LICENSE,
61
- :banner => 'LICENSE'
52
+ parameter :license, :type => String,
53
+ :default => 'CC-by'
62
54
 
63
- class_option :description, :type => :string,
64
- :default => DEFAULT_DESCRIPTION,
65
- :banner => 'TEXT'
55
+ parameter :description, :type => String,
56
+ :default => 'This is a Ronin Repository'
66
57
 
67
- class_option :authors, :type => :array,
68
- :default => DEFAULT_AUTHORS,
69
- :banner => 'NAME [...]'
58
+ parameter :authors, :type => Array[String],
59
+ :default => []
70
60
 
71
- class_option :tests, :type => :boolean
72
- class_option :docs, :type => :boolean
61
+ parameter :tests, :type => true
62
+ parameter :docs, :type => true
73
63
 
64
+ #
65
+ # Sets up the repository generator.
66
+ #
74
67
  def setup
75
- @title = options[:title]
76
- @uri = options[:uri]
77
- @source = options[:source]
78
- @website = options[:website]
79
- @license = options[:license]
80
- @description = options[:description]
81
- @authors = options[:authors]
82
-
83
- @test_suite = options[:test]
84
- @docs = options[:docs]
85
-
86
- @title ||= File.basename(self.path).gsub(/[_\s]+/,' ').capitalize
68
+ @title ||= File.basename(@path).gsub(/[_\s]+/,' ').capitalize
87
69
  @website ||= @source
70
+
71
+ if @authors.empty?
72
+ @authors << DEFAULT_AUTHOR
73
+ end
88
74
  end
89
75
 
90
76
  #
91
77
  # Generates a skeleton repository.
92
78
  #
93
79
  def generate
80
+ template 'ronin.yml.erb', 'ronin.yml'
81
+ template 'Rakefile.erb', 'Rakefile'
82
+
94
83
  mkdir LIB_DIR
95
84
  mkdir File.join(LIB_DIR,'ronin')
96
85
  touch File.join(LIB_DIR,Ronin::Repository::INIT_FILE)
97
86
 
98
87
  mkdir SCRIPT_DIR
99
88
  mkdir Ronin::Repository::DATA_DIR
100
- end
101
89
 
102
- #
103
- # Generates the Rakefile of the repository.
104
- #
105
- def rakefile
106
- erb File.join('ronin','gen','repository','Rakefile.erb'),
107
- 'Rakefile'
108
- end
90
+ if docs?
91
+ template '.yardopts.erb', '.yardopts'
92
+ end
109
93
 
110
- #
111
- # Generates a base RSpec test-suite for the repository.
112
- #
113
- def tests
114
- if options.tests?
115
- cp File.join('ronin','gen','repository','.rspec'), '.rspec'
94
+ if tests?
95
+ cp '.rspec'
116
96
 
117
97
  mkdir 'spec'
118
- cp File.join('ronin','gen','repository','spec','spec_helper.rb'),
119
- File.join('spec','spec_helper.rb')
98
+ cp File.join('spec','spec_helper.rb')
120
99
  end
121
100
  end
122
101
 
123
- #
124
- # Generate files needed for documentation.
125
- #
126
- def docs
127
- if options.docs?
128
- erb File.join('ronin','gen','repository','.yardopts.erb'),
129
- '.yardopts'
130
- end
131
- end
132
-
133
- #
134
- # Generates the XML metadata file for the repository.
135
- #
136
- def metadata
137
- erb File.join('ronin','gen','repository','ronin.yml.erb'),
138
- 'ronin.yml'
139
- end
140
-
141
102
  end
142
103
  end
143
104
  end
@@ -26,7 +26,7 @@ module Ronin
26
26
  #
27
27
  class RubyGenerator < SourceCodeGenerator
28
28
 
29
- file_extension! :rb
29
+ file_extension :rb
30
30
 
31
31
  end
32
32
  end
@@ -22,21 +22,54 @@ require 'ronin/gen/file_generator'
22
22
  module Ronin
23
23
  module Gen
24
24
  #
25
- # A {FileGenerator} class for creating Ruby files.
25
+ # A {FileGenerator} class for creating source-code files.
26
26
  #
27
27
  class SourceCodeGenerator < FileGenerator
28
28
 
29
- class_option :editor, :default => ENV['EDITOR']
30
- class_option :no_edit, :type => :boolean, :default => false
29
+ parameter :editor, :type => String,
30
+ :default => ENV['EDITOR']
31
31
 
32
+ parameter :no_edit, :type => true,
33
+ :default => false
34
+
35
+ #
36
+ # Generates the source code file and spawns a text-editor.
32
37
  #
33
- # Generates the source-code file and spawns a text-editor.
38
+ # @since 1.1.0
34
39
  #
35
- def self.generate(options={},arguments=[],&block)
36
- generator = super(options,arguments,&block)
40
+ # @api semipublic
41
+ #
42
+ def generate
43
+ template self.class.template, @path
44
+
45
+ if (no_edit? && editor?)
46
+ # spawn the text editor for the newly generated file
47
+ system(editor,@path)
48
+ end
49
+ end
50
+
51
+ protected
37
52
 
38
- if (generator.options.no_edit? && generator.options.editor)
39
- system(generator.options.editor,generator.path)
53
+ #
54
+ # The template the Source Code Generator will use.
55
+ #
56
+ # @param [String] name
57
+ # The new template name.
58
+ #
59
+ # @return [String]
60
+ # The template the Source Code Generator will use.
61
+ #
62
+ # @since 1.1.0
63
+ #
64
+ # @api semipublic
65
+ #
66
+ def self.template(name=nil)
67
+ if name
68
+ @template = name
69
+ else
70
+ @template ||= if superclass < SourceCodeGenerator
71
+ superclass.template
72
+ end
40
73
  end
41
74
  end
42
75