Class: Rgit::Repo
- Inherits:
-
Object
- Object
- Rgit::Repo
- Defined in:
- lib/rgit.rb
Constant Summary collapse
- FILE_DIR =
File.(File.join('..', '..', 'files'), __FILE__)
Instance Attribute Summary collapse
-
#repo ⇒ Object
Returns the value of attribute repo.
Instance Method Summary collapse
-
#add_files(pattern) ⇒ Object
add_files -> adds file to queue.
-
#create_repo(key) ⇒ Object
create_repo -> creates new repo.
-
#delete_queue ⇒ Object
delete_queue -> deletes queue.
-
#generate ⇒ Object
generate -> creates rit.conf.
-
#get_files(dir, regex) ⇒ Object
get_files -> recursively finds file matching given regex.
-
#get_repo_files ⇒ Object
get_repo_files -> gets file list from repo.
-
#initialize(repo) ⇒ Repo
constructor
A new instance of Repo.
-
#new_file?(file_path) ⇒ Boolean
new_file? -> check if server’s file version is newer than the local one.
-
#read_config(file, key) ⇒ Object
read_config -> reads key from JSON config file.
-
#sync ⇒ Object
sync.
-
#upload_files(options) ⇒ Object
upload_files -> pushes files to repo.
-
#write_config(file, key, value) ⇒ Object
write_config -> updates config file (uses JSON).
-
#ziphp ⇒ Object
ziphp -> creates zip for the webserver.
Constructor Details
#initialize(repo) ⇒ Repo
Returns a new instance of Repo.
15 16 17 18 |
# File 'lib/rgit.rb', line 15 def initialize(repo) @repo = repo RestClient.proxy = ENV['http_proxy'] end |
Instance Attribute Details
#repo ⇒ Object
Returns the value of attribute repo.
14 15 16 |
# File 'lib/rgit.rb', line 14 def repo @repo end |
Instance Method Details
#add_files(pattern) ⇒ Object
add_files -> adds file to queue
171 172 173 174 175 176 |
# File 'lib/rgit.rb', line 171 def add_files pattern Dir.chdir(".rit_"+@repo) f = File.new("queue.json", "w+") Dir.chdir("../") f.write(JSON.generate(self.get_files(".",Regexp.new(pattern)))) end |
#create_repo(key) ⇒ Object
create_repo -> creates new repo
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/rgit.rb', line 140 def create_repo key if (!Dir.exists? ".rit_empty") Dir.mkdir(".rit_empty") Dir.chdir(".rit_empty") normal = Hash.new() normal["repo"]= key normal["json_sucks"] = "not less than 2 vars" f=File.open("rit.conf","w+") f.write(JSON.generate(normal)) else conf = JSON.parse(File.read("rit.conf")) conf["repo"] = key f = File.new("rit.conf", "w+") f.write(JSON.generate(conf)) end Dir.chdir("../") Dir.mkdir(".rit_"+key) Dir.mkdir(key) Dir.chdir ".rit_"+key data = Hash.new() print "Username: " data["user"] = gets.chop print "Password: " data["key"] = gets.chop print "Repo: " data["repo"] = gets.chop f=File.open("rit.conf","w+") f.write(JSON.generate(data)) Dir.chdir("../") end |
#delete_queue ⇒ Object
delete_queue -> deletes queue
178 179 180 181 182 |
# File 'lib/rgit.rb', line 178 def delete_queue Dir.chdir(".rit_"+@repo) File.delete("queue.json") Dir.chdir("../") end |
#generate ⇒ Object
generate -> creates rit.conf
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rgit.rb', line 56 def generate Dir.chdir(".rit_"+@repo) files = JSON.parse(File.read("queue.json")); Dir.chdir("../") i = 1 puts "Adding to rit.conf #{files.length} file(s)" files.map! do |file| puts "[#{i}/#{files.length}] "+file["dir"]+"/"+file["name"] request = RestClient.post(read_config("rit.conf","repo")+"?action=generate", { :repo => @repo, :dir => file["dir"], :name => read_config("rit.conf","user"), :key => read_config("rit.conf","key"), :last => file["last"], :filename => file["name"] } ); i+=1 end Dir.chdir(".rit_"+@repo) File.delete("queue.json") Dir.chdir("../") end |
#get_files(dir, regex) ⇒ Object
get_files -> recursively finds file matching given regex
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/rgit.rb', line 103 def get_files(dir, regex) returnedFiles = [] i = 0 Find.find(@repo+"/"+dir) do |path| dir, name = File.split(path) if (!File.directory?(File.join(dir,name)) && (File.join(dir,name)) =~ regex) returnedFiles << {:dir => dir, :name => name, :last => File.mtime(path)} puts "Added file: #{dir}/#{name}" i += 1 end end puts "Total: #{i} file(s)" return returnedFiles end |
#get_repo_files ⇒ Object
get_repo_files -> gets file list from repo
131 132 133 134 135 136 137 138 |
# File 'lib/rgit.rb', line 131 def get_repo_files puts "Downloading remote file list" get_repo_conf = RestClient.post(self.read_config("rit.conf","repo")+'?action=update', {:name => self.read_config("rit.conf","user"), :key => self.read_config("rit.conf","key"), :file => File.join(".rit",@repo) }) { |resp, request, result, &block| open(File.join(".rit_"+@repo,"remote.conf"), "wb") do |file| file.write(resp.body) end } end |
#new_file?(file_path) ⇒ Boolean
new_file? -> check if server’s file version is newer than the local one
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/rgit.rb', line 118 def new_file? file_path if !File.exist? file_path return false end local_time = File.mtime(file_path) if !self.read_config("remote.conf",file_path).nil? remote_time = DateTime.parse(self.read_config("remote.conf",file_path)).to_time return local_time < remote_time else return false end end |
#read_config(file, key) ⇒ Object
read_config -> reads key from JSON config file
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/rgit.rb', line 90 def read_config file,key if !FileTest::directory? ".rit_"+@repo puts "You need to create an empty repo first" exit end Dir.chdir(".rit_"+@repo) if File.file? file conf = JSON.parse(File.read(file)) Dir.chdir("../") return conf[key] end end |
#sync ⇒ Object
sync
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/rgit.rb', line 184 def sync self.get_repo_files files = JSON.parse(File.read(File.join(".rit_"+@repo,"remote.conf"))) puts "syncing #{files.length} files" i = 0 files.each do |key, value| print "[#{i+1}/#{files.length}] #{key}" if (!File.exist? key) || (new_file? key) if !File.exist? key file_path = File.split(key); FileUtils.mkdir_p(file_path[0]); end req = RestClient.post(self.read_config("rit.conf","repo")+'?action=update', {:name => self.read_config("rit.conf","user"), :key => self.read_config("rit.conf","key"), :file => key }) { |resp, request, result, &block| puts "\t\t*syncED*" open(key, "wb") do |file| file.write(resp.body) end } else puts "\t\t*UPDATED*" end i += 1 end end |
#upload_files(options) ⇒ Object
upload_files -> pushes files to repo
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rgit.rb', line 20 def upload_files() Dir.chdir(".rit_"+@repo) files = JSON.parse(File.read("queue.json")); Dir.chdir("../") i = 1 self.get_repo_files puts "Pushing #{files.length} file(s)" files.map! do |file| puts "[#{i}/#{files.length}] "+file["dir"]+"/"+file["name"] if (new_file? File.join(file["dir"],file["name"])) puts "Newer version of "+file["dir"]+"/"+file["name"]+" available on the webserver, run rgit --sync" print "Do you really want to overwrite a newer version of "+file["dir"]+"/"+file["name"]+"? (y/n)" choice = gets.chop (choice == "y")? allow = true : allow = false else allow = true end if (allow) request = RestClient.post(read_config("rit.conf","repo")+"?action=upload", { :repo => @repo, :dir => file["dir"], :name => read_config("rit.conf","user"), :key => read_config("rit.conf","key"), :last => file["last"], :rit => File.new(File.join(file["dir"],file["name"]),'rb') } ); end i+=1 end Dir.chdir(".rit_"+@repo) File.delete("queue.json") Dir.chdir("../") end |
#write_config(file, key, value) ⇒ Object
write_config -> updates config file (uses JSON)
81 82 83 84 85 86 87 88 |
# File 'lib/rgit.rb', line 81 def write_config file,key,value Dir.chdir(".rit_"+@repo) conf = JSON.parse(File.read(file)) conf[key] = value f = File.new(file, "w+") f.write(JSON.generate(conf)) Dir.chdir("../") end |
#ziphp ⇒ Object
ziphp -> creates zip for the webserver
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/rgit.rb', line 209 def ziphp if File.exist?("rgit_upload.zip") File.unlink("rgit_upload.zip") end print "Insert username: " name = gets print "Insert password: " pwd = gets Zip::Archive.open('rgit_upload.zip', Zip::CREATE) do |ar| ar.add_dir(".rit_keys") ar.add_dir(".rit"); ar.add_file(File.join(FILE_DIR,"rit.php")) ar.add_buffer(File.join(".rit_keys/",name.chop), pwd.chop) ar.add_file(".rit_keys/.htaccess", File.join(FILE_DIR,".htaccess")) ar.add_file(".rit/.htaccess", File.join(FILE_DIR,".htaccess")) end end |