lib/ronin/generators/generator.rb in ronin-gen-0.1.1 vs lib/ronin/generators/generator.rb in ronin-gen-0.2.0

- old
+ new

@@ -1,7 +1,6 @@ # -#-- # Ronin Gen - A Ruby library for Ronin that provides various generators. # # Copyright (c) 2009 Hal Brodigan (postmodern.mod3 at example.com) # # This program is free software; you can redistribute it and/or modify @@ -15,144 +14,150 @@ # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -#++ # +require 'ronin/templates/template' require 'ronin/static/finders' -require 'ronin/templates/erb' -require 'fileutils' +require 'extlib' +require 'erb' +require 'thor' module Ronin module Generators - class Generator + class Generator < Thor::Group + include Thor::Actions + include Templates::Template include Static::Finders - include Templates::Erb - # - # Runs the generator with the specified _path_. - # - def run(path) - path = File.expand_path(path) - @path = path - - generate! - - @path = nil - return path + def self.inherited(super_class) + class_name = super_class.name.split('::').last.snake_case + super_class.namespace("ronin #{class_name}") end - protected - # - # Default method which invokes the generator. + # Defines the default source root of the generator as the current + # working directory. # - def generate! + # @since 0.2.0 + # + def self.source_root + Dir.pwd end # - # Returns the absolute form of the specified _path_, with respect to - # the +path+ instance variable. + # Invokes the generator. # - def expand_path(sub_path) - return @path if (sub_path.nil? || sub_path == @path) - return File.expand_path(File.join(@path,sub_path)) - end - + # @param [Hash] options + # Class options to use with the generator. # - # Prints the specified _sub_path_. + # @param [Array] arguments + # Additional arguments for the generator. # - def print_path(sub_path) - full_path = expand_path(sub_path) - - sub_path = File.join('.',sub_path) - sub_path << File::SEPARATOR if File.directory?(full_path) - - puts " #{sub_path}" + # @example + # gen.generate + # + # @since 0.2.0 + # + def self.generate(options={},arguments=[]) + generator = self.new(arguments, options) + generator.invoke() end + desc "default generator task" + # - # Touches the file at the specified _sub_path_. + # Default generator method. # - # touch 'init.rb' + # @since 0.2.0 # - def touch(sub_path) - FileUtils.touch(expand_path(sub_path)) - - print_path(sub_path) - return sub_path + def generate end + protected + # - # Opens the file at the specified _sub_path_. If a _block_ is given, - # it will be passed a newly created File object. + # Touches a file. # - # file('metadata.xml') do |file| - # ... - # end + # @param [String] destination + # The relative path to the file to touch. # - def file(sub_path,&block) - File.open(expand_path(sub_path),'w',&block) - - print_path(sub_path) - return sub_path + # @example + # touch 'TODO.txt' + # + # @since 0.2.0 + # + def touch(destination) + create_file(destination) end # - # Creates a directory at the specified _sub_path_. If a _block_ is - # given, it will be passed the _sub_path_ after the directory has - # been created. + # Creates an empty directory. # - # directory 'objects' - # # => "objects" + # @param [String] destination + # The relative path of the directory to create. # - def directory(sub_path,&block) - FileUtils.mkdir_p(expand_path(sub_path)) - - print_path(sub_path) - block.call(sub_path) if block - return sub_path + # @example + # directory 'sub/dir' + # + # @since 0.2.0 + # + def mkdir(destination) + empty_directory(destination) end # - # Copies the _static_file_ to the specified _sub_path_. + # Copies a static-file. # - # copy 'ronin/platform/generators/extension.rb', 'myext/extension.rb' - # # => "myext/extension.rb" + # @param [String] static_file + # The relative path to the static file. # - def copy(static_file,sub_path) - static_file = find_static_file(static_file) - - FileUtils.cp(static_file,expand_path(sub_path)) - - print_path(sub_path) - return sub_path - end - + # @param [String] destination + # The destination to copy the static file to. # - # Renders the ERB template using the specified _static_file_. + # @example + # copy_file 'ronin/platform/generators/extension.rb', + # 'myext/extension.rb' # - # render_template 'ronin/platform/generators/Rakefile.erb' - # # => "..." + # @since 0.2.0 # - def render_template(static_file) - erb_file(find_static_file(static_file)) + def copy_file(static_file,destination) + super(find_static_file(static_file),destination) end # - # Renders the ERB template using the specified _static_file_, - # saving the result to the specified _sub_path_. + # Renders the ERB template at the specified _template_path_ and + # saves the result at the given _destination_. # - # render_file 'ronin/platform/generators/Rakefile.erb', 'Rakefile' - # # => nil + # @param [String] template_path + # The relative path to the template. # - def render_file(static_file,sub_path) - file(sub_path) do |file| - file.write(render_template(static_file)) + # @param [String, nil] destination + # The destination to write the result of the rendered template to. + # + # @return [nil, String] + # If destination is +nil+, the result of the rendered template + # will be returned. + # + # @example + # template 'ronin/platform/generators/Rakefile.erb', 'Rakefile.erb' + # + # @example + # template '_helpers.erb' + # + # @since 0.2.0 + # + def template(template_path,destination=nil) + enter_template(template_path) do |path| + if destination + super(path,destination) + else + ERB.new(File.read(path)).result(binding).chomp + end end end end end