Skip to content

Commit

Permalink
Merged in feature/integrate_responder (pull request #4)
Browse files Browse the repository at this point in the history
KBP-121 integrate responder

Approved-by: Tayfun Öziş ERİKAN <[email protected]>
  • Loading branch information
İsmail Akbudak committed Sep 12, 2017
2 parents bd60c8e + 95f9a5c commit 5efa2dc
Show file tree
Hide file tree
Showing 17 changed files with 351 additions and 88 deletions.
11 changes: 6 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,12 @@ Metrics/BlockLength:
- 'Rakefile'
- '**/*.rake'
- 'spec/**/*.rb'
- 'lib/cybele/app_builder.rb'
- 'lib/cybele/generators/app_generator.rb'

Style/FrozenStringLiteralComment:
EnforcedStyle: when_needed

Style/IndentHeredoc:
Exclude:
- 'spec/features/cli_help_spec.rb'
12 changes: 12 additions & 0 deletions USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Description:
Cybele is a Rails template with Lab2023 standard
defaults.

For full details check our GitHub project:
https://github.com/kebab-project/cybele

Example:
cybele ~/Workplace/blog

This generates a Rails installation in ~/Workplace/blog configured
with our preferred defaults.
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
77 changes: 60 additions & 17 deletions lib/cybele/generators/app_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

module Cybele
class AppGenerator < Rails::Generators::AppGenerator
@options = nil

# Default settings
class_option :database,
type: :string,
Expand All @@ -21,7 +23,13 @@ class AppGenerator < Rails::Generators::AppGenerator
aliases: '-h',
group: :cybele,
desc: 'Show cybele help message and quit'
# Custom settings
# Ask cybele options
class_option :skip_ask,
type: :boolean,
aliases: nil,
default: true,
group: :cybele,
desc: 'Skip ask for cybele options. Default: skip'
class_option :skip_create_database,
type: :boolean,
aliases: nil,
Expand All @@ -35,48 +43,83 @@ class AppGenerator < Rails::Generators::AppGenerator
group: :cybele,
desc: 'Skip sidekiq integration. Default: don\'t skip'

def initialize(*args)
super
# Set options
@options = options.dup

return if @options[:skip_ask]

say 'Ask cybele options', :green
option_with_ask_limited(:database, DATABASES)
option_with_ask_yes(:skip_create_database)
option_with_ask_yes(:skip_sidekiq)
@options.freeze
end

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'
build :use_postgres_config_template if options[:database] == 'postgresql'
if options[:skip_create_database]
say 'don\'t create database'
else
build :create_database
if @options[:database] == 'postgresql'
say 'Set up postgresql template', :green
build :use_postgres_config_template
end

return if @options[:skip_create_database]
say 'Create database', :green
build :create_database
end

def setup_sidekiq
say 'Setting up sidekiq'
if options[:skip_sidekiq]
say 'don\'t use sidekiq'
else
build :use_sidekiq
end
return if @options[:skip_sidekiq]
say 'Setting up sidekiq', :green
build :configure_sidekiq
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

def self.banner
"cybele #{arguments.map(&:usage).join(' ')} [options]"
end

protected

def get_builder_class
Cybele::AppBuilder
end

private

def option_with_ask_yes(key)
say "==> #{key.to_s.humanize}", :green
say 'Type for answer yes: y|yes', :green
say 'Type for answer no: n|no|any character', :yellow

@options = @options.merge(key => yes?('Ans :', :green))
end

def option_with_ask_limited(key, limits)
@options = @options.merge(key => ask("#{key.to_s.humanize} :", limited_to: limits))
end
end
end
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
Loading

0 comments on commit 5efa2dc

Please sign in to comment.