class UcbRailsCli::Cli

Constants

ASSETS
CONFIG_FILES
CONTROLLERS
GEMFILE_ERROR
HELPER_FILES
INSTALL_RAILS

error messages

LAYOUT_FILES
MASTER_KEY_PATH
MIGRATIONS
ROUTES
RSPEC
VERIFY_KEY

Public Instance Methods

new(app_name) click to toggle source
# File lib/ucb_rails_cli/cli.rb, line 31
    def new(app_name)
      puts "Generating #{app_name}..."

      verify_rails or exit_with_error(INSTALL_RAILS)

      verify_master_key or exit_with_error(VERIFY_KEY)

      create_rails_app(app_name) or exit_with_error

      add_to_gemfile(app_name) or exit_with_error(GEMFILE_ERROR)

      add_layout_files(app_name) or exit_with_error(LAYOUT_FILES)

      add_helpers(app_name) or exit_with_error(HELPER_FILES)

      add_assets(app_name) or exit_with_error(ASSETS)

      add_routes(app_name) or exit_with_error(ROUTES)

      add_controllers(app_name) or exit_with_error(CONTROLLERS)

      add_config_files(app_name) or exit_with_error(CONFIG_FILES)

      install_migrations(app_name) or exit_with_error(MIGRATIONS)

      setup_rspec(app_name) or exit_with_error(RSPEC)

      puts <<-END

********************************************************************
Finished! Your new app is in ./#{app_name}

From here, you should set up the database:

  cd #{app_name}
  bin/rails db:create
  bin/rails db:migrate

then start the server as usual:

  bin/rails server

then access the homepage at http://localhost:3000. You should be able
to login with your usual UCB credentials.

Enjoy!

      END
    end
version() click to toggle source
# File lib/ucb_rails_cli/cli.rb, line 25
def version
  say "ucb_rails_cli #{UcbRailsCli::VERSION}"
end

Private Instance Methods

add_assets(app_name) click to toggle source
# File lib/ucb_rails_cli/cli.rb, line 137
def add_assets(app_name)
  puts "Installing assets..."
  result =
    system("cp #{template_dir}/stylesheets/main.sass #{app_name}/app/assets/stylesheets") &&
    system("cp -R #{template_dir}/stylesheets/themes #{app_name}/app/assets/stylesheets") &&
    system("cp #{template_dir}/images/* #{app_name}/app/assets/images")
  if result
    # don't clobber the default file until we're sure everything else went through
    system "cp #{template_dir}/stylesheets/application.css #{app_name}/app/assets/stylesheets"
    system "cp #{template_dir}/javascripts/application.js #{app_name}/app/assets/javascripts"
  else
    false
  end
end
add_config_files(app_name) click to toggle source
# File lib/ucb_rails_cli/cli.rb, line 162
    def add_config_files(app_name)
      puts "Installing config files..."
      system("cp #{template_dir}/config/initializers/ucb_rails_user.rb #{app_name}/config/initializers")
      system("cp #{template_dir}/config/credentials.yml.enc #{app_name}/config/")

      # add exeception_notification config to production.rb config
      prod_config_addition = <<-END_CONFIG

  config.middleware.use ExceptionNotification::Rack,
    email: {
      email_prefix: "[#{app_name}] ",
      sender_address: %{"notifier" <notifier@example.com>},
      exception_recipients: %w{ucb-ist-developers@berkeley.edu}
    }
end
      END_CONFIG
      prod_config_contents =
        `cat #{app_name}/config/environments/production.rb`
        .gsub(/end\s\Z/, prod_config_addition)
      system "echo '#{prod_config_contents}' > #{app_name}/config/environments/production.rb"

      # move master key, unless in ENV
      unless ENV["MASTER_KEY_PATH"].to_s.length > 0
        system "cp ~/.ucb_rails_master_key #{app_name}/config/master.key"
      end

      # set default timezone in application.rb
      application_config_contents =
        `cat #{app_name}/config/application.rb`
        .gsub(/end\s*end\s\Z/, %Q(  config.time_zone = "Pacific Time (US & Canada)"\n  end\nend))
      File.open("#{app_name}/config/application.rb", "w") do |file|
        file.puts(application_config_contents)
      end
      true
    end
add_controllers(app_name) click to toggle source
# File lib/ucb_rails_cli/cli.rb, line 157
def add_controllers(app_name)
  puts "Installing controllers..."
  system "cp #{template_dir}/controllers/* #{app_name}/app/controllers/"
end
add_helpers(app_name) click to toggle source
# File lib/ucb_rails_cli/cli.rb, line 127
def add_helpers(app_name)
  puts "Installing helpers..."
  file_contents = `cat #{template_dir}/helpers/application_helper.rb`
    .gsub("%APP_NAME", humanize(app_name))
    .gsub("%APP_START_YEAR", Date.today.year.to_s)
  system "echo '#{file_contents}' > #{app_name}/app/helpers/application_helper.rb"
  system("cp #{template_dir}/helpers/flash_helper.rb #{app_name}/app/helpers/flash_helper.rb")
  true
end
add_layout_files(app_name) click to toggle source
# File lib/ucb_rails_cli/cli.rb, line 118
def add_layout_files(app_name)
  puts "Installing template files..."
  if system("cp #{template_dir}/views/* #{app_name}/app/views/layouts")
    system "rm -f #{app_name}/app/views/layouts/application.html.erb"
  else
    false
  end
end
add_routes(app_name) click to toggle source
# File lib/ucb_rails_cli/cli.rb, line 152
def add_routes(app_name)
  puts "Installing routes..."
  system "cp #{template_dir}/config/routes.rb #{app_name}/config/routes.rb"
end
add_to_gemfile(app_name) click to toggle source
# File lib/ucb_rails_cli/cli.rb, line 111
def add_to_gemfile(app_name)
  if system("cat #{template_dir}/Gemfile_additions >> #{app_name}/Gemfile")
    puts "Installing new gems..."
    system "cd #{app_name} && bundle install"
  end
end
create_rails_app(app_name) click to toggle source
# File lib/ucb_rails_cli/cli.rb, line 102
def create_rails_app(app_name)
  result = system("rails new #{app_name} #{options[:rails_options]}")
  return true if result

  puts "Unable to run \"rails new\" to create a new app - check the console output \n" +
    "for more details about what went wrong"
  false
end
custom_spec_helper_config() click to toggle source
# File lib/ucb_rails_cli/cli.rb, line 233
    def custom_spec_helper_config
      config = <<-END_CONFIG

  # ucb_rails config
  Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
  config.include UcbRailsUser::SpecHelpers
  UcbRailsUser.config do |config|
    config.user_session_manager = "UcbRailsUser::UserSessionManager::TestSessionManager"
  end
      END_CONFIG
    end
exit_with_error(msg="") click to toggle source
# File lib/ucb_rails_cli/cli.rb, line 83
def exit_with_error(msg="")
  puts msg
  exit -1
end
humanize(str) click to toggle source
# File lib/ucb_rails_cli/cli.rb, line 225
def humanize(str)
  str
    .gsub("_", " ")
    .split
    .map(&:capitalize)
    .join(" ")
end
install_migrations(app_name) click to toggle source
# File lib/ucb_rails_cli/cli.rb, line 198
def install_migrations(app_name)
  puts "Installing migrations from ucb_rails_user..."
  system "cd #{app_name} && bundle install"
  system "cd #{app_name} && bundle exec rails railties:install:migrations"
end
setup_rspec(app_name) click to toggle source
# File lib/ucb_rails_cli/cli.rb, line 204
def setup_rspec(app_name)
  puts "Setting up rspec..."
  success =
    system("cd #{app_name} && bin/spring stop && bin/rails generate rspec:install") &&
      system("rm -rf #{app_name}/test") &&
      system("cp -R #{template_dir}/rspc_files/* #{app_name}/spec")
  success or return false
  rails_helper_contents =
    `cat #{app_name}/spec/rails_helper.rb`
    .gsub(/end\s\Z/, "#{custom_spec_helper_config()}\nend")
    .gsub("require 'rspec/rails'", "require 'rspec/rails'\nrequire 'ucb_rails_user/spec_helpers'")
  File.open("#{app_name}/spec/rails_helper.rb", "w") do |file|
    file.puts(rails_helper_contents)
  end
  true
end
template_dir() click to toggle source
# File lib/ucb_rails_cli/cli.rb, line 221
def template_dir
  "#{File.dirname(__dir__)}/ucb_rails_cli/templates"
end
verify_master_key() click to toggle source
# File lib/ucb_rails_cli/cli.rb, line 98
def verify_master_key
  ENV["RAILS_MASTER_KEY"].to_s.length > 0 || FileTest.file?(MASTER_KEY_PATH)
end
verify_rails() click to toggle source
# File lib/ucb_rails_cli/cli.rb, line 88
def verify_rails
  begin
    `rails --version` =~ /Rails (\d)\./
    major_version = $1&.to_i
    return major_version && major_version >= 5
  rescue Exception => e
    return false
  end
end