class Refinery::CmsGenerator

Public Instance Methods

generate() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 21
def generate
  start_pretending?

  manage_roadblocks! unless self.options[:update]

  ensure_environments_are_sane!

  stop_pretending?

  append_gemfile!

  append_gitignore!

  append_asset_pipeline!

  forced_overwriting?

  copy_files!

  create_decorators!

  mount!

  run_additional_generators! if self.options[:fresh_installation]

  prepare_database!

  deploy_to_hosting?
end

Protected Instance Methods

append_asset_pipeline!() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 53
   def append_asset_pipeline!
     application_css = 'app/assets/stylesheets/application.css'
     if destination_path.join(application_css).file?
       insert_into_file application_css, %q{*= require refinery/formatting
*= require refinery/theme
},      :before => "*= require_self"
     end
   end
append_gemfile!() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 62
    def append_gemfile!
      if destination_path.join('Gemfile').file? &&
         destination_path.join('Gemfile').read !~ %r{group :development, :test do\n.+?gem 'sqlite3'\nend}m
        gsub_file 'Gemfile', %q{gem 'sqlite3'}, %q{group :development, :test do
  gem 'sqlite3'
end}  end
    end
append_gitignore!() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 70
def append_gitignore!
  # Ensure .gitignore exists and append our rules to it.
  create_file ".gitignore" unless destination_path.join('.gitignore').file?
  our_ignore_rules = self.class.source_root.join('.gitignore').read
  our_ignore_rules = our_ignore_rules.split('# REFINERY CMS DEVELOPMENT').first if destination_path != Refinery.root
  append_file ".gitignore", our_ignore_rules
end
append_heroku_gems!() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 78
    def append_heroku_gems!
      production_gems = [
        "gem 'dragonfly-s3_data_store'"
      ]
      production_gems << "gem 'puma'" unless destination_gemfile_has_puma?
      production_gems << "gem 'pg'" unless destination_gemfile_has_postgres?

      append_file "Gemfile", %Q{
# The Ruby version is specified here so that Heroku uses the right version.
ruby #{current_ruby_version.inspect}

# Gems that have been added for Heroku support
group :production do
  {{production_gems}}
end
}.sub("{{production_gems}}", production_gems.join("\n  "))
    end
bundle!() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 100
def bundle!
  run 'bundle install'
end
copy_files!() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 104
def copy_files!
  # The extension installer only installs database templates.
  Pathname.glob(self.class.source_root.join('**', '*')).reject{ |f|
    f.directory? or f.to_s =~ /\/db\//
  }.sort.each do |path|
    copy_file path, path.to_s.gsub(self.class.source_root.to_s, destination_path.to_s)
  end
end
create_decorators!() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 113
def create_decorators!
  # Create decorator directories
  %w[controllers models].each do |decorator_namespace|
    src_file_path = "app/decorators/#{decorator_namespace}/refinery/.keep"
    copy_file self.class.source_root.join(src_file_path), destination_path.join(src_file_path)
  end
end
create_heroku_procfile!() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 121
    def create_heroku_procfile!
      create_file "Procfile" do
        "web: bundle exec puma -C config/puma.rb"
      end unless destination_path.join('Procfile').file?

      create_file "config/puma.rb" do
%{threads Integer(ENV['MIN_THREADS']  || 1), Integer(ENV['MAX_THREADS'] || 16)

workers Integer(ENV['PUMA_WORKERS'] || 3)

rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
preload_app!

on_worker_boot do
  # worker specific setup
  ActiveSupport.on_load(:active_record) do
    ActiveRecord::Base.establish_connection
  end
end
}
      end unless destination_path.join('config', 'puma.rb').file?
    end
current_ruby_version() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 96
def current_ruby_version
  ENV['RUBY_VERSION'].presence || RUBY_VERSION
end
deploy_to_hosting?() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 146
    def deploy_to_hosting?
      if heroku?
        if heroku_toolbelt_missing?
          fail <<-ERROR
  \033[31m[ABORTING]\033[0m Heroku Toolbelt is not installed. Please re-start the installer after installing at:\nhttps://devcenter.heroku.com/articles/heroku-command-line
  ERROR
        end

        append_heroku_gems!

        # Sanity check the heroku application name and save whatever messages are produced.
        message = sanity_check_heroku_application_name!

        create_heroku_procfile!

        bundle!

        # Supply the deploy process with the previous messages to make them visible.
        deploy_to_hosting_heroku!(message)
      end
    end
deploy_to_hosting_heroku!(message = nil) click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 168
def deploy_to_hosting_heroku!(message = nil)
  say_status "Initializing and committing to git..", nil
  run "git init && git add . && git commit -am 'Created application using Refinery CMS #{Refinery.version}'"

  say_status message, nil, :yellow if message

  say_status "Creating Heroku app..", nil
  run ["heroku create",
       (options[:heroku] if heroku?),
       "#{"--stack #{options[:stack]}" if options[:stack]}"
      ].compact.join(' ')

  say_status "Pushing to Heroku (this takes time, be patient)..", nil
  run "git push heroku master"

  say_status "Setting up the Heroku database..", nil
  run "heroku#{' run' if options[:stack] == 'cedar-14'} rake db:migrate"

  say_status "Seeding the Heroku database..", nil
  run "heroku#{' run' if options[:stack] == 'cedar-14'} rake db:seed"

  say_status "Restarting servers...", nil
  run "heroku restart"
end
destination_gemfile_has_gem?(gem_name) click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 239
def destination_gemfile_has_gem?(gem_name)
  destination_path.join('Gemfile').file? &&
    destination_path.join('Gemfile').read =~ %r{gem ['"]#{gem_name}['"]}
end
destination_gemfile_has_postgres?() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 231
def destination_gemfile_has_postgres?
  destination_gemfile_has_gem?('pg')
end
destination_gemfile_has_puma?() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 235
def destination_gemfile_has_puma?
  destination_gemfile_has_gem?('puma')
end
destination_path() click to toggle source

Helper method to quickly convert destination_root to a Pathname for easy file path manipulation

# File lib/generators/refinery/cms/cms_generator.rb, line 194
def destination_path
  @destination_path ||= Pathname.new(self.destination_root)
end
ensure_environments_are_sane!() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 198
def ensure_environments_are_sane!
  # Massage environment files
  %w(development test production).map{ |e| "config/environments/#{e}.rb"}.each do |env|
    next unless destination_path.join(env).file?

    # Refinery does not necessarily expect action_mailer to be available as
    # we may not always require it (currently only the authentication extension).
    # Rails, however, will optimistically place config entries for action_mailer.
    current_mailer_config = File.read(destination_path.join(env)).to_s.
                                 match(%r{^\s.+?config\.action_mailer\..+([\w\W]*\})?}).
                                 to_a.flatten.first

    if current_mailer_config.present?
      new_mailer_config = [
        "  if config.respond_to?(:action_mailer)",
        current_mailer_config.gsub(%r{\A\n+?}, ''). # remove extraneous newlines at the start
                              gsub(%r{^\ \ }) { |line| "  #{line}" }, # add indentation on each line
        "  end"
      ].join("\n")

      gsub_file env, current_mailer_config, new_mailer_config, :verbose => false
    end

    gsub_file env, "config.assets.compile = false", "config.assets.compile = true", :verbose => false
  end
end
forced_overwriting?() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 225
def forced_overwriting?
  force_options = self.options.dup
  force_options[:force] = self.options[:force] || self.options[:update]
  self.options = force_options
end
heroku?() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 244
def heroku?
  options[:heroku].present?
end
heroku_toolbelt_missing?() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 248
def heroku_toolbelt_missing?
  find_executable("heroku").nil?
end
manage_roadblocks!() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 252
def manage_roadblocks!
  %w(public/index.html app/views/layouts/application.html.erb).each do |roadblock|
    if (roadblock_path = destination_path.join(roadblock)).file?
      if self.options[:fresh_installation]
        remove_file roadblock_path, :verbose => true
      else
        say_status :"-- You may need to remove '#{roadblock}' for Refinery to function properly --", nil, :yellow
      end
    end
  end
end
mount!() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 275
    def mount!
      if (routes_file = destination_path.join('config', 'routes.rb')).file? && (self.behavior == :revoke || (routes_file.read !~ %r{mount\ Refinery::Core::Engine}))
        # Append routes
        mount = %Q{
  # This line mounts Refinery's routes at the root of your application.
  # This means, any requests to the root URL of your application will go to Refinery::PagesController#home.
  # If you would like to change where this extension is mounted, simply change the
  # configuration option `mounted_path` to something different in config/initializers/refinery/core.rb
  #
  # We ask that you don't use the :as option here, as Refinery relies on it being the default of "refinery"
  mount Refinery::Core::Engine, at: Refinery::Core.mounted_path

}

        inject_into_file 'config/routes.rb', mount, after: ".routes.draw do"
      end
    end
prepare_database!() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 264
def prepare_database!
  unless self.options[:skip_migrations]
    command = %w[railties:install:migrations]
    unless self.options[:skip_db]
      command |= %w[db:create db:migrate]
      command |= %w[db:seed] unless self.options[:skip_migrations]
    end
    rake command.join(' ')
  end
end
run_additional_generators!() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 293
def run_additional_generators!
  generator_args = []
  generator_args << '--quiet' if self.options[:quiet]
  generator_args << '--skip-migrations' if self.options[:skip_migrations]
  Refinery::CoreGenerator.start generator_args
  Refinery::Authentication::DeviseGenerator.start generator_args if defined?(Refinery::Authentication::DeviseGenerator)
  Refinery::Dragonfly::DragonflyGenerator.start generator_args if defined?(Refinery::Dragonfly::DragonflyGenerator)
  Refinery::ResourcesGenerator.start generator_args if defined?(Refinery::ResourcesGenerator)
  Refinery::PagesGenerator.start generator_args if defined?(Refinery::PagesGenerator)
  Refinery::ImagesGenerator.start generator_args if defined?(Refinery::ImagesGenerator)
  Refinery::I18nGenerator.start generator_args if defined?(Refinery::I18nGenerator)
end
sanity_check_heroku_application_name!() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 306
def sanity_check_heroku_application_name!
  if heroku? && options[:heroku].to_s.include?('_') || options[:heroku].to_s.length > 30
    message = ["\nThe application name '#{options[:heroku]}' that you specified is invalid for Heroku."]
    suggested_name = options[:heroku].dup.to_s
    if suggested_name.include?('_')
      message << "This is because it contains underscores which Heroku does not allow."
      suggested_name.gsub!(/_/, '-')
    end
    if suggested_name.length > 30
      message << "This is#{" also" unless suggested_name.nil?} because it is longer than 30 characters."
      suggested_name = suggested_name[0..29]
    end

    options[:heroku] = suggested_name

    message << "We have changed the name to '#{suggested_name}' for you, hope it suits you.\n"
    message.join("\n")
  end

  options[:heroku] = '' if options[:heroku] == 'heroku'
end
start_pretending?() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 328
def start_pretending?
  # Only pretend to do the next actions if this is Refinery to stay DRY
  if destination_path == Refinery.root
    say_status :'-- pretending to make changes that happen in an actual installation --', nil, :yellow
    old_pretend = self.options[:pretend]
    new_options = self.options.dup
    new_options[:pretend] = true
    self.options = new_options
  end
end
stop_pretending?() click to toggle source
# File lib/generators/refinery/cms/cms_generator.rb, line 339
def stop_pretending?
  # Stop pretending
  if destination_path == Refinery.root
    say_status :'-- finished pretending --', nil, :yellow
    new_options = self.options.dup
    new_options[:pretend] = old_pretend
    self.options = new_options
  end
end