rake-leaves 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +15 -0
  2. data/lib/rake/leaves.rb +192 -0
  3. metadata +58 -0
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NGZkNDRhYWQ3NjIyODBjNjJhYjNlOTM0NWIxY2JlZjNmYTRkODcwMw==
5
+ data.tar.gz: !binary |-
6
+ MjljNGYwNzQ1YTMwNmRiYmE5ZThjNjVmNWE5Yjc4MmUzNDMwYTcyOQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YjE2OTJiNTJkNjgwZTU5NzkyZTVlYTg3ZDVkNWE1YWFmMGFkNzYzYjI3MGE0
10
+ ZGIzZDdhNzU1MTE5ZjhmYmZkMzAxNTYwNmEyMmM5NWM3ZmQ1NWYwM2U5OTU4
11
+ ZGRhZDZhNjRiZGU0ZGRhMzBjM2I3Zjc2YzZlYzJmNzM1Yzk3ODY=
12
+ data.tar.gz: !binary |-
13
+ Nzk2YjZhODMwYjA3ZTMzOGM5MjdiYmY1YzU0MTg3NmM5ZmY3Y2E5OGMxZGQ0
14
+ ZWU5YzBiMzcxOWU1ZThlMTFlN2VmMzhlNjY5YmExZTJkNDdlY2JkZmM4N2Nm
15
+ OTA1ZDIyMzkwMDFlNjdiZGMyYWQwYTgyMzcxNDE0Y2Y2M2IxYWY=
@@ -0,0 +1,192 @@
1
+ #--
2
+ # Copyright (c) 2014 Elliot Dickison
3
+ #
4
+ # This source code is released under the WTFPL License.
5
+ #++
6
+
7
+ module Rake
8
+ module Leaves
9
+
10
+ # Define required task parameters
11
+ def params (*params)
12
+ Rake.application.last_required_params = params
13
+ end
14
+
15
+ # Define optional task parameters
16
+ def optional_params (params)
17
+ Rake.application.last_optional_params = params
18
+ end
19
+
20
+ # Alias a task parameter
21
+ def alias_param (new_name, original_name)
22
+ original_name = original_name.to_sym
23
+ aliases = Rake.application.last_param_aliases
24
+ aliases[original_name] ||= []
25
+ aliases[original_name].push new_name
26
+ Rake.application.last_param_aliases = aliases
27
+ end
28
+
29
+ # Allow the task to request missing [required] arguments from the user
30
+ def request_missing_params
31
+ Rake.application.last_request_missing_params = true
32
+ end
33
+ end
34
+
35
+ class Task
36
+
37
+ attr_writer :request_missing_params
38
+
39
+ alias_method :original_invoke_with_call_chain, :invoke_with_call_chain
40
+
41
+ # Call self.args in self.invoke_with_call_chain so that all the arg
42
+ # checking is done before any tasks are executed
43
+ def invoke_with_call_chain (*args)
44
+ self.args
45
+ original_invoke_with_call_chain *args
46
+ end
47
+
48
+ # Gathers and returns all args for the task. Missing required args are
49
+ # requested via standard in
50
+ def args
51
+ unless @args
52
+ errors = []
53
+ @args = required_params
54
+ .merge(optional_params)
55
+ .inject({}) do |args, (name, default)|
56
+
57
+ # Check for a value supplied under an alias
58
+ param_aliases[name].reverse.each { |alias_name|
59
+ args[name] ||= ENV[alias_name.to_s]
60
+ } if param_aliases[name]
61
+
62
+ # Set the arg to user input or its default value
63
+ args[name] ||= ENV[name.to_s] || default
64
+
65
+ # If an argument is a) missing and b) required, then
66
+ # request it or generate an error message
67
+ if args[name].nil? && required_params.keys.include?(name)
68
+ if @request_missing_params
69
+ args[name] = request_argument name
70
+ else
71
+ errors.push "Missing argument '#{name}'"
72
+ end
73
+ end
74
+
75
+ args
76
+ end
77
+ abort errors.join "\n" unless errors.empty?
78
+ end
79
+ @args
80
+ end
81
+
82
+ # Add a required argument to the task
83
+ def add_required_param (name)
84
+ required_params[name.to_sym] = nil
85
+ end
86
+
87
+ # Add an optional argument to the task (along with an optional default)
88
+ def add_optional_param (name, default = nil)
89
+ optional_params[name.to_sym] = default
90
+ end
91
+
92
+ # Alias a task argument (same usage as alias_method)
93
+ def add_param_alias (new_name, original_name)
94
+ param_aliases[original_name.to_sym] ||= []
95
+ param_aliases[original_name.to_sym].push new_name.to_sym
96
+ end
97
+
98
+ private
99
+
100
+ # required_params getter, cleaner than aliasing initialize to set
101
+ # the default value
102
+ def required_params
103
+ @required_params ||= {}
104
+ end
105
+
106
+ # optional_params getter, cleaner than aliasing initialize to set
107
+ # the default value
108
+ def optional_params
109
+ @optional_params ||= {}
110
+ end
111
+
112
+ # param_aliases getter, cleaner than aliasing initialize to set
113
+ # the default value
114
+ def param_aliases
115
+ @param_aliases ||= {}
116
+ end
117
+
118
+ # Request an argument from the user via standard in
119
+ def request_argument (name)
120
+ STDOUT.print "Enter #{name} for #{@name}: "
121
+ ENV[name.to_s] = STDIN.gets.strip
122
+ end
123
+ end
124
+
125
+ module TaskManager
126
+
127
+ attr_writer :last_required_params, :last_optional_params, :last_param_aliases,
128
+ :last_request_missing_params
129
+
130
+ alias_method :original_intern, :intern
131
+
132
+ # Add arguments to the task immediately after creating it
133
+ def intern (*args)
134
+ task = original_intern *args
135
+ add_params task
136
+ task
137
+ end
138
+
139
+ # last_required_params getter, cleaner than aliasing initialize to set
140
+ # the default value
141
+ def last_required_params
142
+ @last_required_params ||= {}
143
+ end
144
+
145
+ # last_optional_params getter, cleaner than aliasing initialize to set
146
+ # the default value
147
+ def last_optional_params
148
+ @last_optional_params ||= {}
149
+ end
150
+
151
+ # last_param_aliases getter, cleaner than aliasing initialize to set
152
+ # the default value
153
+ def last_param_aliases
154
+ @last_param_aliases ||= {}
155
+ end
156
+
157
+ private
158
+
159
+ # Add parameters to a task based on the last parameters defined (and then clear
160
+ # the last parameters so they don't affect the next task)
161
+ def add_params (task)
162
+
163
+ last_required_params.each { |arg|
164
+ task.add_required_param arg
165
+ }
166
+
167
+ last_optional_params.each { |arg, default|
168
+ task.add_optional_param arg, default
169
+ }
170
+
171
+ last_param_aliases.each { |original_name, new_names|
172
+ new_names.each { |new_name|
173
+ task.add_param_alias new_name, original_name
174
+ }
175
+ }
176
+
177
+ task.request_missing_params = @last_request_missing_params
178
+
179
+ @last_required_params = nil
180
+ @last_optional_params = nil
181
+ @last_param_aliases = nil
182
+ @last_request_missing_params = nil
183
+ end
184
+ end
185
+
186
+ module DSL
187
+
188
+ # Include the new methods that should be accessible globally
189
+ include Leaves
190
+ private(*Leaves.instance_methods(false))
191
+ end
192
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rake-leaves
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Elliot Dickison
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.2.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.2.2
27
+ description: See https://github.com/elliotdickison/rake-leaves/blob/master/README.md
28
+ email: ejdickison@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/rake/leaves.rb
34
+ homepage: https://github.com/elliotdickison/rake-leaves
35
+ licenses:
36
+ - WTFPL
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.2.2
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: User-friendly argument handling for rake tasks.
58
+ test_files: []