bozo 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/bozo/executor.rb CHANGED
@@ -1,282 +1,289 @@
1
- module Bozo
2
-
3
- # Class defining the structure of the build and responsible for executing
4
- # tasks and their dependencies.
5
- class Executor
6
- include Runner
7
-
8
- # Create a new instance.
9
- #
10
- # @param [Configuration] build_configuration
11
- # The build configuration.
12
- # @param [Hash] global_params
13
- # The global parameters
14
- def initialize(build_configuration, global_params)
15
- @build_configuration = build_configuration
16
- @global_params = cli_keys_to_ruby global_params
17
- end
18
-
19
- # Executes the command using the given parameters and environment.
20
- #
21
- # @param [Symbol] command
22
- # The name of the command to execute.
23
- # @param [Hash] params
24
- # The parameters for the command.
25
- # @param [Hash] env
26
- # The environment that the command is run within.
27
- def execute(command, params, env)
28
- with_hooks :build, params, env do
29
- send command, params, env
30
- end
31
- end
32
-
33
- # Runs the clean task.
34
- #
35
- # @param [Hash] params
36
- # The parameters for the command.
37
- # @param [Hash] env
38
- # The environment that the command is run within.
39
- def clean(params, env)
40
- with_hooks :clean, params, env do
41
- log_info 'Cleaning project'
42
- clear_directory 'temp'
43
- clear_directory 'dist'
44
- end
45
- end
46
-
47
- # Runs the uninstall task.
48
- #
49
- # @param [Hash] params
50
- # The parameters for the command.
51
- # @param [Hash] env
52
- # The environment that the command is run within.
53
- def uninstall(params, env)
54
- clean params, env
55
- with_hooks :uninstall, params, env do
56
- log_info 'Uninstalling project'
57
- clear_directory 'packages'
58
- clear_directory 'build'
59
- end
60
- end
61
-
62
- # Runs the dependency retrieval task.
63
- #
64
- # @param [Hash] params
65
- # The parameters for the command.
66
- # @param [Hash] env
67
- # The environment that the command is run within.
68
- def dependencies(params, env)
69
- clean params, env
70
- retrieve_build_dependencies
71
- execute_with_hooks :dependencies, params, env
72
- end
73
-
74
- # Runs the prepare task.
75
- #
76
- # @param [Hash] params
77
- # The parameters for the command.
78
- # @param [Hash] env
79
- # The environment that the command is run within.
80
- def prepare(params, env)
81
- dependencies params, env
82
- execute_with_hooks :prepare, params, env
83
- end
84
-
85
- # Runs the compile task.
86
- #
87
- # @param [Hash] params
88
- # The parameters for the command.
89
- # @param [Hash] env
90
- # The environment that the command is run within.
91
- def compile(params, env)
92
- prepare params, env
93
- execute_with_hooks :compile, params, env
94
- end
95
-
96
- # Runs the test task.
97
- #
98
- # @param [Hash] params
99
- # The parameters for the command.
100
- # @param [Hash] env
101
- # The environment that the command is run within.
102
- def test(params, env)
103
- compile params, env
104
- execute_with_hooks :test, params, env
105
- end
106
-
107
- # Runs the package task.
108
- #
109
- # @param [Hash] params
110
- # The parameters for the command.
111
- # @param [Hash] env
112
- # The environment that the command is run within.
113
- def package(params, env)
114
- test params, env
115
- execute_with_hooks :package, params, env
116
- end
117
-
118
- # Runs the publish task.
119
- #
120
- # @param [Hash] params
121
- # The parameters for the command.
122
- # @param [Hash] env
123
- # The environment that the command is run within.
124
- def publish(params, env)
125
- if build_server?
126
- package params, env
127
- execute_with_hooks :publish, params, env
128
- else
129
- log_warn 'You can only publish a package from build server.'
130
- log_warn ''
131
- log_warn 'If you really need to publish the package then provide the --build-server switch but you should only be doing that as a last resort.'
132
- end
133
- end
134
-
135
- private
136
-
137
- # Executes the executors for the given stage logging all messages with the
138
- # given action.
139
- #
140
- # @param [Symbol] stage
141
- # The name of the stage to execute.
142
- # @param [Hash] params
143
- # The parameters for the command.
144
- # @param [Hash] env
145
- # The environment that the command is run within.
146
- def execute_with_hooks(stage, params, env)
147
- action, executors = stage_definition stage
148
- with_hooks stage, params, env do
149
- log_info '' # blank line for aesthetics
150
- log_info action
151
- log_debug 'No executors' if executors.empty?
152
- executors.each do |executor|
153
- log_debug "#{action} with #{executor.class}"
154
- set_parameters executor, params, env
155
- executor.execute
156
- end
157
- end
158
- end
159
-
160
- # Gets the description and runners associated with the given stage.
161
- #
162
- # @params [Symbol] stage
163
- # The name of the stage.
164
- def stage_definition(stage)
165
- case stage
166
- when :dependencies
167
- return 'Resolving dependencies', @build_configuration.dependency_resolvers
168
- when :compile
169
- return 'Compiling', @build_configuration.compilers
170
- when :test
171
- return 'Running tests', @build_configuration.test_runners
172
- when :package
173
- return 'Packaging', @build_configuration.packagers
174
- when :prepare
175
- return 'Preparing', @build_configuration.preparers
176
- when :publish
177
- return 'Publishing', @build_configuration.publishers
178
- else
179
- raise "Unrecognized stage: #{stage}"
180
- end
181
- end
182
-
183
- # Runs the given code block surrounding it with the hooks for the named
184
- # stage.
185
- #
186
- # @param [Symbol] stage
187
- # The name of the stage being run.
188
- # @param [Hash] params
189
- # The parameters for the command.
190
- # @param [Hash] env
191
- # The environment that the command is run within.
192
- def with_hooks(stage, params, env)
193
- call_receiving_hooks "pre_#{stage}".to_sym, params, env
194
- yield
195
- call_receiving_hooks "post_#{stage}".to_sym, params, env
196
- end
197
-
198
- # Invokes the method for the hook for all hook objects that define a
199
- # method for it.
200
- #
201
- # @param [Symbol] stage
202
- # The name of the hook to call the methods for.
203
- # @param [Hash] params
204
- # The parameters for the command.
205
- # @param [Hash] env
206
- # The environment that the command is run within.
207
- def call_receiving_hooks(stage, params, env)
208
- @build_configuration.hooks.each do |hook|
209
- if hook.respond_to? stage
210
- set_parameters hook, params, env
211
- hook.send stage
212
- end
213
- end
214
- end
215
-
216
- # Assigns the state of the build system to the runner.
217
- #
218
- # @param [Runner] runner
219
- # The runner to assign the state to.
220
- # @param [Hash] params
221
- # The parameters for the step.
222
- # @param [Hash] env
223
- # The environment that the step is run within.
224
- def set_parameters(runner, params, env)
225
- runner.build_configuration = @build_configuration
226
- runner.global_params = @global_params
227
- runner.params = cli_keys_to_ruby params
228
- runner.env = env
229
- end
230
-
231
- # Removes the named directory from the root directory.
232
- #
233
- # @param [String] dir
234
- # The name of the directory to remove.
235
- def clear_directory(dir)
236
- full_path = File.expand_path(dir)
237
- log_debug "Clearing directory #{full_path}"
238
- FileUtils.rm_rf full_path
239
- end
240
-
241
- # Retrieves the required build tools from the configured build tools
242
- # location.
243
- def retrieve_build_dependencies
244
- tools_location = @build_configuration.build_tools_location
245
- required_tools = @build_configuration.build_tools
246
- tools_path = File.expand_path(File.join('build', 'tools'))
247
-
248
- log_info 'Retrieving build tools'
249
-
250
- required_tools.each do |tool|
251
- tool_name = tool.to_s
252
- tool_path = File.join(tools_location, tool_name)
253
- tool_destination = File.join(tools_path, tool_name)
254
-
255
- if File.exist? tool_destination
256
- log_debug "#{tool_name.capitalize} already present"
257
- else
258
- log_debug "Retrieving #{tool_name.capitalize}"
259
- FileUtils.mkdir_p tool_destination
260
- FileUtils.cp_r tool_path, tools_path
261
- log_debug "#{tool_name.capitalize} retrieved"
262
- end
263
- end
264
- end
265
-
266
- # Converts a hash containing CLI-style keys (:'multi-word') and converts
267
- # them into Ruby-style symbols (:multi_word).
268
- #
269
- # @param [Hash] hash
270
- # A hash containing CLI-style keys.
271
- def cli_keys_to_ruby(hash)
272
- hash.keys.to_a.each do |key|
273
- new_key = key.to_s.gsub('-', '_').to_sym
274
- hash[new_key] = hash[key] unless hash.has_key? new_key
275
- end
276
-
277
- hash
278
- end
279
-
280
- end
281
-
1
+ module Bozo
2
+
3
+ # Class defining the structure of the build and responsible for executing
4
+ # tasks and their dependencies.
5
+ class Executor
6
+ include Runner
7
+
8
+ # Create a new instance.
9
+ #
10
+ # @param [Configuration] build_configuration
11
+ # The build configuration.
12
+ # @param [Hash] global_params
13
+ # The global parameters
14
+ def initialize(build_configuration, global_params)
15
+ @build_configuration = build_configuration
16
+ @global_params = cli_keys_to_ruby global_params
17
+ end
18
+
19
+ # Executes the command using the given parameters and environment.
20
+ #
21
+ # @param [Symbol] command
22
+ # The name of the command to execute.
23
+ # @param [Hash] params
24
+ # The parameters for the command.
25
+ # @param [Hash] env
26
+ # The environment that the command is run within.
27
+ def execute(command, params, env)
28
+ with_hooks :build, params, env do
29
+ send command, params, env
30
+ end
31
+ end
32
+
33
+ # Runs the clean task.
34
+ #
35
+ # @param [Hash] params
36
+ # The parameters for the command.
37
+ # @param [Hash] env
38
+ # The environment that the command is run within.
39
+ def clean(params, env)
40
+ with_hooks :clean, params, env do
41
+ log_info 'Cleaning project'
42
+ clear_directory 'temp'
43
+ clear_directory 'dist'
44
+ end
45
+ end
46
+
47
+ # Runs the uninstall task.
48
+ #
49
+ # @param [Hash] params
50
+ # The parameters for the command.
51
+ # @param [Hash] env
52
+ # The environment that the command is run within.
53
+ def uninstall(params, env)
54
+ clean params, env
55
+ with_hooks :uninstall, params, env do
56
+ log_info 'Uninstalling project'
57
+ clear_directory 'packages'
58
+ clear_directory 'build'
59
+ end
60
+ end
61
+
62
+ # Runs the dependency retrieval task.
63
+ #
64
+ # @param [Hash] params
65
+ # The parameters for the command.
66
+ # @param [Hash] env
67
+ # The environment that the command is run within.
68
+ def dependencies(params, env)
69
+ clean params, env
70
+ retrieve_build_dependencies
71
+ execute_with_hooks :dependencies, params, env
72
+ end
73
+
74
+ # Runs the prepare task.
75
+ #
76
+ # @param [Hash] params
77
+ # The parameters for the command.
78
+ # @param [Hash] env
79
+ # The environment that the command is run within.
80
+ def prepare(params, env)
81
+ dependencies params, env
82
+ execute_with_hooks :prepare, params, env
83
+ end
84
+
85
+ # Runs the compile task.
86
+ #
87
+ # @param [Hash] params
88
+ # The parameters for the command.
89
+ # @param [Hash] env
90
+ # The environment that the command is run within.
91
+ def compile(params, env)
92
+ prepare params, env
93
+ execute_with_hooks :compile, params, env
94
+ end
95
+
96
+ # Runs the test task.
97
+ #
98
+ # @param [Hash] params
99
+ # The parameters for the command.
100
+ # @param [Hash] env
101
+ # The environment that the command is run within.
102
+ def test(params, env)
103
+ compile params, env
104
+ execute_with_hooks :test, params, env
105
+ end
106
+
107
+ # Runs the package task.
108
+ #
109
+ # @param [Hash] params
110
+ # The parameters for the command.
111
+ # @param [Hash] env
112
+ # The environment that the command is run within.
113
+ def package(params, env)
114
+ test params, env
115
+ execute_with_hooks :package, params, env
116
+ end
117
+
118
+ # Runs the publish task.
119
+ #
120
+ # @param [Hash] params
121
+ # The parameters for the command.
122
+ # @param [Hash] env
123
+ # The environment that the command is run within.
124
+ def publish(params, env)
125
+ if build_server?
126
+ package params, env
127
+ execute_with_hooks :publish, params, env
128
+ else
129
+ log_warn 'You can only publish a package from build server.'
130
+ log_warn ''
131
+ log_warn 'If you really need to publish the package then provide the --build-server switch but you should only be doing that as a last resort.'
132
+ end
133
+ end
134
+
135
+ private
136
+
137
+ # Executes the executors for the given stage logging all messages with the
138
+ # given action.
139
+ #
140
+ # @param [Symbol] stage
141
+ # The name of the stage to execute.
142
+ # @param [Hash] params
143
+ # The parameters for the command.
144
+ # @param [Hash] env
145
+ # The environment that the command is run within.
146
+ def execute_with_hooks(stage, params, env)
147
+ action, executors = stage_definition stage
148
+ with_hooks stage, params, env do
149
+ log_info '' # blank line for aesthetics
150
+ log_info action
151
+ log_debug 'No executors' if executors.empty?
152
+ executors.each do |executor|
153
+ log_debug "#{action} with #{executor.class}"
154
+ set_parameters executor, params, env
155
+ executor.execute
156
+ end
157
+ end
158
+ end
159
+
160
+ # Gets the description and runners associated with the given stage.
161
+ #
162
+ # @params [Symbol] stage
163
+ # The name of the stage.
164
+ def stage_definition(stage)
165
+ case stage
166
+ when :dependencies
167
+ return 'Resolving dependencies', @build_configuration.dependency_resolvers
168
+ when :compile
169
+ return 'Compiling', @build_configuration.compilers
170
+ when :test
171
+ return 'Running tests', @build_configuration.test_runners
172
+ when :package
173
+ return 'Packaging', @build_configuration.packagers
174
+ when :prepare
175
+ return 'Preparing', @build_configuration.preparers
176
+ when :publish
177
+ return 'Publishing', @build_configuration.publishers
178
+ else
179
+ raise "Unrecognized stage: #{stage}"
180
+ end
181
+ end
182
+
183
+ # Runs the given code block surrounding it with the hooks for the named
184
+ # stage.
185
+ #
186
+ # @param [Symbol] stage
187
+ # The name of the stage being run.
188
+ # @param [Hash] params
189
+ # The parameters for the command.
190
+ # @param [Hash] env
191
+ # The environment that the command is run within.
192
+ def with_hooks(stage, params, env)
193
+ call_receiving_hooks "pre_#{stage}".to_sym, params, env
194
+
195
+ begin
196
+ yield
197
+ rescue
198
+ call_receiving_hooks "failed_#{stage}".to_sym, params, env
199
+ raise
200
+ end
201
+
202
+ call_receiving_hooks "post_#{stage}".to_sym, params, env
203
+ end
204
+
205
+ # Invokes the method for the hook for all hook objects that define a
206
+ # method for it.
207
+ #
208
+ # @param [Symbol] stage
209
+ # The name of the hook to call the methods for.
210
+ # @param [Hash] params
211
+ # The parameters for the command.
212
+ # @param [Hash] env
213
+ # The environment that the command is run within.
214
+ def call_receiving_hooks(stage, params, env)
215
+ @build_configuration.hooks.each do |hook|
216
+ if hook.respond_to? stage
217
+ set_parameters hook, params, env
218
+ hook.send stage
219
+ end
220
+ end
221
+ end
222
+
223
+ # Assigns the state of the build system to the runner.
224
+ #
225
+ # @param [Runner] runner
226
+ # The runner to assign the state to.
227
+ # @param [Hash] params
228
+ # The parameters for the step.
229
+ # @param [Hash] env
230
+ # The environment that the step is run within.
231
+ def set_parameters(runner, params, env)
232
+ runner.build_configuration = @build_configuration
233
+ runner.global_params = @global_params
234
+ runner.params = cli_keys_to_ruby params
235
+ runner.env = env
236
+ end
237
+
238
+ # Removes the named directory from the root directory.
239
+ #
240
+ # @param [String] dir
241
+ # The name of the directory to remove.
242
+ def clear_directory(dir)
243
+ full_path = File.expand_path(dir)
244
+ log_debug "Clearing directory #{full_path}"
245
+ FileUtils.rm_rf full_path
246
+ end
247
+
248
+ # Retrieves the required build tools from the configured build tools
249
+ # location.
250
+ def retrieve_build_dependencies
251
+ tools_location = @build_configuration.build_tools_location
252
+ required_tools = @build_configuration.build_tools
253
+ tools_path = File.expand_path(File.join('build', 'tools'))
254
+
255
+ log_info 'Retrieving build tools'
256
+
257
+ required_tools.each do |tool|
258
+ tool_name = tool.to_s
259
+ tool_path = File.join(tools_location, tool_name)
260
+ tool_destination = File.join(tools_path, tool_name)
261
+
262
+ if File.exist? tool_destination
263
+ log_debug "#{tool_name.capitalize} already present"
264
+ else
265
+ log_debug "Retrieving #{tool_name.capitalize}"
266
+ FileUtils.mkdir_p tool_destination
267
+ FileUtils.cp_r tool_path, tools_path
268
+ log_debug "#{tool_name.capitalize} retrieved"
269
+ end
270
+ end
271
+ end
272
+
273
+ # Converts a hash containing CLI-style keys (:'multi-word') and converts
274
+ # them into Ruby-style symbols (:multi_word).
275
+ #
276
+ # @param [Hash] hash
277
+ # A hash containing CLI-style keys.
278
+ def cli_keys_to_ruby(hash)
279
+ hash.keys.to_a.each do |key|
280
+ new_key = key.to_s.gsub('-', '_').to_sym
281
+ hash[new_key] = hash[key] unless hash.has_key? new_key
282
+ end
283
+
284
+ hash
285
+ end
286
+
287
+ end
288
+
282
289
  end
data/lib/bozo/logging.rb CHANGED
@@ -1,40 +1,40 @@
1
- module Bozo
2
-
3
- # Module that provides functionality for logging.
4
- module Logging
5
-
6
- # Records a `debug` log message.
7
- #
8
- # @param [String] msg
9
- # The message to log.
10
- def log_debug(msg)
11
- puts " #{msg.bright.color(:black)}"
12
- end
13
-
14
- # Records a `fatal` log message.
15
- #
16
- # @param [String] msg
17
- # The message to log.
18
- def log_fatal(msg)
19
- puts msg.color(:red).bright
20
- end
21
-
22
- # Records an `info` log message.
23
- #
24
- # @param [String] msg
25
- # The message to log.
26
- def log_info(msg)
27
- puts msg.color(:cyan).bright
28
- end
29
-
30
- # Records a `warn` log message.
31
- #
32
- # @param [String] msg
33
- # The message to log.
34
- def log_warn(msg)
35
- puts msg.color(:yellow).bright
36
- end
37
-
38
- end
39
-
40
- end
1
+ module Bozo
2
+
3
+ # Module that provides functionality for logging.
4
+ module Logging
5
+
6
+ # Records a `debug` log message.
7
+ #
8
+ # @param [String] msg
9
+ # The message to log.
10
+ def log_debug(msg)
11
+ puts " #{msg.bright.color(:black)}"
12
+ end
13
+
14
+ # Records a `fatal` log message.
15
+ #
16
+ # @param [String] msg
17
+ # The message to log.
18
+ def log_fatal(msg)
19
+ puts msg.color(:red).bright
20
+ end
21
+
22
+ # Records an `info` log message.
23
+ #
24
+ # @param [String] msg
25
+ # The message to log.
26
+ def log_info(msg)
27
+ puts msg.color(:cyan).bright
28
+ end
29
+
30
+ # Records a `warn` log message.
31
+ #
32
+ # @param [String] msg
33
+ # The message to log.
34
+ def log_warn(msg)
35
+ puts msg.color(:yellow).bright
36
+ end
37
+
38
+ end
39
+
40
+ end