everyday_thor_util 1.4.1 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +61 -3
- data/lib/everyday_thor_util/plugin-helper.rb +38 -9
- data/lib/everyday_thor_util/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d2ac4f3f244fb40340d7190bb4021f8d56491c49
|
4
|
+
data.tar.gz: c89b4f721f28e61423298f3327fc8abaee29d455
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91e8839347cc288aa41f8d87e86f3d6d1ce6fe026ae037bf67e9303ea3a78f8ce731c902c573cc0df0738f19a302b8097e9e8e8b5b2c77c9b2abc5e8cbb0c83a
|
7
|
+
data.tar.gz: d7bf6ef60980e017dce1c1cd2dc731aaa435df512a0d61b3792d079b664dfe115563491b6510c6a52ff366ab928119b928f90e7e8293a791b7efb3f6bd3de001
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# EverydayThorUtil
|
2
2
|
|
3
|
-
|
3
|
+
Two parts: `everyday_thor_util/thor-fix` patches `Thor` with a fix for help messages with multi-level command nesting not showing the full command string. `everyday_thor_util/plugin-helper` provides `everyday-plugins` types for `Thor` commands and `Thor` flags
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,11 +18,69 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
###Thor patch
|
22
|
+
An issue I found with `Thor` is that when you have subcommands of a subcommand, the help messages mess up and just show the script name and the last subcommand. `everyday_thor_util/thor-fix` provides a patch for this issue that will make sure that the messages display the full chain of commands by storing the parent command and referencing it (recursively) when getting the banner. If you just want the patch, use
|
23
|
+
```ruby
|
24
|
+
require 'everyday_thor_util/thor-fix'
|
25
|
+
```
|
26
|
+
instead of requiring the base package (which loads the `plugin-helper` module too)
|
27
|
+
|
28
|
+
###Plugin Helper
|
29
|
+
My gem `everyday-plugins` is something I use for my own projects for allowing a plugin-based structure that can support loading plugins from other gems that provide them in the right way. The `everyday_thor_util/plugin-helper` module will provide you with a method to register types for `Thor` commands, flags, and helpers.
|
30
|
+
|
31
|
+
Here's an example (from my `mvn2chain` gem; note that I'm assuming you have already included `everyday-plugins` properly):
|
32
|
+
```ruby
|
33
|
+
require 'everyday_thor_util'
|
34
|
+
EverydayThorUtil::SubCommandTypes.def_types(:command_ids, :flag, :command, :helper)
|
35
|
+
register(:command, id: :path, parent: nil, name: 'path', short_desc: 'path', desc: 'print out the path of the current file') { puts __FILE__ }
|
36
|
+
|
37
|
+
register(:command, id: :dep, parent: nil, name: 'dep', aliases: %w(deps dependency dependencies), short_desc: 'dep SUBCOMMAND ARGS...', desc: 'alter the stored dependencies')
|
38
|
+
|
39
|
+
register(:command, id: :dep_add, parent: :dep, name: 'add', aliases: %w(register reg), short_desc: 'add DEP_ID DEP', desc: 'add a dependency to the list') { |dep_id, dep|
|
40
|
+
#contents excluded for brevity
|
41
|
+
}
|
42
|
+
|
43
|
+
register :flag, name: :force, aliases: ['-f'], parent: :dep_add, type: :boolean, desc: 'force the dependency to be added even if the provided directory does not contain a pom.xml or the dependency ID is already in use'
|
44
|
+
|
45
|
+
register(:helper, name: 'chain_args', parent: nil) { |dep_id, arg_hash, exclude|
|
46
|
+
#contents excluded for brevity
|
47
|
+
}
|
48
|
+
|
49
|
+
root_command = Class.new(Thor)
|
50
|
+
Plugins.get :command, root_command, nil
|
51
|
+
|
52
|
+
root_command.start(ARGV)
|
53
|
+
```
|
54
|
+
|
55
|
+
A parent of `nil` means the parent is the root command. Otherwise, use the `:id` parameter of the parent as the value of `:parent`. For helpers, you can provide `global: true` instead of a parent if you want it to be available in all `Thor` command classes. Command aliases actually duplicate the command and any children because I don't know of a `Thor` feature that lets you provide command aliases. It will automatically replace the value provided for `:name` with the alias name in the `:short_desc` parameter that is used for the copied commands.
|
56
|
+
|
57
|
+
####Define Helper
|
58
|
+
The `plugin-helper` package contains a method for defining a pre-made helper function. Currently, the only one I have created is `print_info`, but more may be added as I come up with them.
|
59
|
+
|
60
|
+
Here is an example of `print_info` (note that I'm assuming you have already included `everyday-plugins` properly):
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
require 'everyday_thor_util'
|
64
|
+
EverydayThorUtil::SubCommandTypes.def_types(:command_ids, :flag, :command, :helper)
|
65
|
+
EverydayThorUtil::SubCommandTypes.def_helper(:helper, :print_info)
|
66
|
+
register(:command, id: :dep_add, parent: :dep, name: 'add', aliases: %w(register reg), short_desc: 'add DEP_ID DEP', desc: 'add a dependency to the list') { |dep_id, dep|
|
67
|
+
print_info(__method__) { |p| eval p }
|
68
|
+
}
|
69
|
+
```
|
70
|
+
|
71
|
+
Now, if you run `mvn2chain dep add -f my_id /path/to/my/id`, it will print
|
72
|
+
|
73
|
+
command: mvn2chain dep add
|
74
|
+
parent_options: {"force"=>true}
|
75
|
+
options: {"force"=>true}
|
76
|
+
dep_id: my_id
|
77
|
+
dep: /path/to/my/id
|
78
|
+
|
79
|
+
If you leave off the block passed to the `print_info` helper, it will not print out the parameter info. This is because the scope change causes it to lose reference to the variables, meaning that in order to show the parameter values, it has to be given a way to turn the parameter name into a value.
|
22
80
|
|
23
81
|
## Contributing
|
24
82
|
|
25
|
-
1. Fork it ( https://github.com/
|
83
|
+
1. Fork it ( https://github.com/henderea/everyday_thor_util/fork )
|
26
84
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
85
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
86
|
4. Push to the branch (`git push origin my-new-feature`)
|
@@ -3,15 +3,13 @@ require 'everyday-plugins'
|
|
3
3
|
include EverydayPlugins
|
4
4
|
require 'thor'
|
5
5
|
|
6
|
-
class
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
6
|
+
class Module
|
7
|
+
def create_method(name, &block)
|
8
|
+
self.send(:define_method, name, &block)
|
9
|
+
end
|
11
10
|
|
12
|
-
|
13
|
-
|
14
|
-
end
|
11
|
+
def dup_method(new_name, old_name)
|
12
|
+
self.send(:alias_method, new_name, old_name)
|
15
13
|
end
|
16
14
|
end
|
17
15
|
|
@@ -106,7 +104,7 @@ module EverydayThorUtil
|
|
106
104
|
end
|
107
105
|
|
108
106
|
def def_helper(helper_symbol, which_helper, method_name = nil, global = true, parent = nil)
|
109
|
-
case(which_helper)
|
107
|
+
case (which_helper)
|
110
108
|
when :print_info
|
111
109
|
register(helper_symbol, name: (method_name || 'print_info'), global: global, parent: parent) { |meth, &eval_block|
|
112
110
|
meth_obj = self.method(meth)
|
@@ -119,6 +117,37 @@ module EverydayThorUtil
|
|
119
117
|
puts "Unknown helper #{which_helper}"
|
120
118
|
end
|
121
119
|
end
|
120
|
+
|
121
|
+
def add_debugging(base, option_sym, env_sym)
|
122
|
+
methods = base.commands.keys - base.subcommands
|
123
|
+
base.class_eval {
|
124
|
+
methods.each { |method_name|
|
125
|
+
original_method = instance_method(method_name)
|
126
|
+
no_commands {
|
127
|
+
define_method(method_name) { |*args, &block|
|
128
|
+
debug = if options.has_key?(option_sym.to_s) || options.has_key?(option_sym.to_sym)
|
129
|
+
options[option_sym.to_sym]
|
130
|
+
else
|
131
|
+
d = ENV[env_sym.to_s]
|
132
|
+
d == '1' || d == 1 || d == 'true' || d == 't'
|
133
|
+
end
|
134
|
+
if debug
|
135
|
+
puts "command: #{self.class.basename2} #{__method__.to_s}"
|
136
|
+
puts "parent_options: #{parent_options.inspect}"
|
137
|
+
puts "options: #{options.inspect}"
|
138
|
+
original_method.parameters.each_with_index { |p, i| puts "#{p[1].to_s}: #{args[i]}" }
|
139
|
+
end
|
140
|
+
begin
|
141
|
+
original_method.bind(self).call(*args, &block)
|
142
|
+
rescue ArgumentError => e
|
143
|
+
base.handle_argument_error(base.commands[method_name], e, args, original_method.arity)
|
144
|
+
end
|
145
|
+
}
|
146
|
+
}
|
147
|
+
}
|
148
|
+
}
|
149
|
+
base.subcommand_classes.values.each { |c| add_debugging(c, option_sym, env_sym) }
|
150
|
+
end
|
122
151
|
end
|
123
152
|
end
|
124
153
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: everyday_thor_util
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eric Henderson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|