diff --git a/dry-cli.gemspec b/dry-cli.gemspec index 38ea65fc..dfd59148 100644 --- a/dry-cli.gemspec +++ b/dry-cli.gemspec @@ -27,6 +27,7 @@ Gem::Specification.new do |spec| # to update dependencies edit project.yml spec.add_runtime_dependency "concurrent-ruby", "~> 1.0" + spec.add_runtime_dependency "dry-effects" spec.add_development_dependency "bundler", ">= 1.6", "< 3" spec.add_development_dependency "rake", "~> 13.0" diff --git a/lib/dry/cli.rb b/lib/dry/cli.rb index 7b13d6d9..84e749fc 100644 --- a/lib/dry/cli.rb +++ b/lib/dry/cli.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true +require 'dry/effects' # Dry # @@ -17,6 +18,8 @@ class CLI require 'dry/cli/banner' require 'dry/cli/inflector' + include Dry::Effects::Handler.Reader(:cli) + # Check if command # # @param command [Object] the command to check @@ -101,7 +104,7 @@ def perform_registry(arguments, out) command, args = parse(result.command, result.arguments, result.names, out) result.before_callbacks.run(command, args) - command.call(**args) + with_cli(self) { command.call(**args) } result.after_callbacks.run(command, args) else usage(result, out) diff --git a/lib/dry/cli/command.rb b/lib/dry/cli/command.rb index 974d6a26..8be23eea 100644 --- a/lib/dry/cli/command.rb +++ b/lib/dry/cli/command.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true require 'forwardable' +require 'open3' +require 'dry/cli/program_name' require 'concurrent/array' require 'dry/cli/option' @@ -15,6 +17,7 @@ class Command def self.inherited(base) super base.extend ClassMethods + base.include Dry::Effects.Reader(:cli) end # @since 0.1.0 @@ -352,6 +355,10 @@ def self.optional_arguments required_arguments optional_arguments ] => 'self.class' + + def invoke(command, *arguments) + cli.call(arguments: [*command, *arguments]) + end end end end diff --git a/spec/integration/commands_spec.rb b/spec/integration/commands_spec.rb index 96300576..1ca3ee62 100644 --- a/spec/integration/commands_spec.rb +++ b/spec/integration/commands_spec.rb @@ -11,4 +11,9 @@ end end end + + it 'calls other commands from inside call' do + output = `foo g scaffold Test` + expect(output).to eq("generate model - model: Test\ngenerate secret - app: \n") + end end diff --git a/spec/support/fixtures/foo b/spec/support/fixtures/foo index 9807746b..0f1d0748 100755 --- a/spec/support/fixtures/foo +++ b/spec/support/fixtures/foo @@ -264,6 +264,17 @@ module Foo puts "generate secret - app: #{app}" end end + + class Scaffold < Dry::CLI::Command + desc 'Generate scaffold' + + argument :model, required: true, desc: 'Scaffold for model' + + def call(model:) + invoke(%w[generate model], model) + invoke(%w[generate secret]) + end + end end class New < Dry::CLI::Command @@ -427,6 +438,7 @@ Foo::CLI::Commands.register 'generate', aliases: ['g'] do |prefix| prefix.register 'migration', Foo::CLI::Commands::Generate::Migration prefix.register 'model', Foo::CLI::Commands::Generate::Model prefix.register 'secret', Foo::CLI::Commands::Generate::Secret + prefix.register 'scaffold', Foo::CLI::Commands::Generate::Scaffold end Foo::CLI::Commands.register 'new', Foo::CLI::Commands::New Foo::CLI::Commands.register 'routes', Foo::CLI::Commands::Routes