rename_gem 0.0.1 → 0.1.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: 8d32a4b2404be84102921721d3e4cbdcddbaa7817cdc30b927ca5dfcbffb3075
4
- data.tar.gz: '049827818ba260ed7e8ff18b235578b60fb8edb08ba7c5f2a9fdd4bb0f504d4e'
3
+ metadata.gz: d5dc142f3588040cd30fc17ace93061f6564834d7c6b00db6d600d4f7d9ab2a6
4
+ data.tar.gz: ff6248367664d2fbe2ee4134bc3d980776d1267008defc86cd997a4fcccc2e00
5
5
  SHA512:
6
- metadata.gz: 393e533dc5b3c1dfb1af538ad2429e39997e25b61e0043904bf89fb4fd76e0d57c221a5c1b8e7065dd41df73b5dc3345d8ae6eedcb78cd60a189f50bd80e7f88
7
- data.tar.gz: 896c084efa30bcbb80000d3452843519593d9e4045fd68a6bcfcdec1f7278f6867a1b6f71e8181f4a0ed7425898b40317b77021e8f473a0ecbb7aaec7df26ee2
6
+ metadata.gz: bdfa2e938d229eaa87365ce1ba9c06fc91ab060f9ef80f058165d3d8da4aae12211a6a7c1118735f41739286dd847b4bc8b19fb67b5e9e4e02033c3b90c6db20
7
+ data.tar.gz: 57bce7a0effb78b67452bcb00225903667ec4bc3fa4a52d6a2f4c933a5b949c8f971a858739d4387fac4a3652c9fb621fa2da2f6d09af23d7d3b56225e16bd90
@@ -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,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,65 @@
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
+ @pathname = Pathname.new(new_path)
48
+ end
49
+
50
+ def file?
51
+ @pathname.file?
52
+ end
53
+
54
+ def directory?
55
+ @pathname.directory?
56
+ end
57
+
58
+ private
59
+
60
+ def dirname
61
+ @pathname.dirname
62
+ end
63
+ end
64
+ end
65
+ 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.1.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.1
4
+ version: 0.1.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-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -188,14 +188,29 @@ 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
198
- homepage: https://github.com/ronanduddy/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
213
+ homepage: https://github.com/ronanduddy/Ruby-Gem-Renamer
199
214
  licenses:
200
215
  - MIT
201
216
  metadata: {}
@@ -214,7 +229,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
214
229
  - !ruby/object:Gem::Version
215
230
  version: '0'
216
231
  requirements: []
217
- rubygems_version: 3.2.0.rc.1
232
+ rubygems_version: 3.1.4
218
233
  signing_key:
219
234
  specification_version: 4
220
235
  summary: A gem that will renames Gems for you