lib/ronin/generators/platform/overlay.rb in ronin-gen-0.1.1 vs lib/ronin/generators/platform/overlay.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 gmail.com) # # This program is free software; you can redistribute it and/or modify @@ -15,11 +14,10 @@ # 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/generators/platform/static' require 'ronin/generators/dir_generator' require 'ronin/platform/overlay' @@ -37,11 +35,11 @@ # The Overlay metadata file METADATA_FILE = Ronin::Platform::Overlay::METADATA_FILE # The Overlay metadata XSL URL - METADATA_XSL = 'http://ronin.rubyforge.org/static/ronin/platform/overlay.xsl' + METADATA_XSL = File.join('ronin','platform','overlay.xsl') # The Overlay lib directory LIB_DIR = Ronin::Platform::Overlay::LIB_DIR # The Overlay objects directory @@ -49,137 +47,113 @@ # Default license to use DEFAULT_LICENSE = 'CC-by' # Default maintainer to use - DEFAULT_MAINTAINER = {:name => 'Name', :email => 'name@example.com'} + DEFAULT_MAINTAINER = {'Name' => 'name@example.com'} # Default description to use DEFAULT_DESCRIPTION = 'This is an Overlay' - # Title of the overlay - attr_accessor :title + class_option :title, :type => :string + class_option :source, :type => :string + class_option :source_view, :type => :string + class_option :website, :type => :string + class_option :license, :type => :string, :default => DEFAULT_LICENSE + class_option :description, :type => :string, :default => DEFAULT_DESCRIPTION + class_option :maintainers, :type => :hash, :default => {}, :banner => 'NAME:EMAIL ...' + class_option :gems, :type => :array, :default => [], :banner => 'GEM ...' + class_option :tasks, :type => :array, :default => [], :banner => 'TASK ...' + class_option :test_suite, :type => :string, :banner => 'test|spec' - # Source URL for the overlay - attr_accessor :source + no_tasks do + def invoke(*names,&block) + @title = options[:title] + @source = options[:source] + @source_view = options[:source_view] + @website = options[:website] + @license = options[:license] + @description = options[:description] + @maintainers = options[:maintainers] + @gems = options[:gems] + @tasks = options[:tasks] + @test_suite = options[:test_suite] - # Source View URL for the overlay - attr_accessor :source_view + @title ||= File.basename(self.path).gsub(/[_\s]+/,' ').capitalize + @source_view ||= @source + @website ||= @source_view - # Website of the overlay - attr_accessor :website + if @maintainers + @maintainers = DEFAULT_MAINTAINER + end - # License of the overlay - attr_accessor :license - - # Maintainers of the overlay - attr_reader :maintainers - - # Description of the overlay - attr_accessor :description - - # Tasks to require for the overlay - attr_reader :tasks - - # - # Creates a new Metadata object with the given _options_. - # - # _options_ may include the following keys: - # <tt>:title</tt>:: Title for the overlay. - # <tt>:source</tt>:: Source URL for the overlay. - # <tt>:source_view</tt>:: Source View URL for the overlay. - # <tt>:website</tt>:: Website for the overlay. - # <tt>:license</tt>:: License for the overlay. Defaults to - # DEFUALT_LICENSE, if not given. - # <tt>:maintainers</tt>:: List of maintainers for the overlay. - # <tt>:description</tt>:: The description of the overlay. - # Defaults to DEFAULT_DESCRIPTION, - # if not given. - # - def initialize(options={}) - @title = options[:title] - @source = options[:source] - @source_view = options[:source_view] - @website = options[:website] - @license = (options[:license] || DEFAULT_LICENSE) - @maintainers = [] - - if options[:maintainers] - @maintainers.merge!(options[:maintainers]) + super(*names,&block) end - - @description = (options[:description] || DEFAULT_DESCRIPTION) - @tasks = Set[] - - if options[:tasks] - @tasks.merge!(options[:tasks]) - end end # - # Adds a new maintainer with the specified _name_ and _email_. - # - def maintainer(name,email) - @maintainers << {:name => name, :email => email} - end - - protected - - # # Generates a skeleton Overlay. # - def generate! - @title ||= File.basename(@path).gsub(/[_\s]+/,' ').capitalize - @source_view ||= @source - @website ||= @source_view - @maintainers << DEFAULT_MAINTAINER if @maintainers.empty? + def generate + mkdir 'static' - directory LIB_DIR + inside File.join('static','ronin','platform') do + copy_file METADATA_XSL, File.basename(METADATA_XSL) + end + + mkdir LIB_DIR touch File.join(LIB_DIR,Ronin::Platform::Overlay::INIT_FILE) - directory OBJECTS_DIR - directory 'tasks' - - generate_rakefile! - generate_metadata! + mkdir OBJECTS_DIR + mkdir 'tasks' end # # Generates the Rakefile of the Overlay. # - def generate_rakefile! - file('Rakefile') do |rakefile| - rakefile << "# -*- ruby -*-\n\n" + def rakefile + case @test_suite + when 'rspec', 'spec' + @tasks << 'spec' unless @tasks.include?('spec') + end - @tasks.each do |task| - rakefile << "require 'ronin/platform/tasks/#{task}'" - end + template File.join('ronin','generators','platform','Rakefile.erb'), 'Rakefile' + end - rakefile << "\n# vim: syntax=Ruby" + # + # Generates a base test suite for the Overlay. + # + def test_suite + case @test_suite + when 'test','unit' + mkdir 'test' + when 'rspec', 'spec' + mkdir 'spec' + copy_file File.join('ronin','generators','platform','spec','spec_helper.rb'),'spec' end end # # Generates the XML metadata file for the Overlay. # - def generate_metadata! - file(METADATA_FILE) do |metadata_file| + def metadata + create_file(METADATA_FILE) do doc = XML::Document.new doc << XML::ProcessingInstruction.new( doc, 'xml-stylesheet', - "type=\"text/xsl\" href=\"#{METADATA_XSL}\"" + "type=\"text/xsl\" href=\"static/#{METADATA_XSL}\"" ) root = XML::Node.new('ronin-overlay',doc) root['version'] = Ronin::VERSION title_tag = XML::Node.new('title',doc) title_tag << XML::Text.new(@title,doc) root << title_tag - if @source + if options[:source] source_tag = XML::Node.new('source',doc) source_tag << XML::Text.new(@source,doc) root << source_tag end @@ -199,37 +173,48 @@ license_tag << XML::Text.new(@license,doc) root << license_tag maintainers_tag = XML::Node.new('maintainers',doc) - @maintainers.each do |author| - if (author[:name] || author[:email]) - maintainer_tag = XML::Node.new('maintainer',doc) + @maintainers.each do |name,email| + maintainer_tag = XML::Node.new('maintainer',doc) - if author[:name] - name_tag = XML::Node.new('name',doc) - name_tag << XML::Text.new(author[:name],doc) - maintainer_tag << name_tag - end + if name + name_tag = XML::Node.new('name',doc) + name_tag << XML::Text.new(name,doc) + maintainer_tag << name_tag + end - if author[:email] - email_tag = XML::Node.new('email',doc) - email_tag << XML::Text.new(author[:email],doc) - maintainer_tag << email_tag - end - - maintainers_tag << maintainer_tag + if email + email_tag = XML::Node.new('email',doc) + email_tag << XML::Text.new(email,doc) + maintainer_tag << email_tag end + + maintainers_tag << maintainer_tag end root << maintainers_tag + unless @gems.empty? + dependencies_tag = XML::Node.new('dependencies',doc) + dependencies_tag << XML::Text.new(gems_tag,doc) + + @gems.each do |gem_name| + gem_tag = XML::Node.new('gem',doc) + gem_tag << XML::Text.new(gem_name,doc) + dependencies_tag << gem_tag + end + + root << dependencies_tag + end + description_tag = XML::Node.new('description',doc) description_tag << XML::Text.new(@description,doc) root << description_tag doc << root - doc.write_xml_to(metadata_file) + doc.to_xml end end end end