lib/generators/cambium/install/config_generator.rb in cambium-0.2.2 vs lib/generators/cambium/install/config_generator.rb in cambium-0.3.0
- old
+ new
@@ -1,192 +1,182 @@
require 'rake'
require 'rails/generators'
+require File.expand_path('../../helpers/_autoloader.rb', __FILE__)
module Cambium
module Install
class ConfigGenerator < Rails::Generators::Base
desc "Setup config files for new rails project"
- # ------------------------------------------ Class Methods
-
source_root File.expand_path('../../templates', __FILE__)
- # ------------------------------------------ Config
-
+ # Initialize git repository. If repo was already initialized, repo is
+ # reinitialized, but that shouldn't hurt anything.
+ #
def git_init
run_cmd "git init"
end
+ # Set vars to use throughout this generator
+ #
def cambium_setup
@config = {
:db => {},
:app => {}
}
end
+ # Read existing project files to set default config values
+ #
def auto_config
- # Read Gemfile
+ # read Gemfile
File.open("#{Rails.root}/Gemfile") do |f|
f.each_line do |line|
- # Database
+ # find database adapter
if line.include?("'mysql2'")
@config[:db][:adapter] = "mysql2"
elsif line.include?("'pg'")
@config[:db][:adapter] = "pg"
elsif line.include?("'sqlite3'")
@config[:db][:adapter] = "sqlite3"
end
- # Rails Version
+ # find rails version
if line.match(/gem\ \'rails\'/)
@config[:app][:version] = line.gsub(/gem\ \'rails\'\,/, '').gsub(/\'/, '').strip
end
end
end
- # Application Class Name
+ # Find the application class name
@config[:app][:name] = Rails.application.class.parent_name
end
- def user_config
- # Database
- if @config[:db][:adapter].present? && yes?("\nCambium, in all its wisdom, thinks your database adapter as #{set_color(@config[:db][:adapter], :green, :bold)}. Is this correct? [yes, no]")
- say("Oh, goodie! Moving on...")
- else
- new_adapter = ask("What would you prefer?", :limited_to => ['mysql2','pg','sqlite3'])
- unless new_adapter == @config[:db][:adapter]
- say set_color("\nPlease add:", :red, :bold)
- say "\n gem '#{new_adapter}'"
- say set_color("\nto your Gemfile. Then run `bundle install` and `bundle exec cambium setup`.", :red, :bold)
- exit
+ # These next few methods let the user adjust the default config if they
+ # wish. Otherwise, we use default values from the previous method.
+ #
+ # --------
+ #
+ # Set correct database adapter.
+ #
+ # Note: If the user wants to change the adapter, we *could* change the
+ # adapter on the fly and install the new one here. Instead, we tell the
+ # user what to do and exit. This make absolutely sure we've gotten rid of
+ # the original adapter in the Gemfile and added the new one.
+ #
+ def set_database_adapter
+ if @config[:db][:adapter].present?
+ if yes?("\nCambium, in all its wisdom, thinks your database adapter as #{set_color(@config[:db][:adapter], :green, :bold)}. Is this correct? [yes, no]")
+ say("Oh, goodie! Moving on...")
+ else
+ new_adapter = ask("What would you prefer?", :limited_to => ['mysql2','pg','sqlite3'])
+ unless new_adapter == @config[:db][:adapter]
+ say set_color("\nPlease add:", :red, :bold)
+ say "\n gem '#{new_adapter}'"
+ say set_color("\nto your Gemfile. Then run `bundle install` and `bundle exec cambium setup`.", :red, :bold)
+ exit
+ end
end
end
+ end
- # Database Name
+ # Set the database base name (where we append "_#{stage}" to each stage)
+ #
+ def set_database_name
@config[:db][:name] = @config[:app][:name].underscore.downcase
db_name = ask "\n#{set_color('Database Base Name:', :green, :bold)} [default: #{@config[:db][:name]}]"
@config[:db][:name] = db_name unless db_name.blank?
+ end
- # Database Credentials
+ # Set database credentials
+ #
+ def set_database_creds
@config[:db][:user] = confirm_ask("#{set_color('Database User', :green, :bold)}: [leave blank for no user]")
if @config[:db][:user].present?
@config[:db][:password] = confirm_ask("#{set_color('Database Password', :green, :bold)}: [leave blank for no password]")
else
@config[:db][:password] = ''
end
+ end
- # Root URL (for mailers)
+ # Set root URL (for mailers)
+ #
+ def set_root_url
+ # development
@config[:app][:dev_url] = ask("\n#{set_color('Development URL', :green, :bold)}: [leave blank for localhost:3000]")
@config[:app][:dev_url] = 'localhost:3000' if @config[:app][:dev_url].blank?
+ environment(
+ "config.action_mailer.default_url_options = { :host => '#{@config[:app][:dev_url]}' }",
+ :env => "development"
+ )
+ # production
@config[:app][:prod_url] = ask("#{set_color('Production URL', :green, :bold)}: [leave blank for localhost:3000]")
@config[:app][:prod_url] = 'localhost:3000' if @config[:app][:prod_url].blank?
+ environment(
+ "config.action_mailer.default_url_options = { :host => '#{@config[:app][:prod_url]}' }",
+ :env => "production"
+ )
+ environment(
+ "config.assets.precompile += %w( admin/admin.css admin/admin.js admin/wysihtml5.css modernizr.js )",
+ :env => "production"
+ )
end
- # ------------------------------------------ Gems & Gemfile
-
- def install_gemfile
- remove_file "Gemfile"
- template "Gemfile.erb", "Gemfile"
- run_cmd "bundle clean"
- end
-
- # ------------------------------------------ Application Settings & Config
-
+ # Add settings to application config file (config/application.rb)
+ #
def add_application_config
- insert_into_file(
- "config/application.rb",
- file_contents("config/application.rb"),
- :after => "class Application < Rails::Application"
- )
+ environment { file_contents("config/application.rb") }
end
- # ------------------------------------------ Environment Settings
-
- def add_env_settings
- insert_into_file "config/environments/development.rb",
- :after => "Rails.application.configure do" do
- "\n\n config.action_mailer.default_url_options = { :host => '#{@config[:app][:dev_url]}' }\n"
- end
- insert_into_file "config/environments/production.rb",
- :after => "Rails.application.configure do" do
- output = ''
- output += "\n\n config.action_mailer.default_url_options = { :host => '#{@config[:app][:prod_url]}' }"
- output += "\n\n config.assets.precompile += %w( admin/admin.css admin/admin.js admin/wysihtml5.css modernizr.js )\n"
- output
- end
- end
-
- # ------------------------------------------ Assets Initializer
-
+ # Assets initializer for Rails 4.1+
+ #
def add_assets_initializer
template "config/initializers/assets.rb",
"config/initializers/assets.rb"
end
- # ------------------------------------------ Database Setup
-
+ # Create database based on custom config
+ #
def setup_database
copy_file "#{Rails.root}/config/database.yml", "config/database.sample.yml"
remove_file "config/database.yml"
template "config/database.#{@config[:db][:adapter]}.yml.erb", "config/database.yml"
- run_cmd "#{rake} db:drop", :quiet => true
- run_cmd "#{rake} db:create"
- run_cmd "#{rake} db:migrate"
+ rake "db:create"
+ rake "db:migrate"
end
- # ------------------------------------------ .gitignore
-
+ # Add custom gitignore file
+ #
def add_gitignore
remove_file ".gitignore"
template "gitignore", ".gitignore"
end
- # ------------------------------------------ Private Methods
-
- private
-
- def run_cmd(cmd, options = {})
- print_table(
- [
- [set_color("run", :green, :bold), cmd]
- ],
- :indent => 9
- )
- if options[:quiet] == true
- `#{cmd}`
- else
- system(cmd)
- end
+ # We used to start the user out by installing all the gems we will need
+ # throughout the project. Now, we're going to ask the user if they want to
+ # replace their Gemfile with our custom file. If they say "no," then we
+ # will install the gems we need at a later step.
+ #
+ def add_recommended_gems
+ msg = "\nWe have a set of gems we recommend by default. You can \n"
+ msg += "start with this Gemfile if you'd like. Of course, we'll \n"
+ msg += "install gems for you when we need them."
+ say msg
+ if yes? "\nWould you like to replace your existing Gemfile with our default?"
+ remove_file "Gemfile"
+ template "Gemfile.erb", "Gemfile"
+ say "\nYour Gemfile has been updated, but now you need to run:"
+ say "\n $ bundle install"
+ say "\nto install all your gems. Feel free to any gems you don't want, "
+ say "\nsince we will add gems to your project as we need them."
+ else
+ say "You got it!"
end
+ end
- def template_file(name)
- File.expand_path("../../templates/#{name}", __FILE__)
- end
-
- def file_contents(template)
- File.read(template_file(template))
- end
-
- def be
- "bundle exec"
- end
-
- def g
- "#{be} rails g"
- end
-
- def rake
- "#{be} rake"
- end
-
- def confirm_ask(question)
- answer = ask("\n#{question}")
- match = ask("CONFIRM #{question}")
- if answer == match
- answer
- else
- say set_color("Did not match.", :red)
- confirm_ask(question)
- end
- end
+ # Outgoing message
+ #
+ def tell_user_we_are_done
+ say "\n#{set_color('Config completed!', :green, :bold)}"
+ end
end
end
end