Skip to content

Commit 5efa2dc

Browse files
author
İsmail Akbudak
committed
Merged in feature/integrate_responder (pull request #4)
KBP-121 integrate responder Approved-by: Tayfun Öziş ERİKAN <[email protected]>
2 parents bd60c8e + 95f9a5c commit 5efa2dc

17 files changed

+351
-88
lines changed

.rubocop.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ AllCops:
33
- 'Rakefile'
44
- 'Gemfile'
55
Excludes:
6-
- bin/**
7-
- spec/**/**/**/**
8-
- test/**/**/**/**
96
- tmp/**/**/**/**
107

118
Documentation:
@@ -24,8 +21,6 @@ Style/AccessorMethodName:
2421
Metrics/MethodLength:
2522
CountComments: false
2623
Max: 15
27-
Exclude:
28-
- 'lib/cybele/app_builder.rb'
2924

3025
Metrics/BlockLength:
3126
CountComments: false
@@ -34,6 +29,12 @@ Metrics/BlockLength:
3429
- 'Rakefile'
3530
- '**/*.rake'
3631
- 'spec/**/*.rb'
32+
- 'lib/cybele/app_builder.rb'
33+
- 'lib/cybele/generators/app_generator.rb'
3734

3835
Style/FrozenStringLiteralComment:
3936
EnforcedStyle: when_needed
37+
38+
Style/IndentHeredoc:
39+
Exclude:
40+
- 'spec/features/cli_help_spec.rb'

USAGE

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Description:
2+
Cybele is a Rails template with Lab2023 standard
3+
defaults.
4+
5+
For full details check our GitHub project:
6+
https://github.com/kebab-project/cybele
7+
8+
Example:
9+
cybele ~/Workplace/blog
10+
11+
This generates a Rails installation in ~/Workplace/blog configured
12+
with our preferred defaults.

bin/build_app

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ echo "Build cybele gem"
88
echo "Remove dummy_app"
99
rm -rf tmp/dummy_app
1010

11+
# Disable spring
12+
export DISABLE_SPRING=1
13+
# or you can run this command
14+
# ps ax | grep spring | grep dummy_app | cut -f1 -d' ' | xargs kill
15+
1116
# Create dummy_app
1217
echo "Create dummy_app"
1318
cybele tmp/dummy_app --skip-create-database

bin/cybele

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
24
require 'pathname'
35

4-
require File.expand_path(File.join('..', 'lib', 'cybele', 'generators', 'app_generator'), File.dirname(__FILE__))
5-
require File.expand_path(File.join('..', 'lib', 'cybele', 'app_builder'), File.dirname(__FILE__))
6-
require File.expand_path(File.join('..', 'lib', 'cybele', 'version'), File.dirname(__FILE__))
6+
source_path = (Pathname.new(__FILE__).dirname + '../lib').expand_path
7+
$LOAD_PATH << source_path
8+
9+
require 'cybele'
710

811
if ARGV.empty?
912
puts 'Please provide a path for the new application'
1013
puts
1114
puts 'See --help for more info'
1215
exit 0
13-
elsif ['-v', '--version'].include? ARGV[0]
16+
elsif %w[-v --version].include? ARGV[0]
1417
puts Cybele::VERSION
1518
exit 0
1619
end
1720

18-
templates_root = File.expand_path(File.join('..', 'templates'), File.dirname(__FILE__))
19-
Cybele::AppGenerator.source_root templates_root
20-
Cybele::AppGenerator.source_paths << Rails::Generators::AppGenerator.source_root << templates_root
21-
Cybele::AppGenerator.start
21+
root = File.expand_path(File.join('..', 'templates'), File.dirname(__FILE__))
22+
Cybele::AppGenerator.source_root root
23+
Cybele::AppGenerator.source_paths << Rails::Generators::AppGenerator.source_root << root
24+
Cybele::AppGenerator.start

lib/cybele.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22

33
require 'cybele/version'
44
require 'cybele/generators/app_generator'
5+
require 'cybele/helpers'
6+
require 'cybele/helpers/sidekiq'
7+
require 'cybele/helpers/responders'
58
require 'cybele/app_builder'

lib/cybele/app_builder.rb

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22

33
module Cybele
44
class AppBuilder < Rails::AppBuilder
5+
include Cybele::Helpers
6+
include Cybele::Helpers::Sidekiq
7+
include Cybele::Helpers::Responders
8+
59
def readme
6-
template 'README.md.erb', 'README.md', force: true
10+
template 'README.md.erb',
11+
'README.md',
12+
force: true
713
end
814

915
def remove_readme_rdoc
10-
remove_file 'README.rdoc', force: true
16+
remove_file 'README.rdoc',
17+
force: true
1118
end
1219

1320
def add_editor_config
@@ -27,42 +34,5 @@ def use_postgres_config_template
2734
def create_database
2835
bundle_command 'exec rake db:create db:migrate'
2936
end
30-
31-
def use_sidekiq
32-
# Add gems
33-
append_file('Gemfile', template_content('sidekiq_Gemfile.erb'))
34-
35-
# Initialize files
36-
template 'sidekiq.rb.erb',
37-
'config/initializers/sidekiq.rb',
38-
force: true
39-
# Add tasks
40-
template 'sidekiq.rake.erb',
41-
'lib/tasks/sidekiq.rake',
42-
force: true
43-
44-
# Add sidekiq.yml
45-
template 'sidekiq.yml.erb',
46-
'config/sidekiq.yml',
47-
force: true
48-
49-
# Add sidekiq_schedule.yml
50-
template 'sidekiq_schedule.yml.erb',
51-
'config/sidekiq_schedule.yml',
52-
force: true
53-
54-
# Add sidekiq routes to routes
55-
prepend_file 'config/routes.rb',
56-
template_content('sidekiq_routes_require.erb')
57-
inject_into_file 'config/routes.rb',
58-
template_content('sidekiq_routes_mount.erb'),
59-
after: 'Rails.application.routes.draw do'
60-
end
61-
62-
private
63-
64-
def template_content(file)
65-
File.read(File.expand_path(find_in_source_paths(file)))
66-
end
6737
end
6838
end

lib/cybele/generators/app_generator.rb

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
module Cybele
77
class AppGenerator < Rails::Generators::AppGenerator
8+
@options = nil
9+
810
# Default settings
911
class_option :database,
1012
type: :string,
@@ -21,7 +23,13 @@ class AppGenerator < Rails::Generators::AppGenerator
2123
aliases: '-h',
2224
group: :cybele,
2325
desc: 'Show cybele help message and quit'
24-
# Custom settings
26+
# Ask cybele options
27+
class_option :skip_ask,
28+
type: :boolean,
29+
aliases: nil,
30+
default: true,
31+
group: :cybele,
32+
desc: 'Skip ask for cybele options. Default: skip'
2533
class_option :skip_create_database,
2634
type: :boolean,
2735
aliases: nil,
@@ -35,48 +43,83 @@ class AppGenerator < Rails::Generators::AppGenerator
3543
group: :cybele,
3644
desc: 'Skip sidekiq integration. Default: don\'t skip'
3745

46+
def initialize(*args)
47+
super
48+
# Set options
49+
@options = options.dup
50+
51+
return if @options[:skip_ask]
52+
53+
say 'Ask cybele options', :green
54+
option_with_ask_limited(:database, DATABASES)
55+
option_with_ask_yes(:skip_create_database)
56+
option_with_ask_yes(:skip_sidekiq)
57+
@options.freeze
58+
end
59+
3860
def setup_editor_config
39-
say 'Add .editor_config file'
61+
say 'Add .editor_config file', :green
4062
build :add_editor_config
4163
end
4264

4365
def setup_ruby_version
44-
say 'Add .ruby-version file'
66+
say 'Add .ruby-version file', :green
4567
build :add_ruby_version
4668
end
4769

4870
def remove_files_we_dont_need
49-
say 'Remove files we don\'t need'
71+
say 'Remove files we don\'t need', :green
5072
build :remove_readme_rdoc
5173
end
5274

5375
def setup_database
54-
say 'Setting up database'
55-
build :use_postgres_config_template if options[:database] == 'postgresql'
56-
if options[:skip_create_database]
57-
say 'don\'t create database'
58-
else
59-
build :create_database
76+
if @options[:database] == 'postgresql'
77+
say 'Set up postgresql template', :green
78+
build :use_postgres_config_template
6079
end
80+
81+
return if @options[:skip_create_database]
82+
say 'Create database', :green
83+
build :create_database
6184
end
6285

6386
def setup_sidekiq
64-
say 'Setting up sidekiq'
65-
if options[:skip_sidekiq]
66-
say 'don\'t use sidekiq'
67-
else
68-
build :use_sidekiq
69-
end
87+
return if @options[:skip_sidekiq]
88+
say 'Setting up sidekiq', :green
89+
build :configure_sidekiq
90+
end
91+
92+
def setup_responders
93+
say 'Setting up responders', :green
94+
build :configure_responders
7095
end
7196

7297
def goodbye
73-
say 'Congratulations! That\'s all...'
98+
say 'Congratulations! That\'s all...', :green
99+
end
100+
101+
def self.banner
102+
"cybele #{arguments.map(&:usage).join(' ')} [options]"
74103
end
75104

76105
protected
77106

78107
def get_builder_class
79108
Cybele::AppBuilder
80109
end
110+
111+
private
112+
113+
def option_with_ask_yes(key)
114+
say "==> #{key.to_s.humanize}", :green
115+
say 'Type for answer yes: y|yes', :green
116+
say 'Type for answer no: n|no|any character', :yellow
117+
118+
@options = @options.merge(key => yes?('Ans :', :green))
119+
end
120+
121+
def option_with_ask_limited(key, limits)
122+
@options = @options.merge(key => ask("#{key.to_s.humanize} :", limited_to: limits))
123+
end
81124
end
82125
end

lib/cybele/helpers.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
module Cybele
4+
module Helpers
5+
private
6+
7+
def replace_in_file(relative_path, find, replace)
8+
path = File.join(destination_root, relative_path)
9+
contents = IO.read(path)
10+
unless contents.gsub!(find, replace)
11+
raise "#{find.inspect} not found in #{relative_path}"
12+
end
13+
File.open(path, 'w') { |file| file.write(contents) }
14+
end
15+
16+
def template_content(file)
17+
File.read(File.expand_path(find_in_source_paths(file)))
18+
end
19+
end
20+
end

lib/cybele/helpers/responders.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
module Cybele
4+
module Helpers
5+
module Responders
6+
def configure_responders
7+
# Add gems
8+
append_file('Gemfile', template_content('responders_Gemfile.erb'))
9+
run_bundle
10+
11+
# Add initializers
12+
bundle_command 'exec rails generate responders:install'
13+
14+
# Add js and json to respond :html
15+
replace_in_file 'app/controllers/application_controller.rb',
16+
'respond_to :html',
17+
'respond_to :html, :js, :json'
18+
replace_in_file 'app/controllers/application_controller.rb',
19+
'require "application_responder"',
20+
"require 'application_responder'"
21+
22+
# Remove comments in locale/responders.yml
23+
uncomment_lines 'config/locales/responders.en.yml', /alert:/
24+
end
25+
end
26+
end
27+
end

lib/cybele/helpers/sidekiq.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
module Cybele
4+
module Helpers
5+
module Sidekiq
6+
def configure_sidekiq
7+
# Add gems
8+
append_file('Gemfile', template_content('sidekiq_Gemfile.erb'))
9+
10+
create_sidekiq_files
11+
12+
# Add sidekiq routes to routes
13+
prepend_file 'config/routes.rb',
14+
template_content('sidekiq_routes_require.erb')
15+
inject_into_file 'config/routes.rb',
16+
template_content('sidekiq_routes_mount.erb'),
17+
after: 'Rails.application.routes.draw do'
18+
end
19+
20+
private
21+
22+
def create_sidekiq_files
23+
# Initialize files
24+
template 'sidekiq.rb.erb',
25+
'config/initializers/sidekiq.rb',
26+
force: true
27+
# Add tasks
28+
template 'sidekiq.rake.erb',
29+
'lib/tasks/sidekiq.rake',
30+
force: true
31+
32+
# Add sidekiq.yml
33+
template 'sidekiq.yml.erb',
34+
'config/sidekiq.yml',
35+
force: true
36+
37+
# Add sidekiq_schedule.yml
38+
template 'sidekiq_schedule.yml.erb',
39+
'config/sidekiq_schedule.yml',
40+
force: true
41+
end
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)