rename_gem 0.0.4 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +43 -0
- data/{bin → exe}/rename_gem +0 -0
- data/lib/rename_gem/cli.rb +16 -0
- data/lib/rename_gem/renamer.rb +19 -0
- data/lib/rename_gem/renamer/context.rb +20 -0
- data/lib/rename_gem/renamer/directory_handler.rb +50 -0
- data/lib/rename_gem/renamer/file_handler.rb +60 -0
- data/lib/rename_gem/renamer/path.rb +59 -0
- data/lib/rename_gem/renamer/possession.rb +20 -0
- data/lib/rename_gem/renamer/reporter.rb +21 -0
- data/lib/rename_gem/renamer/runner.rb +42 -0
- data/lib/rename_gem/renamer/string_replacer.rb +39 -0
- data/lib/rename_gem/version.rb +5 -0
- metadata +20 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '0109bc475b09108b0483e11ecd1f516b8e3f16fd8460dcabdf879f2f67fce7e7'
|
4
|
+
data.tar.gz: 4f605e4c61a0d66cec240067fbe9d9edc7ffa72a7b4b0425438398c07ee17102
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a75d6b664d64edda37b0aa02e6ba07f2f6b7da522fd29b82626c07178b6d7f2ca388d8c5ae4791e9ac7fd7537f07c807c7b97a451e549e65de329a7a7cbbe37d
|
7
|
+
data.tar.gz: e1ffced29709254c4f90220170b501e3c7823e541aea396913c45550c0ef1dd94635d7939e29c491679c08fc2d18b9be5f8e44120c79fa43edf1f6bf3534c999
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# Rename Gem
|
2
|
+
|
3
|
+
This Ruby Gem is a work in progress, its goal is to be able to rename gems.
|
4
|
+
|
5
|
+
[](https://travis-ci.org/ronanduddy/Ruby-Gem-Renamer)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'rename_gem'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
```Shell
|
18
|
+
bundle install
|
19
|
+
```
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
```Shell
|
24
|
+
gem install rename_gem
|
25
|
+
```
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
```Shell
|
30
|
+
bundle exec rename_gem rename -f project_name -t foo_bar -p project
|
31
|
+
```
|
32
|
+
|
33
|
+
## Development
|
34
|
+
|
35
|
+
Run `make test` to run all the tests or `make guard` to use guard for testing. You can also run `make irb` for an interactive prompt that will allow you to experiment.
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ronanduddy/Ruby-Gem-Renamer. Please read [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) for details on our code of conduct.
|
40
|
+
|
41
|
+
## License
|
42
|
+
|
43
|
+
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details
|
data/{bin → exe}/rename_gem
RENAMED
File without changes
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RenameGem
|
4
|
+
require 'thor'
|
5
|
+
require 'rename_gem/renamer'
|
6
|
+
|
7
|
+
class CLI < Thor
|
8
|
+
desc 'rename', 'rename all instances of FROMs to TO for a given file'
|
9
|
+
option :from, type: :string, aliases: '-f', required: true, desc: 'the original name'
|
10
|
+
option :to, type: :string, aliases: '-t', required: true, desc: 'the new name'
|
11
|
+
option :path, type: :string, aliases: '-p', required: true, desc: 'the path to run the renaming'
|
12
|
+
def rename
|
13
|
+
Renamer.run(options)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'pathname'
|
4
|
+
require 'rename_gem/renamer/context'
|
5
|
+
require 'rename_gem/renamer/directory_handler'
|
6
|
+
require 'rename_gem/renamer/file_handler'
|
7
|
+
require 'rename_gem/renamer/path'
|
8
|
+
require 'rename_gem/renamer/possession'
|
9
|
+
require 'rename_gem/renamer/reporter'
|
10
|
+
require 'rename_gem/renamer/runner'
|
11
|
+
require 'rename_gem/renamer/string_replacer'
|
12
|
+
|
13
|
+
module RenameGem
|
14
|
+
module Renamer
|
15
|
+
def self.run(options)
|
16
|
+
Runner.new(options).run
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RenameGem
|
4
|
+
module Renamer
|
5
|
+
class Context
|
6
|
+
attr_reader :from, :to, :pwd, :path
|
7
|
+
|
8
|
+
def initialize(pwd, path, from, to)
|
9
|
+
@pwd = pwd
|
10
|
+
@path = path
|
11
|
+
@from = from
|
12
|
+
@to = to
|
13
|
+
end
|
14
|
+
|
15
|
+
def using(new_path)
|
16
|
+
self.class.new(pwd, new_path, from, to)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RenameGem
|
4
|
+
module Renamer
|
5
|
+
class DirectoryHandler
|
6
|
+
attr_reader :context, :path, :results
|
7
|
+
|
8
|
+
def initialize(context)
|
9
|
+
@context = context
|
10
|
+
@path = Path.new(context.path, context.pwd)
|
11
|
+
@results = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def change_files
|
15
|
+
path.files.each do |pathname|
|
16
|
+
file_handler = FileHandler.new(pathname)
|
17
|
+
results << "Edit #{pathname.path}" if file_handler.edit(context.from, context.to)
|
18
|
+
results << "Rename #{pathname.path} -> #{file_handler.path.filename}" if file_handler.rename(context.from,
|
19
|
+
context.to)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def rename
|
24
|
+
built_path = path.build(replacement(path.filename))
|
25
|
+
|
26
|
+
FileUtils.mv(path.to_s, built_path.to_s)
|
27
|
+
|
28
|
+
results << "Rename #{path.path} -> #{built_path.filename}"
|
29
|
+
@path = built_path
|
30
|
+
|
31
|
+
true
|
32
|
+
rescue StringReplacer::NoMatchError
|
33
|
+
false
|
34
|
+
end
|
35
|
+
|
36
|
+
def directories
|
37
|
+
path.directories.map do |directory_path|
|
38
|
+
self.class.new(context.using(directory_path.path))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def replacement(text)
|
45
|
+
replacer = StringReplacer.new(text)
|
46
|
+
replacer.replace(context.from).with(context.to)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RenameGem
|
4
|
+
module Renamer
|
5
|
+
require 'tempfile'
|
6
|
+
|
7
|
+
class FileHandler
|
8
|
+
attr_reader :path, :file, :possession
|
9
|
+
|
10
|
+
def initialize(path)
|
11
|
+
@path = path
|
12
|
+
@file = File.new(path.to_s)
|
13
|
+
@possession = Possession.new(file)
|
14
|
+
end
|
15
|
+
|
16
|
+
def edit(from, to)
|
17
|
+
temp_file = Tempfile.new(path.filename)
|
18
|
+
changes = false
|
19
|
+
|
20
|
+
file.each_line do |line|
|
21
|
+
temp_file.puts replacement(line, from, to)
|
22
|
+
|
23
|
+
changes = true
|
24
|
+
rescue StringReplacer::NoMatchError
|
25
|
+
temp_file.puts line
|
26
|
+
end
|
27
|
+
|
28
|
+
temp_file.close
|
29
|
+
|
30
|
+
if changes
|
31
|
+
FileUtils.mv(temp_file.path, path.to_s)
|
32
|
+
possession.update(file)
|
33
|
+
end
|
34
|
+
|
35
|
+
temp_file.unlink
|
36
|
+
|
37
|
+
changes
|
38
|
+
end
|
39
|
+
|
40
|
+
def rename(from, to)
|
41
|
+
new_filename = replacement(path.filename, from, to)
|
42
|
+
built_path = path.build(new_filename)
|
43
|
+
|
44
|
+
File.rename(path.to_s, built_path.to_s)
|
45
|
+
@path = built_path
|
46
|
+
|
47
|
+
true
|
48
|
+
rescue StringReplacer::NoMatchError
|
49
|
+
false
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def replacement(text, from, to)
|
55
|
+
replacer = StringReplacer.new(text)
|
56
|
+
replacer.replace(from).with(to)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RenameGem
|
4
|
+
module Renamer
|
5
|
+
require 'pathname'
|
6
|
+
|
7
|
+
class Path
|
8
|
+
attr_reader :path, :pwd
|
9
|
+
|
10
|
+
def initialize(path, pwd)
|
11
|
+
@path = Pathname.new(path)
|
12
|
+
@pwd = Pathname.new(pwd)
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_s
|
16
|
+
absolute_path.to_s
|
17
|
+
end
|
18
|
+
|
19
|
+
def filename
|
20
|
+
absolute_path.basename.to_s
|
21
|
+
end
|
22
|
+
|
23
|
+
def build(new_path)
|
24
|
+
pathname = path.dirname.join(new_path)
|
25
|
+
self.class.new(pathname, pwd)
|
26
|
+
end
|
27
|
+
|
28
|
+
def directories
|
29
|
+
absolute_path.children.select(&:directory?).sort.map do |pathname|
|
30
|
+
self.class.new(relative_path(pathname), pwd)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def files
|
35
|
+
absolute_path.children.select(&:file?).sort.map do |pathname|
|
36
|
+
self.class.new(relative_path(pathname), pwd)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def file?
|
41
|
+
absolute_path.file?
|
42
|
+
end
|
43
|
+
|
44
|
+
def directory?
|
45
|
+
absolute_path.directory?
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def absolute_path
|
51
|
+
pwd.join(path)
|
52
|
+
end
|
53
|
+
|
54
|
+
def relative_path(pathname)
|
55
|
+
pathname.relative_path_from(pwd)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RenameGem
|
4
|
+
module Renamer
|
5
|
+
class Possession
|
6
|
+
attr_reader :mode, :uid, :gid
|
7
|
+
|
8
|
+
def initialize(file)
|
9
|
+
@mode = file.stat.mode
|
10
|
+
@uid = file.stat.uid
|
11
|
+
@gid = file.stat.gid
|
12
|
+
end
|
13
|
+
|
14
|
+
def update(file)
|
15
|
+
file.chmod(mode)
|
16
|
+
file.chown(uid, gid)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RenameGem
|
4
|
+
module Renamer
|
5
|
+
class Reporter
|
6
|
+
attr_reader :results
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@results = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def <<(list)
|
13
|
+
results << list
|
14
|
+
end
|
15
|
+
|
16
|
+
def print
|
17
|
+
results.empty? ? puts('No results, nothing edited or renamed') : puts(results.flatten)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RenameGem
|
4
|
+
module Renamer
|
5
|
+
class Runner
|
6
|
+
EXCLUDED_DIRS = ['.git'].freeze
|
7
|
+
|
8
|
+
attr_reader :options, :reporter, :context
|
9
|
+
|
10
|
+
def initialize(options)
|
11
|
+
@options = options
|
12
|
+
@reporter = Reporter.new
|
13
|
+
@context = Context.new(Dir.pwd, options[:path], options[:from], options[:to])
|
14
|
+
end
|
15
|
+
|
16
|
+
def run
|
17
|
+
directory = DirectoryHandler.new(context)
|
18
|
+
recurse!(directory)
|
19
|
+
|
20
|
+
reporter.print
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def recurse!(directory)
|
26
|
+
directory.change_files unless excluded_directory?(directory.path)
|
27
|
+
|
28
|
+
directory.directories.each do |sub_directory|
|
29
|
+
recurse!(sub_directory)
|
30
|
+
|
31
|
+
sub_directory.rename unless excluded_directory?(sub_directory.path)
|
32
|
+
end
|
33
|
+
|
34
|
+
reporter << directory.results
|
35
|
+
end
|
36
|
+
|
37
|
+
def excluded_directory?(path)
|
38
|
+
EXCLUDED_DIRS.include? path.filename
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RenameGem
|
4
|
+
module Renamer
|
5
|
+
class StringReplacer
|
6
|
+
ChainError = Class.new(StandardError)
|
7
|
+
NoMatchError = Class.new(StandardError)
|
8
|
+
|
9
|
+
attr_reader :content, :target
|
10
|
+
|
11
|
+
def initialize(content)
|
12
|
+
@content = content
|
13
|
+
end
|
14
|
+
|
15
|
+
def replace(target)
|
16
|
+
@target = target
|
17
|
+
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
def with(replacement)
|
22
|
+
raise ChainError, "Usage: replacer.replace('x').with('y')" if target.nil? || replacement.nil?
|
23
|
+
raise NoMatchError, "#{target} not found in #{content}" unless exists?(target)
|
24
|
+
|
25
|
+
content.gsub(target, replacement).gsub(pascal_case(target), pascal_case(replacement))
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def exists?(target)
|
31
|
+
content.include?(target) || content.include?(pascal_case(target))
|
32
|
+
end
|
33
|
+
|
34
|
+
def pascal_case(snake_case)
|
35
|
+
snake_case.split('_').map(&:capitalize).join
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rename_gem
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rónán Duddy
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-12-
|
11
|
+
date: 2020-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -188,14 +188,28 @@ dependencies:
|
|
188
188
|
version: '1.9'
|
189
189
|
description: Have a hard time renaming Gems, find it annoying? Hopefully this should
|
190
190
|
help!
|
191
|
-
email:
|
191
|
+
email:
|
192
|
+
- dev@ronanduddy.xyz
|
192
193
|
executables:
|
193
194
|
- rename_gem
|
194
195
|
extensions: []
|
195
|
-
extra_rdoc_files:
|
196
|
+
extra_rdoc_files:
|
197
|
+
- README.md
|
196
198
|
files:
|
197
|
-
-
|
199
|
+
- README.md
|
200
|
+
- exe/rename_gem
|
198
201
|
- lib/rename_gem.rb
|
202
|
+
- lib/rename_gem/cli.rb
|
203
|
+
- lib/rename_gem/renamer.rb
|
204
|
+
- lib/rename_gem/renamer/context.rb
|
205
|
+
- lib/rename_gem/renamer/directory_handler.rb
|
206
|
+
- lib/rename_gem/renamer/file_handler.rb
|
207
|
+
- lib/rename_gem/renamer/path.rb
|
208
|
+
- lib/rename_gem/renamer/possession.rb
|
209
|
+
- lib/rename_gem/renamer/reporter.rb
|
210
|
+
- lib/rename_gem/renamer/runner.rb
|
211
|
+
- lib/rename_gem/renamer/string_replacer.rb
|
212
|
+
- lib/rename_gem/version.rb
|
199
213
|
homepage: https://github.com/ronanduddy/Ruby-Gem-Renamer
|
200
214
|
licenses:
|
201
215
|
- MIT
|