batch_rename 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 48721868993417bc6fa05e903c312b6a86243be9
4
+ data.tar.gz: 84cd80c60c5560fd8a71bd0d9bde4497e7c2ed51
5
+ SHA512:
6
+ metadata.gz: d26499f7b7d2d6220336f939421a064789f33d08ab6273e9e04eaea0fa41d66f2b0c9a714365b7762734f399f771f31e3a024abea5c64014c7b3384c6984a3ba
7
+ data.tar.gz: 33882775aa1b306e5971d518ee05de86bd4fa4542d948efdbf340d101a3ab6047da88e4d9ed764995bf791c1ecc45265e49faee8a28bcaaced7f65bca71136cf
@@ -0,0 +1 @@
1
+ *.gemspec diff
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in batch_rename.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,14 @@
1
+ Copyright (c) 2015, Claudia Pellegrino
2
+
3
+ Permission to use, copy, modify, and/or distribute this software for any
4
+ purpose with or without fee is hereby granted, provided that the above
5
+ copyright notice and this permission notice appear in all copies.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
+
@@ -0,0 +1,70 @@
1
+ # batch_rename
2
+
3
+ This gem provides a CLI command:
4
+
5
+ > `batch-rename`
6
+
7
+ to **bulk rename files** in OS X using an interactive text editor.
8
+
9
+
10
+ ## Background
11
+
12
+ This gem was inspired by a question from StackExchange user [Yuriy Babenko](http://apple.stackexchange.com/users/71676) in June 2015 where he asked whether there was [an OS X app/way to batch rename files via a purely text interface](http://apple.stackexchange.com/questions/192984).
13
+
14
+
15
+ ## System requirements
16
+
17
+ * This gem has been tested on **OS X Yosemite only.**
18
+ It should be trivial though to make it work on Windows, Linux, or OS X pre-Yosemite. PRs are welcome!
19
+
20
+ * The gem **depends on DiffMerge** to be installed, a visual diff editor developed by SourceGear. See the next section for details.
21
+
22
+
23
+ ## Installation
24
+
25
+ 1. First, make sure you have [DiffMerge](https://sourcegear.com/diffmerge) installed. You can either download it from [SourceGear’s website](https://sourcegear.com/diffmerge/downloads.php) or via [Homebrew Cask](http://caskroom.io) (`brew cask install diffmerge`).
26
+
27
+ 2. Install the gem:
28
+
29
+ ```sh
30
+ gem install batch_rename
31
+ ```
32
+
33
+
34
+ ## Usage
35
+
36
+ 1. Open Terminal.app.
37
+
38
+ 2. `cd` to the directory whose content you want to rename.
39
+
40
+ 3. Enter the command:
41
+
42
+ ```sh
43
+ batch-rename
44
+ ```
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/claui/batch_rename.
49
+
50
+
51
+ ## Credits
52
+
53
+ The idea for batch_rename was taken from [a question](http://apple.stackexchange.com/questions/192984) by Yuriy Babenko on apple.stackexchange.com.
54
+
55
+
56
+ ## Legal notice
57
+
58
+ This gem depends on the software DiffMerge to be installed, a product from [SourceGear, LLC](https://sourcegear.com).
59
+ SourceGear, LLC has no connection to batch_rename or to its author whatsoever. In particular, they do not endorse batch_rename or its usage.
60
+
61
+ A separate [license agreement for DiffMerge](https://sourcegear.com/diffmerge/webhelp/chapter_sourcegearlicenseagreement.html) can be found on SourceGear’s website.
62
+
63
+
64
+ ## License
65
+
66
+ Copyright (c) 2015, Claudia Pellegrino
67
+
68
+ Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
69
+
70
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :test => :spec
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'batch_rename/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "batch_rename"
9
+ spec.version = BatchRename::VERSION
10
+ spec.licenses = ["ISC"]
11
+ spec.authors = ["Claudia"]
12
+ spec.email = ["dieclau@eml.cc"]
13
+
14
+ spec.summary = "Batch rename files in OS X using an interactive editor"
15
+ spec.description = <<-EOF
16
+ This gem provides a CLI command `batch-rename` to bulk rename files in OS&nbsp;X using an interactive text editor. It has been inspired by a question on Stack Exchange.
17
+ EOF
18
+ spec.homepage = "http://github.com/claui/batch_rename"
19
+
20
+ all_files = `git ls-files -z`.split("")
21
+ spec_test_files, spec.files = all_files.partition do |f|
22
+ f.match(%r{^(test|spec|features)/})
23
+ end
24
+
25
+ spec.executables = ["batch-rename"]
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.10"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'batch_rename/cli'
4
+
5
+ BatchRename::CLI.run!
@@ -0,0 +1,8 @@
1
+ # encoding: utf-8
2
+
3
+ require 'shellwords'
4
+ require 'tmpdir'
5
+
6
+ require "batch_rename/catalog"
7
+ require "batch_rename/editor"
8
+ require "batch_rename/version"
@@ -0,0 +1,21 @@
1
+ module BatchRename
2
+ class Catalog
3
+ attr_reader :section, :title, :diff_tmp_dir
4
+
5
+ def escaped_file_name
6
+ Shellwords.escape(file_name)
7
+ end
8
+
9
+ def file_name
10
+ "#{ diff_tmp_dir }/#{ section.capitalize } renaming.utf8"
11
+ end
12
+
13
+ private
14
+
15
+ def initialize(section, title, diff_tmp_dir)
16
+ @section = section
17
+ @title = title
18
+ @diff_tmp_dir = diff_tmp_dir
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,61 @@
1
+ # encoding: utf-8
2
+
3
+ require 'batch_rename'
4
+
5
+ module BatchRename
6
+ class CLI
7
+ EXIT_CODE_SUCCESS = 0
8
+ EXIT_CODE_FAILURE = 1
9
+
10
+ EDITOR = Editor::DiffMerge
11
+
12
+ INSTRUCTIONS = {
13
+ :before => "Original file names – do not edit the field below",
14
+ :after => "Rename your files below. Press ⌘S to save, ⌘Q to quit"
15
+ }
16
+
17
+ attr_reader :catalogs, :diff_tmp_dir
18
+
19
+ def self.run!
20
+ begin
21
+ CLI::new.run!
22
+ exit EXIT_CODE_SUCCESS
23
+ rescue Editor::EditorNotFoundError => e
24
+ warn e.message
25
+ exit EXIT_CODE_FAILURE
26
+ end
27
+ end
28
+
29
+ def run!
30
+ caption = "batch_rename v#{VERSION} - #{ Dir.pwd }"
31
+ write_catalog_files!
32
+
33
+ puts 'Launching graphical editor…'
34
+ result = EDITOR.new(catalogs, caption).launch!.result
35
+
36
+ puts "Renaming #{ result.modified_count } file(s)"
37
+ puts rename!(result)
38
+ end
39
+
40
+ private
41
+
42
+ def initialize
43
+ @diff_tmp_dir = Dir.mktmpdir('batch-rename-')
44
+
45
+ @catalogs = [
46
+ Catalog.new(:before, INSTRUCTIONS[:before], diff_tmp_dir),
47
+ Catalog.new(:after, INSTRUCTIONS[:after], diff_tmp_dir)
48
+ ]
49
+ end
50
+
51
+ def write_catalog_files!
52
+ `ls -A | tee #{ catalogs.map(&:escaped_file_name).join(' >') }`
53
+ end
54
+
55
+ def rename!(result)
56
+ result.modified_pairs
57
+ .map { |pair| pair.map(&Shellwords.method(:escape)) }
58
+ .map { |a, b| `mv -nv #{a} #{b}` }
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ module BatchRename
4
+ class Editor; end
5
+ end
6
+
7
+ require 'batch_rename/editor/editor_base'
8
+ require 'batch_rename/editor/editor_not_found_error'
9
+ require 'batch_rename/editor/result'
10
+
11
+ require 'batch_rename/editor/diff_merge'
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+
3
+ module BatchRename
4
+ class Editor::DiffMerge < Editor::EditorBase
5
+ PATH = '/usr/local/bin/diffmerge'
6
+
7
+ def launch!
8
+ unless system(PATH, *args)
9
+ raise(Editor::EditorNotFoundError.new(PATH))
10
+ end
11
+ self
12
+ end
13
+
14
+ def args
15
+ args = ['--nosplash']
16
+ args << "-c=#{ caption }" if caption
17
+
18
+ catalogs.each_with_index do |catalog, index|
19
+ args << "-t#{ index+1 }=#{ catalog.title }"
20
+ args << catalog.file_name
21
+ end
22
+
23
+ args
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+
3
+ module BatchRename
4
+ class Editor::EditorBase
5
+ attr_reader :catalogs, :caption
6
+
7
+ def result
8
+ @result ||= Editor::Result.new(catalogs)
9
+ end
10
+
11
+ private
12
+
13
+ def initialize(catalogs, caption = nil)
14
+ @catalogs = catalogs
15
+ @caption = caption
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ module BatchRename
4
+ class Editor::EditorNotFoundError < StandardError
5
+ attr_reader :path
6
+
7
+ def message
8
+ "Unable to launch \"#{ path }\"; is DiffMerge installed?"
9
+ end
10
+
11
+ private
12
+
13
+ def initialize(path)
14
+ @path = path
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ module BatchRename
4
+ class Editor::Result
5
+ attr_reader :catalogs
6
+
7
+ def total_count
8
+ pairs.length
9
+ end
10
+
11
+ def modified_count
12
+ modified_pairs.length
13
+ end
14
+
15
+ def pairs
16
+ @pairs = lines.reduce(&:zip)
17
+ end
18
+
19
+ def modified_pairs
20
+ @modified_pairs ||= pairs.select { |a| a.reduce(&:!=) }
21
+ end
22
+
23
+ def to_s
24
+ pairs.to_s
25
+ end
26
+
27
+ private
28
+
29
+ def initialize(catalogs)
30
+ @catalogs = catalogs
31
+ end
32
+
33
+ def lines
34
+ @lines ||= catalogs.map do |catalog|
35
+ File.open(catalog.file_name).each.map(&:chop)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module BatchRename
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: batch_rename
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Claudia
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: |
56
+ This gem provides a CLI command `batch-rename` to bulk rename files in OS&nbsp;X using an interactive text editor. It has been inspired by a question on Stack Exchange.
57
+ email:
58
+ - dieclau@eml.cc
59
+ executables:
60
+ - batch-rename
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - .gitattributes
65
+ - .gitignore
66
+ - .rspec
67
+ - .travis.yml
68
+ - Gemfile
69
+ - LICENSE
70
+ - README.md
71
+ - Rakefile
72
+ - batch_rename.gemspec
73
+ - bin/batch-rename
74
+ - lib/batch_rename.rb
75
+ - lib/batch_rename/catalog.rb
76
+ - lib/batch_rename/cli.rb
77
+ - lib/batch_rename/editor.rb
78
+ - lib/batch_rename/editor/diff_merge.rb
79
+ - lib/batch_rename/editor/editor_base.rb
80
+ - lib/batch_rename/editor/editor_not_found_error.rb
81
+ - lib/batch_rename/editor/result.rb
82
+ - lib/batch_rename/version.rb
83
+ homepage: http://github.com/claui/batch_rename
84
+ licenses:
85
+ - ISC
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.4.4
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Batch rename files in OS X using an interactive editor
107
+ test_files: []