Class: CodeRunner::RepositoryManager

Inherits:
Object
  • Object
show all
Defined in:
lib/coderunner/repository_manager.rb

Overview

A class for managing a coderunner repository, which consists of a managed set of simulation folders and defaults files which are synchronised across systems using git.

Class Method Summary collapse

Class Method Details

.add_folder(folder, copts) ⇒ Object



201
202
203
204
# File 'lib/coderunner/repository_manager.rb', line 201

def add_folder(folder, copts)
  repo = Repository.open_in_subfolder(folder)
  repo.add_folder(folder)
end

.add_remote(name, url, copts) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/coderunner/repository_manager.rb', line 131

def add_remote(name, url, copts)
  url =~ Repository.url_regex
  barefolder = $~[:folder]
  unless barefolder =~ Repository.bare_ext_reg
    raise "Remotes must end in .cr.git for coderunnerrepo"
  end
  Dir.chdir(copts[:Y]){
    repo = Repository.open_in_subfolder(Dir.pwd)
    repo.bare_repo.add_remote(name, url)
  }
end

.bare_repo_command(comm, copts) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/coderunner/repository_manager.rb', line 120

def bare_repo_command(comm, copts)
  Dir.chdir(copts[:Y]) do
    repo = Repository.open_in_subfolder(Dir.pwd)
    Dir.chdir(repo.bare_repo.repo.to_s) do
      system comm
    end
  end
end

.clone_repo(url, name, copts) ⇒ Object



214
215
216
217
218
# File 'lib/coderunner/repository_manager.rb', line 214

def clone_repo(url, name, copts)
  Dir.chdir(copts[:Y]){
    Repository.clone(url, name)
  }
end

.init_repository(name, copts) ⇒ Object



128
129
130
# File 'lib/coderunner/repository_manager.rb', line 128

def init_repository(name, copts)
  Repository.init(name)
end

.list_remotes(copts) ⇒ Object



168
169
170
171
172
173
174
175
# File 'lib/coderunner/repository_manager.rb', line 168

def list_remotes(copts)
  Dir.chdir(copts[:Y]){
    repo = Repository.open_in_subfolder(Dir.pwd)
    repo.bare_repo.remotes.each do |r|
      puts "#{r.name} #{r.url}"
    end
  }
end

.pull_repository(copts) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/coderunner/repository_manager.rb', line 190

def pull_repository(copts)
  Dir.chdir(copts[:Y]){
    repo = Repository.open_in_subfolder(Dir.pwd)
    if copts[:r]
      rems = copts[:r].split(/,/).map{|rname| repo.bare_repo.remote(rname)} 
    else
      rems = repo.bare_repo.remotes
    end
    rems.each{|r| repo.pull(r)}
  }
end

.push_and_create_repository(copts) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/coderunner/repository_manager.rb', line 146

def push_and_create_repository(copts)
  Dir.chdir(copts[:Y]){
    repo = Repository.open_in_subfolder(Dir.pwd)
    bare_repo = repo.bare_repo
    if copts[:r]
      rems = copts[:r].split(/,/).map{|rname| bare_repo.remote(rname)} 
    else
      raise "Must specify remotes using the -r flag when calling push_and_create_repository"
    end
    repo.simple_push(repo.remote("origin"))
    rems.each do |r|
      namehost, folder, barefolder = repo.split_url(r.name)
      try_system %[ssh #{namehost} "mkdir -p #{barefolder} && cd #{barefolder} && git init --bare"]
      bare_repo.push(r)
      try_system %[ssh #{namehost} "git clone #{barefolder} #{folder}"]
      bare_repo.remotes.each do |other_remote|
        next if other_remote.name == r.name
        try_system %[ssh #{namehost} "cd #{barefolder} && git remote add #{other_remote.name} #{other_remote.url}"]
      end
    end 
  }
end

.push_repository(copts) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/coderunner/repository_manager.rb', line 176

def push_repository(copts)
  Dir.chdir(copts[:Y]){
    puts 'opening repo'
    repo = Repository.open_in_subfolder(Dir.pwd)
    if copts[:r]
      puts 'mapping'
      rems = copts[:r].split(/,/).map{|rname| repo.bare_repo.remote(rname)} 
    else
      rems = repo.bare_repo.remotes
    end
    puts 'pusing'
    rems.each{|r| repo.push(r)}
  }
end

.remote_synchronize_down(remote, folder, copts) ⇒ Object



205
206
207
208
# File 'lib/coderunner/repository_manager.rb', line 205

def remote_synchronize_down(remote, folder, copts)
  repo = Repository.open_in_subfolder(folder)
  repo.rsyncd(remote, folder)
end

.remote_synchronize_up(remote, folder, copts) ⇒ Object



209
210
211
212
# File 'lib/coderunner/repository_manager.rb', line 209

def remote_synchronize_up(remote, folder, copts)
  repo = Repository.open_in_subfolder(folder)
  repo.rsyncu(remote, folder)
end

.setup(copts) ⇒ Object

This function gets called before every command and allows arbitrary manipulation of the command options (copts) hash



113
114
115
116
# File 'lib/coderunner/repository_manager.rb', line 113

def setup(copts)
  copts[:Y] ||= Dir.pwd
  copts[:Y] = File.expand_path(copts[:Y])
end

.try_system(str) ⇒ Object



142
143
144
145
# File 'lib/coderunner/repository_manager.rb', line 142

def try_system(str)
  puts str
  raise "Failed command: #{str}" unless system str
end

.verbosityObject



117
118
119
# File 'lib/coderunner/repository_manager.rb', line 117

def verbosity
  2
end