rename_gem 0.0.4 → 0.0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a21c5895ebd74b4eef18e8b97c4afba25be91187de1efda55b4a90fd9f1b8071
4
- data.tar.gz: 43a4b99202463cbc76e5ff24169a41953c8c29630e09a01e4d7ff6f483e2a9bf
3
+ metadata.gz: 0d8afa9f2c90d25b3b98a7583336250643d30f4850dc2f851e330b94bfe1ed40
4
+ data.tar.gz: f5058a8d2c17a5664823669744382061852c8b3b43e82869d60a47a4733a48db
5
5
  SHA512:
6
- metadata.gz: '01328de7d3bd8854c309c2141fa5639e10d874c85a161ef94abf6d201dc90195d0d79f6be5ccc0f522be969ba796c3a3429300f4c9b60ff95e860b80454b1e5a'
7
- data.tar.gz: dc803ed935dcd48eed74264674a2904311f017cf7ef28a69d67f5782875871ac858be9aa759335e113dbd71fe97106d7000ec217ed30178f47c06918376648f9
6
+ metadata.gz: 3d43b5cc99b4d23e6c170e477c17667ab1aa3da442991f919ad0e7218194a7bf504243fb508a28c30726596f76eae96ba1febe7b83f6fc192b8bccfe1fb5589d
7
+ data.tar.gz: f52eacf8f1b0565bd732e712c594d56f944214f83845e2878b32d7729ea6b18a5890407f450c075eb8443a96a36df14b79e2098d2b948d3b9c2af8cdca44a917
@@ -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
+ [![Build Status](https://travis-ci.org/ronanduddy/Ruby-Gem-Renamer.svg?branch=master)](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 hello_world -t foo_bar -p spec/fixtures/
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
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,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rename_gem/renamer/runner'
4
+ require 'rename_gem/renamer/entity'
5
+ require 'rename_gem/renamer/path'
6
+ require 'rename_gem/renamer/file_handler'
7
+ require 'rename_gem/renamer/directory_handler'
8
+ require 'rename_gem/renamer/modifier'
9
+ require 'rename_gem/renamer/possession'
10
+ require 'rename_gem/renamer/string_replacer'
11
+
12
+ module RenameGem
13
+ module Renamer
14
+ def self.run(options)
15
+ path = Pathname.new(Dir.pwd).join(options[:path]).to_s
16
+ entity = Entity.new(path, nil)
17
+ runner = RenameGem::Renamer::Runner.new(options[:from], options[:to])
18
+ runner.run(entity)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RenameGem
4
+ module Renamer
5
+ class DirectoryHandler
6
+ attr_reader :name, :new_name
7
+
8
+ def initialize(name, new_name)
9
+ @name = name
10
+ @new_name = new_name
11
+ end
12
+
13
+ def recurse!(directory)
14
+ directory.files.each { |file| file.change(name).to(new_name) }
15
+
16
+ directory.directories.each do |sub_directory|
17
+ sub_directory.change(name).to(new_name)
18
+
19
+ recurse!(sub_directory)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RenameGem
4
+ module Renamer
5
+ class Entity
6
+ ChainError = Class.new(StandardError)
7
+
8
+ attr_reader :path, :file_handler, :modifier
9
+
10
+ def initialize(path, file_handler)
11
+ @path = Path.new(path)
12
+ @file_handler = file_handler
13
+ @modifier = Modifier.new
14
+ end
15
+
16
+ def change(from)
17
+ modifier.from = from
18
+
19
+ self
20
+ end
21
+
22
+ def to(to)
23
+ modifier.to = to
24
+
25
+ validate_chaining
26
+
27
+ rename
28
+ end
29
+
30
+ def directories
31
+ path.directories.map do |directory_path|
32
+ self.class.new(directory_path.to_s, nil)
33
+ end
34
+ end
35
+
36
+ def files
37
+ path.files.map do |file_path|
38
+ self.class.new(file_path.to_s, FileHandler.new(file_path))
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def rename
45
+ new_path = path.build(modifier.replacement(path.filename)).to_s
46
+
47
+ unless path.to_s == new_path
48
+ file_handler.change(modifier) if path.file?
49
+
50
+ path.rename(new_path)
51
+ puts "rename #{path} to #{new_path}"
52
+
53
+ modifier = Modifier.new
54
+ end
55
+ rescue Modifier::ReplacementNotFound => e
56
+ puts "ignoring #{e.message}"
57
+ end
58
+
59
+ def validate_chaining
60
+ raise ChainError, "Usage: object.change('x').to('y')" unless modifier.valid?
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,35 @@
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 change(modifier)
17
+ temp_file = Tempfile.new(path.filename)
18
+
19
+ file.each_line do |line|
20
+ temp_file.puts modifier.replacement(line)
21
+ rescue Modifier::ReplacementNotFound
22
+ temp_file.puts line
23
+ end
24
+
25
+ temp_file.close
26
+ FileUtils.mv(temp_file.path, path.to_s) if modifier.times_replaced.positive?
27
+ temp_file.unlink
28
+
29
+ possession.update(file)
30
+
31
+ puts "#{modifier.times_replaced} lines changed in #{path}"
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RenameGem
4
+ module Renamer
5
+ class Modifier
6
+ ReplacementNotFound = Class.new(StandardError)
7
+
8
+ attr_accessor :from, :to
9
+ attr_reader :times_replaced
10
+
11
+ def initialize(from = nil, to = nil)
12
+ @from = from
13
+ @to = to
14
+ @times_replaced = 0
15
+ end
16
+
17
+ def valid?
18
+ return false if from.nil? || to.nil?
19
+
20
+ true
21
+ end
22
+
23
+ def replacement(string)
24
+ replacer = StringReplacer.new(string)
25
+ content = replacer.replace(from).with(to)
26
+ @times_replaced += 1
27
+
28
+ content
29
+ rescue StringReplacer::NoMatchError => e
30
+ raise ReplacementNotFound, e.message
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RenameGem
4
+ module Renamer
5
+ class Path
6
+ RenameError = Class.new(StandardError)
7
+
8
+ def initialize(path)
9
+ @pathname = Pathname.new(path)
10
+ end
11
+
12
+ def to_s
13
+ @pathname.to_s
14
+ end
15
+
16
+ def filename
17
+ @pathname.basename.to_s
18
+ end
19
+
20
+ def build(new_path)
21
+ self.class.new(dirname.join(new_path))
22
+ end
23
+
24
+ def directories
25
+ @pathname.children.select(&:directory?).map do |pathname|
26
+ self.class.new(pathname.to_s)
27
+ end
28
+ end
29
+
30
+ def files
31
+ @pathname.children.select(&:file?).map do |pathname|
32
+ self.class.new(pathname.to_s)
33
+ end
34
+ end
35
+
36
+ def rename(new_path)
37
+ if file?
38
+ File.rename(to_s, new_path)
39
+ elsif directory?
40
+ FileUtils.mv(to_s, new_path)
41
+ else
42
+ raise RenameError, "#{self} must be a file or a directory"
43
+ end
44
+
45
+ @pathname = Pathname.new(new_path)
46
+ end
47
+
48
+ def file?
49
+ @pathname.file?
50
+ end
51
+
52
+ def directory?
53
+ @pathname.directory?
54
+ end
55
+
56
+ private
57
+
58
+ def dirname
59
+ @pathname.dirname
60
+ end
61
+ end
62
+ end
63
+ 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,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RenameGem
4
+ module Renamer
5
+ class Runner
6
+ attr_reader :name, :new_name
7
+
8
+ def initialize(name, new_name)
9
+ @name = name
10
+ @new_name = new_name
11
+ end
12
+
13
+ def run(entity)
14
+ if entity.path.file?
15
+ file = Entity.new(entity.to_s, FileHandler.new(entity.path))
16
+ file.change(name).to(new_name)
17
+ return
18
+ end
19
+
20
+ DirectoryHandler.new(name, new_name).recurse!(entity)
21
+ end
22
+ end
23
+ end
24
+ 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
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RenameGem
4
+ VERSION = '0.0.5'
5
+ end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rename_gem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rónán Duddy
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
11
  date: 2020-12-11 00:00:00.000000000 Z
12
12
  dependencies:
@@ -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
- - bin/rename_gem
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/directory_handler.rb
205
+ - lib/rename_gem/renamer/entity.rb
206
+ - lib/rename_gem/renamer/file_handler.rb
207
+ - lib/rename_gem/renamer/modifier.rb
208
+ - lib/rename_gem/renamer/path.rb
209
+ - lib/rename_gem/renamer/possession.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