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
GISTURE_FILE_REGEX =

gisture.yml, gisture.yaml, whatever.gist, whatever.gisture

/\A(gisture\.ya?ml|.+\.gist|.+\.gisture)\Z/

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



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

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)


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

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)


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

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



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

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

Instance Method Details

#clone(&block) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/gisture/repo.rb', line 119

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



114
115
116
117
# File 'lib/gisture/repo.rb', line 114

def clone!(&block)
  destroy_clone!
  clone
end

#clone_pathObject



110
111
112
# File 'lib/gisture/repo.rb', line 110

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

#cloned?Boolean

Returns:

  • (Boolean)


142
143
144
145
146
# File 'lib/gisture/repo.rb', line 142

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

#destroy_clone!Object



138
139
140
# File 'lib/gisture/repo.rb', line 138

def destroy_clone!
  FileUtils.rm_rf(clone_path)
end

#file(path, strategy: nil) ⇒ Object



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

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

#files(path) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/gisture/repo.rb', line 48

def files(path)
  if cloned?
    Dir[::File.join(clone_path, path, '*')].map { |f| Hashie::Mash.new({name: ::File.basename(f), path: ::File.join(path, ::File.basename(f))}) }
  else
    github.repos.contents.get(user: owner, repo: project, path: path).body
  end
end

#gisticulate(gisture, &block) ⇒ Object



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
95
96
97
98
99
100
101
# File 'lib/gisture/repo.rb', line 66

def gisticulate(gisture, &block)
  if gisture.key?(:gistures) || gisture.key?(:gists)
    gists = (gisture[:gistures] || gisture[:gists])
    gists = gists.values if gists.respond_to?(:values)
    clone! if gists.any? { |g| g[:clone] == true } # force clone once up front for a refresh
    gists.map { |gist| gisticulate(gist, &block) }
  else
    clone if gisture[:clone] == true

    run_options = []
    run_options << eval(gisture[:evaluator]) if (!gisture.key?(:strategy) || gisture[:strategy].to_sym == :eval) && gisture.key?(:evaluator)
    run_options = gisture[:executor] if (gisture.key?(:strategy) && gisture[:strategy].to_sym == :exec) && gisture.key?(:executor)

    if gisture[:resources] && !cloned?
      # localize and pull down any relevant resources
      gisture[:resources].each do |resource|
        Gisture.logger.info "[gisture] Localizing resource #{::File.join(owner, project, resource)} into #{clone_path}"
        file(resource).localize!(clone_path)
      end

      # localize the file we're running
      Gisture.logger.info "[gisture] Localizing gisture #{::File.join(owner, project, gisture[:path])} into #{clone_path}"
      gfile = file(gisture[:path], strategy: gisture[:strategy])
      gfile.localize!(clone_path)

      # chdir into the localized temp path
      cwd = Dir.pwd
      Dir.chdir ::File.dirname(gfile.tempfile.path)
      result = gfile.run!(*run_options, &block)
      Dir.chdir cwd
      result
    else
      file(gisture[:path], strategy: gisture[:strategy]).run!(*run_options, &block)
    end
  end
end

#gistures(path) ⇒ Object

TODO move all the gisture/gisticulate stuff into a separate class (Gisture? Gisticulation?)



58
59
60
61
62
63
64
# File 'lib/gisture/repo.rb', line 58

def gistures(path)
  if ::File.basename(path).match(GISTURE_FILE_REGEX)
    [Hashie::Mash.new(YAML.load(file(path).content).symbolize_keys)]
  else
    files(path).select { |f| f.name.match(GISTURE_FILE_REGEX) }.map { |f| Hashie::Mash.new(YAML.load(file(f.path).content).symbolize_keys) }
  end
end

#githubObject



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

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

#repoObject



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

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

#run(path, &block) ⇒ Object Also known as: run!



103
104
105
106
107
# File 'lib/gisture/repo.rb', line 103

def run(path, &block)
  gists = gistures(path)
  clone! if gists.any? { |g| g[:clone] == true } # force clone once up front for a refresh
  gists.map { |g| gisticulate(g, &block) }
end