nucleon 0.1.9 → 0.1.10

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.
@@ -16,6 +16,7 @@ class Save < Nucleon.plugin_class(:action)
16
16
  :commit_failure,
17
17
  :push_failure
18
18
 
19
+ register :path, :str, Dir.pwd
19
20
  register :files, :array, '.'
20
21
 
21
22
  project_config
@@ -34,10 +35,10 @@ class Save < Nucleon.plugin_class(:action)
34
35
  # Operations
35
36
 
36
37
  def execute
37
- super do |node, network|
38
+ super do
38
39
  info('nucleon.actions.save.start')
39
40
 
40
- if project = project_load(Dir.pwd, false)
41
+ if project = project_load(settings[:path], false, false)
41
42
  if commit(project, settings[:files])
42
43
  myself.status = code.push_failure unless push(project)
43
44
  else
@@ -12,7 +12,8 @@ class Update < Nucleon.plugin_class(:action)
12
12
  super do
13
13
  codes :project_failure
14
14
 
15
- project_options
15
+ register :path, :str, Dir.pwd
16
+ project_config
16
17
  end
17
18
  end
18
19
 
@@ -20,10 +21,10 @@ class Update < Nucleon.plugin_class(:action)
20
21
  # Operations
21
22
 
22
23
  def execute
23
- super do |node, network|
24
+ super do
24
25
  info('nucleon.actions.update.start')
25
26
 
26
- project = project_load(Dir.pwd, true)
27
+ project = project_load(settings[:path], false, true)
27
28
  myself.status = code.project_failure unless project
28
29
  end
29
30
  end
@@ -129,7 +129,16 @@ class Bash < Plugin::Command
129
129
 
130
130
  def exec(options = {}, overrides = nil, &code)
131
131
  config = Config.ensure(options)
132
- Nucleon.cli_run(build(export, overrides), config.import({ :ui => @ui }), &code)
132
+ result = Nucleon.cli_run(build(export, overrides), config.import({ :ui => @ui }), &code)
133
+
134
+ if result
135
+ logger.debug("Command status: #{result.status}")
136
+ logger.debug("Command output:\n#{result.output}")
137
+ logger.debug("Command errors:\n#{result.errors}")
138
+ else
139
+ logger.debug("Command returned no result")
140
+ end
141
+ result
133
142
  end
134
143
 
135
144
  #-----------------------------------------------------------------------------
@@ -7,28 +7,28 @@ class Git < Plugin::Project
7
7
  # Project plugin interface
8
8
 
9
9
  def normalize(reload)
10
- super
10
+ unless reload
11
+ @cli = Util::Liquid.new do |method, args, &code|
12
+ options = {}
13
+ options = args.shift if args.length > 0
14
+ git_exec(method, options, args, &code)
15
+ end
16
+ end
17
+ super
11
18
  end
12
19
 
13
20
  #-----------------------------------------------------------------------------
14
21
  # Git interface (local)
15
22
 
16
23
  def ensure_git(reset = false)
17
- if reset || @git_lib.nil?
18
- @git_lib = nil
19
-
24
+ if reset || @repo.nil?
20
25
  if directory.empty?
21
26
  logger.warn("Can not manage Git project at #{directory} as it does not exist")
22
27
  else
23
28
  logger.debug("Ensuring Git instance to manage #{directory}")
24
- @git_lib = Util::Git.new(directory)
25
-
26
- if ! @git_lib.nil? && get(:create, false)
27
- unless File.directory?(directory) && @git_lib.git.exist?
28
- FileUtils.mkdir_p(directory) unless File.directory?(directory)
29
- @git_lib.git.init({ :bare => false })
30
- end
31
- end
29
+ @repo = Util::Git.load(directory, {
30
+ :create => get(:create, false)
31
+ })
32
32
  end
33
33
  end
34
34
  return myself
@@ -40,7 +40,7 @@ class Git < Plugin::Project
40
40
 
41
41
  def can_persist?
42
42
  ensure_git
43
- return true unless @git_lib.nil?
43
+ return true unless @repo.nil?
44
44
  return false
45
45
  end
46
46
 
@@ -98,7 +98,11 @@ class Git < Plugin::Project
98
98
 
99
99
  def new?(reset = false)
100
100
  if get(:new, nil).nil? || reset
101
- set(:new, git.native(:rev_parse, { :all => true }).empty?)
101
+ result = cli.rev_parse({ :all => true })
102
+
103
+ if result && result.status == code.success
104
+ set(:new, result.output.empty?)
105
+ end
102
106
  end
103
107
  get(:new, false)
104
108
  end
@@ -106,17 +110,17 @@ class Git < Plugin::Project
106
110
  #-----------------------------------------------------------------------------
107
111
  # Property accessors / modifiers
108
112
 
109
- def lib
110
- return @git_lib
113
+ def repo
114
+ return @repo if can_persist?
115
+ return nil
111
116
  end
112
-
117
+ protected :repo
118
+
113
119
  #---
114
120
 
115
- def git
116
- return lib.git if can_persist?
117
- return nil
121
+ def cli
122
+ @cli
118
123
  end
119
- protected :git
120
124
 
121
125
  #---
122
126
 
@@ -131,7 +135,9 @@ class Git < Plugin::Project
131
135
 
132
136
  def config(name, options = {})
133
137
  return super do |config|
134
- git.config(config.export, name)
138
+ result = cli.config(config.export, name)
139
+ next Util::Data.value(result.output) if result.status == code.success
140
+ nil
135
141
  end
136
142
  end
137
143
 
@@ -139,7 +145,8 @@ class Git < Plugin::Project
139
145
 
140
146
  def set_config(name, value, options = {})
141
147
  return super do |config, processed_value|
142
- git.config(config.export, name, processed_value)
148
+ result = cli.config(config.export, name, processed_value)
149
+ result.status == code.success
143
150
  end
144
151
  end
145
152
 
@@ -147,7 +154,8 @@ class Git < Plugin::Project
147
154
 
148
155
  def delete_config(name, options = {})
149
156
  return super do |config|
150
- git.config(config.import({ :remove_section => true }).export, name)
157
+ result = cli.config(config.import({ :remove_section => true }).export, name)
158
+ result.status == code.success
151
159
  end
152
160
  end
153
161
 
@@ -160,26 +168,26 @@ class Git < Plugin::Project
160
168
  if new?
161
169
  logger.debug("Project has no sub project configuration yet (has not been committed to)")
162
170
  else
163
- commit = lib.commit(revision)
164
- blob = commit.tree/'.gitmodules' unless commit.nil?
171
+ gitmodules_file = File.join(directory, '.gitmodules')
172
+
173
+ gitmodules_data = ''
174
+ gitmodules_data = Util::Disk.read(gitmodules_file) if File.exists?(gitmodules_file)
165
175
 
166
- if blob
167
- logger.debug("Houston, we have a Git blob!")
176
+ unless gitmodules_data.empty?
177
+ logger.debug("Houston, we have some gitmodules!")
168
178
 
169
- lines = blob.data.gsub(/\r\n?/, "\n" ).split("\n")
179
+ lines = gitmodules_data.gsub(/\r\n?/, "\n" ).split("\n")
170
180
  current = nil
171
181
 
172
182
  lines.each do |line|
173
183
  if line =~ /^\[submodule "(.+)"\]$/
174
184
  current = $1
175
185
  result[current] = {}
176
- result[current]['id'] = (commit.tree/current).id
177
186
 
178
187
  logger.debug("Reading: #{current}")
179
188
 
180
- elsif line =~ /^\t(\w+) = (.+)$/
181
- result[current][$1] = $2
182
- result[current]['id'] = (commit.tree/$2).id if $1 == 'path'
189
+ elsif line =~ /^\s*(\w+)\s*=\s*(.+)\s*$/
190
+ result[current][$1] = $2
183
191
  end
184
192
  end
185
193
  end
@@ -195,11 +203,14 @@ class Git < Plugin::Project
195
203
  return super do
196
204
  if new?
197
205
  logger.debug("Project has no current revision yet (has not been committed to)")
198
- nil
199
-
206
+ nil
200
207
  else
201
- current_revision = git.native(:rev_parse, { :abbrev_ref => true }, 'HEAD').strip
202
-
208
+ current_revision = nil
209
+ result = cli.rev_parse({ :abbrev_ref => true }, 'HEAD')
210
+
211
+ if result && result.status == code.success
212
+ current_revision = result.output
213
+ end
203
214
  logger.debug("Current revision: #{current_revision}")
204
215
  current_revision
205
216
  end
@@ -213,13 +224,11 @@ class Git < Plugin::Project
213
224
  if new?
214
225
  logger.debug("Project can not be checked out (has not been committed to)")
215
226
  else
216
- unless lib.bare
217
- success = safe_exec(false) do
218
- git.checkout({ :raise => true }, revision)
219
- end
227
+ unless repo.bare?
228
+ result = cli.checkout({}, revision)
220
229
  end
221
230
  end
222
- success
231
+ result && result.status == code.success
223
232
  end
224
233
  end
225
234
 
@@ -227,27 +236,29 @@ class Git < Plugin::Project
227
236
 
228
237
  def commit(files = '.', options = {})
229
238
  return super do |config, time, user, message|
230
- safe_exec(false) do
231
- git.reset({}, 'HEAD') # Clear the index so we get a clean commit
239
+ cli.reset({}, 'HEAD') unless new? # Clear the index so we get a clean commit
232
240
 
233
- files = array(files)
241
+ files = array(files)
234
242
 
235
- logger.debug("Adding files to Git index")
236
-
237
- git.add({ :raise => true }, files) # Get all added and updated files
238
- git.add({ :update => true, :raise => true }, files) # Get all deleted files
243
+ logger.debug("Adding files to Git index")
244
+
245
+ cli.add({}, *files) # Get all added and updated files
246
+ cli.add({ :update => true }, *files) # Get all deleted files
239
247
 
240
- commit_options = {
241
- :raise => true,
242
- :m => "#{time} by <#{user}> - #{message}",
243
- :allow_empty => config.get(:allow_empty, false)
244
- }
245
- commit_options[:author] = config[:author] if config.get(:author, false)
248
+ commit_options = {
249
+ :m => "<#{user}> #{message}",
250
+ :allow_empty => config.get(:allow_empty, false)
251
+ }
252
+ commit_options[:author] = config[:author] if config.get(:author, false)
246
253
 
247
- logger.debug("Composing commit options: #{commit_options.inspect}")
248
- git.commit(commit_options)
254
+ logger.debug("Composing commit options: #{commit_options.inspect}")
255
+ result = cli.commit(commit_options)
249
256
 
257
+ if result.status == code.success
250
258
  new?(true)
259
+ true
260
+ else
261
+ false
251
262
  end
252
263
  end
253
264
  end
@@ -265,16 +276,19 @@ class Git < Plugin::Project
265
276
 
266
277
  def add_subproject(path, url, revision, options = {})
267
278
  return super do |config|
268
- safe_exec(false) do
269
- branch_options = ''
270
- branch_options = [ '-b', config[:revision] ] if config.get(:revision, false)
279
+ branch_options = ''
280
+ branch_options = [ '-b', config[:revision] ] if config.get(:revision, false)
271
281
 
272
- path = config[:path]
273
- url = config[:url]
282
+ path = config[:path]
283
+ url = config[:url]
274
284
 
275
- git.submodule({ :raise => true }, 'add', *branch_options, url, path)
285
+ result = cli.submodule({}, 'add', *branch_options, url, path)
276
286
 
277
- config.set(:files, [ '.gitmodules', path ])
287
+ if result.status == code.success
288
+ config.set(:files, [ '.gitmodules', path ])
289
+ true
290
+ else
291
+ false
278
292
  end
279
293
  end
280
294
  end
@@ -283,33 +297,30 @@ class Git < Plugin::Project
283
297
 
284
298
  def delete_subproject(path)
285
299
  return super do |config|
286
- safe_exec(false) do
287
- path = config[:path]
288
- submodule_key = "submodule.#{path}"
300
+ path = config[:path]
301
+ submodule_key = "submodule.#{path}"
289
302
 
290
- logger.debug("Deleting Git configurations for #{submodule_key}")
291
- delete_config(submodule_key)
292
- delete_config(submodule_key, { :file => '.gitmodules' })
303
+ logger.debug("Deleting Git configurations for #{submodule_key}")
304
+ delete_config(submodule_key)
305
+ delete_config(submodule_key, { :file => '.gitmodules' })
293
306
 
294
- logger.debug("Cleaning Git index cache for #{path}")
295
- git.rm({ :cached => true }, path)
307
+ logger.debug("Cleaning Git index cache for #{path}")
308
+ cli.rm({ :cached => true }, path)
296
309
 
297
- logger.debug("Removing Git submodule directories")
298
- FileUtils.rm_rf(File.join(directory, path))
299
- FileUtils.rm_rf(File.join(git.git_dir, 'modules', path))
310
+ logger.debug("Removing Git submodule directories")
311
+ FileUtils.rm_rf(File.join(directory, path))
312
+ FileUtils.rm_rf(File.join(repo.path, 'modules', path))
300
313
 
301
- config.set(:files, [ '.gitmodules', path ])
302
- end
314
+ config.set(:files, [ '.gitmodules', path ])
303
315
  end
304
316
  end
305
317
 
306
318
  #---
307
319
 
308
- def update_subprojects
309
- return super do
310
- safe_exec(false) do
311
- git.submodule({ :raise => true, :timeout => false }, 'update', '--init', '--recursive')
312
- end
320
+ def update_subprojects(options = {})
321
+ return super do |config|
322
+ result = cli.submodule({}, 'update', '--init', '--recursive')
323
+ result.status == code.success
313
324
  end
314
325
  end
315
326
 
@@ -318,9 +329,9 @@ class Git < Plugin::Project
318
329
 
319
330
  def init_remotes
320
331
  return super do
321
- origin_url = config('remote.origin.url').strip
332
+ origin_url = config('remote.origin.url')
322
333
 
323
- logger.debug("Original origin remote url: #{origin_url}")
334
+ logger.debug("Original origin remote url: #{origin_url}") if origin_url
324
335
  origin_url
325
336
  end
326
337
  end
@@ -329,8 +340,8 @@ class Git < Plugin::Project
329
340
 
330
341
  def remote(name)
331
342
  return super do
332
- url = config("remote.#{name}.url").strip
333
- url.empty? ? nil : url
343
+ url = config("remote.#{name}.url")
344
+ url.nil? || url.empty? ? nil : url
334
345
  end
335
346
  end
336
347
 
@@ -338,9 +349,8 @@ class Git < Plugin::Project
338
349
 
339
350
  def set_remote(name, url)
340
351
  return super do |processed_url|
341
- safe_exec(false) do
342
- git.remote({ :raise => true }, 'add', name.to_s, processed_url)
343
- end
352
+ result = cli.remote({}, 'add', name, processed_url)
353
+ result.status == code.success
344
354
  end
345
355
  end
346
356
 
@@ -348,14 +358,12 @@ class Git < Plugin::Project
348
358
 
349
359
  def add_remote_url(name, url, options = {})
350
360
  return super do |config, processed_url|
351
- safe_exec(false) do
352
- git.remote({
353
- :raise => true,
354
- :add => true,
355
- :delete => config.get(:delete, false),
356
- :push => config.get(:push, false)
357
- }, 'set-url', name.to_s, processed_url)
358
- end
361
+ result = cli.remote({
362
+ :add => true,
363
+ :delete => config.get(:delete, false),
364
+ :push => config.get(:push, false)
365
+ }, 'set-url', name, processed_url)
366
+ result.status == code.success
359
367
  end
360
368
  end
361
369
 
@@ -363,13 +371,13 @@ class Git < Plugin::Project
363
371
 
364
372
  def delete_remote(name)
365
373
  return super do
366
- if config("remote.#{name}.url").empty?
374
+ remote = remote(name)
375
+ if ! remote || remote.empty?
367
376
  logger.debug("Project can not delete remote #{name} because it does not exist yet")
368
377
  true
369
378
  else
370
- safe_exec(false) do
371
- git.remote({ :raise => true }, 'rm', name.to_s)
372
- end
379
+ result = cli.remote({}, 'rm', name)
380
+ result.status == code.success
373
381
  end
374
382
  end
375
383
  end
@@ -386,20 +394,11 @@ class Git < Plugin::Project
386
394
  #-----------------------------------------------------------------------------
387
395
  # SSH operations
388
396
 
389
- def git_fetch(remote = :edit, options = {})
397
+ def git_fetch(remote = :edit, options = {}, &block)
390
398
  config = Config.ensure(options)
391
399
  local_revision = config.get(:revision, get(:revision, :master))
392
400
 
393
- result = Nucleon.command({
394
- :command => :git,
395
- :data => { 'git-dir=' => git.git_dir },
396
- :subcommand => {
397
- :command => :fetch,
398
- :args => [ remote ]
399
- }
400
- }, config.get(:provider, Nucleon.type_default(:command))).exec(config.import({ :quiet => true })) do |op, command, data|
401
- block_given? ? yield(op, command, data) : true
402
- end
401
+ result = cli.fetch({}, remote, &block)
403
402
 
404
403
  if result.status == code.success
405
404
  new?(true)
@@ -412,30 +411,20 @@ class Git < Plugin::Project
412
411
 
413
412
  #---
414
413
 
415
- def pull(remote = :edit, options = {})
414
+ def pull(remote = :edit, options = {}, &block)
416
415
  return super do |config, processed_remote|
417
416
  success = false
418
417
 
419
418
  if new? || get(:create, false)
420
419
  success = git_fetch(processed_remote, config)
421
420
  else
422
- flags = []
423
- flags << :tags if config.get(:tags, true)
421
+ pull_options = {}
422
+ pull_options[:tags] = true if config.get(:tags, true)
424
423
 
425
424
  local_revision = config.get(:revision, get(:revision, :master))
426
425
 
427
426
  if checkout(local_revision)
428
- result = Nucleon.command({
429
- :command => :git,
430
- :data => { 'git-dir=' => git.git_dir },
431
- :subcommand => {
432
- :command => :pull,
433
- :flags => flags,
434
- :args => [ processed_remote, local_revision ]
435
- }
436
- }, config.get(:provider, Nucleon.type_default(:command))).exec(config.import({ :quiet => true })) do |op, command, data|
437
- block_given? ? yield(op, command, data) : true
438
- end
427
+ result = cli.pull(pull_options, processed_remote, local_revision, &block)
439
428
 
440
429
  if result.status == code.success
441
430
  new?(true)
@@ -449,26 +438,15 @@ class Git < Plugin::Project
449
438
 
450
439
  #---
451
440
 
452
- def push(remote = :edit, options = {})
441
+ def push(remote = :edit, options = {}, &block)
453
442
  return super do |config, processed_remote|
454
443
  push_branch = config.get(:revision, '')
455
444
 
456
- flags = []
457
- flags << :all if push_branch.empty?
458
- flags << :tags if ! push_branch.empty? && config.get(:tags, true)
459
-
460
- result = Nucleon.command({
461
- :command => :git,
462
- :data => { 'git-dir=' => git.git_dir },
463
- :subcommand => {
464
- :command => :push,
465
- :flags => flags,
466
- :args => [ processed_remote, push_branch ]
467
- }
468
- }, config.get(:provider, :bash)).exec(config.import({ :quiet => true })) do |op, command, data|
469
- block_given? ? yield(op, command, data) : true
470
- end
445
+ push_options = {}
446
+ push_options[:all] = true if push_branch.empty?
447
+ push_options[:tags] = true if ! push_branch.empty? && config.get(:tags, true)
471
448
 
449
+ result = cli.push(push_options, processed_remote, push_branch, &block)
472
450
  result.status == code.success
473
451
  end
474
452
  end
@@ -495,6 +473,62 @@ class Git < Plugin::Project
495
473
  end
496
474
  end
497
475
  end
476
+
477
+ #---
478
+
479
+ def git_exec(command, options = {}, args = [])
480
+ result = nil
481
+
482
+ if can_persist?
483
+ check_value = lambda do |value|
484
+ next false if value.nil?
485
+ next false unless value.is_a?(String) || value.is_a?(Symbol)
486
+ next false if value.to_s.empty?
487
+ true
488
+ end
489
+
490
+ localize(repo.workdir) do
491
+ flags = []
492
+ data = {}
493
+ processed_args = []
494
+
495
+ options.each do |key, value|
496
+ cli_option = key.to_s.gsub('_', '-')
497
+
498
+ if value.is_a?(TrueClass) || value.is_a?(FalseClass)
499
+ flags << cli_option if value == true
500
+
501
+ elsif check_value.call(value)
502
+ data[cli_option] = value.to_s
503
+ end
504
+ end
505
+
506
+ args.each do |value|
507
+ if check_value.call(value)
508
+ processed_args << value.to_s
509
+ end
510
+ end
511
+
512
+ command_provider = get(:command_provider, Nucleon.type_default(:command))
513
+ quiet = get(:quiet, true)
514
+
515
+ result = Nucleon.command({
516
+ :command => :git,
517
+ :data => { 'git-dir=' => repo.path },
518
+ :subcommand => {
519
+ :command => command.to_s.gsub('_', '-'),
520
+ :flags => flags,
521
+ :data => data,
522
+ :args => processed_args
523
+ }
524
+ }, command_provider).exec({ :quiet => quiet }) do |op, cli_command, cli_data|
525
+ block_given? ? yield(op, cli_command, cli_data) : true
526
+ end
527
+ end
528
+ end
529
+ result
530
+ end
531
+ protected :git_exec
498
532
  end
499
533
  end
500
534
  end