module Speedo module Generators class InstallGenerator < Rails::Generators::Base source_root File.expand_path("../../../../", __FILE__) desc 'Installs Speedo files to get you going fast.' # app/views/layouts/application.html.haml def layout if File.exist? 'app/views/layouts/application.html.haml' if yes? 'application.html.haml already exists. Do you want the template as speedo.html.haml (for manual integration)? [yN]' template 'app/views/layouts/speedo/application.html.haml', 'app/views/layouts/speedo.html.haml' end else template 'app/views/layouts/speedo/application.html.haml', 'app/views/layouts/application.html.haml' end if File.exist? 'app/views/layouts/application.html.erb' if yes? 'Do you want to remove the application.html.erb layout (otherwise delete it manually)? [yN]' remove_file 'app/views/layouts/application.html.erb' end end end def navbar template 'app/views/layouts/speedo/_navbar.html.haml', 'app/views/layouts/_navbar.html.haml' if_file 'app/views/layouts/application.html.haml' do |path| if File.read(path).match /render 'layouts\/speedo\/navbar'/ gsub_file path, /render 'layouts\/speedo\/navbar'/, "render 'layouts/navbar'" end end end # app/assets/stylesheets/speedo.css.scss def copy_stylesheets css = ['speedo','base','plugins','theme','helpers'] css.each do |file| copy_file "app/assets/stylesheets/speedo/#{file}.css.scss", "app/assets/stylesheets/speedo/#{file}.css.scss" end end # app/assets/stylesheets/application.css.scss def copy_application_css if File.exist? 'app/assets/stylesheets/application.css.scss' if yes? 'application.css.scss already exists. Do you want to replace it? [yN]' copy_file 'app/assets/stylesheets/application.css.scss', 'app/assets/stylesheets/application.css.scss' else say 'Make sure to @import "speedo"; in your application.css.scss, and @import other files - do NOT use *=require.' end else copy_file 'app/assets/stylesheets/application.css.scss', 'app/assets/stylesheets/application.css.scss' end if File.exist? 'app/assets/stylesheets/application.css' if yes? 'Do you want to remove application.css (otherwise delete it manually)? [yN]' remove_file 'app/assets/stylesheets/application.css' end end end # app/assets/javascripts/swim.js.coffee def javascript copy_file 'app/assets/javascripts/swim.js.coffee', 'app/assets/javascripts/swim.js.coffee' end def alter_application_js if_file 'app/assets/javascripts/application.js' do |path| unless File.read(path).match /require speedo/ inject_into_file path, before: '//= require_tree .' do "//= require speedo\n" end end end end # config/locales/speedo.en.yml def locales if File.exist? 'config/locales/en.yml' unless File.read('config/locales/en.yml').match /app_name/ inject_into_file 'config/locales/en.yml', after: 'en:' do "\n app_name: 'See config/locales to name this app'" end end else copy_file 'config/locales/speedo.en.yml', 'config/locales/speedo.en.yml' end end def routes route 'mount Speedo::Engine => "/speedo"' end def run_formtastic_generator generate "formtastic:install" end def bootstrap_formtastic if File.exist? 'config/initializers/formtastic.rb' unless File.read('config/initializers/formtastic.rb').match /FormtasticBootstrap/ append_file 'config/initializers/formtastic.rb' do "\n# config/initializers/formtastic.rb" "\nFormtastic::Helpers::FormHelper.builder = FormtasticBootstrap::FormBuilder" end end else say "Make sure to add `Formtastic::Helpers::FormHelper.builder = FormtasticBootstrap::FormBuilder` to config/initializers/formtastic.rb." end end private def if_file path if File.exist? path yield path else say "#{path} not found. Add '//= require_tree .' to application.js." end end end end end