Class: DryRun::Github

Inherits:
Object
  • Object
show all
Defined in:
lib/dryrun/github.rb

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Github

Returns a new instance of Github.



10
11
12
13
# File 'lib/dryrun/github.rb', line 10

def initialize(url)
  @base_url = url
  @destination = get_destination
end

Instance Method Details

#clonable_urlObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dryrun/github.rb', line 27

def clonable_url
  starts_with_git = @base_url.split(//).first(4).join.eql? "git@"
  ends_with_git = @base_url.split(//).last(4).join.eql? ".git"

  # ends with git but doesnt start with git
  if ends_with_git and !starts_with_git
    return @base_url
  end

  # ends with git but doesnt start with git
  if !ends_with_git and !starts_with_git
    return "#{@base_url}.git"
  end

  @base_url

  # end
end

#clone(branch, tag) ⇒ Object

CLONE THE REPOSITORY



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
# File 'lib/dryrun/github.rb', line 49

def clone(branch, tag)
  clonable = self.clonable_url

  tmpdir = Dir.tmpdir+"/dryrun/#{@destination}"
  folder_exists = File.directory?(tmpdir)
  
  if folder_exists
    Dir.chdir tmpdir
    is_git_repo = system("git rev-parse")
    
    if !is_git_repo
      FileUtils.rm_rf(tmpdir)  
      DryrunUtils.execute("git clone #{clonable} #{tmpdir}")  
      DryrunUtils.execute("git checkout #{branch}")
    else
      puts "Found project in #{tmpdir.green}..."
      DryrunUtils.execute("git reset --hard HEAD")
      DryrunUtils.execute("git fetch --all")
      DryrunUtils.execute("git checkout #{branch}")
      DryrunUtils.execute("git pull origin #{branch}")
    end

  else
    DryrunUtils.execute("git clone #{clonable} #{tmpdir}")  
  end

  if tag
    Dir.chdir tmpdir
    DryrunUtils.execute("git checkout #{tag}")
  end

  tmpdir
end

#get_destinationObject



15
16
17
# File 'lib/dryrun/github.rb', line 15

def get_destination
  Digest::SHA256.hexdigest @base_url
end

#is_validObject



19
20
21
22
23
24
25
# File 'lib/dryrun/github.rb', line 19

def is_valid
   starts_with_git = @base_url.split(//).first(4).join.eql? "git@"
   starts_with_http = @base_url.split(//).first(7).join.eql? "http://"
   starts_with_https = @base_url.split(//).first(8).join.eql? "https://"

  return (starts_with_git or starts_with_https or starts_with_http)
end