lib/yac.rb in wosmvp-yac-0.0.2 vs lib/yac.rb in wosmvp-yac-0.0.3
- old
+ new
@@ -6,35 +6,34 @@
YACRC = File.join("#{ENV['HOME']}",".yacrc")
FileUtils.cp(File.join(File.dirname(__FILE__), "..","resources","yacrc"), YACRC) unless File.exist?(YACRC)
CONFIG = YAML.load_file(File.join(ENV['HOME'],".yacrc"))
CONFIG["root"] ||= File.join(ENV['HOME'],".yac")
- @main_path = File.join(CONFIG["root"],"/main/")
- @pri_path = File.join(CONFIG["root"],"/private/")
+ @main_path, @pri_path = File.join(CONFIG["root"],"/main/"), File.join(CONFIG["root"],"/private/")
@main_git = Git.open(@main_path) if File.exist?(@main_path)
@pri_git = Git.open(@pri_path)if File.exist?(@pri_path)
def new(args)
unless File.exist?(@main_path) && File.exist?(@pri_path)
return unless init
end
@all_result = []
- help && exit if args.empty?
+ (help && exit) if args.empty?
case args.first
when "show" then show(args[1,args.size])
when "name" then search(args[1,args.size],"name")
when "content" then search(args[1,args.size],"content")
when "update" then update(args[1,args.size])
- when "add" then add(args[1,args.size])
- when "edit" then edit(args[1,args.size])
+ when /^(add|edit)$/ then edit(args[1,args.size])
when "help" then help
when "shell" then shell(args[1,args.size])
when "rm" then rm(args[1,args.size])
when "init" then init
else show(args)
end
show_possible_result
+ rescue
end
def show(args)
args.each {|x| show_single(x)}
end
@@ -43,44 +42,44 @@
args.each {|x| search_single(x,type)}
end
def update(args)
begin
- if args
- @main_path.pull if args.to_s =~ /main/
+ unless args.empty?
@pri_git.pull if args.to_s =~ /pri/
+ @main_git.pull if args.to_s =~ /main/
else
- @main_path.pull
+ @main_git.pull
@pri_git.pull
end
rescue
- puts "ERROR: can not update #{args}"
+ puts "ERROR: can not update the repository,"
puts $!
end
end
def edit(args)
args.each {|x| edit_single(x)}
end
- alias add edit
def rm(args)
args.each {|x| rm_single(x)}
end
def help
format_file(File.dirname(__FILE__)+"/../README.rdoc")
end
- def shell(args = "pri")
- puts "Comming Soon :)"
- #case args.to_s
- #when /main/
- # system("cd #{@main_path}")
- #when /pri/
- # system("cd #{@pri_path}")
- #end
+ def shell(args)
+ case args.to_s
+ when /main/
+ puts "\033[31m Welcome To The Main Yac Repository \033[0m"
+ system "cd #{@main_path}; sh"
+ else
+ puts "\033[31m Welcome To The Private Yac Repository \033[0m"
+ system "cd #{@pri_path}; sh"
+ end
end
protected
def format_file(file)
@level = 0
@@ -120,16 +119,14 @@
end
end
def edit_single(args)
full_path(args)
+ prepare_dir
system("#{editor} #{@file_path}")
- begin
- @working_git.add
- @working_git.commit_all(" #{args.sub(/^@/,"")}.ch Updated")
- rescue
- end
+ @working_git.add
+ @working_git.commit_all(" #{args.sub(/^@/,"")}.ch Updated")
end
def editor
ENV['VISUAL'] || ENV['EDITOR'] || "vim"
end
@@ -231,7 +228,12 @@
puts "Private repository has already been initialized."
end
@main_git = Git.open(@main_path) if File.exist?(@main_path)
@pri_git = Git.open(@pri_path)if File.exist?(@pri_path)
puts "Repository init done."
+ end
+
+ def prepare_dir
+ dirseparator = @file_path.rindex(File::Separator)+1
+ FileUtils.mkdir_p(@file_path[0,dirseparator])
end
end