1
- # encoding: UTF-8
2
- require "thor/group"
3
-
1
+ require 'thor/group'
4
2
module Natra
5
3
module Generators
6
4
class AppGenerator < Thor ::Group
7
5
include Thor ::Actions
6
+ desc 'Creates a new Sinatra application'
7
+ argument :name , type : :string , desc : 'The name of the new application'
8
+ class_option :capistrano , type : :boolean , desc : 'Include Capistrano configuration'
9
+ class_option :redis , type : :boolean , desc : 'Include Redis configuration'
10
+ class_option :rvm , type : :boolean , desc : 'Create .ruby-version (ruby-2.1.0) and .ruby-gemset'
11
+ class_option :bundle , type : :boolean , desc : 'Run bundle after generating the app'
12
+ class_option :git , type : :boolean , desc : 'Initialize a Git repository'
8
13
9
- desc "Creates a new Sinatra application"
10
- argument :name , :type => :string , :desc => "The name of the new application"
11
- class_option :capistrano , :type => :boolean , :desc => "Include Capistrano configuration"
12
- class_option :redis , :type => :boolean , :desc => "Include Redis configuration"
13
- class_option :rvm , :type => :boolean , :desc => "Create .ruby-version (ruby-2.1.0) and .ruby-gemset"
14
- class_option :bundle , :type => :boolean , :desc => "Run bundle after generating the app"
15
- class_option :git , :type => :boolean , :desc => "Initialize a Git repository"
16
-
17
- # Creates instance variables from options passed to natra.
18
14
def setup
19
15
@app_path = name . directory_name
20
16
@name = name . file_name
21
-
22
- options . each do |key , value |
23
- instance_variable_set "@#{ key . to_s } " . to_sym , value
24
- end
17
+ options . each { |key , value | instance_variable_set "@#{ key } " . to_sym , value }
25
18
end
26
19
27
20
def self . source_root
28
- File . expand_path ( File . join ( File . dirname ( __FILE__ ) , " templates" ) )
21
+ File . expand_path ( File . join ( File . dirname ( __FILE__ ) , ' templates' ) )
29
22
end
30
23
31
- # Create empty directories
32
24
def create_empty_directories
33
- %w{ config/initializers lib spec } . each do |dir |
34
- empty_directory File . join ( @app_path , dir )
35
- end
36
-
25
+ %w[ config/initializers lib spec ] . each { |dir | empty_directory File . join ( @app_path , dir ) }
37
26
empty_directory File . join ( @app_path , 'db/migrate' )
38
-
39
- create_file File . join ( @app_path , "lib" , ".keep" )
40
- template "config/environment.rb" , File . join ( @app_path , "config/environment.rb" )
27
+ create_file File . join ( @app_path , 'lib' , '.keep' )
28
+ template 'config/environment.rb' , File . join ( @app_path , 'config/environment.rb' )
41
29
end
42
-
30
+
43
31
def create_seeds_file
44
- create_file File . join ( @app_path , "db" , " seeds.rb" )
32
+ create_file File . join ( @app_path , 'db' , ' seeds.rb' )
45
33
end
46
34
47
35
def initialize_db
48
- copy_file ( 'bin/setup' , File . join ( @app_path , " bin/setup" ) )
36
+ copy_file ( 'bin/setup' , File . join ( @app_path , ' bin/setup' ) )
49
37
end
50
-
38
+
51
39
def uuid_setup
52
- template 'db/migrate/add_extensions.rb' , File . join ( @app_path , "db/migrate/#{ Time . now . strftime ( '%Y%m%d' ) } 0000_add_extensions.rb" )
40
+ template 'db/migrate/add_extensions.rb' , File . join ( @app_path , "db/migrate/#{ Time . now . strftime ( '%Y%m%d' ) } 0000_add_extensions.rb" )
53
41
end
54
-
42
+
55
43
def create_public_directory
56
- template " public/favicon.ico" , File . join ( @app_path , " public/favicon.ico" )
44
+ template ' public/favicon.ico' , File . join ( @app_path , ' public/favicon.ico' )
57
45
end
58
46
59
47
def create_app_directory
60
- %w{ app/controllers app/views app/models } . each do |dir |
61
- directory dir , File . join ( @app_path , dir )
62
- end
48
+ %w[ app/controllers app/views app/models ] . each { |dir | directory dir , File . join ( @app_path , dir ) }
63
49
end
64
50
65
51
def create_app_spec
66
- template " spec/application_controller_spec.rb" , File . join ( @app_path , " spec/application_controller_spec.rb" )
52
+ template ' spec/application_controller_spec.rb' , File . join ( @app_path , ' spec/application_controller_spec.rb' )
67
53
end
68
54
69
55
def create_spec_helper
70
- template " spec/spec_helper.rb" , File . join ( @app_path , " spec/spec_helper.rb" )
56
+ template ' spec/spec_helper.rb' , File . join ( @app_path , ' spec/spec_helper.rb' )
71
57
end
72
58
73
59
def create_config
74
- template " config.ru" , File . join ( @app_path , " config.ru" )
60
+ template ' config.ru' , File . join ( @app_path , ' config.ru' )
75
61
end
76
62
77
63
def create_gemfile
78
- template " Gemfile" , File . join ( @app_path , " Gemfile" )
64
+ template ' Gemfile' , File . join ( @app_path , ' Gemfile' )
79
65
end
80
66
81
67
def create_rakefile
82
- template " Rakefile" , File . join ( @app_path , " Rakefile" )
68
+ template ' Rakefile' , File . join ( @app_path , ' Rakefile' )
83
69
end
84
70
85
71
def create_readme
86
- template ( " README.md" , File . join ( @app_path , " README.md" ) )
72
+ template ( ' README.md' , File . join ( @app_path , ' README.md' ) )
87
73
end
88
74
89
75
def create_db_config
90
- template ( " config/db.yml" , File . join ( @app_path , " config/db.yml" ) )
76
+ template ( ' config/db.yml' , File . join ( @app_path , ' config/db.yml' ) )
91
77
end
92
78
93
79
def create_database_initializer
94
- template ( " config/initializers/database.rb" , File . join ( @app_path , " config/initializers/database.rb" ) )
80
+ template ( ' config/initializers/database.rb' , File . join ( @app_path , ' config/initializers/database.rb' ) )
95
81
end
96
82
97
83
def create_redis_config
98
- copy_file ( " config/redis.yml" , File . join ( @app_path , " config/redis.yml" ) ) if @redis
84
+ copy_file ( ' config/redis.yml' , File . join ( @app_path , ' config/redis.yml' ) ) if @redis
99
85
end
100
86
101
87
def create_redis_initializer
102
- template ( " config/initializers/redis.rb" , File . join ( @app_path , " config/initializers/redis.rb" ) ) if @redis
88
+ template ( ' config/initializers/redis.rb' , File . join ( @app_path , ' config/initializers/redis.rb' ) ) if @redis
103
89
end
104
90
105
91
def create_gitignore
106
- copy_file " gitignore" , File . join ( @app_path , '.gitignore' )
92
+ copy_file ' gitignore' , File . join ( @app_path , '.gitignore' )
107
93
end
94
+
108
95
def create_rspec
109
96
copy_file 'rspec' , File . join ( @app_path , '.rspec' )
110
97
end
98
+
111
99
def create_rubocop
112
- copy_file 'rubocop.yml' , File . join ( @app_path , '.rubocop.yml' )
100
+ copy_file 'rubocop.yml' , File . join ( @app_path , '.rubocop.yml' )
113
101
end
102
+
114
103
def create_docker
115
104
copy_file 'Dockerfile' , File . join ( @app_path , 'Dockerfile' )
116
105
end
106
+
117
107
def create_docker_compose
118
- template ( 'docker-compose.yml' , File . join ( @app_path , " docker-compose.yml" ) )
108
+ template ( 'docker-compose.yml' , File . join ( @app_path , ' docker-compose.yml' ) )
119
109
end
110
+
120
111
def create_guardfile
121
- copy_file 'Guardfile' , File . join ( @app_path , 'Guardfile' )
112
+ copy_file 'Guardfile' , File . join ( @app_path , 'Guardfile' )
122
113
end
114
+
123
115
def create_spec_support
124
- create_file File . join ( @app_path , " spec/support/" , " .keep" )
116
+ create_file File . join ( @app_path , ' spec/support/' , ' .keep' )
125
117
end
118
+
126
119
def create_secrets
127
- template ( 'secrets.env' , File . join ( @app_path , " secrets.env" ) )
120
+ template ( 'secrets.env' , File . join ( @app_path , ' secrets.env' ) )
128
121
end
129
-
122
+
130
123
def create_capistrano_config
131
- if @capistrano
132
- inside ( @app_path ) do
133
- run ( 'cap install' )
134
- end
135
- end
124
+ inside ( @app_path ) { run ( 'cap install' ) } if @capistrano
136
125
end
137
126
138
127
def create_rvm_gemset
@@ -146,15 +135,11 @@ def create_rvm_gemset
146
135
end
147
136
148
137
def initialize_git_repo
149
- inside ( @app_path ) do
150
- run ( 'git init .' ) if @git
151
- end
138
+ inside ( @app_path ) { run ( 'git init .' ) if @git }
152
139
end
153
140
154
141
def install_dependencies
155
- inside ( @app_path ) do
156
- run ( 'bundle' ) if @bundle
157
- end
142
+ inside ( @app_path ) { run ( 'bundle' ) if @bundle }
158
143
end
159
144
end
160
145
end
0 commit comments