cmd_tools 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/bak CHANGED
@@ -1,49 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
- # -*- coding: utf-8 -*-
3
- # bak is an abbreviation of backup.
4
- # Backup files and directories in the current directory into ./bak_since_ directory.
5
2
 
6
3
  if ARGV.empty?
7
- puts <<-EOS
8
- #
9
- $ bak file1 file2 ...
10
- #
11
- EOS
4
+ puts "bak file1 file2 ..."
12
5
 
13
6
  exit
14
7
  end
15
8
 
16
- require 'fileutils'
17
- require 'find'
18
-
19
- BAK_DIR_HEAD = "bak_since_"
20
- BAK_DIR_REG = /(\A|\/)#{BAK_DIR_HEAD}\d{12}\z/
21
- BAK_PREFIX = 'bak.'
22
- CURRENT_TIME = Time.now.strftime("%y%m%d%H%M%S")
23
- BAK_DIR\
24
- = Dir.foreach('.')\
25
- .select{|path| path =~ BAK_DIR_REG}\
26
- .select{|path| File.directory?(path)}\
27
- .first\
28
- || BAK_DIR_HEAD + CURRENT_TIME
29
- BAK_DIR_IN_BAK_DIR_REG = /\A#{BAK_DIR}\/.*#{BAK_DIR_HEAD}\d{12}\z/
30
-
31
- FileUtils.mkdir_p(BAK_DIR)
32
- (ARGV.map{|path| path.chomp('/')} - ['.', '..', BAK_DIR])\
33
- .select{|path| File.exist?(path)}\
34
- .each{|path|
35
- dest = File.join(BAK_DIR, "#{BAK_PREFIX}#{path}.#{CURRENT_TIME}#{File.extname(path)}")
36
- $stdout.puts "Back up #{path}"
37
- FileUtils.cp_r(path, dest)
38
-
39
- Find.find(dest){|path_in_dest|
40
- if path_in_dest =~ BAK_DIR_IN_BAK_DIR_REG && File.directory?(path_in_dest)
41
- $stdout.puts "Delete #{path_in_dest}? [y/N]"
42
- if $stdin.gets.strip =~ /\A[yY]/
43
- $stdout.puts "Deleting: #{path_in_dest}"
44
- FileUtils.rm_rf(path_in_dest)
45
- Find.prune
46
- end
47
- end
48
- }
49
- }
9
+ require "cmd_tools"
10
+ puts ::CmdTools::Bin::Bak.run(ARGV).join("\t")
data/bin/tsh CHANGED
@@ -1,26 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
- # -*- coding: utf-8 -*-
3
- # tsh is an abbreviation of trash.
4
- # Move files and directories to ~/.myTrash directory.
5
- # Should be safer than direct rm.
6
2
 
7
3
  if ARGV.empty?
8
- puts <<-EOS
9
- #
10
- >tsh fileOrDir1 fileOrDir2 ...
11
- #
12
- EOS
4
+ puts "tsh fileOrDir1 fileOrDir2 ..."
13
5
 
14
6
  exit
15
7
  end
16
8
 
17
- require 'fileutils'
18
- MY_TRASH_DIR = File.join(ENV['HOME'], '.myTrash')
19
- CURRENT_TIME = Time.now.strftime("%y%m%d%H%M%S.")
20
- PREFIX = File.join(MY_TRASH_DIR, CURRENT_TIME)
21
-
22
- FileUtils.mkdir_p(MY_TRASH_DIR)
23
- puts (ARGV - %w(. ..))\
24
- .select{|path| File.exist?(path)}\
25
- .each{|path| FileUtils.mv(path, PREFIX + File.basename(path))}\
26
- .join("\t")
9
+ require 'cmd_tools'
10
+ puts ::CmdTools::Bin::Tsh.run(ARGV).join("\t")
@@ -0,0 +1,66 @@
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
@@ -0,0 +1,30 @@
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
@@ -0,0 +1,6 @@
1
+ module CmdTools
2
+ module Bin
3
+ require 'ruby_patch'
4
+ extend ::RubyPatch::AutoLoad
5
+ end
6
+ end
@@ -1,3 +1,3 @@
1
1
  module CmdTools
2
- VERSION = '0.1.3'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/cmd_tools.rb CHANGED
@@ -1,3 +1,5 @@
1
1
  module CmdTools
2
2
  require 'cmd_tools/version'
3
+ require 'ruby_patch'
4
+ extend ::RubyPatch::AutoLoad
3
5
  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: 0.1.3
4
+ version: 0.2.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-07-06 00:00:00.000000000 Z
12
+ date: 2012-08-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ruby_patch
16
- requirement: &2155437300 !ruby/object:Gem::Requirement
16
+ requirement: &79803450 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0.3'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2155437300
24
+ version_requirements: *79803450
25
25
  description: ! 'Command line tools. tsh: mv to ~/.myTrash. bak: bakup.'
26
26
  email:
27
27
  executables:
@@ -37,6 +37,9 @@ files:
37
37
  - bin/tsh
38
38
  - cmd_tools.gemspec
39
39
  - lib/cmd_tools.rb
40
+ - lib/cmd_tools/bin.rb
41
+ - lib/cmd_tools/bin/bak.rb
42
+ - lib/cmd_tools/bin/tsh.rb
40
43
  - lib/cmd_tools/version.rb
41
44
  homepage:
42
45
  licenses: []
@@ -58,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
61
  version: '0'
59
62
  requirements: []
60
63
  rubyforge_project:
61
- rubygems_version: 1.8.15
64
+ rubygems_version: 1.8.11
62
65
  signing_key:
63
66
  specification_version: 3
64
67
  summary: Command line tools.