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.



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

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



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

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

Instance Method Details

#clean_exeObject



86
87
88
89
90
91
92
93
94
# File 'lib/my_help.rb', line 86

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

#edit_help(file) ⇒ Object



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

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
55
# 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', '個別(specific)ヘルプのList表示.'){list_helps}
    opt.on('-e NAME', '--edit NAME', 'NAME(例:test_help)をEdit編集.'){|file| edit_help(file)}
    opt.on('-i NAME', '--init NAME', 'NAME(例:test_help)のtemplateを作成.'){|file| init_help(file)}
    opt.on('-m', '--make', 'make and install:local 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('--initialize','initialize local help directory'){initialize_local}
  end
  begin
    command_parser.parse!(@argv)
  rescue=> eval
    p eval
  end
  exit
end

#init_help(file) ⇒ Object



96
97
98
99
100
# File 'lib/my_help.rb', line 96

def init_help(file)
  p target_help=File.join(@target_dir,file)
  p template = File.join(File.dirname(@source_dir),'my_help','template_help')
  FileUtils::Verbose.cp(template,target_help)
end

#install_localObject



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

def install_local
  Dir.chdir(File.expand_path('../..',@target_dir))
  p Dir.pwd
  system "git add -A"
  system "git commit -m 'update exe dirs'"
  system "Rake install:local"
end

#list_helpsObject



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

def list_helps
  print "Specific help file:\n"
  Dir.entries(@target_dir)[2..-1].each{|file|
    print "  "+file+"\n"
  }
end

#make_helpObject



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

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 << 'target_dir = File.expand_path("../../lib/daddygongon", __FILE__)'+"\n"
    exe_cont << "target_dir = File.join(ENV['HOME'],'.my_help')"+"\n"
    exe_cont << "help_file = File.join(target_dir,'#{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)
    }
  }
end

#set_help_dir_if_not_existsObject



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

def set_help_dir_if_not_exists
  return if File::exists?(@target_dir)
  FileUtils.mkdir_p(@target_dir)
  helps =Dir.entries(@source_dir)
  helps[2..-1].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



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

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