lib/yac.rb in yac-0.1.6 vs lib/yac.rb in yac-1.0.0

- old
+ new

@@ -4,83 +4,99 @@ module Yac include Format extend self 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, @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 = [] + init unless File.exist?(@main_path) && File.exist?(@pri_path) (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 "search" then search(args[1,args.size]) when "update" then update(args[1,args.size]) - when /^(add|edit)$/ then edit(args[1,args.size]) + when "push" then push(args[1,args.size]) + when "add" then add(args[1,args.size]) + when "edit" then edit(args[1,args.size]) when /^(help|-h|yac|--help)$/ then help - when /^(sh|shell)$/ then shell(args[1,args.size]) + when "sh" then shell(args[1,args.size]) when "rm" then rm(args[1,args.size]) - when "init" then init + when "mv" then rename(args[1,args.size]) + when /^version|-v$/ then + load File.join(File.dirname(__FILE__), "..", "yac.gemspec");colorful("yac, version: " + VER,"notice") else show(args) end - show_possible_result rescue end def init FileUtils.mkdir_p(CONFIG['root']) {"main" => @main_path,"private" => @pri_path}.each do |name,path| - unless File.exist?(path) + if File.exist?(path) + colorful("#{name} repository has already initialized.","notice") + else if CONFIG["#{name}"] && CONFIG["#{name}"]['clone-from'] - puts "Initialize #{name} repository from #{CONFIG[name]['clone-from']} to #{CONFIG['root']}/#{name}" + colorful("Initialize #{name} repository from #{CONFIG[name]['clone-from']} to #{CONFIG['root']}/#{name}","notice") Git.clone(CONFIG["#{name}"]['clone-from'], name, :path => CONFIG['root']) else - puts "Initialize #{name} repository from scratch to #{CONFIG['root']}/#{name}" - git = Git.init(path) - git.add - git.commit_all("init #{name} repository") + colorful("Initialize #{name} repository from scratch to #{CONFIG['root']}/#{name}","notice") + Git.init(path) end - puts "#{name} repository initialized." @main_git = Git.open(@main_path) if File.exist?(@main_path) @pri_git = Git.open(@pri_path)if File.exist?(@pri_path) end end end def show(args) args.each {|x| show_single(x)} end - def search(args,type = "name") - args.each {|x| search_single(x,type)} + def search(args) + args.each {|x| search_content(x)} end def update(args) - unless args.empty? - @pri_git.pull if args.to_s =~ /pri/ - @main_git.pull if args.to_s =~ /main/ - else - @main_git.pull && @pri_git.pull + case args.to_s + when /main/ then result = `cd "#{@main_path}" && git pull ` #NOTE There is an bug in the git.gem,Then I will write a new git library use shell command + when /all/ then result = `cd "#{@pri_path}" && git pull && cd "#{@main_path}" && git pull ` + else result = `cd "#{@pri_path}" && git pull` end + colorful(result,"notice") rescue - puts "ERROR: can not update the repository,\n #{$!}" + colorful("ERROR: can not update the repository,\n\n#{$!}","warn") end + def push(args) + case args.to_s + when /main/ then result=@main_git.push + when /all/ then result=@pri_git.push && result << @main_git.push + else result = @pri_git.push + end + colorful(result,"notice") + rescue + colorful("Usage:\nyac push ( main | all )\n\nTry `yac -h` for more help\n\n#{$1}","warn") + end + def edit(args) args.each {|x| edit_single(x)} end + def add(args) + args.each {|x| add_single(x)} + end + def rm(args) args.each {|x| rm_single(x)} end def help @@ -88,99 +104,148 @@ end def shell(args) case args.to_s when /main/ - colorful(" Welcome To The Main Yac Repository","head1") - system "cd #{@main_path}; sh" + colorful(" Welcome To The Main Yac Repository","notice") + system "cd \"#{@main_path}\"; sh" else - colorful(" Welcome To The Private Yac Repository","head1") - system "cd #{@pri_path}; sh" + colorful(" Welcome To The Private Yac Repository","notice") + system "cd \"#{@pri_path}\"; sh" end end - protected - def rm_single(args) - full_path(args) - begin - @working_git.remove(@file_path) - @working_git.commit_all("#{args.sub(/^@/,"")}.ch removed") - rescue Git::GitExecuteError - FileUtils.rm_rf(@file_path) + def rename(args) + (colorful("Usage:\nyac mv [orign_name] [new_name]\n\nTry `yac -h` for more help","warn");exit) unless args.size == 2 + file = search_name(args[0],"Rename") + new_name = file.sub(/^((?:.*)?(?:main|private)\/)(.*)(\..*)/,'\1'+args[1]+'\3') + if confirm("You Are Renaming #{file} To #{new_name}") + `mv "#{file}" "#{new_name}"` + @working_git.add + @working_git.commit_all("#{clean_filename(file)} Renamed to #{clean_filename(new_name)}") end end - def edit_single(args) - full_path(args) - prepare_dir - system("#{editor} #{@file_path}") - @working_git.add - @working_git.commit_all(" #{args.sub(/^@/,"")}.ch Updated") + protected + def add_single(args) + if args.include?('/') && args =~ /(@?)(?:(.*)\/)(.+)/ + path = $1.empty? ? @pri_path : @main_path + all_path = %x{ + find #{path} -type d -iwholename '#{path}*#{$2}*' -not -iwholename '*.git*'| sed 's/^.*\\(private\\|main\\)\\//#{$1}/' + }.to_a + colorful("Which directory do you want to use:","notice") + choosed_path = choose_one(all_path.concat([$1+$2]).uniq) + args = choosed_path + "/" + $3 if choosed_path + end + file = full_path(args+".ch") + if confirm("You Are Adding #{file}") + edit_text(file) + @working_git.add + @working_git.commit_all("#{clean_filename(file)} Added") + end end def show_single(args) - result = search_single(args) - if result.size == 1 - colorful(result.first,"filename") - format_file(result.first) - else - result.map do |x| - if x =~ /\/#{args.sub(/^@/,"")}\.\w+/ - colorful(x,"filename") - format_file(x) - end + file = search_name(args,"Show") + format_file(file) + end + + def rm_single(args) + file = search_name(args,"Remove") + if confirm("You Are Removing #{file}.") + begin + @working_git.remove(file) + @working_git.commit_all("#{clean_filename(file)} Removed") + rescue Git::GitExecuteError + FileUtils.rm_rf(file) end end end - def search_single(args,type="name") - if type =~ /name/ - search_name(args) - else - search_content(args) - end + def edit_single(args) + file = search_name(args,"Edit") + edit_file(file) + @working_git.add + @working_git.commit_all("#{clean_filename(file)} Updated") end - def search_name(args) - if args =~ /^@/ && main = args.sub(/^@/,"") - @private_result = [] - @main_result = @main_git.ls_files.keys.grep(/#{main}/) - else - @private_result = @pri_git.ls_files.keys.grep(/#{args}/) - @main_result = @main_git.ls_files.keys.grep(/#{args}/) + def search_name(args,msg = nil) + path = (args =~ /^(@)/) ? [@main_path] : [@main_path , @pri_path] + result = [] + path.each do |x| + result.concat(`find "#{x}" -type f -iwholename '#{x}*#{args.sub(/^@/,'').strip}*' -not -iwholename '*.git*'| sed 's/^.*\\(private\\|main\\)\\//#{x=~/main/ ? '@':'' }/'`.to_a) end - @all_result << @main_result.collect {|x| "@" + x} << @private_result - - @private_result.map do |x| - @main_result.delete(x) - end - return (@main_result.collect {|x| @main_path +x}).concat(@private_result.collect {|x| @pri_path +x}) + return result.empty? ? (colorful("Nothing Found About < #{args} >","warn")) : + (colorful("The Results About < #{args} > To #{msg || "Operate"} :","notice");full_path(choose_one(result))) end def search_content(args) - result = `cd #{@pri_path} && grep -n #{args} -R *.ch 2>/dev/null` - result << `cd #{@main_path} && grep -n #{args} -R *.ch 2>/dev/null | sed 's/^/@/g'` + args.sub!(/^"(.*)"/,'\1') + result = `cd "#{@pri_path}" && grep -n -i -P '#{args}' -R *.ch 2>/dev/null`.to_a + result.concat(`cd "#{@main_path}" && grep -n -i -P '#{args}' -R *.ch 2>/dev/null | sed 's/^/@/g'`.to_a) + all_result = [] result.each do |x| stuff = x.split(':',3) - colorful(title_of_file(stuff[0]),"filename",false) + colorful(stuff[0],"filename",false) print " " colorful(stuff[1],"line_number",false) print " " - format_section(empha(stuff[2],nil,/((#{args}))/),true) + format_section(empha(stuff[2],nil,/((#{args}))/i),true) + all_result.concat(stuff[0].to_a) end + loop do + colorful("All files Contain #{args.strip},Choose one to show","notice") + file = full_path(choose_one(all_result)) + file ? format_file(file) : exit + end end - def prepare_dir - dirseparator = @file_path.rindex(File::Separator)+1 - FileUtils.mkdir_p(@file_path[0,dirseparator]) - end - def full_path(args) + return false unless args if args =~ /^@/ - @file_path = @main_path + args.sub(/^@/,"") + ".ch" @working_git = @main_git + file = @main_path + args.sub(/^@/,"") else - @file_path = @pri_path + args + ".ch" @working_git = @pri_git + file = @pri_path + args end + return file.strip + end + + def confirm(*msg) + colorful("#{msg.to_s}\nAre You Sure (Y/N) (q to quit):","notice",false) + case STDIN.gets + when /n|q/i + return false + when /y/i + return true + else + colorful("Please Input A Valid String,","warn") + confirm(msg) + end + end + + # Choose one file to operate + def choose_one(stuff) + if stuff.size == 1 + return stuff[0] + elsif stuff.size > 1 + stuff.each_index do |x| + colorful("%2s" % (x+1).to_s,"line_number",false) + printf " %-20s \t" % [stuff[x].rstrip] + print "\n" if (x+1)%4 == 0 + end + printf "\n" + num = choose_range(stuff.size) + return stuff[num-1].to_s.strip + end + rescue #Rescue for user input q to quit + end + + #choose a valid range + def choose_range(size) + colorful("Please Input A Valid Number To Choose (1..#{size}) (q to quit): ","notice",false) + num = STDIN.gets + return if num =~ /q/i + (1..size).member?(num.to_i) ? (return num.to_i) : choose_range(size) end end