require 'rails/generators' module TmattCms module Generators class InstallGenerator < Rails::Generators::Base source_root File.expand_path('../templates', __FILE__) # class_option :globalize, :type => :boolean, :default => true, :description => 'Include translatable files' def remove_double remove_file 'app/models/double.rb' end def copy_git_ignore copy_file 'git_ignore/gitignore', '.gitignore' end def config_application_rb filename = 'config/application.rb' if File.readlines(filename).grep(/concerns/).size == 0 line_count = get_line_count filename insert_at_line(filename, line_count - 2, [ "\n\t\tconfig.autoload_paths += %W(\#{config.root}/app/models/concerns)" ]) end if File.readlines(filename).grep(/DATE_FORMATS/).size == 0 line_count = get_line_count filename insert_at_line(filename, line_count - 2, [ "\n\t\tTime::DATE_FORMATS[:aus] = '%l:%M %p on %d %B %Y'", "\t\tDate::DATE_FORMATS[:aus] = '%d %B %Y'", "\t\tTime::DATE_FORMATS[:zh] = '%l:%M %p on %d %B %Y'", "\t\tDate::DATE_FORMATS[:zh] = '%d %B %Y'", ]) end end def add_application_yml copy_file 'config/application.yml', 'config/application.yml' filename = 'config/application.rb' if File.readlines(filename).grep(/application\.yml/).size == 0 insert_at_line(filename, 8, ["\nENV.update YAML.load_file('config/application.yml')[Rails.env] rescue {}"]) end end def config_google_smtp filename = 'config/environments/development.rb' if File.readlines(filename).grep(/smtp/).size == 0 line_count = get_line_count filename insert_at_line(filename, line_count - 1, [ "\n\tconfig.action_mailer.default_url_options = { :host => ENV['DEVELOPMENT_URL'] }", "\tconfig.action_mailer.delivery_method = :smtp", "\tconfig.action_mailer.smtp_settings = {", "\t\t:address => ENV['SMTP_ADDRESS'],", "\t\t:port => ENV['SMTP_PORT'].to_i,", "\t\t:user_name => ENV['MAILER_EMAIL'],", "\t\t:password => ENV['MAILER_PASSWORD'],", "\t\t:authentication => :plain", "\t}", ]) end filename = 'config/environments/production.rb' if File.readlines(filename).grep(/smtp/).size == 0 line_count = get_line_count filename insert_at_line(filename, line_count - 1, [ "\n\tconfig.action_mailer.default_url_options = { :host => ENV['PRODUCTION_URL'] }", "\tconfig.action_mailer.asset_host = \"\#{ENV['PROTOCOL']}\#{ENV['PRODUCTION_URL']}\"", "\tconfig.action_mailer.delivery_method = :smtp", "\tconfig.action_mailer.smtp_settings = {", "\t\t:address => ENV['SMTP_ADDRESS'],", "\t\t:port => ENV['SMTP_PORT'].to_i,", "\t\t:user_name => ENV['MAILER_EMAIL'],", "\t\t:password => ENV['MAILER_PASSWORD'],", "\t\t:authentication => :plain,", "\t\t:enable_starttls_auto => true", "\t}", ]) end end def copy_input_files copy_file 'inputs/concerns/translation_hint.rb', 'app/inputs/concerns/translation_hint.rb' copy_file 'inputs/ckeditor_translation_input.rb', 'app/inputs/ckeditor_translation_input.rb' copy_file 'inputs/translation_input.rb', 'app/inputs/translation_input.rb' end def copy_translatable copy_file 'concerns/translatable.rb', 'app/models/concerns/translatable.rb' end def config_locales copy_file 'config/locales/views/dashboard/en.yml', 'config/locales/views/dashboard/en.yml' copy_file 'config/locales/views/dashboard/zh.yml', 'config/locales/views/dashboard/zh.yml' copy_file 'config/locales/views/global/en.yml', 'config/locales/views/global/en.yml' copy_file 'config/locales/views/global/zh.yml', 'config/locales/views/global/zh.yml' filename = 'config/application.rb' if File.readlines(filename).grep(/config', 'locales', '\*\*'/).size == 0 line_count = get_line_count filename insert_at_line(filename, line_count - 2, [ "\n\t\tconfig.i18n.enforce_available_locales = false", "\t\tconfig.i18n.fallbacks = true", "\t\tconfig.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]", "\t\tconfig.i18n.default_locale = 'en'", ]) end end def copy_db_init copy_file 'concerns/db_init.rb', 'app/models/concerns/db_init.rb' end def copy_pretty_url remove_file 'app/models/concerns/pretty_url.rb' copy_file 'concerns/pretty_url.rb', 'app/models/concerns/pretty_url.rb' end def copy_string_addition copy_file 'concerns/string_addition.rb', 'app/models/concerns/string_addition.rb' end def copy_stylesheet_files copy_file 'css/application/main.css.scss', 'app/assets/stylesheets/application/main.css.scss' copy_file 'css/application/mixin.css.scss', 'app/assets/stylesheets/application/mixin.css.scss' copy_file 'css/application/z_index.css.scss', 'app/assets/stylesheets/application/z_index.css.scss' copy_file 'css/application/variables.css.scss', 'app/assets/stylesheets/application/variables.css.scss' remove_file 'app/assets/stylesheets/application/application.css' copy_file 'css/application/application.scss', 'app/assets/stylesheets/application/application.scss' remove_file 'app/assets/stylesheets/application/index.css.scss' copy_file 'css/application/index.css.scss', 'app/assets/stylesheets/application/index.css.scss' remove_file 'app/assets/stylesheets/style.css.scss' copy_file 'css/style.css.scss', 'app/assets/stylesheets/style.css.scss' copy_file 'css/layouts/header.css.scss', 'app/assets/stylesheets/layouts/header.css.scss' copy_file 'css/layouts/footer.css.scss', 'app/assets/stylesheets/layouts/footer.css.scss' end def copy_javascript_files remove_file 'app/assets/javascripts/application.js' copy_file 'js/application.js', 'app/assets/javascripts/application.js' end def copy_application_layout copy_file 'views/layouts/application.html.haml', 'app/views/layouts/application.html.haml' end def config_seed_bank copy_file 'seed_bank/all.seeds.rb', 'db/seeds/all.seeds.rb' copy_file '.keep', 'db/seeds/resources/images/.keep' copy_file '.keep', 'db/seeds/resources/csv/.keep' end def add_polyfills_js if File.readlines('config/initializers/assets.rb').grep(/polyfills/).size == 0 File.open(File.join('config', 'initializers', 'assets.rb'), 'a+') do |f| f.puts "\n\n" f.puts 'Rails.application.config.assets.precompile += %w( polyfills.js )' end end end def config_magnific_popup_rails copy_file 'css/application/magnific_popup_rails.css.scss', 'app/assets/stylesheets/application/magnific_popup_rails.css.scss' copy_file 'js/utils/magnificPopup.js', 'app/assets/javascripts/utils/magnificPopup.js' if File.readlines('config/initializers/assets.rb').grep(/magnificPopup/).size == 0 File.open(File.join('config', 'initializers', 'assets.rb'), 'a+') do |f| f.puts "\n\n" f.puts 'Rails.application.config.assets.precompile += %w( utils/magnificPopup.js )' end end end def config_sidekiq copy_file 'config/initializers/active_job.rb', 'config/initializers/active_job.rb' if File.readlines('Capfile').grep(/sidekiq/).size == 0 File.open(File.join('Capfile'), 'a+') do |f| f.puts "\n\n" f.puts "require 'capistrano/sidekiq'" f.puts "# require 'capistrano/sidekiq/monit'" end end end def config_ckeditor filename = 'config/application.rb' if File.readlines(filename).grep(/ckeditor/).size == 0 line_count = get_line_count filename insert_at_line(filename, line_count - 2, ["\n\t\tconfig.assets.precompile += %w( ckeditor/* )"]) end copy_file 'css/application/ckeditor.css.scss', 'app/assets/stylesheets/application/ckeditor.css.scss' if File.readlines('config/initializers/assets.rb').grep(/ckeditor/).size == 0 File.open(File.join('config', 'initializers', 'assets.rb'), 'a+') do |f| f.puts "\n\n" f.puts 'Rails.application.config.assets.precompile += %w( ckeditor/* )' end end end def config_paperclip filename = 'config/environments/development.rb' if File.readlines(filename).grep(/Paperclip/).size == 0 line_count = get_line_count filename insert_at_line(filename, line_count - 1, ["\n\tPaperclip.options[:command_path] = '/usr/local/bin/'"]) end filename = 'config/environments/production.rb' if File.readlines(filename).grep(/Paperclip/).size == 0 line_count = get_line_count filename insert_at_line(filename, line_count - 1, ["\n\tPaperclip.options[:command_path] = '/usr/bin/'"]) end end def config_jasny_bootstrap copy_file 'css/utils/jasny-bootstrap.min.css', 'app/assets/stylesheets/utils/jasny-bootstrap.min.css' copy_file 'js/utils/jasny-bootstrap.min.js', 'app/assets/javascripts/utils/jasny-bootstrap.min.js' if File.readlines('config/initializers/assets.rb').grep(/jasny/).size == 0 File.open(File.join('config', 'initializers', 'assets.rb'), 'a+') do |f| f.puts "\n\n" f.puts 'Rails.application.config.assets.precompile += %w( utils/jasny-bootstrap.min.css )' f.puts 'Rails.application.config.assets.precompile += %w( utils/jasny-bootstrap.min.js )' end end end def add_dashboard copy_file 'views/dashboard/partial/_sidebar.html.haml', 'app/views/dashboard/partial/_sidebar.html.haml' copy_file 'views/layouts/dashboard.html.haml', 'app/views/layouts/dashboard.html.haml' filename = 'config/routes.rb' if File.readlines(filename).grep(/dashboard/).size == 0 line_count = get_line_count filename insert_at_line(filename, line_count - 1, ["\n\tnamespace :dashboard do", "\n\tend"]) end end def add_image_cover copy_file 'views/utils/_image_cover.html.haml', 'app/views/utils/_image_cover.html.haml' end def copy_gemfile copy_file 'gemfile/Gemfile', 'Gemfile' end def config_deploy filename = 'config/deploy.rb' text = File.read(filename) new_contents = text.gsub(/vm_ruby_string, '2\.0\.0'/, "vm_ruby_string, '2.3.1'") File.open(filename, "w") {|file| file.puts new_contents } if File.readlines(filename).grep(/capistrano-db-tasks/).size == 0 insert_at_line(filename, 4, ["require 'capistrano-db-tasks'"]) end if File.readlines(filename).grep(/project_name/).size == 0 line_count = get_line_count filename insert_at_line(filename, line_count - 8, [ "set :branch, 'master'", "set :rails_env, 'production'", "set :migrate_env, 'production'", "set :use_sudo, false", 'set :deploy_via, :remote_cache', 'set :db_local_clean, true', "set :locals_rails_env, 'development'", "\n\nproject_name = 'project_name'", "server_address = 'server_address'", "server_ip = 'server_ip'", "\n\nputs 'Enter rake command[db:migrate]: '", 'db_cmd = STDIN.gets.chomp', "db_cmd = 'db:migrate' if db_cmd == ''", "\nset :server_address, server_address", 'server server_ip, :app, :web, :db, :primary => true', "\nafter 'deploy:restart', 'deploy:cleanup'", "after 'deploy:restart', 'unicorn:restart'", "after 'deploy:restart', 'sidekiq:restart'", "after 'deploy', 'assets:precompile'", "\nafter 'bundle:install', :roles => :web do", "\trun \"ln -s \#{shared_path}/config/application.yml \#{release_path}/config/application.yml\"", "\trun \"ln -s \#{shared_path}/config/database.yml \#{release_path}/config/database.yml\"", "\trun \"ln -s \#{shared_path}/config/secrets.yml \#{release_path}/config/secrets.yml\"", 'end', "\nnamespace :deploy do", "\ttask :start do ; end", "\ttask :stop do ; end", "\ttask :restart, :roles => :app, :except => { :no_release => true } do", "\t\trun \"cd '/home/deploy/\#{project_name}/current' ; bundle exec rake \#{db_cmd} RAILS_ENV=production\"", "\tend", 'end', ], false) end end def add_front_page copy_file 'views/pages/index.html.haml', 'app/views/pages/index.html.haml' copy_file 'controllers/pages_controller.rb', 'app/controllers/pages_controller.rb' filename = 'config/routes.rb' if File.readlines(filename).grep(/pages#index/).size == 0 line_count = get_line_count filename insert_at_line(filename, line_count - 1, ["\n\troot 'pages#index'"]) end end def config_devise_async copy_file 'config/initializers/devise_async.rb', 'config/initializers/devise_async.rb' copy_file 'config/sidekiq.yml', 'config/sidekiq.yml' end private def get_line_count(filename) `wc -l "#{filename}"`.strip.split(' ')[0].to_i + 1 end def insert_at_line(filename, at_line, contents, append_rest = true) open(filename, 'r+') do |f| while (at_line-=1) > 0 f.readline end pos = f.pos rest = f.read f.seek pos f.puts append_rest ? contents + [rest] : contents end end end end end