Created
February 13, 2012 15:24
-
-
Save AlexParamonov/1817577 to your computer and use it in GitHub Desktop.
file used to archive and restore code to/from cloud
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "rubygems" # ruby1.9 doesn't "require" it though | |
require "facets/string/unindent" | |
require "thor" | |
class Migrate < Thor | |
class Logger | |
def initialize(color, console) | |
@color = color | |
@console = console | |
end | |
def log(message) | |
@console.say message.unindent, @color | |
end | |
alias_method :<<, :log | |
end | |
attr_reader :green, :red, :cyan | |
def initialize(*args) | |
super(*args) | |
@green = Logger.new :green, self | |
@red = Logger.new :red, self | |
@cyan = Logger.new :cyan, self | |
end | |
desc "pack", "archive Project sources to cloud" | |
def pack | |
project_links.each do |link| | |
archive = File.dirname(link.path) << "/" << File.basename(link.target) << ".tar.gz" | |
if File.exist?(archive) and not changed?(link.target, archive) | |
cyan.log "Skipped #{link.target}" | |
next | |
end | |
green.log <<-LOG | |
----------------- | |
Packing | |
----------------- | |
#{link.target} | |
to | |
#{archive} | |
LOG | |
File.delete archive rescue nil | |
`tar czf "#{archive}" -C "#{link.target}" "."` | |
end | |
say 'done' | |
end | |
desc "unpack", "restore sources from cloud" | |
method_option :force, :aliases => "-f", :desc => "Overwrite files even they where changed after archive was created" | |
def unpack | |
project_links.each do |link| | |
file_name = File.basename(link.target) | |
timestamp = File.dirname(link.target) << "/" << ".#{file_name}.timestamp" | |
archive = File.dirname(link.path) << "/" << file_name << ".tar.gz" | |
unless File.exist?(archive) | |
red.log "Archive '#{archive}' does not exists" | |
next | |
end | |
if options[:force].nil? and changed?(link.target, archive) | |
red.log "Files at '#{link.target}' has been modified after archive was created!" | |
`find "#{link.target}" -newer "#{archive}"`.split.each do |file| | |
red.log file | |
end | |
red.log "Resolve conflicts and rerun with '-f' option\n" | |
next | |
end | |
if File.exist?(timestamp) and File.mtime(timestamp) > File.mtime(archive) | |
cyan.log "Skipped '#{link.target}'" | |
next | |
end | |
green.log <<-LOG | |
----------------- | |
Extracting | |
----------------- | |
#{archive} | |
to | |
#{link.target} | |
LOG | |
`rm -rf "#{link.target}"` | |
`mkdir -p "#{link.target}"` | |
`tar xfz "#{archive}" -C "#{link.target}"` | |
`touch "#{timestamp}"` | |
end | |
say 'done' | |
end | |
private | |
def project_links | |
link_struct = Struct.new(:path, :target) | |
`find ./Projects -type l`.split("\n").map do |link| | |
link_struct.new(File.absolute_path(link), `readlink "#{link}"`.strip) | |
end | |
end | |
def changed?(dir, archive) | |
not `find "#{dir}" -newer "#{archive}" | wc -l`.to_i == 0 | |
end | |
end | |
Migrate.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment