cmd_tools 1.1.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,26 +1,7 @@
1
1
  = CmdTools
2
2
  == What is this?
3
3
  Simple small command line utilities.
4
- == Executables
5
- === +tsh+
6
- *rm* alternative. Move files and directories into ~/.myTrash with +TIME_NOW+. prefix.
7
- ==== Usage
8
- tsh file1 file2 *~ dir1 dir2 1206??_photo
9
- === +bak+
10
- Back up files and directories within current directory into ./bak_since_SOME_NUMBERES directory with bak.+TIME_NOW+. prefix.
11
- ==== Usage
12
- bak file1 file2 *~ dir1 dir2 1206??_photo
13
- === +cmd_tools_emacs_launcher_for_alternate_editor_of_emacsclient+
14
- Deleted: please use +emacs_launcher_(g|c)ui+.
15
- === +emacs_launcher_(g|c)ui+
16
- Launch emacs in (G|C)UI mode.
17
- 1. If emacs daemon is not running, start a new daemon.
18
- 2. If GUI mode and no frame exist, create a new frame.
19
- 3. Open +files+ by emacsclient in (G|C)UI mode.
20
- ==== Usage
21
- alias em='emacs_launcher_gui'
22
- alias e='emacs_launcher_cui'
23
- === +emacs_launcher_stop+
24
- Stop emacs daemon launched by +emacs_launcher_(g|c)ui+.
25
- ==== Usage
26
- emacs_launcher_stop
4
+ == Usage
5
+ Please type
6
+ cmd_tools help
7
+ on a command line to see usages.
data/bin/cmd_tools ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'cmd_tools'
4
+ ::CmdTools::Runner.start
data/cmd_tools.gemspec CHANGED
@@ -12,21 +12,21 @@ Gem::Specification.new do |s|
12
12
  s.add_development_dependency 'ruby_patch', '~> 0.3'
13
13
  s.author = 'kshramt'
14
14
  s.description = <<-EOS
15
- Command line tools:
16
- tsh: mv files to ~/.myTrash.
17
- bak: backup files.
18
- emacs_launcher_(g|c)ui: launch emacsclient and daemon (if necessary).
15
+ Command line tools. Please type
16
+
17
+ $ cmd_tools help
18
+
19
+ to see details.
19
20
  EOS
20
- s.executables << 'bak'
21
- s.executables << 'tsh'
22
- s.executables << 'emacs_launcher_gui'
23
- s.executables << 'emacs_launcher_cui'
24
- s.executables << 'emacs_launcher_stop'
21
+ s.executables << 'cmd_tools'
25
22
  s.post_install_message = <<-EOS
26
23
 
27
24
  # CmdTools.
28
- alias em='emacs_launcher_gui'
29
- alias e='emacs_launcher_cui'
25
+ alias bak='cmd_tools backup'
26
+ alias tsh='cmd_tools trash'
27
+ alias em='cmd_tools emacs_launch --mode=gui'
28
+ alias e='cmd_tools emacs_launch --mode=cui'
29
+ alias emacs_stop=''cmd_tools emacs_stop'
30
30
 
31
31
  EOS
32
32
  s.required_ruby_version = '~> 1.9'
@@ -0,0 +1,60 @@
1
+ module ::CmdTools::Command::Backup
2
+ require 'fileutils'
3
+ require 'find'
4
+ require 'ruby_patch'
5
+ extend ::RubyPatch::AutoLoad
6
+
7
+ BAK_DIR_HEAD = "bak_since_"
8
+ BAK_DIR_REG = /(\A|\/)#{BAK_DIR_HEAD}\d{12}\z/
9
+ BAK_PREFIX = 'bak.'
10
+
11
+ # Back up files and directories to <tt>bak_dir</tt>.
12
+ # @param [Array<String>] files Files and directories to be backed up.
13
+ def self.run(*files)
14
+ time_stamp = Time.now.ymdhms
15
+ bak_dir = get_bak_dir(time_stamp)
16
+ nested_bak_dir_reg = /\A#{bak_dir}\/.*#{BAK_DIR_HEAD}\d{12}\z/
17
+ FileUtils.mkdir_p(bak_dir)
18
+ (files.flatten.map{|f| f.chomp('/')} - ['.', '..', bak_dir])\
19
+ .select{|f| File.exist?(f)}\
20
+ .each{|f|
21
+ dest = get_dest(bak_dir, f, time_stamp)
22
+ begin
23
+ FileUtils.cp_r(f, dest)
24
+ rescue
25
+ warn "WARN: Failed to back up #{f}."
26
+ end
27
+
28
+ delete_nested_bak_dir(dest, nested_bak_dir_reg) if(File.directory?(dest))
29
+ }
30
+ end
31
+
32
+ private
33
+
34
+ def self.get_bak_dir(time_stamp)
35
+ Dir.foreach('.')\
36
+ .select{|f| File.directory?(f)}\
37
+ .select{|f| f =~ BAK_DIR_REG}\
38
+ .first\
39
+ or BAK_DIR_HEAD + time_stamp
40
+ end
41
+
42
+ def self.get_dest(bak_dir, file, time_stamp)
43
+ ext = File.extname(file)
44
+ File.join(bak_dir, [BAK_PREFIX, file, '.', time_stamp, ext].join)
45
+ end
46
+
47
+ def self.delete_nested_bak_dir(dest, nested_bak_dir_reg)
48
+ Find.find(dest){|f| # I did not used #select method chain style to use Find.prune.
49
+ next unless File.directory?(f)
50
+ next unless f =~ nested_bak_dir_reg
51
+
52
+ $stdout.puts "Delete #{f}? [y/N]"
53
+ if $stdin.gets.strip =~ /\Ay/i
54
+ $stdout.puts "Deleting: #{f}"
55
+ FileUtils.rm_rf(f)
56
+ Find.prune
57
+ end
58
+ }
59
+ end
60
+ end
@@ -0,0 +1,42 @@
1
+ module ::CmdTools::Command::EmacsLaunch
2
+ require 'ruby_patch'
3
+ extend ::RubyPatch::AutoLoad
4
+
5
+ # Open +files+ by +mode+ (:gui/:cui) mode.
6
+ # Launch emacs daemon if necessary.
7
+ # Create new frame if necessary.
8
+ # You can edit ~/.config/cmd_tools/config.yaml to specify an emacs executable.
9
+ # For example, if you are a Mac user who uses MacPorts, following modification will be useful.
10
+ # - :emacs: emacs
11
+ # + :emacs: /Applications/MacPorts/Emacs.app/Contents/MacOS/Emacs
12
+ def self.run(mode, *files)
13
+ system "#{::CmdTools::Config.emacs} --daemon" unless self.daemon_running?
14
+
15
+ files = files.flatten.join(' ')
16
+ case mode
17
+ when :gui
18
+ if self.number_of_frames <= 1 # emacs daemon has one (invisible) frame.
19
+ exec "emacsclient -c -n #{files}"
20
+ else
21
+ exec "emacsclient -n #{files}"
22
+ end
23
+ when :cui
24
+ exec "emacsclient -t #{files}"
25
+ else
26
+ raise ArgumentError, "Expected :gui or :cui, but got #{mode}."
27
+ end
28
+ end
29
+
30
+ # Stop emacs daemon.
31
+ def self.stop
32
+ exec "emacsclient -e '(kill-emacs)'"
33
+ end
34
+
35
+ def self.daemon_running?
36
+ system "emacsclient -e '()' > /dev/null 2>&1"
37
+ end
38
+
39
+ def self.number_of_frames
40
+ `emacsclient -e "(length (visible-frame-list))"`.to_i
41
+ end
42
+ end
@@ -0,0 +1,24 @@
1
+ module ::CmdTools::Command::Trash
2
+ require 'fileutils'
3
+ require 'ruby_patch'
4
+ extend ::RubyPatch::AutoLoad
5
+
6
+ TRASH_DIR = File.join(ENV['HOME'], '.myTrash')
7
+
8
+ # Move files and directories into <tt>TRASH_DIR</tt>.
9
+ # @param [Array<String>] files Files and directories to be moved into <tt>TRASH_DIR</tt>.
10
+ def self.run(*files)
11
+ FileUtils.mkdir_p(TRASH_DIR)
12
+ time = Time.now.ymdhms
13
+ prefix = File.join(TRASH_DIR, time) + '.'
14
+ (files.flatten - ['.', '..'])\
15
+ .select{|f| File.exist?(f)}\
16
+ .each{|f|
17
+ begin
18
+ FileUtils.mv(f, prefix + File.basename(f))
19
+ rescue
20
+ warn "WARN: Failed to discard #{f}."
21
+ end
22
+ }
23
+ end
24
+ end
@@ -0,0 +1,4 @@
1
+ module ::CmdTools::Command
2
+ require 'ruby_patch'
3
+ extend ::RubyPatch::AutoLoad
4
+ end
@@ -0,0 +1,37 @@
1
+ require 'thor'
2
+
3
+ class ::CmdTools::Runner < Thor
4
+ require 'ruby_patch'
5
+ extend ::RubyPatch::AutoLoad
6
+
7
+ desc "backup FILE1 FILE2 ...", "Backup files and directories."
8
+ def backup(*files)
9
+ puts ::CmdTools::Command::Backup.run(*files).join("\t")
10
+ end
11
+
12
+ desc "trash FILE1 FILE2 ...", "Move files and directories to ~/.myTrash"
13
+ def trash(*files)
14
+ puts ::CmdTools::Command::Trash.run(*files).join("\t")
15
+ end
16
+
17
+ desc "emacs_launch FILE1 FILE2 ...", "Launch emacs in MODE mode."
18
+ long_desc <<-EOS
19
+ Launch emacs in (G|C)UI mode.
20
+
21
+ 1. If emacs daemon is not running, start a new daemon.
22
+
23
+ 2. If GUI mode and no frame exist, create a new frame.
24
+
25
+ 3. Open files by emacsclient in (G|C)UI mode.
26
+ EOS
27
+ method_option :mode, type: :string, required: true, desc: "MODE should be 'gui' or 'cui'."
28
+ def emacs_launch(*files)
29
+ mode = options['mode'].to_sym
30
+ ::CmdTools::Command::EmacsLaunch.run(mode, *files)
31
+ end
32
+
33
+ desc "emacs_stop", "Stop emacs daemon."
34
+ def emacs_stop
35
+ ::CmdTools::Command::EmacsLaunch.stop
36
+ end
37
+ end
@@ -1,3 +1,3 @@
1
1
  module CmdTools
2
- VERSION = '1.1.1'
2
+ VERSION = '2.0.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cmd_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 2.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-27 00:00:00.000000000 Z
12
+ date: 2012-09-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ruby_patch
16
- requirement: &75179250 !ruby/object:Gem::Requirement
16
+ requirement: &69899570 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,32 +21,33 @@ dependencies:
21
21
  version: '0.3'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *75179250
25
- description: ! "Command line tools:\n tsh: mv files to ~/.myTrash.\n bak: backup
26
- files.\n emacs_launcher_(g|c)ui: launch emacsclient and daemon (if necessary).\n"
24
+ version_requirements: *69899570
25
+ description: ! 'Command line tools. Please type
26
+
27
+
28
+ $ cmd_tools help
29
+
30
+
31
+ to see details.
32
+
33
+ '
27
34
  email:
28
35
  executables:
29
- - bak
30
- - tsh
31
- - emacs_launcher_gui
32
- - emacs_launcher_cui
33
- - emacs_launcher_stop
36
+ - cmd_tools
34
37
  extensions: []
35
38
  extra_rdoc_files: []
36
39
  files:
37
40
  - README.rdoc
38
- - bin/bak
39
- - bin/emacs_launcher_cui
40
- - bin/emacs_launcher_gui
41
- - bin/emacs_launcher_stop
42
- - bin/tsh
41
+ - bin/cmd_tools
43
42
  - cmd_tools.gemspec
44
43
  - lib/cmd_tools.rb
45
44
  - lib/cmd_tools/bin.rb
46
- - lib/cmd_tools/bin/bak.rb
47
- - lib/cmd_tools/bin/emacs_launcher.rb
48
- - lib/cmd_tools/bin/tsh.rb
45
+ - lib/cmd_tools/command.rb
46
+ - lib/cmd_tools/command/backup.rb
47
+ - lib/cmd_tools/command/emacs_launch.rb
48
+ - lib/cmd_tools/command/trash.rb
49
49
  - lib/cmd_tools/config.rb
50
+ - lib/cmd_tools/runner.rb
50
51
  - lib/cmd_tools/version.rb
51
52
  homepage:
52
53
  licenses: []
@@ -54,9 +55,15 @@ post_install_message: ! '
54
55
 
55
56
  # CmdTools.
56
57
 
57
- alias em=''emacs_launcher_gui''
58
+ alias bak=''cmd_tools backup''
59
+
60
+ alias tsh=''cmd_tools trash''
61
+
62
+ alias em=''cmd_tools emacs_launch --mode=gui''
63
+
64
+ alias e=''cmd_tools emacs_launch --mode=cui''
58
65
 
59
- alias e=''emacs_launcher_cui''
66
+ alias emacs_stop=''''cmd_tools emacs_stop''
60
67
 
61
68
 
62
69
  '
@@ -82,3 +89,4 @@ signing_key:
82
89
  specification_version: 3
83
90
  summary: Command line tools.
84
91
  test_files: []
92
+ has_rdoc:
data/bin/bak DELETED
@@ -1,10 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- if ARGV.empty?
4
- puts "bak file1 file2 ..."
5
-
6
- exit
7
- end
8
-
9
- require "cmd_tools"
10
- puts ::CmdTools::Bin::Bak.run(ARGV).join("\t")
@@ -1,4 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "cmd_tools"
4
- ::CmdTools::Bin::EmacsLauncher.run(:cui, ARGV)
@@ -1,4 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "cmd_tools"
4
- ::CmdTools::Bin::EmacsLauncher.run(:gui, ARGV)
@@ -1,4 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "cmd_tools"
4
- ::CmdTools::Bin::EmacsLauncher.stop
data/bin/tsh DELETED
@@ -1,10 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- if ARGV.empty?
4
- puts "tsh fileOrDir1 fileOrDir2 ..."
5
-
6
- exit
7
- end
8
-
9
- require 'cmd_tools'
10
- puts ::CmdTools::Bin::Tsh.run(ARGV).join("\t")
@@ -1,66 +0,0 @@
1
- module CmdTools
2
- module Bin
3
-
4
- # bak is an abbreviation of backup.
5
- module Bak
6
- require 'fileutils'
7
- require 'find'
8
- require 'ruby_patch'
9
- extend ::RubyPatch::AutoLoad
10
-
11
- BAK_DIR_HEAD = "bak_since_"
12
- BAK_DIR_REG = /(\A|\/)#{BAK_DIR_HEAD}\d{12}\z/
13
- BAK_PREFIX = 'bak.'
14
-
15
- # Back up files and directories to <tt>bak_dir</tt>.
16
- # @param [Array<String>] files Files and directories to be backed up.
17
- def self.run(*files)
18
- time_stamp = Time.now.ymdhms
19
- bak_dir = get_bak_dir(time_stamp)
20
- nested_bak_dir_reg = /\A#{bak_dir}\/.*#{BAK_DIR_HEAD}\d{12}\z/
21
- FileUtils.mkdir_p(bak_dir)
22
- (files.flatten.map{|f| f.chomp('/')} - ['.', '..', bak_dir])\
23
- .select{|f| File.exist?(f)}\
24
- .each{|f|
25
- dest = get_dest(bak_dir, f, time_stamp)
26
- begin
27
- FileUtils.cp_r(f, dest)
28
- rescue
29
- warn "WARN: Failed to back up #{f}."
30
- end
31
-
32
- delete_nested_bak_dir(dest, nested_bak_dir_reg) if(File.directory?(dest))
33
- }
34
- end
35
-
36
- private
37
-
38
- def self.get_bak_dir(time_stamp)
39
- Dir.foreach('.')\
40
- .select{|f| File.directory?(f)}\
41
- .select{|f| f =~ BAK_DIR_REG}\
42
- .first\
43
- or BAK_DIR_HEAD + time_stamp
44
- end
45
-
46
- def self.get_dest(bak_dir, file, time_stamp)
47
- ext = File.extname(file)
48
- File.join(bak_dir, [BAK_PREFIX, file, '.', time_stamp, ext].join)
49
- end
50
-
51
- def self.delete_nested_bak_dir(dest, nested_bak_dir_reg)
52
- Find.find(dest){|f| # I did not used #select method chain style to use Find.prune.
53
- next unless File.directory?(f)
54
- next unless f =~ nested_bak_dir_reg
55
-
56
- $stdout.puts "Delete #{f}? [y/N]"
57
- if $stdin.gets.strip =~ /\Ay/i
58
- $stdout.puts "Deleting: #{f}"
59
- FileUtils.rm_rf(f)
60
- Find.prune
61
- end
62
- }
63
- end
64
- end
65
- end
66
- end
@@ -1,46 +0,0 @@
1
- module CmdTools
2
- module Bin
3
- module EmacsLauncher
4
- require 'ruby_patch'
5
- extend ::RubyPatch::AutoLoad
6
-
7
- # Open +files+ by +mode+ (:gui/:cui) mode.
8
- # Launch emacs daemon if necessary.
9
- # Create new frame if necessary.
10
- # You can edit ~/.config/cmd_tools/config.yaml to specify an emacs executable.
11
- # For example, if you are a Mac user who uses MacPorts, following modification will be useful.
12
- # - :emacs: emacs
13
- # + :emacs: /Applications/MacPorts/Emacs.app/Contents/MacOS/Emacs
14
- def self.run(mode, *files)
15
- system "#{::CmdTools::Config.emacs} --daemon" unless self.daemon_running?
16
-
17
- files = files.flatten.join(' ')
18
- case mode
19
- when :gui
20
- if self.number_of_frames <= 1 # emacs daemon has one (invisible) frame.
21
- exec "emacsclient -c -n #{files}"
22
- else
23
- exec "emacsclient -n #{files}"
24
- end
25
- when :cui
26
- exec "emacsclient -t #{files}"
27
- else
28
- raise ArgumentError, "Unknown mode: #{mode}"
29
- end
30
- end
31
-
32
- # Stop emacs daemon.
33
- def self.stop
34
- exec "emacsclient -e '(kill-emacs)'"
35
- end
36
-
37
- def self.daemon_running?
38
- system "emacsclient -e '()' > /dev/null 2>&1"
39
- end
40
-
41
- def self.number_of_frames
42
- `emacsclient -e "(length (visible-frame-list))"`.to_i
43
- end
44
- end
45
- end
46
- end
@@ -1,30 +0,0 @@
1
- module CmdTools
2
- module Bin
3
-
4
- # tsh is an abbreviation of trash.
5
- module Tsh
6
- require 'fileutils'
7
- require 'ruby_patch'
8
- extend ::RubyPatch::AutoLoad
9
-
10
- TRASH_DIR = File.join(ENV['HOME'], '.myTrash')
11
-
12
- # Move files and directories into <tt>TRASH_DIR</tt>.
13
- # @param [Array<String>] files Files and directories to be moved into <tt>TRASH_DIR</tt>.
14
- def self.run(*files)
15
- FileUtils.mkdir_p(TRASH_DIR)
16
- time = Time.now.ymdhms
17
- prefix = File.join(TRASH_DIR, time) + '.'
18
- (files.flatten - ['.', '..'])\
19
- .select{|f| File.exist?(f)}\
20
- .each{|f|
21
- begin
22
- FileUtils.mv(f, prefix + File.basename(f))
23
- rescue
24
- warn "WARN: Failed to discard #{f}."
25
- end
26
- }
27
- end
28
- end
29
- end
30
- end