Skip to content

Commit

Permalink
Invoke commands to combine
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanShamatov committed Jan 30, 2020
1 parent fac7e9d commit 40d202a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions dry-cli.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 4 additions & 1 deletion lib/dry/cli.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true
require 'dry/effects'

# Dry
#
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions lib/dry/cli/command.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require 'forwardable'
require 'open3'
require 'dry/cli/program_name'
require 'concurrent/array'
require 'dry/cli/option'

Expand All @@ -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
Expand Down Expand Up @@ -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
5 changes: 5 additions & 0 deletions spec/integration/commands_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions spec/support/fixtures/foo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 40d202a

Please sign in to comment.