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



183
184
185
186
# File 'lib/coderunner/repository_manager.rb', line 183

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

.add_remote(name, url, copts) ⇒ Object



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

def add_remote(name, url, copts)
  Dir.chdir(copts[:Y]){
    repo = Repository.open(Dir.pwd)
    repo.add_remote(name, url)
  }
end

.init_repository(name, copts) ⇒ Object



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

def init_repository(name, copts)
  repo = Repository.init(name)
  repo.
  repo.init_readme
  repo.init_defaults_folder
end

.pull_repository(copts) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
# File 'lib/coderunner/repository_manager.rb', line 172

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

.push_and_create_repository(copts) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/coderunner/repository_manager.rb', line 131

def push_and_create_repository(copts)
  Dir.chdir(copts[:Y]){
    repo = Repository.open(Dir.pwd)
    if copts[:r]
      rems = copts[:r].split(/,/).map{|rname| repo.remote(rname)} 
    else
      raise "Must specify remotes using the -r flag when calling push_and_create_repository"
    end
    rems.each do |r|
      r.url =~ (/ssh:\/\/(?<namehost>[^\/]+)(?<folder>.*$)/)
      namehost = $~[:namehost]
      folder = $~[:folder]
      p namehost, folder
      try_system %[git bundle create .tmpbundle --all]
      try_system %[ssh #{namehost} "mkdir -p #{folder}"]
      try_system %[scp .tmpbundle #{namehost}:#{folder}/../.]
      try_system %[rm .tmpbundle]
      #try_system %[ssh #{namehost} "cd #{folder} && git clone .tmpbundle #{repname = File.basename(repo.dir.to_s)} "]
      try_system %[ssh #{namehost} "cd #{folder} && git clone ../.tmpbundle ."]
      #try_system %[ssh #{namehost} "cd #{folder}/#{repname} && git remote rm origin"]
      try_system %[ssh #{namehost} "cd #{folder} && git remote rm origin"]
      #push_repository(copts.dup.absorb(r: r.name))
      repo.remotes.each do |other_remote|
        next if other_remote.name == r.name
        try_system %[ssh #{namehost} "cd #{folder} && git remote add #{other_remote.name} #{other_remote.url}"]
        #try_system %[ssh #{namehost} "cd #{folder} && git remote add #{other_remote.name} #{other_remote.url}"]
      end
    end 
  }
end

.push_repository(copts) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/coderunner/repository_manager.rb', line 161

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

.remote_synchronize_down(remote, folder, copts) ⇒ Object



187
188
189
190
# File 'lib/coderunner/repository_manager.rb', line 187

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



191
192
193
194
# File 'lib/coderunner/repository_manager.rb', line 191

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



109
110
111
112
# File 'lib/coderunner/repository_manager.rb', line 109

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

.try_system(str) ⇒ Object



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

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

.verbosityObject



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

def verbosity
  2
end