root/lang/ruby/reposh/trunk/reposh.rb @ 6276

Revision 6276, 3.8 kB (checked in by yhara, 5 years ago)

lang/ruby/reposh: new: vi editing mode (ver.0.1.3)

  • Property svn:executable set to *
Line 
1#
2# reposh.rb - Reposh - Simple VCS Manager Shell
3# usage:
4#   read bottom of this file
5#
6require 'readline'
7require 'yaml'
8require 'optparse'
9
10class Hash
11  def recursive_merge(other)
12    self.merge(other) do |key, my_val, other_val|
13      # for values of a same key
14      if my_val.is_a? Hash and other_val.is_a? Hash
15        my_val.recursive_merge(other_val) # XXX: hang-ups for cyclic hash?
16      else
17        other_val
18      end
19    end
20  end
21end
22
23class Reposh
24  VERSION = "0.1.3"
25  CONF_DEFAULT = {
26    "global" => {
27      "editing_mode" => nil,
28    },
29    "system" => {
30      "default" => {
31        "binpath" => nil,
32        "prompt" => "> ",
33        "default_cmd" => "status",
34      },
35      "darcs" => {
36        "default_cmd" => "whatsnew --summary",
37      }
38    }
39  }
40
41  def initialize
42  end
43
44  def run
45    parse_option(ARGV)
46    @conf_path ||= File.join(ENV["HOME"], ".reposh.yaml")
47    @system_name ||= guess_system
48
49    @conf = load_config(@conf_path)
50    @editing_mode = @conf["global"]["editing_mode"]
51    @binpath     = get_conf(@system_name, "binpath") || @system_name
52    @prompt      = get_conf(@system_name, "prompt")
53    @default_cmd = get_conf(@system_name, "default_cmd")
54    run_loop
55  end
56
57  def parse_option(args)
58    o = OptionParser.new{|opt|
59      opt.on("-c confpath",
60             "path to .reposh.yaml"){|path|
61        @confpath = path
62      }
63      opt.on("-s system",
64             "vcs command name (eg. svn, svk, hg)"){|sys|
65        @system_name = sys
66      }
67      opt.on("-h", "--help",
68             "show this message"){
69        puts opt
70        exit
71      }
72      opt.on("-v", "--version",
73             "show version information"){
74        puts VERSION
75        exit
76      }
77    }
78    o.parse(args)
79  end
80
81  def load_config(path)
82    if File.exist?(path)
83      config_hash = YAML.load(File.read(path))
84      CONF_DEFAULT.recursive_merge(config_hash)
85    else
86      CONF_DEFAULT
87    end
88  end
89
90  def guess_system
91    case
92    when File.directory?(".hg")
93      "hg"
94    when File.directory?("_darcs")
95      "darcs"
96    when File.directory?(".svn")
97      "svn"
98    else
99      "svk"
100    end
101  end
102 
103  def get_conf(system, prop)
104    if @conf["system"][system]
105      @conf["system"][system][prop]
106    else
107      @conf["system"]["default"][prop]
108    end
109  end
110
111  def run_loop
112    if @editing_mode == "vi"
113      Readline.vi_editing_mode
114    end
115
116    puts "Welcome to reposh #{VERSION} (mode: #{@system_name})"
117    loop do
118      cmd = Readline.readline(@prompt, true)
119      cmd = @default_cmd if cmd == ""
120
121      case cmd
122      when "%reload"
123        load __FILE__
124      when "%env"
125        require 'pp'
126        pp ENV
127      when "%version"
128        puts VERSION
129      when nil, "exit", "quit"
130        puts ""
131        exit
132      when /^:(.*)/
133        puts $1
134        execute $1
135      else
136        execute "#{@binpath} #{cmd}"
137      end
138    end
139  end
140
141  def execute(cmd)
142    result = system(cmd)
143    if (not result) and ($?.nil? or $?.exitstatus == 127)
144      puts "error: failed to exec '#{cmd}'"
145    end
146  end
147end
148
149if $0==__FILE__
150  Reposh.new.run
151end
152
153=begin
154== Reposh - what's this?
155Without reposh, you type like this:
156  $ svk di
157  $ svk ci
158  $ svk st
159  $ ls
160but this is not DRY :)
161
162With reposh, you can omit typing all 'svk's:
163  $ reposh
164  Welcome to reposh x.y.z (mode: svk)
165  > di
166  > ci
167  >          # just push [Enter] for 'svk status'
168  > :ls      # start with ':' to run shell commands
169
170== How to use
171(1) write /home/(your name)/.reposh.yaml
172(2) cd to your working directory
173(3) reposh.rb [Enter]
174
175== Options
176see reposh.rb --help
177
178== Commands
179* exit, quit, ^D(^Z)
180  * Quit reposh
181* :ls ~/
182  * Run "ls ~/" by shell
183* [Enter]
184  * Equals to "status" (you can change this by .reposh.yaml)
185* %reload
186  * Reload reposh.rb (for reposh developper)
187  * Some more commands starts with % are supported: see source
188
189All other commands are passed to vcs system.
190
191== Configuration
192see sample.reposh.yaml
193
194=end
Note: See TracBrowser for help on using the browser.