Class: Release::Gem::Cli::VcsAction

Inherits:
Object
  • Object
show all
Defined in:
lib/release/gem/vcs_cli_action.rb

Instance Method Summary collapse

Constructor Details

#initialize(root, opts = { }) ⇒ VcsAction

Returns a new instance of VcsAction.



10
11
12
13
14
15
16
17
# File 'lib/release/gem/vcs_cli_action.rb', line 10

def initialize(root, opts = {  })
  opts = {} if opts.nil?
  opts[:ui] = TTY::Prompt.new
  @inst = Action::VcsAction.new(root,opts)
  @prmt = TTY::Prompt.new
  @color = opts[:msgColor] || :yellow
  @discardColor = opts[:discardColor] || false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mtd, *args, &block) ⇒ Object

push



588
589
590
# File 'lib/release/gem/vcs_cli_action.rb', line 588

def method_missing(mtd, *args, &block)
  @inst.send(mtd, *args, &block)
end

Instance Method Details

#add(*input, &block) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/release/gem/vcs_cli_action.rb', line 64

def add(*input, &block)
  @inst.add do |ops, *args|
    preset = false
    if block
      res = block.call(ops, *args)
      if res.nil?
        preset = true
      else
        res
      end
    else
      preset = true
    end

    if preset
      case ops
      when :select_files_to_add

        mfiles = args.first
        @prmt.puts pmsg("\n Files already added to staging : ")
        mfiles[:staged].each do |k,v|
          v.each do |vv|
            @prmt.puts " * #{vv}"
          end
        end

        @prmt.puts ""
        res = []
        [:modified, :new, :deleted].each do |cat|
          mfiles[cat].each do |k,v|
            v.each do |vv|
              res << vv 
            end
          end
        end

        sel = @prmt.multi_select pmsg("\n Following are files that could be added to version control : ") do |m|

          res.sort.each do |f|
            m.choice f, f.path
          end

          m.choice "Done", :done
          m.choice "Abort", :abort
        end

        if sel.include?(:abort)
          raise Release::Gem::Abort, "User aborted"
        else
          sel
        end

      when :files_added_successfully
        v = args.first
        @prmt.puts "\n #{v[:count]} file(s) added successfully.\n#{v[:output]}"

      when :files_failed_to_be_added
        v = args.first
        @prmt.puts "\n File(s) failed to be added. Error was : \n#{v[:output]}"

      end

    end

  end # block of add()
end

#commit(*args, &block) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/release/gem/vcs_cli_action.rb', line 375

def commit(*args, &block)
  res = @inst.commit do |ops, *args|
    
    preset = false
    if block
      res = block.call(ops, *args)
      if res.nil?
        preset = true
      else
        res
      end
    else
      preset = true
    end

    if preset

      case ops
      when :select_files_to_commit
        mfiles = args.first
        @prmt.puts pmsg("\n Files already added to staging : ")
        mfiles[:staged].each do |k,v|
          v.each do |vv|
            @prmt.puts " * #{vv}"
          end
        end

        @prmt.puts ""

        files = []
        [:modified, :new, :deleted].each do |cat|
          mfiles[cat].each do |k,v|
            v.each do |vv|
              files << vv
              #m.choice vv, vv.path
            end
          end

        end


        sel = @prmt.multi_select pmsg("\n Following are files that could be added to version control : ") do |m|
          
          files.sort.each do |f|
            m.choice f, f.path
          end

          m.choice "Skip", :skip #if mfiles[:counter] == 0
          m.choice "Done", :done
          m.choice "Abort", :abort
        end

        if sel.include?(:abort)
          raise Release::Gem::Abort, "User aborted"
        elsif sel.include?(:skip)
          :skip 
        else
          res = :done if sel.include?(:done)
          s = sel.clone
          s.delete_if { |e| e == :done }
          if not_empty?(s)
            st, cres = add_to_staging(*s) if not_empty?(s)
            if st
              @prmt.puts pmsg("\n Files added successfully", :green)
            else
              @prmt.puts pmsg("\n Files failed to be added. Message was : #{cres}", :red)
            end
          end

          res

        end

      when :commit_message
        msg = ""
        loop do
          msg = @prmt.ask(pmsg("\n Commit message : "), required: true)
          confirm = @prmt.yes?(pmsg(" Commit message : #{msg}\n Proceed? No to provide a new commit message "))
          if confirm
            break
          end
        end

        msg

      when :staged_elements_of_commit

        elements = args.first
        @prmt.puts pmsg("\n Following files/directories shall be committed in this session : ")
        elements.each do |k,v|
          v.each do |vv|
            @prmt.puts " * #{vv}"
          end
        end

      when :commit_successful
        @prmt.puts pmsg("\n Changes committed",:green)
        @prmt.puts args.first

      when :commit_failed
        @prmt.puts pmsg("\n Changes failed to be committed. Error was : #{args.first}")

      end
    end
  end # commit

end

#delete_file(*args, &block) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/release/gem/vcs_cli_action.rb', line 295

def delete_file(*args, &block)
  res = @inst.delete_file do |ops, *args|
    
    preset = false
    if block
      res = block.call(ops, *args)
      if res.nil?
        preset = true
      else
        res
      end
    else
      preset = true
    end

    if preset

      case ops
      when :select_files_to_delete
        mfiles = args.first

        files = []
        [:new, :staged, :modified].each do |cat|
          mfiles[cat].each do |k,v|
            v.each do |vv|
              files << vv
              #m.choice vv, vv
            end
          end

        end


        sel = @prmt.multi_select pmsg("\n Following are files that could be deleted : ") do |m|
          files.sort do |f|
            m.choice f, f
          end
          m.choice "Done", :done
          m.choice "Abort", :abort
        end


        if sel.include?(:abort)
          raise Release::Gem::Abort, "User aborted"
        else
          sel
        end

      when :confirm_nontrack_delete
        v = args.first
        @prmt.yes?(pmsg("\n Delete non-tracked file '#{v}'? "))

      when :nontrack_file_deleted
        v = args.first
        @prmt.puts pmsg("\n Non tracked file '#{v}' deleted ")

      when :confirm_vcs_delete
        v = args.first
        not @prmt.no?(pmsg("\n Delete version-controlled file '#{v}'?\n After delete the file will no longer keeping track of changes. "))

      when :vcs_file_deleted
        v = args.first
        @prmt.puts pmsg("\n Version-controlled file '#{v}' deleted ")

      when :confirm_staged_delete
        v = args.first
        @prmt.yes?(pmsg("\n Delete staged file '#{v}'?\n After delete the file shall be removed from staging. The file will still exist physically "))

      when :staged_file_deleted
        v = args.first
        @prmt.puts pmsg("\n Staged file '#{v}' deleted ")

      end
    end
  end # delete_file block

end

#diff(*input, &block) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/release/gem/vcs_cli_action.rb', line 131

def diff(*input, &block)
  @inst.diff do |ops, *args|
    preset = false
    if block
      res = block.call(ops, *args)
      if res.nil?
        preset = true
      else
        res
      end
    else
      preset = true
    end

    if preset
      case ops
      when :select_files_to_diff

        mfiles = args.first
        res = []
        [:modified, :staged].each do |cat|
          mfiles[cat].each do |k,v|
            v.each do |vv|
              res << vv 
            end
          end
        end

        sel = @prmt.multi_select pmsg("\n Select files for diff operation : ") do |m|

          res.sort.each do |f|
            m.choice f, f.path
          end

          m.choice "Done", :done
          m.choice "Abort", :abort
        end

        if sel.include?(:abort)
          raise Release::Gem::Abort, "User aborted"
        else
          sel
        end

      when :diff_file_result
        v = args.first
        @prmt.puts "Diff result for file '#{v[:file]}'"
        puts v[:output].light_blue
        STDIN.gets

      when :diff_file_error
        v = args.first
        @prmt.puts "\n Failed to diff file '#{v[:file]}'. Error was : \n#{v[:output]}"

      end

    end

  end # block of add()
end

#exec(&block) ⇒ Object



19
20
21
# File 'lib/release/gem/vcs_cli_action.rb', line 19

def exec(&block)
  instance_eval(&block) if block
end

#ignore(*input, &block) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/release/gem/vcs_cli_action.rb', line 192

def ignore(*input, &block)
  @inst.ignore do |ops, *args|
    preset = false
    if block
      res = block.call(ops, *args)
      if res.nil?
        preset = true
      else
        res
      end
    else
      preset = true
    end

    if preset
      case ops
      when :select_files_to_ignore

        mfiles = args.first
        sel = @prmt.multi_select pmsg("\n Following are files that could be ignored : ") do |m|

          mfiles[:files].sort.each do |v|
            m.choice v, v.path
          end


          m.choice "Done", :done
          m.choice "Abort", :abort
        end

        if sel.include?(:abort)
          raise Release::Gem::Abort, "User aborted"
        else
          sel
        end

      when :files_ignored_successfully
        v = args.first
        @prmt.puts "\n #{v[:count]} file(s) ignored successfully.\n#{v[:output]}"

      when :files_failed_to_be_ignored
        v = args.first
        @prmt.puts "\n File(s) failed to be ignored. Error was : \n#{v[:output]}"

      end

    end

  end # block of add()
end

#manage_workspace(*args, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/release/gem/vcs_cli_action.rb', line 23

def manage_workspace(*args, &block)
 
  loop do

    ops = @prmt.select(pmsg("\n Please select a VCS workspace operation : ")) do |m|

      m.choice "Add", :add
      m.choice "Ignore", :ignore
      m.choice "Diff", :diff
      m.choice "Remove staged file", :remove_staged
      m.choice "Delete file", :del
      m.choice "Commit", :commit
      m.choice "Done", :done
      m.choice "Abort", :abort

    end

    case ops
    when :abort
      raise Release::Gem::Abort, "User aborted"
    when :add
      add
    when :diff
      diff
    when :ignore
      ignore
    when :remove_staged
      remove_staged
    when :del
      delete_file
    when :commit
      commit
      break
    when :done
      break
    end

  end

end

#pmsg(msg, color = nil) ⇒ Object



592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# File 'lib/release/gem/vcs_cli_action.rb', line 592

def pmsg(msg, color = nil)
  if not msg.nil?
    if @discardColor == true
      msg
    else
      if not_empty?(color)
        msg.send(color)
      elsif not_empty?(@msgColor)
        msg.send(@msgColor)
      else
        msg
      end
    end
  end
end

#push(*args, &block) ⇒ Object

tag



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/release/gem/vcs_cli_action.rb', line 525

def push(*args, &block)

  @inst.push do |ops, *args|
    preset = false
    if block
      res = block.call(ops, *args)
      if res.nil?
        preset = true
      else
        res
      end
    else
      preset = true
    end

    if preset

      case ops
      when :select_remote
        val = args.first
        sel = @prmt.select(pmsg("\n Please select one of the remote config below : ")) do |m|
          val.each do |k,v|
            m.choice k, k
          end
          m.choice "Skip pushing source code", :skip
          m.choice "Abort", :abort
        end
        raise Release::Gem::Abort, "User aborted" if sel == :abort

        sel

      when :no_remote_repos_defined
        add = @prmt.yes?(pmsg("\n No remote configuration defined. Add one now?"))
        if add
          name = @prmt.ask(pmsg("\n Name of the repository : "), value: "origin", required: true)
          url = @prmt.ask(pmsg(" URL of the repository : "), required: true)

          st, res = add_remote(name, url)
          if st
            @prmt.puts pmsg("\n Remote configuration added successfully",:green)
            name
          else
            raise Release::Gem::Abort, "Failed to add remote configuration. Error was : #{res}"
          end
        end

      when :push_successful
        @prmt.puts pmsg("\n Push success!",:green)
        @prmt.puts args.first

      when :push_failed
        @prmt.puts pmsg("\nPush failed. Error was : #{args.first}",:red)

      when :no_changes_to_push
        val = args.first
        @prmt.puts pmsg("\n Local is in sync with remote (#{val[:remote]}/#{val[:branch]}). Push is not required. ")
      end
    end

  end

end

#remove_staged(*input, &block) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/release/gem/vcs_cli_action.rb', line 243

def remove_staged(*input, &block)
  @inst.remove_from_staging do |ops, *args|
    preset = false
    if block
      res = block.call(ops, *args)
      if res.nil?
        preset = true
      else
        res
      end
    else
      preset = true
    end

    if preset
      case ops
      when :select_files_to_remove

        mfiles = args.first

        sel = @prmt.multi_select pmsg("\n Following are files that could be removed from staging : ") do |m|

          mfiles[:files].sort.each do |v|
            m.choice v, v.path
          end


          m.choice "Done", :done
          m.choice "Abort", :abort
        end

        if sel.include?(:abort)
          raise Release::Gem::Abort, "User aborted"
        else
          sel
        end

      when :files_removed_successfully
        v = args.first
        @prmt.puts "\n #{v[:count]} file(s) removed successfully.\n#{v[:output]}"

      when :files_removed_to_be_ignored
        v = args.first
        @prmt.puts "\n File(s) failed to be removed. Error was : \n#{v[:output]}"

      end

    end

  end # block
end

#tag(*args, &block) ⇒ Object

Commit



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/release/gem/vcs_cli_action.rb', line 483

def tag(*args, &block)

  @inst.tag(*args) do |ops, *args|

    preset = false
    if block
      res = block.call(ops, *args)
      if res.nil?
        preset = true
      else
        res
      end
    else
      preset = true
    end

    if preset

      case ops
      when :tag_message
        v = args.first
        @prmt.ask(pmsg("\n Please provide message for the tag : "), value: "Auto tagging by gem-release gem during releasing version #{v[:tag]}", required: true)

      when :tagging_success
        v = args.first
        @prmt.puts pmsg("\n Tagging of source code with tag '#{v[:tag]}' is successful.", :green)
        @prmt.puts v[:output]

      when :tagging_failed
        v = args.first
        @prmt.puts pmsg("\n Tagging of source code with tag '#{v[:tag]}' failed. Error was : #{v[:output]}", :red)

      when :no_tagging_required
        @prmt.puts pmsg("\n No tagging required. Source head is the tagged item ", :green)

      end
    end # preset ?

  end

end