hanami-cli 0.1.1 → 0.2.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f2637f9e319848200c09ef2c4722edf57a1c98c6bafed69d8ec2a9e1102cbd79
4
- data.tar.gz: c68a7b04d298ee216ad0ea019ede253ab4bc16726f8e72b9efcde209edef68bc
3
+ metadata.gz: a068faee4ac4b225d048b6552c2f3c996dcebaeb2c4b45e7496882bd1633507b
4
+ data.tar.gz: 51c34e8bd8306949cf344507bbf906a7876e47d5d39a26b70e94ad78e4191f0f
5
5
  SHA512:
6
- metadata.gz: 0c8efe76cf2730a6a7275c635a338d7f44b1df691cd7930372caefc5693a4925676ef0a1f05057d00e9a8e1c1c89b721d55cc377dcd2bb1af3fe666dbc57c99c
7
- data.tar.gz: 12b1afc361a418e06670da8c40e4ce2e6eb38bc68c32d584a7a80c5b85ef5a67f696fa2ae42fcb53734a4124664a9138ac4fd72a13c314c22df4a999d578673f
6
+ metadata.gz: 5c5817a42a314f553bc42b24f04e9a574e44efae82d462721ff9c27bb37ab4ef3c9096156a6166e94ce11728d1e6f0e4a009b8feb3e48e0a670e034e3e5c6d16
7
+ data.tar.gz: 349d5bcc6d5214feb852a01cbf57a185a8448072f1590e6160a4878cb550a80361c7abbec463cd787ed621c80046322351ec67d611f59f19ac2423ba4beb3176
data/CHANGELOG.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # Hanami::CLI
2
2
  General purpose Command Line Interface (CLI) framework for Ruby
3
3
 
4
+ ## v0.2.0.beta1 - 2018-02-28
5
+ ### Added
6
+ - [Anton Davydov] Register `before`/`after` callbacks for commands
7
+
4
8
  ## v0.1.1 - 2018-02-27
5
9
  ### Added
6
10
  - [Luca Guidi] Official support for Ruby: MRI 2.5
data/Gemfile CHANGED
@@ -6,6 +6,6 @@ unless ENV['TRAVIS']
6
6
  gem 'yard', require: false
7
7
  end
8
8
 
9
- gem 'hanami-utils', '~> 1.1', require: false, git: 'https://github.com/hanami/utils.git', branch: '1.1.x'
9
+ gem 'hanami-utils', '1.2.0.beta1', require: false, git: 'https://github.com/hanami/utils.git', branch: 'develop'
10
10
 
11
11
  gem 'hanami-devtools', require: false, git: 'https://github.com/hanami/devtools.git'
data/README.md CHANGED
@@ -25,6 +25,7 @@ General purpose Command Line Interface (CLI) framework for Ruby.
25
25
  - [Subcommands](#subcommands-1)
26
26
  - [Aliases](#aliases)
27
27
  - [Subcommand aliases](#subcommand-aliases)
28
+ - [Callbacks](#callbacks)
28
29
  - [Development](#development)
29
30
  - [Contributing](#contributing)
30
31
  - [Alternatives](#alternatives)
@@ -451,6 +452,52 @@ Commands:
451
452
  generated configuration
452
453
  ```
453
454
 
455
+ ### Callbacks
456
+
457
+ Third party gems can register _before_ and _after_ callbacks to enhance a command.
458
+
459
+ From the `foo` gem we have a command `hello`.
460
+
461
+ ```ruby
462
+ #!/usr/bin/env ruby
463
+ require "hanami/cli"
464
+
465
+ module Foo
466
+ module CLI
467
+ module Commands
468
+ extend Hanami::CLI::Registry
469
+
470
+ class Hello < Hanami::CLI::Command
471
+ argument :name, required: true
472
+
473
+ def call(name:, **)
474
+ puts "hello #{name}"
475
+ end
476
+ end
477
+ end
478
+ end
479
+ end
480
+
481
+ Foo::CLI::Commands.register "hello", Foo::CLI::Commands::Hello
482
+
483
+ cli = Hanami::CLI.new(Foo::CLI::Commands)
484
+ cli.call
485
+ ```
486
+
487
+ The `foo-bar` gem enhances `hello` command with callbacks:
488
+
489
+ ```
490
+ Foo::CLI::Commands.before("hello") { |args| puts "debug: #{args.inspect}" } # syntax 1
491
+ Foo::CLI::Commands.after "hello", &->(args) { puts "bye, #{args.fetch(:name)}" } # syntax 2
492
+ ```
493
+
494
+ ```shell
495
+ % foo hello Anton
496
+ debug: {:name=>"Anton"}
497
+ hello Anton
498
+ bye, Anton
499
+ ```
500
+
454
501
  ## Development
455
502
 
456
503
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/hanami-cli.gemspec CHANGED
@@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
25
25
  f.match(%r{^(test|spec|features)/})
26
26
  end
27
27
 
28
- spec.add_dependency "hanami-utils", "~> 1.1"
28
+ spec.add_dependency "hanami-utils", "1.2.0.beta1"
29
29
  spec.add_dependency "concurrent-ruby", "~> 1.0"
30
30
 
31
31
  spec.add_development_dependency "bundler"
data/lib/hanami/cli.rb CHANGED
@@ -7,6 +7,7 @@ module Hanami
7
7
  # @since 0.1.0
8
8
  class CLI
9
9
  require "hanami/cli/version"
10
+ require "hanami/cli/errors"
10
11
  require "hanami/cli/command"
11
12
  require "hanami/cli/registry"
12
13
  require "hanami/cli/parser"
@@ -51,7 +52,10 @@ module Hanami
51
52
 
52
53
  if result.found?
53
54
  command, args = parse(result, out)
55
+
56
+ result.before_callbacks.run(self, args)
54
57
  command.call(args)
58
+ result.after_callbacks.run(self, args)
55
59
  else
56
60
  usage(result, out)
57
61
  end
@@ -1,6 +1,6 @@
1
1
  require "forwardable"
2
- require "hanami/cli/option"
3
2
  require "concurrent/array"
3
+ require "hanami/cli/option"
4
4
 
5
5
  module Hanami
6
6
  class CLI
@@ -1,4 +1,5 @@
1
1
  require "concurrent/hash"
2
+ require 'hanami/utils/callbacks'
2
3
 
3
4
  module Hanami
4
5
  class CLI
@@ -96,6 +97,14 @@ module Hanami
96
97
  # @api private
97
98
  attr_reader :command
98
99
 
100
+ # @since 0.1.0
101
+ # @api private
102
+ attr_reader :before_callbacks
103
+
104
+ # @since 0.1.0
105
+ # @api private
106
+ attr_reader :after_callbacks
107
+
99
108
  # @since 0.1.0
100
109
  # @api private
101
110
  def initialize(parent = nil)
@@ -103,6 +112,9 @@ module Hanami
103
112
  @children = Concurrent::Hash.new
104
113
  @aliases = Concurrent::Hash.new
105
114
  @command = nil
115
+
116
+ @before_callbacks = Utils::Callbacks::Chain.new
117
+ @after_callbacks = Utils::Callbacks::Chain.new
106
118
  end
107
119
 
108
120
  # @since 0.1.0
@@ -183,6 +195,18 @@ module Hanami
183
195
  def command
184
196
  @node.command
185
197
  end
198
+
199
+ # @since 0.2.0
200
+ # @api private
201
+ def before_callbacks
202
+ @node.before_callbacks
203
+ end
204
+
205
+ # @since 0.2.0
206
+ # @api private
207
+ def after_callbacks
208
+ @node.after_callbacks
209
+ end
186
210
  end
187
211
  end
188
212
  end
@@ -0,0 +1,16 @@
1
+ module Hanami
2
+ class CLI
3
+ # @since 0.2.0
4
+ class Error < StandardError
5
+ end
6
+
7
+ # @since 0.2.0
8
+ class UnkwnownCommandError < Error
9
+ # @since 0.2.0
10
+ # @api private
11
+ def initialize(command_name)
12
+ super("unknown command: `#{command_name}'")
13
+ end
14
+ end
15
+ end
16
+ end
@@ -80,12 +80,84 @@ module Hanami
80
80
  end
81
81
  end
82
82
 
83
+ # Register a before callback.
84
+ #
85
+ # @param command_name [String] the name used for command registration
86
+ # @param callback [Proc] the callback
87
+ #
88
+ # @raise [Hanami::CLI::UnkwnownCommandError] if the command isn't registered
89
+ #
90
+ # @since 0.2.0
91
+ #
92
+ # @example
93
+ # require "hanami/cli"
94
+ #
95
+ # module Foo
96
+ # module Commands
97
+ # extend Hanami::CLI::Registry
98
+ #
99
+ # class Hello < Hanami::CLI::Command
100
+ # def call(*)
101
+ # puts "hello"
102
+ # end
103
+ # end
104
+ #
105
+ # register "hello", Hello
106
+ # before "hello", -> { puts "I'm about to say.." }
107
+ # end
108
+ # end
109
+ def before(command_name, &callback)
110
+ command(command_name).before_callbacks.append(&callback)
111
+ end
112
+
113
+ # Register an after callback.
114
+ #
115
+ # @param command_name [String] the name used for command registration
116
+ # @param callback [Proc] the callback
117
+ #
118
+ # @raise [Hanami::CLI::UnkwnownCommandError] if the command isn't registered
119
+ #
120
+ # @since 0.2.0
121
+ #
122
+ # @example
123
+ # require "hanami/cli"
124
+ #
125
+ # module Foo
126
+ # module Commands
127
+ # extend Hanami::CLI::Registry
128
+ #
129
+ # class Hello < Hanami::CLI::Command
130
+ # def call(*)
131
+ # puts "hello"
132
+ # end
133
+ # end
134
+ #
135
+ # register "hello", Hello
136
+ # after "hello", -> { puts "world" }
137
+ # end
138
+ # end
139
+ def after(command_name, &callback)
140
+ command(command_name).after_callbacks.append(&callback)
141
+ end
142
+
83
143
  # @since 0.1.0
84
144
  # @api private
85
145
  def get(arguments)
86
146
  @commands.get(arguments)
87
147
  end
88
148
 
149
+ private
150
+
151
+ COMMAND_NAME_SEPARATOR = " ".freeze
152
+
153
+ # @since 0.2.0
154
+ # @api private
155
+ def command(command_name)
156
+ get(command_name.split(COMMAND_NAME_SEPARATOR)).tap do |result|
157
+ raise UnkwnownCommandError.new(command_name) unless result.found?
158
+ end
159
+ end
160
+
89
161
  # Command name prefix
90
162
  #
91
163
  # @since 0.1.0
@@ -1,6 +1,6 @@
1
1
  module Hanami
2
2
  class CLI
3
3
  # @since 0.1.0
4
- VERSION = "0.1.1".freeze
4
+ VERSION = "0.2.0.beta1".freeze
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanami-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-27 00:00:00.000000000 Z
11
+ date: 2018-02-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hanami-utils
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: '1.1'
19
+ version: 1.2.0.beta1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: '1.1'
26
+ version: 1.2.0.beta1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: concurrent-ruby
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -102,6 +102,7 @@ files:
102
102
  - lib/hanami/cli/banner.rb
103
103
  - lib/hanami/cli/command.rb
104
104
  - lib/hanami/cli/command_registry.rb
105
+ - lib/hanami/cli/errors.rb
105
106
  - lib/hanami/cli/option.rb
106
107
  - lib/hanami/cli/parser.rb
107
108
  - lib/hanami/cli/program_name.rb
@@ -126,9 +127,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
126
127
  version: '0'
127
128
  required_rubygems_version: !ruby/object:Gem::Requirement
128
129
  requirements:
129
- - - ">="
130
+ - - ">"
130
131
  - !ruby/object:Gem::Version
131
- version: '0'
132
+ version: 1.3.1
132
133
  requirements: []
133
134
  rubyforge_project:
134
135
  rubygems_version: 2.7.5