hanami-cli 1.0.0.alpha1 → 2.0.0.alpha6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +50 -0
  3. data/.gitignore +4 -2
  4. data/.rspec +1 -0
  5. data/.rubocop.yml +28 -1
  6. data/CHANGELOG.md +32 -1
  7. data/CODE_OF_CONDUCT.md +84 -0
  8. data/Gemfile +9 -2
  9. data/LICENSE.txt +21 -0
  10. data/README.md +12 -605
  11. data/Rakefile +5 -10
  12. data/exe/hanami +10 -0
  13. data/hanami-cli.gemspec +22 -17
  14. data/lib/hanami/cli/bundler.rb +73 -0
  15. data/lib/hanami/cli/command.rb +14 -386
  16. data/lib/hanami/cli/command_line.rb +17 -0
  17. data/lib/hanami/cli/commands/application.rb +63 -0
  18. data/lib/hanami/cli/commands/db/utils/database.rb +122 -0
  19. data/lib/hanami/cli/commands/db/utils/database_config.rb +48 -0
  20. data/lib/hanami/cli/commands/db/utils/mysql.rb +27 -0
  21. data/lib/hanami/cli/commands/db/utils/postgres.rb +49 -0
  22. data/lib/hanami/cli/commands/db/utils/sqlite.rb +37 -0
  23. data/lib/hanami/cli/commands/gem/new.rb +77 -0
  24. data/lib/hanami/cli/commands/gem/version.rb +18 -0
  25. data/lib/hanami/cli/commands/gem.rb +21 -0
  26. data/lib/hanami/cli/commands/monolith/console.rb +49 -0
  27. data/lib/hanami/cli/commands/monolith/db/create.rb +25 -0
  28. data/lib/hanami/cli/commands/monolith/db/create_migration.rb +29 -0
  29. data/lib/hanami/cli/commands/monolith/db/drop.rb +25 -0
  30. data/lib/hanami/cli/commands/monolith/db/migrate.rb +40 -0
  31. data/lib/hanami/cli/commands/monolith/db/reset.rb +26 -0
  32. data/lib/hanami/cli/commands/monolith/db/rollback.rb +55 -0
  33. data/lib/hanami/cli/commands/monolith/db/sample_data.rb +40 -0
  34. data/lib/hanami/cli/commands/monolith/db/seed.rb +40 -0
  35. data/lib/hanami/cli/commands/monolith/db/setup.rb +24 -0
  36. data/lib/hanami/cli/commands/monolith/db/structure/dump.rb +25 -0
  37. data/lib/hanami/cli/commands/monolith/db/version.rb +26 -0
  38. data/lib/hanami/cli/commands/monolith/generate/action.rb +62 -0
  39. data/lib/hanami/cli/commands/monolith/generate/slice.rb +62 -0
  40. data/lib/hanami/cli/commands/monolith/generate.rb +14 -0
  41. data/lib/hanami/cli/commands/monolith/install.rb +16 -0
  42. data/lib/hanami/cli/commands/monolith/version.rb +18 -0
  43. data/lib/hanami/cli/commands/monolith.rb +55 -0
  44. data/lib/hanami/cli/commands.rb +26 -0
  45. data/lib/hanami/cli/error.rb +8 -0
  46. data/lib/hanami/cli/generators/context.rb +38 -0
  47. data/lib/hanami/cli/generators/gem/application/monolith/action.erb +21 -0
  48. data/lib/hanami/cli/generators/gem/application/monolith/application.erb +8 -0
  49. data/lib/hanami/cli/generators/gem/application/monolith/config_ru.erb +5 -0
  50. data/lib/hanami/cli/generators/gem/application/monolith/entities.erb +9 -0
  51. data/lib/hanami/cli/generators/gem/application/monolith/env.erb +0 -0
  52. data/lib/hanami/cli/generators/gem/application/monolith/functions.erb +13 -0
  53. data/lib/hanami/cli/generators/gem/application/monolith/gemfile.erb +19 -0
  54. data/lib/hanami/cli/generators/gem/application/monolith/keep.erb +0 -0
  55. data/lib/hanami/cli/generators/gem/application/monolith/operation.erb +18 -0
  56. data/lib/hanami/cli/generators/gem/application/monolith/rakefile.erb +3 -0
  57. data/lib/hanami/cli/generators/gem/application/monolith/readme.erb +1 -0
  58. data/lib/hanami/cli/generators/gem/application/monolith/repository.erb +13 -0
  59. data/lib/hanami/cli/generators/gem/application/monolith/routes.erb +4 -0
  60. data/lib/hanami/cli/generators/gem/application/monolith/settings.erb +6 -0
  61. data/lib/hanami/cli/generators/gem/application/monolith/types.erb +10 -0
  62. data/lib/hanami/cli/generators/gem/application/monolith/validation_contract.erb +14 -0
  63. data/lib/hanami/cli/generators/gem/application/monolith/view_context.erb +15 -0
  64. data/lib/hanami/cli/generators/gem/application/monolith.rb +83 -0
  65. data/lib/hanami/cli/generators/gem/application.rb +21 -0
  66. data/lib/hanami/cli/generators/monolith/action/action.erb +13 -0
  67. data/lib/hanami/cli/generators/monolith/action/template.erb +0 -0
  68. data/lib/hanami/cli/generators/monolith/action/template.html.erb +2 -0
  69. data/lib/hanami/cli/generators/monolith/action/view.erb +13 -0
  70. data/lib/hanami/cli/generators/monolith/action.rb +123 -0
  71. data/lib/hanami/cli/generators/monolith/action_context.rb +76 -0
  72. data/lib/hanami/cli/generators/monolith/slice/action.erb +9 -0
  73. data/lib/hanami/cli/generators/monolith/slice/entities.erb +9 -0
  74. data/lib/hanami/cli/generators/monolith/slice/keep.erb +0 -0
  75. data/lib/hanami/cli/generators/monolith/slice/repository.erb +10 -0
  76. data/lib/hanami/cli/generators/monolith/slice/routes.erb +2 -0
  77. data/lib/hanami/cli/generators/monolith/slice/view.erb +9 -0
  78. data/lib/hanami/cli/generators/monolith/slice.rb +56 -0
  79. data/lib/hanami/cli/generators/monolith/slice_context.rb +33 -0
  80. data/lib/hanami/cli/repl/core.rb +55 -0
  81. data/lib/hanami/cli/repl/irb.rb +38 -0
  82. data/lib/hanami/cli/repl/pry.rb +29 -0
  83. data/lib/hanami/cli/system_call.rb +51 -0
  84. data/lib/hanami/cli/url.rb +34 -0
  85. data/lib/hanami/cli/version.rb +2 -3
  86. data/lib/hanami/cli.rb +9 -122
  87. data/lib/hanami/console/context.rb +53 -0
  88. data/lib/hanami/console/plugins/slice_readers.rb +24 -0
  89. data/lib/hanami/rake_tasks.rb +52 -0
  90. metadata +135 -43
  91. data/.circleci/config.yml +0 -63
  92. data/.travis.yml +0 -22
  93. data/lib/hanami/cli/banner.rb +0 -129
  94. data/lib/hanami/cli/command_registry.rb +0 -215
  95. data/lib/hanami/cli/errors.rb +0 -46
  96. data/lib/hanami/cli/option.rb +0 -134
  97. data/lib/hanami/cli/parser.rb +0 -144
  98. data/lib/hanami/cli/program_name.rb +0 -21
  99. data/lib/hanami/cli/registry.rb +0 -330
  100. data/lib/hanami/cli/usage.rb +0 -91
  101. data/script/ci +0 -61
data/hanami-cli.gemspec CHANGED
@@ -1,36 +1,41 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- lib = File.expand_path("../lib", __FILE__)
4
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
- require "hanami/cli/version"
3
+ require_relative "lib/hanami/cli/version"
6
4
 
7
5
  Gem::Specification.new do |spec|
8
6
  spec.name = "hanami-cli"
9
7
  spec.version = Hanami::CLI::VERSION
10
8
  spec.authors = ["Luca Guidi"]
11
9
  spec.email = ["me@lucaguidi.com"]
12
- spec.licenses = ["MIT"]
13
10
 
14
11
  spec.summary = "Hanami CLI"
15
- spec.description = "Hanami framework to build command line interfaces with Ruby"
16
- spec.homepage = "http://hanamirb.org"
12
+ spec.description = "Hanami command line"
13
+ spec.homepage = "https://hanamirb.org"
14
+ spec.license = "MIT"
17
15
 
18
16
  spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
19
  spec.metadata["source_code_uri"] = "https://github.com/hanami/cli"
20
+ spec.metadata["changelog_uri"] = "https://github.com/hanami/cli/blob/master/CHANGELOG.md"
20
21
 
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
26
+ end
21
27
  spec.bindir = "exe"
22
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
23
29
  spec.require_paths = ["lib"]
24
- spec.required_ruby_version = ">= 2.5.0"
25
-
26
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
- f.match(%r{^(test|spec|features)/})
28
- end
30
+ spec.metadata["rubygems_mfa_required"] = "true"
31
+ spec.required_ruby_version = ">= 3.0"
29
32
 
30
- spec.add_dependency "hanami-utils", "~> 2.0.alpha"
31
- spec.add_dependency "concurrent-ruby", "~> 1.0"
33
+ spec.add_dependency "bundler", "~> 2.1"
34
+ spec.add_dependency "rake", "~> 13.0"
35
+ spec.add_dependency "dry-cli", "~> 0.6"
36
+ spec.add_dependency "dry-files", "~> 0.1"
37
+ spec.add_dependency "dry-inflector", "~> 0.2"
32
38
 
33
- spec.add_development_dependency "bundler", ">= 1.6", "< 3"
34
- spec.add_development_dependency "rake", "~> 12.0"
35
- spec.add_development_dependency "rspec", "~> 3.7"
39
+ spec.add_development_dependency "rspec", "~> 3.9"
40
+ spec.add_development_dependency "rubocop", "~> 1.0"
36
41
  end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler"
4
+ require "open3"
5
+ require "etc"
6
+ require "dry/files"
7
+ require_relative "./system_call"
8
+
9
+ module Hanami
10
+ module CLI
11
+ class Bundler
12
+ BUNDLE_GEMFILE = "BUNDLE_GEMFILE"
13
+ private_constant :BUNDLE_GEMFILE
14
+
15
+ DEFAULT_GEMFILE_PATH = "Gemfile"
16
+ private_constant :DEFAULT_GEMFILE_PATH
17
+
18
+ def self.require(*groups)
19
+ return unless File.exist?(ENV[BUNDLE_GEMFILE] || DEFAULT_GEMFILE_PATH)
20
+
21
+ ::Bundler.require(*groups)
22
+ end
23
+
24
+ def initialize(fs: Dry::Files.new, system_call: SystemCall.new)
25
+ @fs = fs
26
+ @system_call = system_call
27
+ end
28
+
29
+ def install
30
+ parallelism_level = Etc.nprocessors
31
+ bundle "install --jobs=#{parallelism_level} --quiet --no-color"
32
+ end
33
+
34
+ def install!
35
+ install.tap do |result|
36
+ raise "Bundle install failed\n\n\n#{result.err.inspect}" unless result.successful?
37
+ end
38
+ end
39
+
40
+ def exec(cmd, env: nil, &blk)
41
+ bundle("exec #{cmd}", env: env, &blk)
42
+ end
43
+
44
+ def bundle(cmd, env: nil, &blk)
45
+ bundle_bin = which("bundle")
46
+ hanami_env = "HANAMI_ENV=#{env} " unless env.nil?
47
+
48
+ system_call.call("#{hanami_env}#{bundle_bin} #{cmd}",
49
+ env: {BUNDLE_GEMFILE => fs.expand_path(DEFAULT_GEMFILE_PATH)}, &blk)
50
+ end
51
+
52
+ private
53
+
54
+ attr_reader :fs
55
+
56
+ attr_reader :system_call
57
+
58
+ # Adapted from https://stackoverflow.com/a/5471032/498386
59
+ def which(cmd)
60
+ exts = ENV["PATHEXT"] ? ENV["PATHEXT"].split(";") : [""]
61
+
62
+ ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
63
+ exts.each do |ext|
64
+ exe = fs.join(path, "#{cmd}#{ext}")
65
+ return exe if fs.executable?(exe) && !fs.directory?(exe)
66
+ end
67
+ end
68
+
69
+ nil
70
+ end
71
+ end
72
+ end
73
+ end
@@ -1,398 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "forwardable"
4
- require "concurrent/array"
5
- require "hanami/cli/option"
6
- require "hanami/utils/class_attribute"
3
+ require "dry/cli"
4
+ require "dry/files"
5
+ require "dry/inflector"
7
6
 
8
7
  module Hanami
9
- class CLI
10
- # Base class for commands
11
- #
12
- # @since 0.1.0
13
- class Command
14
- # @since 0.1.0
15
- # @api private
16
- def self.inherited(base)
17
- super
18
- base.extend ClassMethods
8
+ module CLI
9
+ class Command < Dry::CLI::Command
10
+ def initialize(out: $stdout, fs: Dry::Files.new, inflector: Dry::Inflector.new)
11
+ super()
12
+ @out = out
13
+ @fs = fs
14
+ @inflector = inflector
19
15
  end
20
16
 
21
- # @since 0.1.0
22
- # @api private
23
- module ClassMethods
24
- # @since 0.1.0
25
- # @api private
26
- #
27
- # rubocop:disable Metrics/MethodLength
28
- def self.extended(base)
29
- super
30
- return unless extend?(base)
17
+ private
31
18
 
32
- base.class_eval do
33
- include Utils::ClassAttribute
19
+ attr_reader :out
34
20
 
35
- class_attribute :description
36
- self.description = nil
21
+ attr_reader :fs
37
22
 
38
- class_attribute :examples
39
- self.examples = Concurrent::Array.new
40
-
41
- class_attribute :arguments
42
- self.arguments = Concurrent::Array.new
43
-
44
- class_attribute :options
45
- self.options = Concurrent::Array.new
46
- end
47
- end
48
- # rubocop:enable Metrics/MethodLength
49
-
50
- # Only add class attributes if a command is inheriting directly from `Hanami::CLI::Command`.
51
- # In this way, its subclasses can inherit arguments/options from the parent class.
52
- #
53
- # @return [TrueClass,FalseClass] the result of the check
54
- #
55
- # @since 1.0.0
56
- # @api private
57
- #
58
- # @example
59
- # # For this class `extend?` will return `true`
60
- # class Server < Hanami::CLI::Command
61
- # option :engine
62
- #
63
- # def call(**options)
64
- # # start the server
65
- # end
66
- # end
67
- #
68
- # # For this class `extend?` will return `false`
69
- # class ServerReloader < Server
70
- # option :reload, type: :boolean, default: true
71
- #
72
- # def call(**options)
73
- # reload = options.fetch(:reload)
74
- # super unless reload
75
- #
76
- # # activate reloading
77
- # end
78
- # end
79
- def self.extend?(base)
80
- base.superclass == Hanami::CLI::Command
81
- end
82
- end
83
-
84
- # Set the description of the command
85
- #
86
- # @param description [String] the description
87
- #
88
- # @since 0.1.0
89
- #
90
- # @example
91
- # require "hanami/cli"
92
- #
93
- # class Echo < Hanami::CLI::Command
94
- # desc "Prints given input"
95
- #
96
- # def call(*)
97
- # # ...
98
- # end
99
- # end
100
- def self.desc(description)
101
- self.description = description
102
- end
103
-
104
- # Describe the usage of the command
105
- #
106
- # @param examples [Array<String>] one or more examples
107
- #
108
- # @since 0.1.0
109
- #
110
- # @example
111
- # require "hanami/cli"
112
- #
113
- # class Server < Hanami::CLI::Command
114
- # example [
115
- # " # Basic usage (it uses the bundled server engine)",
116
- # "--server=webrick # Force `webrick` server engine",
117
- # "--host=0.0.0.0 # Bind to a host",
118
- # "--port=2306 # Bind to a port",
119
- # "--no-code-reloading # Disable code reloading"
120
- # ]
121
- #
122
- # def call(*)
123
- # # ...
124
- # end
125
- # end
126
- #
127
- # # $ foo server --help
128
- # # # ...
129
- # #
130
- # # Examples:
131
- # # foo server # Basic usage (it uses the bundled server engine)
132
- # # foo server --server=webrick # Force `webrick` server engine
133
- # # foo server --host=0.0.0.0 # Bind to a host
134
- # # foo server --port=2306 # Bind to a port
135
- # # foo server --no-code-reloading # Disable code reloading
136
- def self.example(*examples)
137
- self.examples += examples.flatten
138
- end
139
-
140
- # Specify an argument
141
- #
142
- # @param name [Symbol] the argument name
143
- # @param options [Hash] a set of options
144
- #
145
- # @since 0.1.0
146
- #
147
- # @example Optional argument
148
- # require "hanami/cli"
149
- #
150
- # class Hello < Hanami::CLI::Command
151
- # argument :name
152
- #
153
- # def call(name: nil, **)
154
- # if name.nil?
155
- # puts "Hello, stranger"
156
- # else
157
- # puts "Hello, #{name}"
158
- # end
159
- # end
160
- # end
161
- #
162
- # # $ foo hello
163
- # # Hello, stranger
164
- #
165
- # # $ foo hello Luca
166
- # # Hello, Luca
167
- #
168
- # @example Required argument
169
- # require "hanami/cli"
170
- #
171
- # class Hello < Hanami::CLI::Command
172
- # argument :name, required: true
173
- #
174
- # def call(name:, **)
175
- # puts "Hello, #{name}"
176
- # end
177
- # end
178
- #
179
- # # $ foo hello Luca
180
- # # Hello, Luca
181
- #
182
- # # $ foo hello
183
- # # ERROR: "foo hello" was called with no arguments
184
- # # Usage: "foo hello NAME"
185
- #
186
- # @example Multiple arguments
187
- # require "hanami/cli"
188
- #
189
- # module Generate
190
- # class Action < Hanami::CLI::Command
191
- # argument :app, required: true
192
- # argument :action, required: true
193
- #
194
- # def call(app:, action:, **)
195
- # puts "Generating action: #{action} for app: #{app}"
196
- # end
197
- # end
198
- # end
199
- #
200
- # # $ foo generate action web home
201
- # # Generating action: home for app: web
202
- #
203
- # # $ foo generate action
204
- # # ERROR: "foo generate action" was called with no arguments
205
- # # Usage: "foo generate action APP ACTION"
206
- #
207
- # @example Description
208
- # require "hanami/cli"
209
- #
210
- # class Hello < Hanami::CLI::Command
211
- # argument :name, desc: "The name of the person to greet"
212
- #
213
- # def call(name: nil, **)
214
- # # ...
215
- # end
216
- # end
217
- #
218
- # # $ foo hello --help
219
- # # Command:
220
- # # foo hello
221
- # #
222
- # # Usage:
223
- # # foo hello [NAME]
224
- # #
225
- # # Arguments:
226
- # # NAME # The name of the person to greet
227
- # #
228
- # # Options:
229
- # # --help, -h # Print this help
230
- def self.argument(name, options = {})
231
- arguments << Argument.new(name, options)
232
- end
233
-
234
- # Command line option (aka optional argument)
235
- #
236
- # @param name [Symbol] the param name
237
- # @param options [Hash] a set of options
238
- #
239
- # @since 0.1.0
240
- #
241
- # @example Basic usage
242
- # require "hanami/cli"
243
- #
244
- # class Console < Hanami::CLI::Command
245
- # param :engine
246
- #
247
- # def call(engine: nil, **)
248
- # puts "starting console (engine: #{engine || :irb})"
249
- # end
250
- # end
251
- #
252
- # # $ foo console
253
- # # starting console (engine: irb)
254
- #
255
- # # $ foo console --engine=pry
256
- # # starting console (engine: pry)
257
- #
258
- # @example List values
259
- # require "hanami/cli"
260
- #
261
- # class Console < Hanami::CLI::Command
262
- # param :engine, values: %w(irb pry ripl)
263
- #
264
- # def call(engine: nil, **)
265
- # puts "starting console (engine: #{engine || :irb})"
266
- # end
267
- # end
268
- #
269
- # # $ foo console
270
- # # starting console (engine: irb)
271
- #
272
- # # $ foo console --engine=pry
273
- # # starting console (engine: pry)
274
- #
275
- # # $ foo console --engine=foo
276
- # # Error: Invalid param provided
277
- #
278
- # @example Description
279
- # require "hanami/cli"
280
- #
281
- # class Console < Hanami::CLI::Command
282
- # param :engine, desc: "Force a console engine"
283
- #
284
- # def call(engine: nil, **)
285
- # # ...
286
- # end
287
- # end
288
- #
289
- # # $ foo console --help
290
- # # # ...
291
- # #
292
- # # Options:
293
- # # --engine=VALUE # Force a console engine: (irb/pry/ripl)
294
- # # --help, -h # Print this help
295
- #
296
- # @example Boolean
297
- # require "hanami/cli"
298
- #
299
- # class Server < Hanami::CLI::Command
300
- # param :code_reloading, type: :boolean, default: true
301
- #
302
- # def call(code_reloading:, **)
303
- # puts "staring server (code reloading: #{code_reloading})"
304
- # end
305
- # end
306
- #
307
- # # $ foo server
308
- # # starting server (code reloading: true)
309
- #
310
- # # $ foo server --no-code-reloading
311
- # # starting server (code reloading: false)
312
- #
313
- # # $ foo server --help
314
- # # # ...
315
- # #
316
- # # Options:
317
- # # --[no]-code-reloading
318
- #
319
- # @example Aliases
320
- # require "hanami/cli"
321
- #
322
- # class Server < Hanami::CLI::Command
323
- # param :port, aliases: ["-p"]
324
- #
325
- # def call(options)
326
- # puts "staring server (port: #{options.fetch(:port, 2300)})"
327
- # end
328
- # end
329
- #
330
- # # $ foo server
331
- # # starting server (port: 2300)
332
- #
333
- # # $ foo server --port=2306
334
- # # starting server (port: 2306)
335
- #
336
- # # $ foo server -p 2306
337
- # # starting server (port: 2306)
338
- #
339
- # # $ foo server --help
340
- # # # ...
341
- # #
342
- # # Options:
343
- # # --port=VALUE, -p VALUE
344
- def self.option(name, options = {})
345
- self.options << Option.new(name, options)
346
- end
347
-
348
- # @since 0.1.0
349
- # @api private
350
- def self.params
351
- (arguments + options).uniq
352
- end
353
-
354
- # @since 0.1.0
355
- # @api private
356
- def self.default_params
357
- params.each_with_object({}) do |param, result|
358
- result[param.name] = param.default unless param.default.nil?
359
- end
360
- end
361
-
362
- # @since 0.1.0
363
- # @api private
364
- def self.required_arguments
365
- arguments.select(&:required?)
366
- end
367
-
368
- # @since 0.1.0
369
- # @api private
370
- def self.optional_arguments
371
- arguments.reject(&:required?)
372
- end
373
-
374
- extend Forwardable
375
-
376
- delegate %i[
377
- description
378
- examples
379
- arguments
380
- options
381
- params
382
- default_params
383
- required_arguments
384
- optional_arguments
385
- ] => "self.class"
386
-
387
- # @since 0.1.0
388
- # @api private
389
- attr_reader :command_name
390
-
391
- # @since 0.1.0
392
- # @api private
393
- def initialize(command_name:, **)
394
- @command_name = command_name
395
- end
23
+ attr_reader :inflector
396
24
  end
397
25
  end
398
26
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "./bundler"
4
+
5
+ module Hanami
6
+ module CLI
7
+ class CommandLine
8
+ def initialize(bundler: CLI::Bundler.new)
9
+ @bundler = bundler
10
+ end
11
+
12
+ def call(command)
13
+ @bundler.exec(command)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/files"
4
+
5
+ require_relative "db/utils/database"
6
+
7
+ module Hanami
8
+ module CLI
9
+ module Commands
10
+ class Application < Hanami::CLI::Command
11
+ module Environment
12
+ def call(**opts)
13
+ env = opts[:env]
14
+ ENV["HANAMI_ENV"] = env.to_s
15
+ super(**opts)
16
+ end
17
+ end
18
+
19
+ def self.inherited(klass)
20
+ super
21
+ klass.option(:env, required: false, default: "development", desc: "Application's environment")
22
+ klass.prepend(Environment)
23
+ end
24
+
25
+ def application
26
+ @application ||=
27
+ begin
28
+ require "hanami/prepare"
29
+ Hanami.application
30
+ end
31
+ end
32
+
33
+ def run_command(klass, *args)
34
+ klass.new(
35
+ out: out,
36
+ inflector: application.inflector,
37
+ fs: Dry::Files
38
+ ).call(*args)
39
+ end
40
+
41
+ def measure(desc)
42
+ start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
43
+ result = yield
44
+ stop = Process.clock_gettime(Process::CLOCK_MONOTONIC)
45
+
46
+ if result
47
+ out.puts "=> #{desc} in #{(stop - start).round(4)}s"
48
+ else
49
+ out.puts "!!! => #{desc.inspect} FAILED"
50
+ end
51
+ end
52
+
53
+ def database
54
+ @database ||= Commands::DB::Utils::Database[application]
55
+ end
56
+
57
+ def database_config
58
+ database.config
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end