Skip to content

Commit 55fd722

Browse files
committed
Invoke commands to combine
1 parent fac7e9d commit 55fd722

File tree

5 files changed

+30
-1
lines changed

5 files changed

+30
-1
lines changed

dry-cli.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
2727

2828
# to update dependencies edit project.yml
2929
spec.add_runtime_dependency "concurrent-ruby", "~> 1.0"
30+
spec.add_runtime_dependency "dry-effects"
3031

3132
spec.add_development_dependency "bundler", ">= 1.6", "< 3"
3233
spec.add_development_dependency "rake", "~> 13.0"

lib/dry/cli.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
require 'dry/effects'
23

34
# Dry
45
#
@@ -17,6 +18,8 @@ class CLI
1718
require 'dry/cli/banner'
1819
require 'dry/cli/inflector'
1920

21+
include Dry::Effects::Handler.Reader(:cli)
22+
2023
# Check if command
2124
#
2225
# @param command [Object] the command to check
@@ -101,7 +104,7 @@ def perform_registry(arguments, out)
101104
command, args = parse(result.command, result.arguments, result.names, out)
102105

103106
result.before_callbacks.run(command, args)
104-
command.call(**args)
107+
with_cli(self) { command.call(**args) }
105108
result.after_callbacks.run(command, args)
106109
else
107110
usage(result, out)

lib/dry/cli/command.rb

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
require 'forwardable'
4+
require 'open3'
5+
require 'dry/cli/program_name'
46
require 'concurrent/array'
57
require 'dry/cli/option'
68

@@ -15,6 +17,7 @@ class Command
1517
def self.inherited(base)
1618
super
1719
base.extend ClassMethods
20+
base.include Dry::Effects.Reader(:cli)
1821
end
1922

2023
# @since 0.1.0
@@ -352,6 +355,11 @@ def self.optional_arguments
352355
required_arguments
353356
optional_arguments
354357
] => 'self.class'
358+
359+
def invoke(command, *arguments)
360+
command = [command] unless command.is_a?(Array)
361+
cli.call(arguments: [*command, *arguments])
362+
end
355363
end
356364
end
357365
end

spec/integration/commands_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@
1111
end
1212
end
1313
end
14+
15+
it 'calls other commands from inside call' do
16+
output = `foo g scaffold Test`
17+
expect(output).to eq("generate model - model: Test\ngenerate secret - app: \n")
18+
end
1419
end

spec/support/fixtures/foo

+12
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,17 @@ module Foo
264264
puts "generate secret - app: #{app}"
265265
end
266266
end
267+
268+
class Scaffold < Dry::CLI::Command
269+
desc 'Generate scaffold'
270+
271+
argument :model, required: true, desc: 'Scaffold for model'
272+
273+
def call(model:)
274+
invoke(%w[g model], model)
275+
invoke(%w[g secret])
276+
end
277+
end
267278
end
268279

269280
class New < Dry::CLI::Command
@@ -427,6 +438,7 @@ Foo::CLI::Commands.register 'generate', aliases: ['g'] do |prefix|
427438
prefix.register 'migration', Foo::CLI::Commands::Generate::Migration
428439
prefix.register 'model', Foo::CLI::Commands::Generate::Model
429440
prefix.register 'secret', Foo::CLI::Commands::Generate::Secret
441+
prefix.register 'scaffold', Foo::CLI::Commands::Generate::Scaffold
430442
end
431443
Foo::CLI::Commands.register 'new', Foo::CLI::Commands::New
432444
Foo::CLI::Commands.register 'routes', Foo::CLI::Commands::Routes

0 commit comments

Comments
 (0)