@@ -25,6 +25,27 @@ class New < Command
25
25
SKIP_ASSETS_DEFAULT = false
26
26
private_constant :SKIP_ASSETS_DEFAULT
27
27
28
+ # @since 2.2.0
29
+ # @api private
30
+ SKIP_DB_DEFAULT = false
31
+ private_constant :SKIP_DB_DEFAULT
32
+
33
+ # @since 2.2.0
34
+ # @api private
35
+ DATABASE_SQLITE = "sqlite"
36
+
37
+ # @since 2.2.0
38
+ # @api private
39
+ DATABASE_POSTGRES = "postgres"
40
+
41
+ # @since 2.2.0
42
+ # @api private
43
+ DATABASE_MYSQL = "mysql"
44
+
45
+ # @since 2.2.0
46
+ # @api private
47
+ SUPPORTED_DATABASES = [ DATABASE_SQLITE , DATABASE_POSTGRES , DATABASE_MYSQL ] . freeze
48
+
28
49
desc "Generate a new Hanami app"
29
50
30
51
# @since 2.0.0
@@ -47,14 +68,28 @@ class New < Command
47
68
# @api private
48
69
option :skip_assets , type : :boolean , required : false ,
49
70
default : SKIP_ASSETS_DEFAULT ,
50
- desc : "Skip assets"
71
+ desc : "Skip including hanami-assets"
72
+
73
+ # @since 2.2.0
74
+ # @api private
75
+ option :skip_db , type : :boolean , required : false ,
76
+ default : SKIP_DB_DEFAULT ,
77
+ desc : "Skip including hanami-db"
78
+
79
+ # @since 2.2.0
80
+ # @api private
81
+ option :database , type : :string , required : false ,
82
+ default : DATABASE_SQLITE ,
83
+ desc : "Database adapter (supported: sqlite, mysql, postgres)"
51
84
52
85
# rubocop:disable Layout/LineLength
53
86
example [
54
- "bookshelf # Generate a new Hanami app in `bookshelf/' directory, using `Bookshelf' namespace" ,
55
- "bookshelf --head # Generate a new Hanami app, using Hanami HEAD version from GitHub `main' branches" ,
56
- "bookshelf --skip-install # Generate a new Hanami app, but it skips Hanami installation" ,
57
- "bookshelf --skip-assets # Generate a new Hanami app without assets"
87
+ "bookshelf # Generate a new Hanami app in `bookshelf/' directory, using `Bookshelf' namespace" ,
88
+ "bookshelf --head # Generate a new Hanami app, using Hanami HEAD version from GitHub `main' branches" ,
89
+ "bookshelf --skip-install # Generate a new Hanami app, but it skips Hanami installation" ,
90
+ "bookshelf --skip-assets # Generate a new Hanami app without hanmai-assets" ,
91
+ "bookshelf --skip-db # Generate a new Hanami app without hanami-db" ,
92
+ "bookshelf --database={sqlite|postgres|mysql} # Generate a new Hanami app with a specified database (default: sqlite)" ,
58
93
]
59
94
# rubocop:enable Layout/LineLength
60
95
@@ -75,20 +110,36 @@ def initialize(
75
110
@system_call = system_call
76
111
end
77
112
78
- # rubocop:enable Metrics/ParameterLists
79
-
80
- # rubocop:disable Metrics/AbcSize
113
+ # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
81
114
82
115
# @since 2.0.0
83
116
# @api private
84
- def call ( app :, head : HEAD_DEFAULT , skip_install : SKIP_INSTALL_DEFAULT , skip_assets : SKIP_ASSETS_DEFAULT , **)
117
+ def call (
118
+ app :,
119
+ head : HEAD_DEFAULT ,
120
+ skip_install : SKIP_INSTALL_DEFAULT ,
121
+ skip_assets : SKIP_ASSETS_DEFAULT ,
122
+ skip_db : SKIP_DB_DEFAULT ,
123
+ database : nil
124
+ )
125
+ # rubocop:enable Metrics/ParameterLists
85
126
app = inflector . underscore ( app )
86
127
87
128
raise PathAlreadyExistsError . new ( app ) if fs . exist? ( app )
129
+ raise ConflictingOptionsError . new ( "--skip-db" , "--database" ) if skip_db && database
130
+
131
+ normalized_database ||= normalize_database ( database )
88
132
89
133
fs . mkdir ( app )
90
134
fs . chdir ( app ) do
91
- context = Generators ::Context . new ( inflector , app , head : head , skip_assets : skip_assets )
135
+ context = Generators ::Context . new (
136
+ inflector ,
137
+ app ,
138
+ head : head ,
139
+ skip_assets : skip_assets ,
140
+ skip_db : skip_db ,
141
+ database : normalized_database
142
+ )
92
143
generator . call ( app , context : context ) do
93
144
if skip_install
94
145
out . puts "Skipping installation, please enter `#{ app } ' directory and run `bundle exec hanami install'"
@@ -112,14 +163,27 @@ def call(app:, head: HEAD_DEFAULT, skip_install: SKIP_INSTALL_DEFAULT, skip_asse
112
163
end
113
164
end
114
165
end
115
- # rubocop:enable Metrics/AbcSize
166
+ # rubocop:enable Metrics/AbcSize, Metrics/PerceivedComplexity
116
167
117
168
private
118
169
119
170
attr_reader :bundler
120
171
attr_reader :generator
121
172
attr_reader :system_call
122
173
174
+ def normalize_database ( database )
175
+ case database
176
+ when nil , "sqlite" , "sqlite3"
177
+ DATABASE_SQLITE
178
+ when "mysql" , "mysql2"
179
+ DATABASE_MYSQL
180
+ when "postgres" , "postgresql" , "pg"
181
+ DATABASE_POSTGRES
182
+ else
183
+ raise DatabaseNotSupportedError . new ( database , SUPPORTED_DATABASES )
184
+ end
185
+ end
186
+
123
187
def run_install_command! ( head :)
124
188
head_flag = head ? " --head" : ""
125
189
bundler . exec ( "hanami install#{ head_flag } " ) . tap do |result |
0 commit comments