class Pineapples::AppGenerator

Constants

TEMPLATING_ENGINES

Attributes

app_name[RW]
app_root[RW]
settings[RW]

Public Class Methods

new(options) click to toggle source
# File lib/pineapples/app_generator.rb, line 57
def initialize(options)
  @app_name = options.app_name.gsub(/\s+/, '-')
  @app_root = options.app_root || File.expand_path(app_name)
  @debug = options.debug || false
  @pretend = options.pretend || false
  @verbose = options.verbose || true
  @testing = options.testing || false

  @settings = self.class.settings
end

Public Instance Methods

ask_user_settings() click to toggle source
# File lib/pineapples/app_generator.rb, line 82
def ask_user_settings
  settings[:heroku].ask_setting
  settings[:template_engine].ask_setting
end
convert_views() click to toggle source
# File lib/pineapples/app_generator.rb, line 231
def convert_views
  if haml?
    # TODO: convert all views to haml
  elsif slim?
    # TODO: convert all views to slim
  end
end
copy_example_files() click to toggle source
# File lib/pineapples/app_generator.rb, line 195
def copy_example_files
  say_title 'Copying sample files'
  template '.example.env', '.env'
  copy_file '.example.rspec', '.rspec'
end
create_app_files() click to toggle source
# File lib/pineapples/app_generator.rb, line 122
def create_app_files
  directory 'app'
  empty_directory_with_keep_file  'app/assets/fonts'
  empty_directory_with_keep_file  'app/assets/images'

  empty_directory_with_keep_file  'app/mailers'
  empty_directory_with_keep_file  'app/models' if !needs_user_model?
end
create_bin_files() click to toggle source
# File lib/pineapples/app_generator.rb, line 131
def create_bin_files
  directory 'bin'
  in_app_root do
    shell 'chmod -v 755 bin'
    shell 'chmod -v 644 bin/*'
  end
end
create_config_files() click to toggle source
# File lib/pineapples/app_generator.rb, line 139
def create_config_files
  directory 'config'
end
create_db_files() click to toggle source
# File lib/pineapples/app_generator.rb, line 143
def create_db_files
  empty_directory 'db/migrate'
  copy_migration 'enable_postgres_extensions'
  copy_migration 'create_data_migrations'
  if devise?
    copy_migration 'devise_create_users'
    copy_migration 'add_role_field_to_users' if user_role_field?
  end
end
create_files() click to toggle source
# File lib/pineapples/app_generator.rb, line 87
def create_files
  create_root_files
  create_app_files
  create_bin_files
  create_config_files
  create_db_files
  create_lib_files
  create_public_files
  create_spec_files
  create_misc_folders
  copy_example_files
end
create_lib_files() click to toggle source
# File lib/pineapples/app_generator.rb, line 153
def create_lib_files
  directory 'lib'
  keep_file 'lib/assets'
end
create_misc_folders() click to toggle source
# File lib/pineapples/app_generator.rb, line 184
def create_misc_folders
  empty_directory_with_keep_file 'log'

  empty_directory 'tmp/pids'

  inside 'vendor/assets' do
    empty_directory_with_keep_file 'javascripts'
    empty_directory_with_keep_file 'stylesheets'
  end
end
create_public_files() click to toggle source
# File lib/pineapples/app_generator.rb, line 158
def create_public_files
  directory 'public'
end
create_root_files() click to toggle source
# File lib/pineapples/app_generator.rb, line 100
def create_root_files
  create_file '.ruby-version', Pineapples::RUBY_VERSION
  create_file '.ruby-gemset',  app_name
  copy_file   '.editor-config'
  template    '.example.env'
  copy_file   '.example.rspec'
  copy_file   '.gitignore'
  template    '.simplecov'
  copy_file   'browserlist'
  template    'config.ru'
  template    'Gemfile'
  copy_file   'Guardfile'
  copy_file   'Procfile'
  template    'Procfile.dev'
  copy_file   'Rakefile'
  template    'README.md'
  if heroku?
    copy_file '.buildpacks'
    copy_file 'Aptfile'
  end
end
create_rvm_gemset() click to toggle source
# File lib/pineapples/app_generator.rb, line 214
def create_rvm_gemset
  say_title 'Creating project-specific RVM gemset'
  shell "rvm gemset create #{app_name}"
end
create_spec_files() click to toggle source
# File lib/pineapples/app_generator.rb, line 162
def create_spec_files
  directory 'spec'
  inside 'spec' do
    empty_directory_with_keep_file 'controllers'
    empty_directory_with_keep_file 'features'
    empty_directory_with_keep_file 'factories'
    empty_directory_with_keep_file 'helpers'
    empty_directory_with_keep_file 'jobs'
    empty_directory_with_keep_file 'mailers'
    empty_directory_with_keep_file 'models'
    empty_directory_with_keep_file 'policies' if pundit?
    empty_directory_with_keep_file 'presenters'
    empty_directory_with_keep_file 'services'
    empty_directory_with_keep_file 'lib'

    empty_directory_with_keep_file 'support/features'
    empty_directory_with_keep_file 'support/matchers'
    empty_directory_with_keep_file 'support/mixins'
    empty_directory_with_keep_file 'support/shared_examples'
  end
end
debug?() click to toggle source
# File lib/pineapples/app_generator.rb, line 268
def debug?
  @debug
end
execute?() click to toggle source
# File lib/pineapples/app_generator.rb, line 276
def execute?
  !pretend?
end
pretend?() click to toggle source
# File lib/pineapples/app_generator.rb, line 272
def pretend?
  @pretend
end
run_after_bundle_callbacks() click to toggle source
# File lib/pineapples/app_generator.rb, line 262
def run_after_bundle_callbacks
  @after_bundle_callbacks.each(&:call)
end
setup_app() click to toggle source
# File lib/pineapples/app_generator.rb, line 201
def setup_app
  create_rvm_gemset if rvm_installed? & use_rvm?
  shell_with_app_gemset 'bundle install'

  setup_database

  setup_gems

  to_new_hash_syntax!

  setup_git
end
setup_database() click to toggle source
# File lib/pineapples/app_generator.rb, line 219
def setup_database
  say_title 'Preparing database'
  shell_with_app_gemset 'bundle exec rake db:drop' if testing?
  shell_with_app_gemset 'bundle exec rake db:setup'
  shell_with_app_gemset 'bundle exec rake db:migrate'
end
setup_gems() click to toggle source
# File lib/pineapples/app_generator.rb, line 226
def setup_gems
  shell_with_app_gemset 'rails g kaminari:views default'
  erb2haml 'app/views/kaminari'
end
setup_git() click to toggle source
# File lib/pineapples/app_generator.rb, line 245
def setup_git
  if !preexisting_git_repo?
    say_title 'Setting up git repo'
    in_app_root do
      git :init
      git add: '-A .'
      git commit: %(-n -m "Generated Rails #{RAILS_VERSION.gsub('~> ', '')} project via pineapples gem")
      git checkout: '-b staging'
      git checkout: '-b dev'
      if git_repo_url?
        git remote: "add origin #{git_repo_url.shellescape}"
        git push: '-u origin --all'
      end
    end
  end
end
start!() click to toggle source
# File lib/pineapples/app_generator.rb, line 68
def start!
  valid_const! && check_target!
  create_app_root

  # ask_user_settings

  create_files

  setup_app
rescue Pineapples::Error => error
  (debug? || ENV['PINEAPPLES_DEBUG'] == '1') ? (raise error) : say(error.message.light_red)
  exit 1
end
testing?() click to toggle source
# File lib/pineapples/app_generator.rb, line 284
def testing?
  @testing
end
to_new_hash_syntax!() click to toggle source
# File lib/pineapples/app_generator.rb, line 239
def to_new_hash_syntax!
  # Converting files generated by generators like kaminari to new hash syntax
  say_title 'Converting Ruby hash rockets to new syntax'
  convert_directory_to_new_hash_syntax 'app/views/kaminari'
end
verbose?() click to toggle source
# File lib/pineapples/app_generator.rb, line 280
def verbose?
  @verbose
end

Protected Instance Methods

check_target!() click to toggle source
# File lib/pineapples/app_generator.rb, line 295
def check_target!
  if Dir["#{app_root}/*"].present?
    puts "I won't grow pineapples there, the target directory isn't empty."
    exit 1
  end
end
create_app_root() click to toggle source
# File lib/pineapples/app_generator.rb, line 290
def create_app_root
  FileUtils::mkdir_p(app_root)
  say_status :create_root, 'Created application root'
end