Skip to content

Commit

Permalink
KBP-121 #time 7h implement responders gem and code refactor for app b…
Browse files Browse the repository at this point in the history
…uilder
  • Loading branch information
İsmail Akbudak committed Sep 11, 2017
1 parent bd60c8e commit 74e8468
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 80 deletions.
7 changes: 2 additions & 5 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ AllCops:
- 'Rakefile'
- 'Gemfile'
Excludes:
- bin/**
- spec/**/**/**/**
- test/**/**/**/**
- tmp/**/**/**/**

Documentation:
Expand All @@ -24,8 +21,6 @@ Style/AccessorMethodName:
Metrics/MethodLength:
CountComments: false
Max: 15
Exclude:
- 'lib/cybele/app_builder.rb'

Metrics/BlockLength:
CountComments: false
Expand All @@ -34,6 +29,8 @@ Metrics/BlockLength:
- 'Rakefile'
- '**/*.rake'
- 'spec/**/*.rb'
- 'lib/cybele/app_builder.rb'
- 'lib/cybele/generators/app_generator.rb'

Style/FrozenStringLiteralComment:
EnforcedStyle: when_needed
5 changes: 5 additions & 0 deletions bin/build_app
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ echo "Build cybele gem"
echo "Remove dummy_app"
rm -rf tmp/dummy_app

# Disable spring
export DISABLE_SPRING=1
# or you can run this command
# ps ax | grep spring | grep dummy_app | cut -f1 -d' ' | xargs kill

# Create dummy_app
echo "Create dummy_app"
cybele tmp/dummy_app --skip-create-database
19 changes: 11 additions & 8 deletions bin/cybele
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'pathname'

require File.expand_path(File.join('..', 'lib', 'cybele', 'generators', 'app_generator'), File.dirname(__FILE__))
require File.expand_path(File.join('..', 'lib', 'cybele', 'app_builder'), File.dirname(__FILE__))
require File.expand_path(File.join('..', 'lib', 'cybele', 'version'), File.dirname(__FILE__))
source_path = (Pathname.new(__FILE__).dirname + '../lib').expand_path
$LOAD_PATH << source_path

require 'cybele'

if ARGV.empty?
puts 'Please provide a path for the new application'
puts
puts 'See --help for more info'
exit 0
elsif ['-v', '--version'].include? ARGV[0]
elsif %w[-v --version].include? ARGV[0]
puts Cybele::VERSION
exit 0
end

templates_root = File.expand_path(File.join('..', 'templates'), File.dirname(__FILE__))
Cybele::AppGenerator.source_root templates_root
Cybele::AppGenerator.source_paths << Rails::Generators::AppGenerator.source_root << templates_root
Cybele::AppGenerator.start
root = File.expand_path(File.join('..', 'templates'), File.dirname(__FILE__))
Cybele::AppGenerator.source_root root
Cybele::AppGenerator.source_paths << Rails::Generators::AppGenerator.source_root << root
Cybele::AppGenerator.start
3 changes: 3 additions & 0 deletions lib/cybele.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

require 'cybele/version'
require 'cybele/generators/app_generator'
require 'cybele/helpers'
require 'cybele/helpers/sidekiq'
require 'cybele/helpers/responders'
require 'cybele/app_builder'
48 changes: 9 additions & 39 deletions lib/cybele/app_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

module Cybele
class AppBuilder < Rails::AppBuilder
include Cybele::Helpers
include Cybele::Helpers::Sidekiq
include Cybele::Helpers::Responders

def readme
template 'README.md.erb', 'README.md', force: true
template 'README.md.erb',
'README.md',
force: true
end

def remove_readme_rdoc
remove_file 'README.rdoc', force: true
remove_file 'README.rdoc',
force: true
end

def add_editor_config
Expand All @@ -27,42 +34,5 @@ def use_postgres_config_template
def create_database
bundle_command 'exec rake db:create db:migrate'
end

def use_sidekiq
# Add gems
append_file('Gemfile', template_content('sidekiq_Gemfile.erb'))

# Initialize files
template 'sidekiq.rb.erb',
'config/initializers/sidekiq.rb',
force: true
# Add tasks
template 'sidekiq.rake.erb',
'lib/tasks/sidekiq.rake',
force: true

# Add sidekiq.yml
template 'sidekiq.yml.erb',
'config/sidekiq.yml',
force: true

# Add sidekiq_schedule.yml
template 'sidekiq_schedule.yml.erb',
'config/sidekiq_schedule.yml',
force: true

# Add sidekiq routes to routes
prepend_file 'config/routes.rb',
template_content('sidekiq_routes_require.erb')
inject_into_file 'config/routes.rb',
template_content('sidekiq_routes_mount.erb'),
after: 'Rails.application.routes.draw do'
end

private

def template_content(file)
File.read(File.expand_path(find_in_source_paths(file)))
end
end
end
23 changes: 14 additions & 9 deletions lib/cybele/generators/app_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,46 @@ class AppGenerator < Rails::Generators::AppGenerator
desc: 'Skip sidekiq integration. Default: don\'t skip'

def setup_editor_config
say 'Add .editor_config file'
say 'Add .editor_config file', :green
build :add_editor_config
end

def setup_ruby_version
say 'Add .ruby-version file'
say 'Add .ruby-version file', :green
build :add_ruby_version
end

def remove_files_we_dont_need
say 'Remove files we don\'t need'
say 'Remove files we don\'t need', :green
build :remove_readme_rdoc
end

def setup_database
say 'Setting up database'
say 'Setting up database', :green
build :use_postgres_config_template if options[:database] == 'postgresql'
if options[:skip_create_database]
say 'don\'t create database'
say 'don\'t create database', :yellow
else
build :create_database
end
end

def setup_sidekiq
say 'Setting up sidekiq'
say 'Setting up sidekiq', :green
if options[:skip_sidekiq]
say 'don\'t use sidekiq'
say 'don\'t use sidekiq', :yellow
else
build :use_sidekiq
build :configure_sidekiq
end
end

def setup_responders
say 'Setting up responders', :green
build :configure_responders
end

def goodbye
say 'Congratulations! That\'s all...'
say 'Congratulations! That\'s all...', :green
end

protected
Expand Down
20 changes: 20 additions & 0 deletions lib/cybele/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Cybele
module Helpers
private

def replace_in_file(relative_path, find, replace)
path = File.join(destination_root, relative_path)
contents = IO.read(path)
unless contents.gsub!(find, replace)
raise "#{find.inspect} not found in #{relative_path}"
end
File.open(path, 'w') { |file| file.write(contents) }
end

def template_content(file)
File.read(File.expand_path(find_in_source_paths(file)))
end
end
end
27 changes: 27 additions & 0 deletions lib/cybele/helpers/responders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module Cybele
module Helpers
module Responders
def configure_responders
# Add gems
append_file('Gemfile', template_content('responders_Gemfile.erb'))
run_bundle

# Add initializers
bundle_command 'exec rails generate responders:install'

# Add js and json to respond :html
replace_in_file 'app/controllers/application_controller.rb',
'respond_to :html',
'respond_to :html, :js, :json'
replace_in_file 'app/controllers/application_controller.rb',
'require "application_responder"',
"require 'application_responder'"

# Remove comments in locale/responders.yml
uncomment_lines 'config/locales/responders.en.yml', /alert:/
end
end
end
end
44 changes: 44 additions & 0 deletions lib/cybele/helpers/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

module Cybele
module Helpers
module Sidekiq
def configure_sidekiq
# Add gems
append_file('Gemfile', template_content('sidekiq_Gemfile.erb'))

create_sidekiq_files

# Add sidekiq routes to routes
prepend_file 'config/routes.rb',
template_content('sidekiq_routes_require.erb')
inject_into_file 'config/routes.rb',
template_content('sidekiq_routes_mount.erb'),
after: 'Rails.application.routes.draw do'
end

private

def create_sidekiq_files
# Initialize files
template 'sidekiq.rb.erb',
'config/initializers/sidekiq.rb',
force: true
# Add tasks
template 'sidekiq.rake.erb',
'lib/tasks/sidekiq.rake',
force: true

# Add sidekiq.yml
template 'sidekiq.yml.erb',
'config/sidekiq.yml',
force: true

# Add sidekiq_schedule.yml
template 'sidekiq_schedule.yml.erb',
'config/sidekiq_schedule.yml',
force: true
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Create new project with default configuration' do

before(:all) do
drop_dummy_database
remove_project_directory
Expand Down Expand Up @@ -46,4 +47,23 @@
rake_file = content('lib/tasks/sidekiq.rake')
expect(rake_file).to match(/^namespace :sidekiq/)
end
end

it 'uses responders' do
gemfile_file = content('Gemfile')
expect(gemfile_file).to match(/^gem 'responders'/)

lib_file = content('lib/application_responder.rb')
expect(lib_file).to match(/^class ApplicationResponder/)

controller_file = content('app/controllers/application_controller.rb')
expect(controller_file).to match("^require 'application_responder'")
expect(controller_file).to match('self.responder = ApplicationResponder')
expect(controller_file).to match('respond_to :html, :js, :json')

locale_file = content('config/locales/responders.en.yml')
expect(locale_file).not_to match('# alert:')
expect(locale_file).to match('create:')
expect(locale_file).to match('update:')
expect(locale_file).to match('destroy:')
end
end
41 changes: 41 additions & 0 deletions spec/features/new_not_default_project_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Create new project without default configuration' do
before(:all) do
drop_dummy_database
remove_project_directory
run_cybele('--database=sqlite3 --skip-create-database --skip-sidekiq')
setup_app_dependencies
end

it 'uses sqlite3 instead of default pg in Gemfile' do
gemfile_file = content('Gemfile')
expect(gemfile_file).not_to match(/^gem 'pg'/)
expect(gemfile_file).to match(/^gem 'sqlite3'/)
end

it 'uses sqlite3 database template' do
database_file = content('config/database.yml')
expect(database_file).to match(/^default: &default/)
expect(database_file).to match(/adapter: sqlite3/)
end

it 'do not use sidekiq' do
gemfile_file = content('Gemfile')
expect(gemfile_file).not_to match(/^gem 'sidekiq'/)
expect(gemfile_file).not_to match(/^gem 'sidekiq-cron'/)
expect(gemfile_file).not_to match(/^gem 'cocaine'/)

expect(File).not_to exist(file_project_path('config/sidekiq.yml'))

expect(File).not_to exist(file_project_path('config/sidekiq_schedule.yml'))
expect(File).not_to exist(file_project_path('config/initializers/sidekiq.rb'))
expect(File).not_to exist(file_project_path('lib/tasks/sidekiq.rake'))

routes_file = content('config/routes.rb')
expect(routes_file).not_to match("^require 'sidekiq/web'")
expect(routes_file).not_to match("^require 'sidekiq/cron/web'")
end
end
8 changes: 5 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# frozen_string_literal: true

require 'bundler/setup'

Bundler.require(:default, :test)

require (Pathname.new(__FILE__).dirname + '../lib/cybele').expand_path
source_path = (Pathname.new(__FILE__).dirname + '../lib/cybele').expand_path
require source_path

Dir['./spec/support/**/*.rb'].each { |file| require file }

Expand All @@ -21,5 +24,4 @@

# Use the specified formatter
config.formatter = :documentation

end
end
Loading

0 comments on commit 74e8468

Please sign in to comment.