Class: Gisture::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/gisture/repo.rb

Constant Summary collapse

REPO_URL_REGEX =
/\A((http[s]?:\/\/)?github\.com\/)?([a-z0-9_\-\.]*)\/([a-z0-9_\-\.]*)\/?\Z/i
FILE_URL_REGEX =
/\A((http[s]?:\/\/)?github\.com\/)?(([a-z0-9_\-\.]*)\/([a-z0-9_\-\.]*))(\/[a-z0-9_\-\.\/]+)\Z/i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ownerObject (readonly)

Returns the value of attribute owner.



3
4
5
# File 'lib/gisture/repo.rb', line 3

def owner
  @owner
end

#projectObject (readonly)

Returns the value of attribute project.



3
4
5
# File 'lib/gisture/repo.rb', line 3

def project
  @project
end

Class Method Details

.file(path, strategy: nil) ⇒ Object



8
9
10
11
# File 'lib/gisture/repo.rb', line 8

def file(path, strategy: nil)
  repo, file = parse_file_url(path)
  new(repo).file(file, strategy: strategy)
end

.parse_file_url(file_url) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
# File 'lib/gisture/repo.rb', line 23

def parse_file_url(file_url)
  matched = file_url.match(FILE_URL_REGEX)
  raise ArgumentError, "Invalid argument: '#{file_url}' is not a valid file path." if matched.nil?
  [matched[3], matched[6]]
end

.parse_repo_url(repo_url) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
# File 'lib/gisture/repo.rb', line 17

def parse_repo_url(repo_url)
  matched = repo_url.match(REPO_URL_REGEX)
  raise ArgumentError, "Invalid argument: '#{repo_url}' is not a valid repo URL." if matched.nil?
  [matched[3], matched[4]]
end

.run!(path, strategy: nil, &block) ⇒ Object



13
14
15
# File 'lib/gisture/repo.rb', line 13

def run!(path, strategy: nil, &block)
  file(path, strategy: strategy).run!(&block)
end

Instance Method Details

#clone(&block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gisture/repo.rb', line 60

def clone(&block)
  return self if cloned?

  Gisture.logger.info "[gisture] Cloning #{owner}/#{project} into #{clone_path}"

  repo_url = "https://#{Gisture.configuration.github.auth_str}@github.com/#{owner}/#{project}.git"
  Git.clone(repo_url, project, path: ::File.dirname(clone_path))

  FileUtils.rm_rf("#{clone_path}/.git")
  ::File.write("#{clone_path}/.gisture", Time.now.to_i.to_s)

  if block_given?
    instance_eval &block
    destroy_clone!
  end

  self
end

#clone!(&block) ⇒ Object



55
56
57
58
# File 'lib/gisture/repo.rb', line 55

def clone!(&block)
  destroy_clone!
  clone
end

#clone_pathObject



51
52
53
# File 'lib/gisture/repo.rb', line 51

def clone_path
  @clone_path ||= ::File.join(Gisture.configuration.tmpdir, owner, project)
end

#cloned?Boolean

Returns:

  • (Boolean)


83
84
85
86
87
# File 'lib/gisture/repo.rb', line 83

def cloned?
  ::File.read("#{clone_path}/.gisture").strip
rescue
  false
end

#destroy_clone!Object



79
80
81
# File 'lib/gisture/repo.rb', line 79

def destroy_clone!
  FileUtils.rm_rf(clone_path)
end

#file(path, strategy: nil) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/gisture/repo.rb', line 38

def file(path, strategy: nil)
  if cloned?
    Gisture::ClonedFile.new(clone_path, path, basename: "#{owner}/#{project}", strategy: strategy)
  else
    file = github.repos.contents.get(user: owner, repo: project, path: path).body
    Gisture::RepoFile.new(file, basename: "#{owner}/#{project}", strategy: strategy)
  end
end

#githubObject



30
31
32
# File 'lib/gisture/repo.rb', line 30

def github
  @github ||= Github.new(Gisture.configuration.github.to_h)
end

#repoObject



34
35
36
# File 'lib/gisture/repo.rb', line 34

def repo
  @repo ||= github.repos.get user: owner, repo: project
end

#run!(path, strategy: nil, &block) ⇒ Object



47
48
49
# File 'lib/gisture/repo.rb', line 47

def run!(path, strategy: nil, &block)
  file(path, strategy: strategy).run!(&block)
end