rename_gem 0.0.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cb56e690b1f73635ebedd262913cbcc537ed65002444943f037db4bdbf853b93
4
- data.tar.gz: db1dea49877e57cb3b84c2a604a036a535c97a2d9901366e2486172addb12484
3
+ metadata.gz: d94c4504bc60d9df94db95f491c1c305504b62ccdbe81b855d7e5239423a41a8
4
+ data.tar.gz: 2aaaab3e85bb4fd5be9a83d91dced99f44f2b2deb418ad4bb87944c3ca5f8efe
5
5
  SHA512:
6
- metadata.gz: 7d4bba51b8f0b8ba519ab8c7ea340e072531d7df11b12246990f774de4632fb8ccd9147a74798fc882cb7b1bd34c1dfadc5ad42aca2b216f11131e4e267a6c28
7
- data.tar.gz: aa4df0369ff656cfacf26153c51d800077843dfe98d27332201df0814b6b93bf7c5472c88893b621df198f39c2f637570b180a61c1ff60288923d022b085466d
6
+ metadata.gz: 31827b02f7039de9904e09b0414956184e5854a36e194354478fb14eba3701cc1da6a2efd812270b5533e1cc9c1f7171a22e84c435c7a396daf92db52dad26f2
7
+ data.tar.gz: b3066e038a12fcf962418c29b43c4960aac1d37342bfdfd57433f7c2e78910788c625ec60d998eaf998fdc0fc78256cd017265636a9df317f29cca3be70f3419
@@ -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,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rename_gem/renamer'
4
+ require 'rename_gem/cli'
5
+
6
+ # Base/top module
7
+ module RenameGem
8
+ end
@@ -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,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+ require 'rename_gem/renamer/directory_handler'
5
+ require 'rename_gem/renamer/entity'
6
+ require 'rename_gem/renamer/file_handler'
7
+ require 'rename_gem/renamer/modifier'
8
+ require 'rename_gem/renamer/path'
9
+ require 'rename_gem/renamer/possession'
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
+ path = Pathname.new(Dir.pwd).join(options[:path]).to_s
17
+ entity = Entity.new(path, nil)
18
+ runner = RenameGem::Renamer::Runner.new(options[:from], options[:to])
19
+ runner.run(entity)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RenameGem
4
+ module Renamer
5
+ class DirectoryHandler
6
+ EXCLUDED_DIRS = ['.git'].freeze
7
+
8
+ attr_reader :name, :new_name
9
+
10
+ def initialize(name, new_name)
11
+ @name = name
12
+ @new_name = new_name
13
+ end
14
+
15
+ def recurse!(directory)
16
+ directory.files.each { |file| file.change(name).to(new_name) }
17
+
18
+ directory.directories.each do |sub_directory|
19
+ sub_directory.change(name).to(new_name)
20
+
21
+ recurse!(sub_directory) unless excluded_directory?(sub_directory)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def excluded_directory?(dir)
28
+ EXCLUDED_DIRS.include? dir.path.filename
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,60 @@
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
+ file_handler.change(modifier) if path.file?
28
+ rename
29
+ end
30
+
31
+ def directories
32
+ path.directories.map do |directory_path|
33
+ self.class.new(directory_path.to_s, nil)
34
+ end
35
+ end
36
+
37
+ def files
38
+ path.files.map do |file_path|
39
+ self.class.new(file_path.to_s, FileHandler.new(file_path))
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def rename
46
+ new_path = path.build(modifier.replacement(path.filename)).to_s
47
+
48
+ path.rename(new_path)
49
+
50
+ modifier = Modifier.new
51
+ rescue Modifier::ReplacementNotFound => e
52
+ # nothing
53
+ end
54
+
55
+ def validate_chaining
56
+ raise ChainError, "Usage: object.change('x').to('y')" unless modifier.valid?
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,39 @@
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, :changes
9
+
10
+ def initialize(path)
11
+ @path = path
12
+ @file = File.new(path.to_s)
13
+ @possession = Possession.new(file)
14
+ @changes = false
15
+ end
16
+
17
+ def change(modifier)
18
+ temp_file = Tempfile.new(path.filename)
19
+
20
+ file.each_line do |line|
21
+ temp_file.puts modifier.replacement(line)
22
+ @changes = true
23
+ rescue Modifier::ReplacementNotFound
24
+ temp_file.puts line
25
+ end
26
+
27
+ temp_file.close
28
+
29
+ if @changes
30
+ FileUtils.mv(temp_file.path, path.to_s)
31
+ possession.update(file)
32
+ puts "Edit #{path}"
33
+ end
34
+
35
+ temp_file.unlink
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,30 @@
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
+
10
+ def initialize(from = nil, to = nil)
11
+ @from = from
12
+ @to = to
13
+ end
14
+
15
+ def valid?
16
+ return false if from.nil? || to.nil?
17
+
18
+ true
19
+ end
20
+
21
+ def replacement(string)
22
+ replacer = StringReplacer.new(string)
23
+
24
+ replacer.replace(from).with(to)
25
+ rescue StringReplacer::NoMatchError => e
26
+ raise ReplacementNotFound, e.message
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RenameGem
4
+ module Renamer
5
+ require 'pathname'
6
+
7
+ class Path
8
+ RenameError = Class.new(StandardError)
9
+
10
+ def initialize(path)
11
+ @pathname = Pathname.new(path)
12
+ end
13
+
14
+ def to_s
15
+ @pathname.to_s
16
+ end
17
+
18
+ def filename
19
+ @pathname.basename.to_s
20
+ end
21
+
22
+ def build(new_path)
23
+ self.class.new(dirname.join(new_path))
24
+ end
25
+
26
+ def directories
27
+ @pathname.children.select(&:directory?).map do |pathname|
28
+ self.class.new(pathname.to_s)
29
+ end
30
+ end
31
+
32
+ def files
33
+ @pathname.children.select(&:file?).map do |pathname|
34
+ self.class.new(pathname.to_s)
35
+ end
36
+ end
37
+
38
+ def rename(new_path)
39
+ if file?
40
+ File.rename(to_s, new_path)
41
+ elsif directory?
42
+ FileUtils.mv(to_s, new_path)
43
+ else
44
+ raise RenameError, "#{self} must be a file or a directory"
45
+ end
46
+
47
+ puts "Rename #{self} -> #{self.class.new(new_path).filename}"
48
+ @pathname = Pathname.new(new_path)
49
+ end
50
+
51
+ def file?
52
+ @pathname.file?
53
+ end
54
+
55
+ def directory?
56
+ @pathname.directory?
57
+ end
58
+
59
+ private
60
+
61
+ def dirname
62
+ @pathname.dirname
63
+ end
64
+ end
65
+ end
66
+ 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.2.0'
5
+ 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.0.2
4
+ version: 0.2.0
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
- date: 2020-12-10 00:00:00.000000000 Z
11
+ date: 2020-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -188,13 +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
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
198
213
  homepage: https://github.com/ronanduddy/Ruby-Gem-Renamer
199
214
  licenses:
200
215
  - MIT