Class: Release::Gem::Action::VcsAction

Inherits:
Object
  • Object
show all
Includes:
TR::CondUtils
Defined in:
lib/release/gem/vcs_action.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of VcsAction.

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/release/gem/vcs_action.rb', line 16

def initialize(root, opts = {  })
  raise VcsActionError, "root cannot be null" if is_empty?(root)
  @ws = Gvcs::Workspace.new(root) 

  oopts = opts || {}
  @ui = oopts[:ui]
  @engine = oopts[:engine]
  st, path = @ws.workspace_root

  #@gitDir = File.join(path.strip,".git")
  #p @gitDir
  #@gitBack = File.join(path.strip,".git_bak")
  #p @gitBack
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



452
453
454
455
456
457
458
459
460
461
462
# File 'lib/release/gem/vcs_action.rb', line 452

def method_missing(mtd, *args, &block)
  if @ws.respond_to?(mtd)
    @ws.send(mtd, *args, &block)
  else
    if not @engine.nil? and @engine.respond_to?(mtd)
      @engine.send(mtd, *args, &block)
    else
      super
    end
  end
end

Instance Attribute Details

#uiObject

Returns the value of attribute ui.



14
15
16
# File 'lib/release/gem/vcs_action.rb', line 14

def ui
  @ui
end

Instance Method Details

#add(&block) ⇒ Object



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

def add(&block)
  
  if block
    
    loop do

      stgDir, stgFiles = @ws.staged_files
      modDir, modFiles = @ws.modified_files
      newDir, newFiles = @ws.new_files
      delDir, delFiles = @ws.deleted_files

      modFiles.delete_if { |f| stgFiles.include?(f) }
      modDir.delete_if { |f| stgDir.include?(f) }
      
      newFiles.delete_if { |f| stgFiles.include?(f) }
      newDir.delete_if { |f| stgDir.include?(f) }

      delFiles.delete_if { |f| stgFiles.include?(f) }
      delDir.delete_if { |f| stgDir.include?(f) }

      res = block.call(:select_files_to_add, { modified: { files: modFiles, dirs: modDir }, new: { files: newFiles, dirs: newDir }, deleted: { files: delFiles, dirs: delDir }, staged: { files: stgFiles, dirs: stgDir }, vcs: self } )

      doneTriggered = false
      sel = res.clone
      if sel.include?(:done)
        sel.delete_if { |e| e == :done }
        doneTriggered = true
      end

      if not_empty?(sel)
        st, rres = @ws.add_to_staging(*sel)
        if st
          block.call(:files_added_successfully, { count: sel.length, output: rres })
        else
          block.call(:files_failed_to_be_added, { output: rres } )
        end
      else
        block.call(:no_files_given)
      end

      break if doneTriggered

    end

  end

end

#add_to_staging(*files) ⇒ Object



422
423
424
# File 'lib/release/gem/vcs_action.rb', line 422

def add_to_staging(*files)
  @ws.add_to_staging(*files) 
end

#add_to_staging_if_commit_before(*files) ⇒ Object



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/release/gem/vcs_action.rb', line 426

def add_to_staging_if_commit_before(*files)

  stgDir, stgFiles = @ws.staged_files
  modDir, modFiles = @ws.modified_files
  #newDir, newFiles = @ws.new_files
  delDir, delFiles = @ws.deleted_files

  mFiles = modFiles.map { |e| e.path }
  sFiles = stgFiles.map { |e| e.path }
  dFiles = delFiles.map { |e| e.path }

  res = []
  files.each do |f|
    if (mFiles.include?(f) or dFiles.include?(f)) and not sFiles.include?(f)
      res << f
    end
  end

  if not_empty?(res)
    @ws.add_to_staging(*res)
  end

end

#commit(msg = nil, &block) ⇒ Object



278
279
280
281
282
283
284
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
334
335
336
337
338
# File 'lib/release/gem/vcs_action.rb', line 278

def commit(msg = nil, &block)

  res = :value
  if block

    counter = 0
    loop do

      stgDir, stgFiles = @ws.staged_files
      modDir, modFiles = @ws.modified_files
      newDir, newFiles = @ws.new_files
      delDir, delFiles = @ws.deleted_files

      modFiles.delete_if { |f| stgFiles.include?(f) }
      modDir.delete_if { |f| stgDir.include?(f) }
      
      newFiles.delete_if { |f| stgFiles.include?(f) }
      newDir.delete_if { |f| stgDir.include?(f) }

      delFiles.delete_if { |f| stgFiles.include?(f) }
      delDir.delete_if { |f| stgDir.include?(f) }

      # block should call vcs for add, remove, ignore and other operations
      res = block.call(:select_files_to_commit, { modified: { files: modFiles, dirs: modDir }, new: { files: newFiles, dirs: newDir }, deleted: { files: delFiles, dirs: delDir }, staged: { files: stgFiles, dirs: stgDir }, vcs: self, counter: counter } )

      break if res == :skip or res == :done

      counter += 1

    end

    if res == :done

      stgDir, stgFiles = @ws.staged_files
      block.call(:staged_elements_of_commit, { files: stgFiles, dirs: stgDir })

      msg = block.call(:commit_message) if is_empty?(msg) 
      raise VcsActionError, "Commit message is empty" if is_empty?(msg)

      cp "Commit with user message : #{msg}"
      st, res = @ws.commit(msg)
      if st
        block.call(:commit_successful, res) if block
      else
        block.call(:commit_failed, res) if block
      end
      [st, res]

    end

  else

    msg = "Auto commit all ('-am' flag) by gem-release gem" if is_empty?(msg)
    cp msg
    # changed files only without new files
    @ws.commit_all(msg)
  end

  res

end

#delete_file(*files, &block) ⇒ Object



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

def delete_file(*files, &block)
  
  if block
    
    loop do

      stgDir, stgFiles = @ws.staged_files
      modDir, modFiles = @ws.modified_files
      newDir, newFiles = @ws.new_files

      modFiles.delete_if { |f| stgFiles.include?(f) }
      modDir.delete_if { |f| stgDir.include?(f) }
      
      newFiles.delete_if { |f| stgFiles.include?(f) }
      newDir.delete_if { |f| stgDir.include?(f) }

      res = block.call(:select_files_to_delete, { modified: { files: modFiles, dirs: modDir }, new: { files: newFiles, dirs: newDir }, staged: { files: stgFiles, dirs: stgDir } } )

      doneTriggered = false
      sel = res.clone
      if sel.include?(:done)
        sel.delete_if { |e| e == :done }
        doneTriggered = true
      end

      if not_empty?(sel)
        staged = []
        nonTrack = []
        sel.each do |s|
          if s.is_a?(GitCli::Delta::NewFile)
            confirm = block.call(:confirm_nontrack_delete, s.path)
            if confirm
              FileUtils.rm(s.path)
              block.call(:nontrack_file_deleted, s.path)
            end
          elsif s.is_a?(GitCli::Delta::ModifiedFile)
            # not staged
            confirm = block.call(:confirm_vcs_delete, s.path)
            puts "vcs confirm : #{confirm}"
            if confirm
              @ws.remove_from_vcs(s.path)
              block.call(:vcs_file_deleted, s.path)
            end
          elsif s.is_a?(GitCli::Delta::StagedFile)
            confirm = block.call(:confirm_staged_delete, s.path)
            if confirm
              @ws.remove_from_staging(s.path)
              block.call(:staged_file_deleted, s.path)
            end
          end
        end

      else
        block.call(:no_files_given)
      end

      break if doneTriggered

    end

  end
end

#diff(&block) ⇒ Object



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
130
131
132
133
# File 'lib/release/gem/vcs_action.rb', line 93

def diff(&block)
  
  if block
    
    loop do

      stgDir, stgFiles = @ws.staged_files
      modDir, modFiles = @ws.modified_files

      modFiles.delete_if { |f| stgFiles.include?(f) }
      modDir.delete_if { |f| stgDir.include?(f) }
      
      res = block.call(:select_files_to_diff, { modified: { files: modFiles, dirs: modDir },  staged: { files: stgFiles, dirs: stgDir }, vcs: self } )

      doneTriggered = false
      sel = res.clone
      if sel.include?(:done)
        sel.delete_if { |e| e == :done }
        doneTriggered = true
      end

      if not_empty?(sel)
        sel.each do |s|
          st, rres = @ws.diff_file(s)
          if st
            block.call(:diff_file_result, { file: s, output: rres })
          else
            block.call(:diff_file_error, { file: s, output: rres } )
          end
        end
      else
        block.call(:no_files_given)
      end

      break if doneTriggered

    end

  end

end

#exec(&block) ⇒ Object

def dev_mode_end

if File.exist?(@gitBack)
  FileUtils.mv(@gitBack, @gitDir)
end

end



41
42
43
# File 'lib/release/gem/vcs_action.rb', line 41

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

#ignore(*files, &block) ⇒ Object



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

def ignore(*files, &block)
  
  if block
    
    loop do

      newDir, newFiles = @ws.new_files

      res = block.call(:select_files_to_ignore, { files: newFiles, dirs: newDir } )

      doneTriggered = false
      sel = res.clone
      if sel.include?(:done)
        sel.delete_if { |e| e == :done }
        doneTriggered = true
      end

      if not_empty?(sel)
        st, rres = @ws.ignore(*sel)
        if st
          block.call(:files_ignored_successfully, { count: sel.length, output: rres })
        else
          block.call(:files_failed_to_be_ignored, { output: rres } )
        end
      else
        block.call(:no_files_given)
      end

      break if doneTriggered

    end

  else

    @ws.ignore(*files) if not_empty?(files)

  end

end

#push(remote = nil, branch = nil, &block) ⇒ Object

Raises:



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

def push(remote = nil, branch= nil, &block)
 
  remoteConf = @ws.remote_config
  if is_empty?(remoteConf)
    if block
      remote = block.call(:no_remote_repos_defined)
    end
  else
    if is_empty?(remote)
      if block
        remote = block.call(:select_remote, remoteConf)
      else
        remote = remoteConf.keys.first
      end
    end
  end

  raise VcsActionError, "Push repository remote cannot be empty" if is_empty?(remote)

  if remote != :skip

    branch = @ws.current_branch if is_empty?(branch)


    if is_local_ahead_of_remote?("#{remote}/#{branch}", branch)

      cp "pushing to #{remote}/#{branch}"
      st, res = @ws.push_changes_with_tags(remote, branch)

      if st
        block.call(:push_successful, res) if block
      else
        block.call(:push_failed, res) if block
      end

      [st, res]

    else
      block.call(:no_changes_to_push, { remote: remote, branch: branch }) if block
    end
  end

end

#remove_from_staging(*files, &block) ⇒ Object



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

def remove_from_staging(*files, &block)
  
  if block
    
    loop do

      stgDir, stgFiles = @ws.staged_files

      res = block.call(:select_files_to_remove, { files: stgFiles, dirs: stgDir } )

      doneTriggered = false
      sel = res.clone
      if sel.include?(:done)
        sel.delete_if { |e| e == :done }
        doneTriggered = true
      end

      if not_empty?(sel)
        st, rres = @ws.remove_from_staging(*sel)
        if st
          block.call(:files_removed_successfully, { count: sel.length, output: rres })
        else
          block.call(:files_removed_to_be_ignored, { output: rres } )
        end
      else
        block.call(:no_files_given)
      end

      break if doneTriggered

    end

  else

    @ws.removed_from_staging(*files) if not_empty?(files)

  end

end

#tag(*args, &block) ⇒ Object



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

def tag(*args, &block)

  opts = args.first || {  }
  tag = opts[:tag]
  msg = opts[:message]

  if not @ws.tag_points_at?("HEAD")

    if is_empty?(tag)
      raise VcsActionError, "tag name cannot be empty" if not block
      tag = block.call(:tag_name)
      raise VcsActionError, "tag name cannot be empty" if is_empty?(tag)
    end

    if is_empty?(msg)
      if block
        msg = block.call(:tag_message, { tag: tag })
      end
    end

    cp "tagged with name : #{tag} and message : #{msg}"
    st, res = @ws.create_tag(tag, msg)
    if st
      block.call(:tagging_success, { tag: tag, output: res }) if block
    else
      block.call(:tagging_failed, { tag: tag, output: res }) if block
    end

    [st, res]

  else

    block.call(:no_tagging_required) if block
    [true, "No tagging required"]
  end

end