ronin-gen 1.1.1 → 1.2.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -28,34 +28,48 @@ module Ronin
28
28
  #
29
29
  class DirGenerator < Generator
30
30
 
31
- # The destination path for the generator
32
- parameter :path, :type => String,
33
- :description => 'The destination path'
31
+ # The directory to generate
32
+ attr_accessor :path
34
33
 
35
34
  #
36
- # Invokes the new Dir Generator.
35
+ # Initializes the directory generator.
37
36
  #
38
37
  # @param [String] path
39
- # The path for the generator.
38
+ # The path to the directory to be generated.
40
39
  #
41
40
  # @param [Hash{Symbol => Object}] options
42
41
  # Additional options for the generator.
43
42
  #
44
- # @api public
43
+ # @yield [generator]
44
+ # The given block will be passed the newly created generator.
45
+ #
46
+ # @yieldparam [DirGenerator]
47
+ # The newly created generator.
48
+ #
49
+ # @api semipublic
50
+ #
51
+ # @since 1.2.0
45
52
  #
46
- def self.generate(path,options={})
47
- super(options.merge(:path => path))
53
+ def initialize(path=nil,options={},&block)
54
+ @path = path
55
+
56
+ super(options,&block)
48
57
  end
49
58
 
50
59
  #
51
60
  # Creates the directory and invokes the generator.
52
61
  #
53
- # @api semipublic
62
+ # @raise [RuntimeError]
63
+ # {#path} was not set.
64
+ #
65
+ # @api public
54
66
  #
55
67
  # @since 1.1.0
56
68
  #
57
69
  def generate!
58
- require_params :path
70
+ unless @path
71
+ raise("#{self.class}#path was not set")
72
+ end
59
73
 
60
74
  FileUtils.mkdir_p(@path)
61
75
  FileUtils.chdir(@path) { super }
@@ -26,23 +26,50 @@ module Ronin
26
26
  #
27
27
  class FileGenerator < Generator
28
28
 
29
- # The destination path for the generator
30
- parameter :path, :type => String,
31
- :description => 'File to generate'
29
+ # The file to generate
30
+ attr_accessor :path
32
31
 
33
32
  #
34
- # Invokes the new File Generator.
33
+ # Initializes the file generator.
35
34
  #
36
35
  # @param [String] path
37
- # The path for the generator.
36
+ # The path to the file to be generated.
38
37
  #
39
38
  # @param [Hash{Symbol => Object}] options
40
39
  # Additional options for the generator.
41
40
  #
41
+ # @yield [generator]
42
+ # The given block will be passed the newly created generator.
43
+ #
44
+ # @yieldparam [FileGenerator]
45
+ # The newly created generator.
46
+ #
47
+ # @api semipublic
48
+ #
49
+ # @since 1.2.0
50
+ #
51
+ def initialize(path=nil,options={},&block)
52
+ @path = path
53
+
54
+ super(options,&block)
55
+ end
56
+
57
+ #
58
+ # Sets up the generator and calls {#generate}.
59
+ #
60
+ # @raise [RuntimeError]
61
+ # {#path} was not set.
62
+ #
63
+ # @since 1.2.0
64
+ #
42
65
  # @api public
43
66
  #
44
- def self.generate(path,options={})
45
- super(options.merge(:path => path))
67
+ def generate!
68
+ unless @path
69
+ raise("#{self.class}#path was not set")
70
+ end
71
+
72
+ super
46
73
  end
47
74
 
48
75
  #
@@ -53,8 +80,6 @@ module Ronin
53
80
  # @api semipublic
54
81
  #
55
82
  def setup
56
- require_params :path
57
-
58
83
  if (self.class.file_extension && File.extname(@path).empty?)
59
84
  @path += ".#{self.class.file_extension}"
60
85
  end
@@ -73,7 +73,7 @@ module Ronin
73
73
  if @generators.empty?
74
74
  directory = File.join('lib',Generators.namespace_root)
75
75
 
76
- Installation.each_file_in(directory,:rb) do |path|
76
+ Installation.each_file_in(directory,'rb') do |path|
77
77
  # strip the tailing '.rb' file extension
78
78
  name = path.chomp('.rb')
79
79
 
@@ -18,13 +18,11 @@
18
18
  #
19
19
 
20
20
  require 'ronin/gen/config'
21
+ require 'ronin/gen/actions'
21
22
  require 'ronin/support/inflector'
22
- require 'ronin/templates/erb'
23
- require 'ronin/ui/output'
24
23
 
25
24
  require 'parameters'
26
25
  require 'data_paths/finders'
27
- require 'fileutils'
28
26
 
29
27
  module Ronin
30
28
  module Gen
@@ -93,15 +91,11 @@ module Ronin
93
91
 
94
92
  include Parameters
95
93
  include DataPaths::Finders
96
- include FileUtils
97
- include Templates::Erb
94
+ include Actions
98
95
 
99
96
  #
100
97
  # Initializes the generator.
101
98
  #
102
- # @param [String] path
103
- # The destination path for the generator.
104
- #
105
99
  # @param [Hash{Symbol => Object}] options
106
100
  # The options for the generator.
107
101
  #
@@ -134,43 +128,16 @@ module Ronin
134
128
  # @api semipublic
135
129
  #
136
130
  def self.generator_name
137
- class_name = self.name.sub('Ronin::Gen::Generators::','')
138
-
139
- return Support::Inflector.underscore(class_name.gsub('::',':'))
140
- end
141
-
142
- #
143
- # The default data directory of the generator.
144
- #
145
- # @param [String] new_dir
146
- # The new data directory.
147
- #
148
- # @return [String, nil]
149
- # The data directory that the generator will search for source files
150
- # within.
151
- #
152
- # @since 1.1.0
153
- #
154
- # @api semipublic
155
- #
156
- def self.data_dir(new_dir=nil)
157
- if new_dir
158
- @data_dir = new_dir
159
- else
160
- @data_dir ||= if superclass < Generator
161
- superclass.data_dir
162
- end
163
- end
131
+ @generator_name ||= Support::Inflector.underscore(
132
+ self.name.sub('Ronin::Gen::Generators::','').gsub('::',':')
133
+ )
164
134
  end
165
135
 
166
136
  #
167
137
  # Invokes the generator.
168
138
  #
169
- # @param [Hash] options
170
- # Class options to use with the generator.
171
- #
172
139
  # @param [Array] arguments
173
- # Additional arguments for the generator.
140
+ # Arguments for {#initialize}.
174
141
  #
175
142
  # @yield [generator]
176
143
  # The given block will be passed the new generator.
@@ -188,8 +155,8 @@ module Ronin
188
155
  #
189
156
  # @api public
190
157
  #
191
- def self.generate(options={},&block)
192
- generator = new(options,&block)
158
+ def self.generate(*arguments,&block)
159
+ generator = new(*arguments,&block)
193
160
 
194
161
  generator.generate!
195
162
  return generator
@@ -234,473 +201,29 @@ module Ronin
234
201
  protected
235
202
 
236
203
  #
237
- # Runs a command.
238
- #
239
- # @param [String] command
240
- # The command or program to run.
241
- #
242
- # @param [Array<String>] arguments
243
- # Additional arguments to run the program with.
244
- #
245
- # @since 1.1.0
246
- #
247
- # @api semipublic
248
- #
249
- def run(command,*arguments)
250
- print_action command, *arguments
251
-
252
- system(command,*arguments)
253
- end
254
-
255
- #
256
- # Changes the permissions of a files or directories.
257
- #
258
- # @param [String, Integer] mode
259
- # The new permissions for the files or directories.
260
- #
261
- # @param [Array<String>] paths
262
- # The path to the files or directories.
263
- #
264
- # @since 1.1.0
265
- #
266
- # @api semipublic
267
- #
268
- # @see http://rubydoc.info/stdlib/fileutils/FileUtils#chmod-instance_method
269
- #
270
- def chmod(mode,paths)
271
- print_action 'chmod', mode.to_s(8), *paths
272
-
273
- super(mode,paths)
274
- end
275
-
276
- #
277
- # Changes the permissions of files/directories, recursively.
278
- #
279
- # @param [String, Integer] mode
280
- # The new permissions for the files or directories.
281
- #
282
- # @param [Array<String>] paths
283
- # The path to the files or directories.
284
- #
285
- # @since 1.1.0
286
- #
287
- # @api semipublic
288
- #
289
- # @see http://rubydoc.info/stdlib/fileutils/FileUtils#chmod_R-instance_method
290
- #
291
- def chmod_R(mode,paths)
292
- print_action 'chmod -R', mode.to_s(8)
293
-
294
- super(mode,paths)
295
- end
296
-
297
- #
298
- # Changes ownership of files or directories.
299
- #
300
- # @param [String, nil] user
301
- # The new owner of the files or directories.
302
- #
303
- # @param [String, nil] group
304
- # The new group for the files or directories.
305
- #
306
- # @param [Array<String>] paths
307
- # The path to the files or directories.
308
- #
309
- # @since 1.1.0
310
- #
311
- # @api semipublic
312
- #
313
- # @see http://rubydoc.info/stdlib/fileutils/FileUtils#chown-instance_method
314
- #
315
- def chown(user,group,paths)
316
- print_action 'chown', "#{user}:#{group}", *paths
317
-
318
- super(user,group,paths)
319
- end
320
-
321
- #
322
- # Changes ownership of files/directories, recursively.
323
- #
324
- # @param [String, nil] user
325
- # The new owner of the files or directories.
326
- #
327
- # @param [String, nil] group
328
- # The new group for the files or directories.
329
- #
330
- # @param [Array<String>] paths
331
- # The path to the files or directories.
332
- #
333
- # @since 1.1.0
334
- #
335
- # @api semipublic
336
- #
337
- # @see http://rubydoc.info/stdlib/fileutils/FileUtils#chown_R-instance_method
338
- #
339
- def chown_R(user,group,paths)
340
- print_action 'chown -R', "#{user}:#{group}", *paths
341
-
342
- super(user,group,paths)
343
- end
344
-
345
- #
346
- # Copies a data file.
347
- #
348
- # @param [String] file
349
- # The relative path to the data file.
350
- #
351
- # @param [String] destination
352
- # The destination to copy the data file to.
353
- #
354
- # @since 0.2.0
355
- #
356
- # @see http://rubydoc.info/stdlib/fileutils/FileUtils#cp-instance_method
357
- #
358
- def cp(file,destination=file)
359
- print_action 'cp', destination
360
-
361
- super(data_file(file),destination)
362
- end
363
-
364
- #
365
- # Copies the contents of all data directories.
366
- #
367
- # @param [String] directory
368
- # The data directories to copy from.
369
- #
370
- # @param [String, nil] destination
371
- # The optional destination directory to copy the files to.
372
- #
373
- # @param [Hash] config
374
- # The optional configuration information.
375
- #
376
- # @option config [Boolean] :recursive (false)
377
- # Recursively copies the contents.
378
- #
379
- # @since 1.0.0
380
- #
381
- # @see http://rubydoc.info/stdlib/fileutils/FileUtils#cp_r-instance_method
382
- #
383
- def cp_r(directory,destination=directory)
384
- print_action 'cp -r', destination
385
-
386
- data_dirs(directory) do |dir|
387
- super(dir,destination)
388
- end
389
- end
390
-
391
- #
392
- # Installs a file.
393
- #
394
- # @param [String] src
395
- # The file to install.
396
- #
397
- # @param [String] dest
398
- # The destination path for the file.
399
- #
400
- # @param [Hash] options
401
- # Additional options.
402
- #
403
- # @option options [String, Integer] :mode
404
- # The permissions of the installed file.
405
- #
406
- # @since 1.1.0
407
- #
408
- # @api semipublic
409
- #
410
- # @see http://rubydoc.info/stdlib/fileutils/FileUtils#install-instance_method
411
- #
412
- def install(src,dest,options={})
413
- options = {:mode => options[:mode]} # only pass in :mode
414
-
415
- print_action 'install', src, dest
416
-
417
- super(data_file(src),dest,options)
418
- end
419
-
420
- #
421
- # Creates a hard link.
422
- #
423
- # @param [String] src
424
- # The path file/directory for the hard link.
425
- #
426
- # @param [String] dest
427
- # The destination file/directory of the hard link.
428
- #
429
- # @since 1.1.0
430
- #
431
- # @api semipublic
432
- #
433
- # @see http://rubydoc.info/stdlib/fileutils/FileUtils#ln-instance_method
434
- #
435
- def ln(src,dest)
436
- print_action 'ln', src, dest
437
-
438
- super(src,dest)
439
- end
440
-
441
- #
442
- # Creates a symbolic link.
443
- #
444
- # @param [String] src
445
- # The path file/directory for the symbolic link.
446
- #
447
- # @param [String] dest
448
- # The destination file/directory of the symbolic link.
449
- #
450
- # @since 1.1.0
451
- #
452
- # @api semipublic
453
- #
454
- # @see http://rubydoc.info/stdlib/fileutils/FileUtils#ln_s-instance_method
455
- #
456
- def ln_s(src,dest)
457
- print_action 'ln -s', src, dest
458
-
459
- super(src,dest)
460
- end
461
-
462
- #
463
- # Forcibly creates a symbolic link.
464
- #
465
- # @param [String] src
466
- # The path file/directory for the symbolic link.
467
- #
468
- # @param [String] dest
469
- # The destination file/directory of the symbolic link.
470
- #
471
- # @since 1.1.0
472
- #
473
- # @api semipublic
474
- #
475
- # @see http://rubydoc.info/stdlib/fileutils/FileUtils#ln_sf-instance_method
476
- #
477
- def ln_sf(src,dest)
478
- print_action 'ln -sf', src, dest
479
-
480
- super(src,dest)
481
- end
482
-
483
- #
484
- # Creates an empty directory.
485
- #
486
- # @param [String] dir
487
- # The relative path of the directory to create.
488
- #
489
- # @example
490
- # mkdir 'sub/dir'
491
- #
492
- # @since 0.2.0
493
- #
494
- # @see http://rubydoc.info/stdlib/fileutils/FileUtils#mkdir-instance_method
495
- #
496
- def mkdir(dir)
497
- print_action 'mkdir', dir
498
-
499
- super(dir)
500
- end
501
-
502
- #
503
- # Creates an empty directory.
504
- #
505
- # @param [String] dir
506
- # The relative path of the directory to create.
507
- #
508
- # @example
509
- # mkdir 'sub/dir'
510
- #
511
- # @since 0.2.0
512
- #
513
- # @see http://rubydoc.info/stdlib/fileutils/FileUtils#mkdir_p-instance_method
514
- #
515
- def mkdir_p(dir)
516
- print_action 'mkdir -p', dir
517
-
518
- super(dir)
519
- end
520
-
521
- #
522
- # Moves a file or directory.
523
- #
524
- # @param [String] src
525
- # The path to the file or directory.
526
- #
527
- # @param [String] dest
528
- # The new path to move the file or directory to.
529
- #
530
- # @api semipublic
531
- #
532
- # @since 1.1.0
533
- #
534
- # @see http://rubydoc.info/stdlib/fileutils/FileUtils#mv-instance_method
535
- #
536
- def mv(src,dest)
537
- print_action 'mv', src, dest
538
-
539
- super(src,dest)
540
- end
541
-
542
- #
543
- # Removes one or more files.
544
- #
545
- # @param [Array<String>] paths
546
- # The paths of the files and directories to remove.
547
- #
548
- # @param [Hash] options
549
- # Additional options.
550
- #
551
- # @option options [Boolean] :force
552
- # Specifies whether to forcible remove the files.
553
- #
554
- # @since 1.1.0
555
- #
556
- # @api semipublic
557
- #
558
- # @see http://rubydoc.info/stdlib/fileutils/FileUtils#rm-instance_method
559
- #
560
- def rm(paths,options={})
561
- options = {:force => options[:force]} # only pass in :force
562
-
563
- print_action 'rm', *paths
564
-
565
- super(paths,options)
566
- end
567
-
568
- #
569
- # Recursively removes files and directories.
570
- #
571
- # @param [Array<String>] paths
572
- # The paths of the files and directories to remove.
573
- #
574
- # @param [Hash] options
575
- # Additional options.
576
- #
577
- # @option options [Boolean] :force
578
- # Specifies whether to forcible remove the files.
579
- #
580
- # @since 1.1.0
581
- #
582
- # @api semipublic
583
- #
584
- # @see http://rubydoc.info/stdlib/fileutils/FileUtils#rm_r-instance_method
585
- #
586
- def rm_r(paths,options={})
587
- options = {:force => options[:force]} # only pass in :force
588
-
589
- print_action 'rm -r', *paths
590
-
591
- super(paths,options)
592
- end
593
-
594
- #
595
- # Forcibly removes files and directories, recursively.
596
- #
597
- # @param [Array<String>] paths
598
- # The paths of the files and directories to remove.
599
- #
600
- # @since 1.1.0
601
- #
602
- # @api semipublic
603
- #
604
- # @see http://rubydoc.info/stdlib/fileutils/FileUtils#rm_rf-instance_method
605
- #
606
- def rm_rf(paths)
607
- print_action 'rm -rf', *paths
608
-
609
- super(paths)
610
- end
611
-
612
- #
613
- # Removes one or more directories.
614
- #
615
- # @param [Array<String>] dirs
616
- # The paths of the directories.
617
- #
618
- # @since 1.1.0
619
- #
620
- # @api semipublic
621
- #
622
- # @see http://rubydoc.info/stdlib/fileutils/FileUtils#rmdir-instance_method
623
- #
624
- def rmdir(dirs)
625
- print_action 'rmdir', *dirs
626
-
627
- super(dirs)
628
- end
629
-
630
- #
631
- # Touches a file.
632
- #
633
- # @param [String] destination
634
- # The relative path to the file to touch.
635
- #
636
- # @example
637
- # touch 'TODO.txt'
638
- #
639
- # @since 0.2.0
640
- #
641
- # @see http://rubydoc.info/stdlib/fileutils/FileUtils#touch-instance_method
642
- #
643
- def touch(file)
644
- print_action 'touch', file
645
-
646
- return super(file)
647
- end
648
-
649
- #
650
- # Opens a file for writing.
651
- #
652
- # @param [String] path
653
- # The path of the file to write to.
204
+ # The default data directory of the generator.
654
205
  #
655
- # @yield [file]
656
- # The given block will be passed the newly opened file.
206
+ # @param [String] new_dir
207
+ # The new data directory.
657
208
  #
658
- # @yieldparam [File]
659
- # The new file file, opened for writing.
209
+ # @return [String, nil]
210
+ # The data directory that the generator will search for source files
211
+ # within.
660
212
  #
661
213
  # @since 1.1.0
662
214
  #
663
215
  # @api semipublic
664
216
  #
665
- def write(path,&block)
666
- File.open(path,'wb',&block)
667
- end
668
-
669
- #
670
- # Renders the ERB template and saves the result.
671
- #
672
- # @param [String] template_path
673
- # The relative path to the template.
674
- #
675
- # @param [String, nil] destination
676
- # The destination to write the result of the rendered template to.
677
- #
678
- # @return [nil, String]
679
- # If destination is `nil`, the result of the rendered template
680
- # will be returned.
681
- #
682
- # @example
683
- # template 'Rakefile.erb', 'Rakefile'
684
- #
685
- # @example
686
- # template '_helpers.erb'
687
- #
688
- # @since 0.2.0
689
- #
690
- def template(template_path,dest=nil)
691
- if dest
692
- print_action 'erb', dest
693
-
694
- File.open(dest,'w') do |file|
695
- file.write(erb_file(template_path))
696
- end
217
+ def self.data_dir(new_dir=nil)
218
+ if new_dir
219
+ @data_dir = new_dir
697
220
  else
698
- erb_file(template_path).chomp
221
+ @data_dir ||= if superclass < Generator
222
+ superclass.data_dir
223
+ end
699
224
  end
700
225
  end
701
226
 
702
- private
703
-
704
227
  #
705
228
  # Joins the path with the Generators {data_dir}.
706
229
  #
@@ -790,40 +313,6 @@ module Ronin
790
313
  each_data_dir(data_path(directory),&block)
791
314
  end
792
315
 
793
- # ANSI Bold code
794
- BOLD = "\e[1m"
795
-
796
- # ANSI Green code
797
- GREEN = "\e[32m"
798
-
799
- # ANSI Clear code
800
- CLEAR = "\e[0m"
801
-
802
- #
803
- # Prints a file action.
804
- #
805
- # @param [String] command
806
- # The command/options that represents the file action.
807
- #
808
- # @param [Array<String>] arguments
809
- # Additional arguments related to the file action.
810
- #
811
- # @since 1.1.0
812
- #
813
- # @api private
814
- #
815
- def print_action(command,*arguments)
816
- unless UI::Output.silent?
817
- arguments = arguments.join(' ')
818
-
819
- if $stdout.tty?
820
- command = BOLD + GREEN + command + CLEAR
821
- end
822
-
823
- puts "\t#{command}\t#{arguments}"
824
- end
825
- end
826
-
827
316
  end
828
317
  end
829
318
  end