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.



13
14
15
16
17
18
19
20
21
# File 'lib/release/gem/vcs_cli_action.rb', line 13

def initialize(root, opts = {  })
  opts = {} if opts.nil?
  opts[:ui] = TTY::Prompt.new
  @inst = Action::VcsAction.new(root,opts)
  @root = root
  @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



656
657
658
# File 'lib/release/gem/vcs_cli_action.rb', line 656

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

Instance Method Details

#add(*input, &block) ⇒ Object



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
130
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
# File 'lib/release/gem/vcs_cli_action.rb', line 104

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

        Cli.cls
        Cli.print_header(@root)
        mfiles = args.first
        print_workspace_items(mfiles)

        #@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 : "), filter: true) do |m|

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

          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



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
482
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# File 'lib/release/gem/vcs_cli_action.rb', line 447

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
        print_workspace_items(mfiles)
        
        #@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
            end
          end

        end

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

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

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

        elsif sel.include?(:skip)
          []

        else
          # done
          sel
        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

#commit_all(*args, &block) ⇒ Object

delete_file



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
# File 'lib/release/gem/vcs_cli_action.rb', line 413

def commit_all(*args, &block)
  st, res = @inst.commit_all do |ops, *args|
    case ops
    when :commit_message

      mfiles = args.first
      @prmt.puts pmsg("\n Files eligible to be committed : ")
      [:staged, :modified, :deleted].each do |cat|
        mfiles[cat].each do |k,v|
          v.each do |vv|
            @prmt.puts pastel.magenta(" #{vv}")
          end
        end

      end

      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
    end

  end

  res

end

#delete_file(*args, &block) ⇒ Object



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
372
373
374
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
# File 'lib/release/gem/vcs_cli_action.rb', line 335

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 : "), filter: true) 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



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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
# File 'lib/release/gem/vcs_cli_action.rb', line 174

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 : "), filter: true) do |m|

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

          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 pastel.bright_blue(v[:output])
        puts ""
        puts pastel.inverse("[Enter to close]")
        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



23
24
25
# File 'lib/release/gem/vcs_cli_action.rb', line 23

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

#ignore(*input, &block) ⇒ Object



236
237
238
239
240
241
242
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
# File 'lib/release/gem/vcs_cli_action.rb', line 236

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 : "), filter: true) do |m|

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

          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



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
63
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
# File 'lib/release/gem/vcs_cli_action.rb', line 27

def manage_workspace(*args, &block)
 
  loop do

    Cli.cls
    Cli.print_header(@root)

    # workspace overview
    overview 

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

      if @inst.has_staged_files? or @inst.has_modified_files? or @inst.has_deleted_files?
        m.choice "Commit all changes", :commit_all
      end

      if @inst.has_staged_files?
        m.choice "Commit staged changes", :commit
      end

      if @inst.has_new_files?
        m.choice "Add", :add
        m.choice "Ignore a file", :ignore
      end

      if @inst.has_staged_files?
        m.choice "Remove staged file", :remove_staged
      end

      if @inst.has_modified_files?
        m.choice "Diff a file", :diff
      end

      if @inst.has_staged_files? or @inst.has_modified_files? or @inst.has_deleted_files? or @inst.has_new_files?
        m.choice "Delete file", :del
      end

      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 :commit_all
      commit_all
      break
    when :done
      break
    end

  end

end

#overviewObject



96
97
98
99
100
101
102
# File 'lib/release/gem/vcs_cli_action.rb', line 96

def overview
  @inst.overview do |ops, val|

    print_workspace_items(val)

  end
end

#pastelObject

print_workspace_items



710
711
712
# File 'lib/release/gem/vcs_cli_action.rb', line 710

def pastel
  Gem.pastel
end

#pmsg(msg, color = nil) ⇒ Object



660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
# File 'lib/release/gem/vcs_cli_action.rb', line 660

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


677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
# File 'lib/release/gem/vcs_cli_action.rb', line 677

def print_workspace_items(itm, opts= {})

  opts = {} if opts.nil?
  title = opts[:title] || "Workspace Status : "
  @prmt.puts pmsg(" #{title}")

  mfiles = itm

  mfiles[:staged].each do |k,v|
    v.each do |vv|
      @prmt.puts pastel.on_yellow(" * #{vv}")
    end
  end

  @prmt.puts ""
  res = []
  [:modified, :deleted, :new].each do |cat|
    mfiles[cat].each do |k,v|
      v.each do |vv|
        case cat
        when :new
          @prmt.puts vv
        when :modified
          @prmt.puts pastel.bright_yellow(vv)
        when :deleted
          @prmt.puts pastel.strikethrough(vv)
        end
      end
    end
  end

end

#push(*args, &block) ⇒ Object

tag



588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
# File 'lib/release/gem/vcs_cli_action.rb', line 588

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 : "), filter: true) do |m|
          val.each do |k,v|
            m.choice "#{k} [#{v["push"]}]", 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

        else
          # no add... cannot sing already so exit
          @prmt.puts pmsg("\n No remote configuration defined. Push operation skipped.", :green)
          
        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



285
286
287
288
289
290
291
292
293
294
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
# File 'lib/release/gem/vcs_cli_action.rb', line 285

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 : "), filter: true) do |m|

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

          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



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 546

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