Class: MyHelp::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/my_help.rb

Constant Summary collapse

USER_INST_DIR =
"USER INSTALLATION DIRECTORY:"
INST_DIR =
"INSTALLATION DIRECTORY:"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = []) ⇒ Command

Returns a new instance of Command.



15
16
17
18
19
20
# File 'lib/my_help.rb', line 15

def initialize(argv=[])
  @argv = argv
  @default_help_dir = File.expand_path("../../lib/daddygongon", __FILE__)
  @local_help_dir = File.join(ENV['HOME'],'.my_help')
  set_help_dir_if_not_exists
end

Class Method Details

.run(argv = []) ⇒ Object



11
12
13
# File 'lib/my_help.rb', line 11

def self.run(argv=[])
  new(argv).execute
end

Instance Method Details

#clean_exeObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/my_help.rb', line 110

def clean_exe
  local_help_entries.each{|file|
    next if ['emacs_help','e_h','my_help','my_todo'].include?(file)
    file = File.basename(file,'.yml')
    [file, short_name(file)].each{|name|
      p target=File.join('exe',name)
      FileUtils::Verbose.rm(target)
    }
  }
end

#delete_help(file) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/my_help.rb', line 56

def delete_help(file)
  del_files=[]
  del_files << File.join(@local_help_dir,file)
   exe_dir=File.join(File.expand_path('../..',@default_help_dir),'exe')
   del_files << File.join(exe_dir,file)
  p del_files << File.join(exe_dir,short_name(file))
  print "Are you sure to delete these files?[yes]"
  if gets.chomp=='yes' then
    del_files.each{|file| FileUtils.rm(file,:verbose=>true)}
  end
end

#edit_help(file) ⇒ Object



131
132
133
134
# File 'lib/my_help.rb', line 131

def edit_help(file)
  p target_help=File.join(@local_help_dir,file)
  system "emacs #{target_help}.yml"
end

#executeObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/my_help.rb', line 33

def execute
  @argv << '--help' if @argv.size==0
  command_parser = OptionParser.new do |opt|
    opt.on('-v', '--version','show program Version.') { |v|
      opt.version = MyHelp::VERSION
      puts opt.ver
    }
    opt.on('-l', '--list', 'list specific helps'){list_helps}
    opt.on('-e NAME', '--edit NAME', 'edit NAME help(eg test_help)'){|file| edit_help(file)}
    opt.on('-i NAME', '--init NAME', 'initialize NAME help(eg test_help).'){|file| init_help(file)}
    opt.on('-m', '--make', 'make executables for all helps.'){make_help}
    opt.on('-c', '--clean', 'clean up exe dir.'){clean_exe}
    opt.on('--install_local','install local after edit helps'){install_local}
    opt.on('--delete NAME','delete NAME help'){|file| delete_help(file)}
  end
  begin
    command_parser.parse!(@argv)
  rescue=> eval
    p eval
  end
  exit
end

#init_help(file) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/my_help.rb', line 121

def init_help(file)
  p target_help=File.join(@local_help_dir,file+'.yml')
  if File::exists?(target_help)
    puts "File exists. rm it first to initialize it."
    exit
  end
  p template = File.join(@default_help_dir,'template_help.yml')
  FileUtils::Verbose.cp(template,target_help)
end

#install_localObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/my_help.rb', line 70

def install_local
  Dir.chdir(File.expand_path('../..',@default_help_dir))
  p pwd_dir = Dir.pwd
  # check that the working dir should not the gem installed dir,
  # which destroys itself.
  status, stdout, stderr = systemu "gem env|grep '#{USER_INST_DIR}'"
  if stdout==""
    status, stdout, stderr = systemu "gem env|grep '#{INST_DIR}'"
  end
  p system_inst_dir = stdout.split(': ')[1].chomp
  if pwd_dir == system_inst_dir
    puts "Download my_help from github, and using bundle for edit helps\n"
    puts "Read README in detail.\n"
    exit
  end
  system "git add -A"
  system "git commit -m 'update exe dirs'"
  system "Rake install:local"
end

#list_helpsObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/my_help.rb', line 146

def list_helps
  print "Specific help file:\n"
  local_help_entries.each{|file|
    file_path=File.join(@local_help_dir,file)
    file = File.basename(file,'.yml')
    begin
      help = YAML.load(File.read(file_path))
    rescue=> eval
      p eval
      print "\n YAML load error in #{file}."
      print "  See the line shown above and revise by\n"
      print "  emacs #{file_path}\n"
      exit
    end
    print "  #{file}\t:#{help[:head][0]}\n"
  }
end

#local_help_entriesObject



136
137
138
139
140
141
142
143
144
# File 'lib/my_help.rb', line 136

def local_help_entries
  entries= []
  Dir.entries(@local_help_dir).each{|file|
    next unless file.include?('_')
    next if file[0]=='#' or file[-1]=='~' or file[0]=='.'
    entries << file
  }
  return entries
end

#make_helpObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/my_help.rb', line 95

def make_help
  local_help_entries.each{|file|
    exe_cont="#!/usr/bin/env ruby\nrequire 'specific_help'\n"
    exe_cont << "help_file = File.join(ENV['HOME'],'.my_help','#{file}')\n"
    exe_cont << "SpecificHelp::Command.run(help_file, ARGV)\n"
    file = File.basename(file,'.yml')
    [file, short_name(file)].each{|name|
      p target=File.join('exe',name)
      File.open(target,'w'){|file| file.print exe_cont}
      FileUtils.chmod('a+x', target, :verbose => true)
    }
  }
  install_local
end

#set_help_dir_if_not_existsObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/my_help.rb', line 22

def set_help_dir_if_not_exists
  return if File::exists?(@local_help_dir)
  FileUtils.mkdir_p(@local_help_dir, :verbose=>true)
  Dir.entries(@default_help_dir).each{|file|
    next if file=='template_help.yml'
    file_path=File.join(@local_help_dir,file)
    next if File::exists?(file_path)
    FileUtils.cp((File.join(@default_help_dir,file)),@local_help_dir,:verbose=>true)
  }
end

#short_name(file) ⇒ Object



90
91
92
93
# File 'lib/my_help.rb', line 90

def short_name(file)
  file_name=file.split('_')
  return file_name[0][0]+"_"+file_name[1][0]
end