Class: Crew::Home
- Inherits:
-
Object
show all
- Defined in:
- lib/crew/home.rb,
lib/crew/home/dsl.rb
Defined Under Namespace
Classes: DSL, UnknownContextError
Constant Summary
collapse
- CREW_CONF_FILE =
"config"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#add_callback(type, &cb) ⇒ Object
-
#add_context(name, file = nil, &blk) ⇒ Object
-
#add_tester(context_name, &blk) ⇒ Object
-
#context_new(name) ⇒ Object
-
#contexts ⇒ Object
-
#docs ⇒ Object
-
#each_task ⇒ Object
-
#each_task_for_source(source) ⇒ Object
-
#find_and_add_task(name) ⇒ Object
-
#find_task(name) ⇒ Object
-
#in_context(context_or_name, opts = {}) ⇒ Object
-
#initialize(path) ⇒ Home
constructor
-
#perform_setup ⇒ Object
-
#run(context_name, name, *args) ⇒ Object
-
#run_in_context(context_or_name, label, opts = {}, &blk) ⇒ Object
-
#shell(context_name) ⇒ Object
-
#task_available ⇒ Object
-
#task_diff ⇒ Object
-
#task_edit(name) ⇒ Object
-
#task_info(name) ⇒ Object
-
#task_install(name) ⇒ Object
-
#task_install_all ⇒ Object
-
#task_list ⇒ Object
-
#task_new(name) ⇒ Object
-
#task_remove(name) ⇒ Object
-
#task_update(name = nil) ⇒ Object
-
#test(opts = {}) ⇒ Object
-
#test_path ⇒ Object
Constructor Details
#initialize(path) ⇒ Home
Returns a new instance of Home.
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/crew/home.rb', line 33
def initialize(path)
@home_path = File.expand_path(path)
@task_path = File.join(@home_path, "tasks")
@data_path = File.join(@home_path, "data")
@config_path = File.join(@home_path, "config")
@contexts_path = File.join(@home_path, "contexts")
@sources = [File.expand_path("../../../.crew/tasks", __FILE__)]
@callbacks = {before: [], after: []}
@contexts = {}
@testers = {}
@logger = Logger.new
load!
end
|
Instance Attribute Details
#callbacks ⇒ Object
Returns the value of attribute callbacks.
30
31
32
|
# File 'lib/crew/home.rb', line 30
def callbacks
@callbacks
end
|
#context ⇒ Object
Returns the value of attribute context.
30
31
32
|
# File 'lib/crew/home.rb', line 30
def context
@context
end
|
#data_path ⇒ Object
Returns the value of attribute data_path.
30
31
32
|
# File 'lib/crew/home.rb', line 30
def data_path
@data_path
end
|
#default_context_name ⇒ Object
Returns the value of attribute default_context_name.
31
32
33
|
# File 'lib/crew/home.rb', line 31
def default_context_name
@default_context_name
end
|
#default_test_name ⇒ Object
Returns the value of attribute default_test_name.
31
32
33
|
# File 'lib/crew/home.rb', line 31
def default_test_name
@default_test_name
end
|
#home_path ⇒ Object
Returns the value of attribute home_path.
30
31
32
|
# File 'lib/crew/home.rb', line 30
def home_path
@home_path
end
|
#in_setup ⇒ Object
Returns the value of attribute in_setup.
30
31
32
|
# File 'lib/crew/home.rb', line 30
def in_setup
@in_setup
end
|
#logger ⇒ Object
Returns the value of attribute logger.
30
31
32
|
# File 'lib/crew/home.rb', line 30
def logger
@logger
end
|
#setup_mode ⇒ Object
Returns the value of attribute setup_mode.
31
32
33
|
# File 'lib/crew/home.rb', line 31
def setup_mode
@setup_mode
end
|
#task_path ⇒ Object
Returns the value of attribute task_path.
30
31
32
|
# File 'lib/crew/home.rb', line 30
def task_path
@task_path
end
|
Class Method Details
.init(*args) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/crew/home.rb', line 14
def self.init(*args)
opts = args.last.is_a?(Hash) ? args.pop : {}
path = args.size == 1 ? args.shift : Dir.pwd
path = File.join(path, ".crew")
crew_config_path = File.join(path, CREW_CONF_FILE)
raise "#{crew_config_path} file is in the way" if File.exist?(crew_config_path) && !opts[:force]
FileUtils.mkdir_p(path)
template = File.read(File.expand_path("../template/config.erb", __FILE__))
out = ERB.new(template).result
puts "Writing #{CREW_CONF_FILE} file"
File.open(crew_config_path, "w") do |f|
f << out
end
new(path)
end
|
Instance Method Details
#add_callback(type, &cb) ⇒ Object
238
239
240
|
# File 'lib/crew/home.rb', line 238
def add_callback(type, &cb)
@callbacks[type] << cb
end
|
#add_context(name, file = nil, &blk) ⇒ Object
249
250
251
|
# File 'lib/crew/home.rb', line 249
def add_context(name, file = nil, &blk)
@contexts[name] = Context.new(self, name, file, &blk)
end
|
#add_tester(context_name, &blk) ⇒ Object
157
158
159
|
# File 'lib/crew/home.rb', line 157
def add_tester(context_name, &blk)
@testers[context_name] = Tester.new(self, context_name, &blk)
end
|
#context_new(name) ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/crew/home.rb', line 64
def context_new(name)
load_contexts!
if @contexts[name]
raise "Cannot create #{name}, context already in the way"
else
template = File.read(File.expand_path("../template/context.rb.erb", __FILE__))
path = "#{name}.rb"
out = ERB.new(template).result
puts "Writing #{path} file"
out_path = File.join(@contexts_path, path)
FileUtils.mkdir_p(File.dirname(out_path))
File.open(out_path, "w") do |f|
f << out
end
end
end
|
#contexts ⇒ Object
81
82
83
84
85
|
# File 'lib/crew/home.rb', line 81
def contexts
@contexts.each do |name, context|
yield name, context
end
end
|
#docs ⇒ Object
123
124
125
126
127
|
# File 'lib/crew/home.rb', line 123
def docs
path = Docs.new(self).generate
puts "Docs generated at #{path}"
path
end
|
#each_task ⇒ Object
267
268
269
270
271
272
|
# File 'lib/crew/home.rb', line 267
def each_task
Dir[File.join(@task_path, "**/*.crew")].each do |file|
name = path_to_name(@task_path, file)
yield Task.new(self, name, file)
end
end
|
#each_task_for_source(source) ⇒ Object
274
275
276
277
278
279
|
# File 'lib/crew/home.rb', line 274
def each_task_for_source(source)
Dir[File.join(source, "**/*.crew")].each do |file|
name = path_to_name(source, file)
yield Task.new(self, name, file)
end
end
|
#find_and_add_task(name) ⇒ Object
257
258
259
260
261
262
263
264
265
|
# File 'lib/crew/home.rb', line 257
def find_and_add_task(name)
if parts = find_task(name)
add_task(*parts)
task_path = File.join(*parts)
Task.new(self, name, task_path)
else
raise "no such task `#{name}'"
end
end
|
#find_task(name) ⇒ Object
253
254
255
|
# File 'lib/crew/home.rb', line 253
def find_task(name)
find_task_in_paths(name, [@task_path] + @sources)
end
|
#in_context(context_or_name, opts = {}) ⇒ Object
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
# File 'lib/crew/home.rb', line 287
def in_context(context_or_name, opts = {})
previous_context = @context
@context = case context_or_name
when Context
context_or_name
when Module
Context.new(self, context_or_name.to_s) do
adapter(context_or_name, opts)
end
when String
context_name = context_or_name
@contexts.key?(context_name) ?
@contexts[context_name] : raise(UnknownContextError.new(context_name))
end
begin
raise UnknownContextError.new(context_or_name) if @context.nil?
yield
ensure
@context = previous_context
end
end
|
87
88
89
90
91
92
|
# File 'lib/crew/home.rb', line 87
def perform_setup
@in_setup = true
yield
ensure
@in_setup = false
end
|
#run(context_name, name, *args) ⇒ Object
242
243
244
245
246
247
|
# File 'lib/crew/home.rb', line 242
def run(context_name, name, *args)
context_name ||= @default_context_name
in_context(context_name) do
@context.task(name).run!(*args)
end
end
|
#run_in_context(context_or_name, label, opts = {}, &blk) ⇒ Object
281
282
283
284
285
|
# File 'lib/crew/home.rb', line 281
def run_in_context(context_or_name, label, opts = {}, &blk)
in_context(context_or_name, opts) do
@context.run(label, &blk)
end
end
|
#shell(context_name) ⇒ Object
114
115
116
117
118
119
120
121
|
# File 'lib/crew/home.rb', line 114
def shell(context_name)
context_name ||= @default_context_name
in_context(context_name) do
@context.run "shell" do
pry
end
end
end
|
#task_available ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/crew/home.rb', line 100
def task_available
names = Set.new
list do |task|
names << task.name
end
@sources.each do |source|
each_task_for_source(source) do |task|
task.from_source = source
yield task, names.include?(task.name)
end
end
end
|
#task_diff ⇒ Object
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/crew/home.rb', line 173
def task_diff
only_local = []
different = []
each_task do |task|
source_path = find_task_in_paths(task.name, @sources)
if source_path.nil?
only_local << task.path
elsif File.read(File.join(*source_path)) != File.read(task.path)
different << [task.path, File.join(*source_path)]
end
end
puts "Diff -- #{only_local.size} tasks only present locally, #{different.size} tasks different"
unless only_local.empty?
only_local.each do |dest|
puts "Only present #{dest}"
end
end
unless different.empty?
different.each do |(dest, src)|
puts "Comparing #{dest} with #{src}"
puts `diff #{dest} #{src}`
end
end
end
|
#task_edit(name) ⇒ Object
129
130
131
132
133
134
135
136
137
|
# File 'lib/crew/home.rb', line 129
def task_edit(name)
raise "$EDITOR not defined" unless ENV.key?('EDITOR')
if source_path = find_task_in_paths(name, @task_path)
puts "Editing #{source_path[1]} into #{source_path[0]}"
exec "#{ENV['EDITOR']} #{File.join(source_path)}"
else
raise "unable to find task #{name} to install"
end
end
|
#task_info(name) ⇒ Object
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/crew/home.rb', line 139
def task_info(name)
if source_path = find_task_in_paths(name, @task_path)
puts "Task installed, located at #{File.join(*source_path)}"
else
puts "Task #{name} is not installed"
puts
@sources.each do |source|
path = name_to_path(name)
task_name = File.join(source, path)
if File.exist?(task_name)
puts "Task available in #{@source} at #{task_name}"
else
puts "Not found in #{@source}"
end
end
end
end
|
#task_install(name) ⇒ Object
206
207
208
209
210
211
212
213
214
215
216
217
|
# File 'lib/crew/home.rb', line 206
def task_install(name)
if source_path = find_task_in_paths(name, @sources)
puts "Installing #{source_path[1]} into #{source_path[0]}"
if add_task(source_path[0], source_path[1])
puts "Task installed!"
else
puts "Nothing done, aready installed"
end
else
raise "unable to find task #{name} to install"
end
end
|
#task_install_all ⇒ Object
219
220
221
222
223
224
225
226
|
# File 'lib/crew/home.rb', line 219
def task_install_all
@sources.each do |source|
each_task_for_source(source) do |task|
task.from_source = source
install task.name
end
end
end
|
#task_list ⇒ Object
94
95
96
97
98
|
# File 'lib/crew/home.rb', line 94
def task_list
each_task_for_source(@task_path) do |task|
yield task
end
end
|
#task_new(name) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/crew/home.rb', line 47
def task_new(name)
dest, path = find_task_in_paths(name, @task_path)
if path
raise "Cannot create #{name}, task already in the way"
else
path = name_to_path(name)
template = File.read(File.expand_path("../template/task.crew.erb", __FILE__))
out = ERB.new(template).result
puts "Writing #{path} file"
out_path = File.join(@task_path, path)
FileUtils.mkdir_p(File.dirname(out_path))
File.open(out_path, "w") do |f|
f << out
end
end
end
|
#task_remove(name) ⇒ Object
228
229
230
231
232
233
234
235
236
|
# File 'lib/crew/home.rb', line 228
def task_remove(name)
if path = File.join(@task_path, name_to_path(name))
if File.exist?(path)
File.unlink(path)
end
else
raise "unable to find task #{name} to install"
end
end
|
#task_update(name = nil) ⇒ Object
198
199
200
201
202
203
204
|
# File 'lib/crew/home.rb', line 198
def task_update(name = nil)
if name
update_task(name)
else
update_all
end
end
|
#test(opts = {}) ⇒ Object
161
162
163
164
165
166
167
|
# File 'lib/crew/home.rb', line 161
def test(opts = {})
clean_tests
@testers.each do |name, tester|
puts "Running test suite #{name}..."
tester.run(opts)
end
end
|
#test_path ⇒ Object
169
170
171
|
# File 'lib/crew/home.rb', line 169
def test_path
File.join(data_path)
end
|