Class: LZCodeInner

Inherits:
Object
  • Object
show all
Defined in:
lib/source/LZCodeInner.rb

Instance Method Summary collapse

Instance Method Details

#begin(params) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/source/LZCodeInner.rb', line 21

def begin(params)
	if params.size < 1
 	showInfo()
	else
$OperationType=params[0]
$Keyword=params[1]
start()
	end
end

#checkExistDir(key) ⇒ Object

Tools



32
33
34
35
36
37
38
39
40
# File 'lib/source/LZCodeInner.rb', line 32

def checkExistDir(key)
  isExist = false
  Dir.foreach("./") do |file|
    if File.directory?("#{file}") && file == key
        isExist = true
    end
  end
  return isExist
end

#clearObject

Clear



139
140
141
# File 'lib/source/LZCodeInner.rb', line 139

def clear()
  LZCodeGitLab.new.resetInfo()
end

#cyan(s) ⇒ Object



68
69
70
# File 'lib/source/LZCodeInner.rb', line 68

def cyan(s)
  return "\033[36m#{s}\033[0m"
end

#downloadObject



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
277
278
279
280
# File 'lib/source/LZCodeInner.rb', line 219

def download()
  if $Keyword == nil
    showInfo()
  end

  info = LZCodeGitLab.new.getRepoInfo($Keyword)
  
  if info.keys.count > 0

    $name = info["name"]
    $addr = info["ssh_url_to_repo"]
    $path = info["path"]
    $pid = info["id"]
    $forkInfo = info["forked_from_project"]

    #源仓库
    if $forkInfo
      $fork_addr = info["forked_from_project"]["ssh_url_to_repo"]
      run_download($path,$addr,$fork_addr)
    #fork仓库
    else
      result = ''
      while result != 'Y' && result != 'n'
        print "Do you want to fork the repository?(Y/n):"
        result = STDIN.gets.chomp
      end
      #需要fork
      if result == 'Y'
        result = LZCodeGitLab.new.getMatchRepo($pid,$name)
        #已经存在fork仓库
        if result.keys.size > 0
          puts 'The repository already forks!'
          #下载fork repo
          $ssh_url = result["ssh_url_to_repo"]
          $fork_url = result["forked_from_project"]["ssh_url_to_repo"]

          run_download($path,$ssh_url,$fork_url)

        else
          #fork repo
          puts 'Performing fork...'
          f_info = LZCodeGitLab.new.forkRepo($pid,$name)
          if f_info.keys.size > 0
            puts 'Operation completed!'
            $ssh_url = f_info["ssh_url_to_repo"]
            $fork_url = f_info["forked_from_project"]["ssh_url_to_repo"]
            run_download($path,$ssh_url,$fork_url)
          else
            puts 'Operation failure!'
          end
        end
      #不需要fork
      elsif result == 'n'
        run_download($path,$addr,'')
      end
    end

  else
    puts "No search result by the keyword."
  end
  
end

#green(s) ⇒ Object



56
57
58
# File 'lib/source/LZCodeInner.rb', line 56

def green(s)
  return "\033[32m#{s}\033[0m"
end

#magenta(s) ⇒ Object



60
61
62
# File 'lib/source/LZCodeInner.rb', line 60

def magenta(s)
  return "\033[35m#{s}\033[0m"
end

#removeObject

remove



144
145
146
# File 'lib/source/LZCodeInner.rb', line 144

def remove()
  info = LZCodeGitLab.new.removeRepo($Keyword)
end

#run_download(path, addr, forkAddr) ⇒ Object



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
# File 'lib/source/LZCodeInner.rb', line 184

def  run_download(path,addr,forkAddr)
  #检查本地是否已经存在
  t = checkExistDir(path)
  if t == false
    #执行下载操作
    run_s_download(path,addr,forkAddr)
  else
    #本地已经存在,是否覆盖?
    isCover = ''
    while isCover != 'Y' && isCover != 'n'
      print "Do you want to overwrite the local repository?(Y/n):"
      isCover = STDIN.gets.chomp
    end
    if isCover == 'Y'
      #删除目录,进行下载
      system("rm -rf #{path}")
      run_s_download(path,addr,forkAddr)
    elsif isCover == 'n'
      #是否进行更新操作?
      isUpdate = ''
      while isUpdate != 'Y' && isUpdate != 'n'
        print "Do you want to update the local repository?(Y/n):"
        isUpdate = STDIN.gets.chomp
      end
      if isUpdate == 'Y'
      #执行更新操作
        run_update(path)
      elsif isUpdate == 'n'
      #退出 say bye!
        sayBye()
      end
    end
  end
end

#run_s_download(path, addr, forkAddr) ⇒ Object

Download



174
175
176
177
178
179
180
181
182
# File 'lib/source/LZCodeInner.rb', line 174

def run_s_download(path,addr,forkAddr)
  puts 'Performing download...'
  log = system("#{$ScriptPath}/z_download.sh #{path} #{addr} #{forkAddr} #{ForkName}")
  if log == true
    puts 'Operation completed!'
  else
    puts 'Operation failure!'
  end
end

#run_update(path) ⇒ Object

Update



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/source/LZCodeInner.rb', line 149

def run_update(path)
  #检查本地是否已经存在
  t = checkExistDir(path)
  if t == false
    puts 'Not found the in repository current directory!'
    sayBye()
  else
    puts 'Performing update...'
    log = system("#{$ScriptPath}/z_update.sh #{path} #{ForkName}")
    if log == true
	  puts 'Operation completed!'
	else
	  puts 'Operation failure!'
	end
  end
end

#sayByeObject

SayBye



43
44
45
46
# File 'lib/source/LZCodeInner.rb', line 43

def sayBye
  puts 'Have a nice day!'
  exit
end

#searchObject

Search



134
135
136
# File 'lib/source/LZCodeInner.rb', line 134

def search()
  LZCodeGitLab.new.searchRepoInfo($Keyword)
end

#showInfoObject

end



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
# File 'lib/source/LZCodeInner.rb', line 82

def showInfo()
    puts ' '
    puts white_("Usage:")
    puts ' '
    puts green("    lzcode ")+cyan("option ")+magenta("keyword")
    puts ' '
    puts white("    An automatic tool of GitLab.")
    puts ' '
    puts white_("Option:")
    puts ' '
    puts cyan("     -d ")+white("   Download a repository.(Fork & Clone)")
    puts ' '
    puts cyan("     -u ")+white("   Update a repository.(Fetch & Merge & Stash)")
    puts ' '
    puts cyan("     -s ")+white("   Submit a repository.(Commit & Push & Create Merge Request)")
    puts ' '
    puts cyan("     -r ")+white("   Remove a fork repository on a server.")
    puts ' '
    puts cyan("     -q ")+white("   Query a repository information.")
    puts ' '
    puts cyan("     -c ")+white("   Clear user information.")
    puts ' '
    puts white_("Tips:")
    puts ' '
    puts yellow("  Please use 'Tab' to auto complete!") 
    exit
end

#startObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/source/LZCodeInner.rb', line 110

def start()
  $ScriptPath = File.dirname(Pathname.new(__FILE__).realpath)
  case $OperationType
  when '-d'
    download()
  when '-u'
    update()
  when '-s'
    submit()
  when '-r'
    remove()
  when '-q'
    search()
  when '-c'
    clear()
  when '--help'
    showInfo()
  else
     puts 'Operation failed!'
     showInfo()
  end
end

#submitObject

Submit



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
# File 'lib/source/LZCodeInner.rb', line 283

def submit
  t = checkExistDir($Keyword)
  if t == false
    puts 'Not found the in repository current directory!'
    sayBye()
  else
    puts 'Performing commit...'
    log = system("#{$ScriptPath}/z_submit.sh #{$Keyword} #{$ForkName}")
    if log == true

      puts 'Operation completed!'

      isFork = `cd #{$Keyword};git remote -v | grep #{ForkName} | head -1`

      if isFork.chomp.include?ForkName

        last_msg = `cd #{$Keyword};git log --pretty=format:"%s" head -1`
        s = `cd #{$Keyword};git remote -v | head -1`
        addr = s.split(" ")[1]
        branch = `cd #{$Keyword};git branch | awk  '$1 == "*"{print $2}'`

        if last_msg.length > 0 && addr.length > 0 && branch.length > 0
          puts 'Performing create merge request...'
          info = LZCodeGitLab.new.creatMR(addr,branch.chomp,$Keyword,last_msg.chomp)
          if info == true
            puts 'Operation completed!'
          else
            puts 'Operation failure!'
          end
        end
      end
    else
      puts 'Operation failure!'
    end
  end
end

#updateObject



166
167
168
169
170
171
# File 'lib/source/LZCodeInner.rb', line 166

def update()
  if $Keyword == nil
    showInfo()
  end
  run_update($Keyword)
end

#white(s) ⇒ Object



52
53
54
# File 'lib/source/LZCodeInner.rb', line 52

def white(s)
  return "\033[37m#{s}\033[0m"
end

#white_(s) ⇒ Object



48
49
50
# File 'lib/source/LZCodeInner.rb', line 48

def white_(s)
  return "\033[37;4;1m#{s}\033[0m"
end

#yellow(s) ⇒ Object



64
65
66
# File 'lib/source/LZCodeInner.rb', line 64

def yellow(s)
  return "\033[33m#{s}\033[0m"
end