Class: MyHelp::Command

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = []) ⇒ Command

Returns a new instance of Command.



17
18
19
20
21
22
# File 'lib/my_help.rb', line 17

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

Class Method Details

.run(argv = []) ⇒ Object



13
14
15
# File 'lib/my_help.rb', line 13

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

Instance Method Details

#clean_exeObject



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

def clean_exe
  Dir.entries(@target_dir)[2..-1].each{|file|
    next if file[0]=='#' or file[-1]=='~'
    next if file.include?('emacs_help') or file.include?('e_h')
    next if file.include?('template_help') or file.include?('t_h')
    [file, short_name(file)].each{|name|
      p target=File.join('exe',name)
      FileUtils::Verbose.rm(target)
    }
  }
end

#edit_help(file) ⇒ Object



116
117
118
119
# File 'lib/my_help.rb', line 116

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

#executeObject



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 34

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}
  end
  begin
    command_parser.parse!(@argv)
  rescue=> eval
    p eval
  end
  exit
end

#init_help(file) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/my_help.rb', line 106

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

#install_localObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/my_help.rb', line 56

def install_local
  Dir.chdir(File.expand_path('../..',@source_dir))
  p pwd_dir = Dir.pwd
  # check that the working dir should not the gem installed dir,
  # which destroys itself.
  inst_dir="USER INSTALLATION DIRECTORY:"
  status, stdout, stderr = systemu "gem env|grep '#{inst_dir}'"
  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



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

def list_helps
  print "Specific help file:\n"
  Dir.entries(@target_dir)[2..-1].each{|file|
    next if file[0]=='#' or file[-1]=='~'
    file_path=File.join(@target_dir,file)
    help_cont = YAML.load(File.read(file_path))
    print "  #{file}\t:#{help_cont[:head][0][0..-1]}\n"
  }
end

#make_helpObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/my_help.rb', line 79

def make_help
  Dir.entries(@target_dir)[2..-1].each{|file|
    next if file[0]=='#' or file[-1]=='~'
    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, 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



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

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

#short_name(file) ⇒ Object



74
75
76
77
# File 'lib/my_help.rb', line 74

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