Sha256: 2e0ff3740821df0c66faf390065476aaca548ea93cf2bc2e0b0976b88e33146f

Contents?: true

Size: 1.81 KB

Versions: 40

Compression:

Stored size: 1.81 KB

Contents

require 'fileutils'
require 'find'

class VulcanizeGenerator < Rails::Generator::NamedBase
  
  TEMPLATE_ROOT = File.dirname(__FILE__) + "/templates"
  TEMPLATE_FILE = "templates.yml"
    
  def manifest
    record do |m|
      templates = [file_name] + actions
      templates.each do |t|
        apply_template(m, t)
      end
    end
  end

  def apply_template(m, name)
    sp = source_path("#{name}/")
    unless File.directory?(sp)
      raise Rails::Generator::UsageError.new("Invalid template #{name}, use one of #{valid_templates.join(', ')}")
    end
    
    templ_conf = load_template_config(sp)
    deps = templ_conf['dependent_templates'] || []
    deps.each do |dep|
      apply_template(m, dep)
    end
    
    Find.find(sp) do |f|
      Find.prune if File.basename(f) =~ /^(CVS|\.svn)$/
      Find.prune if f == "#{sp}#{TEMPLATE_FILE}"
      
      rel = f.gsub(/#{source_root}\//, '')
      dest_rel = rel.gsub(/^#{name}\//, '')
      m.directory(dest_rel) if File.directory?(f)
      if File.file?(f)
        # force scripts to be executable
        opts = (File.read(f) =~ /^#!/) ? {:chmod => 0755} : {}
        m.file(rel, dest_rel, opts)
      end

    end
  end

  protected
    def valid_templates
      valid = Dir.entries(TEMPLATE_ROOT).delete_if {|e| e =~ /(^\.)|svn|CVS/ }
    end
    
    def load_template_config(template_dir)
      templ_file = "#{template_dir}/templates.yml"
      templ_conf = YAML.load(File.read(templ_file)) rescue {}
      return templ_conf
    end

    def banner
      usage = "Usage: #{$0} vulcanize template_name ...\n"
      usage << "where template_name is one of:\n\n"
      valid_templates.each do |t|
        templ_conf = load_template_config("#{TEMPLATE_ROOT}/#{t}")
        desc = templ_conf['description']
        usage << "    #{t}: #{desc}\n" 
      end
      return usage
    end
end

Version data entries

40 entries across 40 versions & 5 rubygems

Version Path
sml-rubber-0.9.1 generators/vulcanize/vulcanize_generator.rb
sml-rubber-0.9.10 generators/vulcanize/vulcanize_generator.rb
sml-rubber-0.9.11 generators/vulcanize/vulcanize_generator.rb
sml-rubber-0.9.13 generators/vulcanize/vulcanize_generator.rb
sml-rubber-0.9.3 generators/vulcanize/vulcanize_generator.rb
sml-rubber-0.9.4 generators/vulcanize/vulcanize_generator.rb
sml-rubber-0.9.5 generators/vulcanize/vulcanize_generator.rb
sml-rubber-0.9.6 generators/vulcanize/vulcanize_generator.rb
sml-rubber-0.9.7 generators/vulcanize/vulcanize_generator.rb
sml-rubber-0.9.8 generators/vulcanize/vulcanize_generator.rb
sml-rubber-0.9.9 generators/vulcanize/vulcanize_generator.rb
wr0ngway-rubber-1.0.0 generators/vulcanize/vulcanize_generator.rb
wr0ngway-rubber-1.0.1 generators/vulcanize/vulcanize_generator.rb
axtro-rubber-1.2.3 generators/vulcanize/vulcanize_generator.rb
axtro-rubber-1.2.2 generators/vulcanize/vulcanize_generator.rb
rubber-1.3.2 generators/vulcanize/vulcanize_generator.rb
rubber-1.3.1 generators/vulcanize/vulcanize_generator.rb
rubber-1.3.0 generators/vulcanize/vulcanize_generator.rb
rubber-1.2.1 generators/vulcanize/vulcanize_generator.rb
axtro-rubber-1.2.1 generators/vulcanize/vulcanize_generator.rb