Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 7f01e84e5d
Fetching contributors…

Cannot retrieve contributors at this time

file 61 lines (51 sloc) 2.507 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
module WebAppTheme
  class ThemeGenerator < Rails::Generators::Base
    source_root File.expand_path('../templates', __FILE__)
    
    argument :layout_name, :type => :string, :default => 'application'
    
    class_option :theme, :type => :string, :default => :default, :desc => 'Specify the layout theme'
    class_option :app_name, :type => :string, :default => 'Web App', :desc => 'Specify the application name'
    class_option :engine, :type => :string, :default => 'erb', :desc => 'Specify the template engine'
    class_option :no_layout, :type => :boolean, :default => false, :desc => 'Use this option if you want to generate only stylesheets'
    class_option :layout_type, :type => :string, :default => 'admin', :desc => 'Layout type, admin or sign'
    
    def copy_layout
      return if options.no_layout
      admin_layout_name = options.layout_type == 'sign' ? "layout_sign.html.erb" : "layout_admin.html.erb"
      case options.engine
      when 'erb'
        template admin_layout_name, "app/views/layouts/#{layout_name.underscore}.html.erb"
      when 'haml'
        generate_haml_layout(admin_layout_name)
      end
    end

    def copy_base_stylesheets
      copy_file "#{stylesheets_path}/base.css", "app/assets/stylesheets/web-app-theme/base.css"
      copy_file "#{stylesheets_path}/override.css", "app/assets/stylesheets/web-app-theme/override.css"
    end
    
    def copy_theme_stylesheets
      directory "#{stylesheets_path}/themes/#{options.theme}", "app/assets/stylesheets/web-app-theme/themes/#{options.theme}"
    end
    
    def copy_images
      directory "#{images_path}", "app/assets/images/web-app-theme"
    end
    
  protected
  
    def stylesheets_path
      "../../../../../stylesheets"
    end
    
    def images_path
      "../../../../../images"
    end
    
    def generate_haml_layout(admin_layout_name)
      require 'haml'
      Dir.mktmpdir('web-app-theme-haml') do |haml_root|
        tmp_html_path = "#{haml_root}/#{admin_layout_name}"
        tmp_haml_path = "#{haml_root}/#{admin_layout_name}.haml"
        template admin_layout_name, tmp_html_path, :verbose => false
        `html2haml --erb --xhtml #{tmp_html_path} #{tmp_haml_path}`
        copy_file tmp_haml_path, "app/views/layouts/#{layout_name.underscore}.html.haml"
      end
    rescue LoadError
      say "HAML is not installed, or it is not specified in your Gemfile."
      exit
    end
  end
end
Something went wrong with that request. Please try again.