|
# -*- encoding: utf-8 -*- |
|
#----------------------------------------------------------------------------- |
|
# This software is provided "AS IS" and without any warranties. |
|
#----------------------------------------------------------------------------- |
|
|
|
require 'find' |
|
require 'fileutils' |
|
|
|
#============================================================================= |
|
|
|
module Tmbox |
|
BUNDLES_PATH = "/Users/#{%x('whoami').chomp}/Library/Application\ Support/TextMate/Bundles" |
|
|
|
class << self |
|
|
|
def bundles_path? |
|
if File::directory?(BUNDLES_PATH) |
|
true |
|
else |
|
raise "Bundles directory is not found" |
|
end |
|
end |
|
|
|
def box_path |
|
Find.find(ENV["HOME"]) do |path| |
|
if File.basename(path) == "Dropbox" && FileTest.directory?(path) |
|
return path |
|
else |
|
next |
|
end |
|
end |
|
end |
|
|
|
def to_dropbox |
|
dropbox = self.box_path |
|
if dropbox |
|
Dir.mkdir("#{dropbox}/Bundles") unless Dir.exists? "#{dropbox}/Bundles" |
|
puts "Directory 'Bundles' in Dropbox folder was successfully created" |
|
else |
|
raise "Dropbox directory is not found" |
|
end |
|
if Dir["#{dropbox}/Bundles/*"].empty? |
|
if FileUtils.cp_r Dir["#{BUNDLES_PATH}/*"], "#{dropbox}/Bundles" |
|
puts "Files successfully copied from #{BUNDLES_PATH}" |
|
else |
|
raise "Unable to copy files from #{BUNDLES_PATH}" |
|
end |
|
else |
|
raise "Directory #{dropbox}/Bundles are not empty" |
|
end |
|
if FileUtils.rm_r Dir["#{BUNDLES_PATH}"] |
|
puts "Directory #{BUNDLES_PATH} was deleted" |
|
else |
|
raise "Can't delete directory #{BUNDLES_PATH}" |
|
end |
|
FileUtils.symlink("#{dropbox}/Bundles", "#{BUNDLES_PATH}") |
|
end |
|
|
|
def from_dropbox |
|
dropbox = self.box_path |
|
if dropbox |
|
if FileUtils.rm_r Dir["#{BUNDLES_PATH}"] |
|
puts "Directory #{BUNDLES_PATH} was deleted" |
|
else |
|
raise "Can't delete directory #{BUNDLES_PATH}" |
|
end |
|
FileUtils.symlink("#{dropbox}/Bundles", "#{BUNDLES_PATH}") |
|
else |
|
raise "Dropbox directory is not found" |
|
end |
|
end |
|
|
|
def reset |
|
dropbox = self.box_path |
|
if FileUtils.safe_unlink("#{BUNDLES_PATH}") |
|
puts "Symlink #{BUNDLES_PATH} was deleted" |
|
else |
|
raise "Can't delete symlink #{BUNDLES_PATH}" |
|
end |
|
if Dir["#{dropbox}/Bundles/*"].empty? |
|
raise "Unable to copy files from #{dropbox}/Bundles. Dir are empty." |
|
else |
|
Dir.mkdir("#{BUNDLES_PATH}") unless Dir.exists? "#{BUNDLES_PATH}" |
|
FileUtils.cp_r Dir["#{dropbox}/Bundles/*"], "#{BUNDLES_PATH}" |
|
puts "Files successfully copied from Dropbox to #{BUNDLES_PATH}" |
|
end |
|
end |
|
|
|
end # class << self |
|
|
|
end |
|
|
|
#============================================================================= |
|
|
|
def start(option) |
|
case option |
|
when :to_dropbox ; Tmbox.to_dropbox |
|
when :from_dropbox ; Tmbox.from_dropbox |
|
when :reset ; Tmbox.reset |
|
else puts "Sorry! Unknown option." |
|
end |
|
end |
|
|
|
# Start |
|
|
|
message = %Q( |
|
============================================================================== |
|
Choose your destiny: |
|
|
|
\e[0;33mto_dropbox\e[0m - transfer TextMate bundles into Dropbox folder. |
|
Create a symlink to that folder. |
|
\e[0;33mfrom_dropbox\e[0m - delete the content of default TextMate bundles folder. |
|
Create a symlink to a folder Bundles in Dropbox. |
|
\e[0;33mreset\e[0m - delete symlink to a folder Bundles in Dropbox. |
|
==============================================================================) |
|
|
|
puts message |
|
print "\e[0;36mWrite your option: \e[0m" |
|
start(gets.chomp.to_sym) |
|
puts "\n" |