Module: Yac

Extended by:
Yac
Includes:
Format
Included in:
Yac
Defined in:
lib/yac.rb

Constant Summary collapse

YACRC =
File.join("#{ENV['HOME']}",".yacrc")
CONFIG =
YAML.load_file(File.join(ENV['HOME'],".yacrc"))

Constants included from Format

Format::Doc_Error, Format::Image_Error, Format::Pdf_Error

Instance Method Summary collapse

Methods included from Format

#clean_filename, #colorful, #edit_file, #edit_text, #empha, #format_file, #format_section, #prepare_dir

Instance Method Details

#add(args) ⇒ Object



102
103
104
# File 'lib/yac.rb', line 102

def add(args)
  args.each {|x| add_single(x)}
end

#edit(args) ⇒ Object



98
99
100
# File 'lib/yac.rb', line 98

def edit(args)
  args.each {|x| edit_single(x)}
end

#git_command(env, command) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/yac.rb', line 85

def git_command(env,command)
  case env.to_s
  when /main/ then git_path = [@main_path]
  when /all/ then git_path = [@main_path,@pri_path]
  else git_path = [@pri_path]
  end

  git_path.each do |x|
    colorful(x,'filename')
    colorful( `cd #{x} && git #{command}` ,"notice")
  end
end

#helpObject



110
111
112
# File 'lib/yac.rb', line 110

def help
  format_file(File.dirname(__FILE__)+"/../README.rdoc")
end

#initObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/yac.rb', line 42

def init
  FileUtils.mkdir_p(CONFIG['root'])
  {"main" => @main_path,"private" => @pri_path}.each do |name,path|
    if File.exist?(path)
      colorful("#{name} repository has already initialized.","notice")
    else
      if CONFIG["#{name}"] && CONFIG["#{name}"]['clone-from']
        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
        colorful("Initialize #{name} repository from scratch to #{CONFIG['root']}/#{name}","notice")
        Git.init(path)
      end
      @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

#log(args) ⇒ Object



81
82
83
# File 'lib/yac.rb', line 81

def log(args)
  git_command(args,'log --color --date-order --reverse')
end

#new(args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/yac.rb', line 20

def new(args)
  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 "search" then search(args[1,args.size])
  when "update" then update(args[1,args.size])
  when "push" then push(args[1,args.size])
  when "log" then log(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" then shell(args[1,args.size])
  when "rm" then rm(args[1,args.size])
  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
rescue
end

#push(args) ⇒ Object



75
76
77
78
79
# File 'lib/yac.rb', line 75

def push(args)
  git_command(args,'push')
rescue
  colorful("Usage:\nyac push ( main | all )\n\nTry `yac -h` for more help\n\n#{$1}","warn")
end

#rename(args) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/yac.rb', line 125

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")
  #You can use $ yac mv linux.ch linux/ to rename linux.ch to linux/linux.ch
  new_filename = args[1].sub(/\/$/,file.sub(/.*\/(.*)(\..*)/,'/\1')).sub(/^(@)?/,file =~ /^#{@main_path}/ ? "@":"")
  new_name = add_file(new_filename ,file.sub(/.*(\..*)/,'\1'))
  if confirm("You Are Renaming #{file} To #{new_name}")
    prepare_dir(new_name)
    `mv "#{file}" "#{new_name}"`
    @working_git.add
    @working_git.commit_all("#{clean_filename(file)} Renamed to #{clean_filename(new_name)}")
  end
end

#rm(args) ⇒ Object



106
107
108
# File 'lib/yac.rb', line 106

def rm(args)
  args.each {|x| rm_single(x)}
end

#search(args) ⇒ Object



65
66
67
# File 'lib/yac.rb', line 65

def search(args)
  args.each {|x| search_content(x)}
end

#shell(args) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'lib/yac.rb', line 114

def shell(args)
  case args.to_s
  when /main/
    colorful(" Welcome To The Main Yac Repository","notice")
    system "cd \"#{@main_path}\"; sh"
  else
    colorful(" Welcome To The Private Yac Repository","notice")
    system "cd \"#{@pri_path}\"; sh"
  end
end

#show(args) ⇒ Object



61
62
63
# File 'lib/yac.rb', line 61

def show(args)
  args.each {|x| show_single(x)}
end

#update(args) ⇒ Object



69
70
71
72
73
# File 'lib/yac.rb', line 69

def update(args)
  git_command(args,'pull')
rescue
  colorful("ERROR: can not update the repository,\n\n#{$!}","warn")
end