Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Invoke commands to combine #82

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IvanShamatov as the comment states: to update dependencies you need to edit project.yml 😄


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'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


# 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
8 changes: 8 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,11 @@ def self.optional_arguments
required_arguments
optional_arguments
] => 'self.class'

def invoke(command, *arguments)
command = [command] unless command.is_a?(Array)
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[g model], model)
invoke(%w[g 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