rename_gem 0.0.5 → 0.3.3

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: 0d8afa9f2c90d25b3b98a7583336250643d30f4850dc2f851e330b94bfe1ed40
4
- data.tar.gz: f5058a8d2c17a5664823669744382061852c8b3b43e82869d60a47a4733a48db
3
+ metadata.gz: 784509e546ac37bc2ccbe06f438a4ccd4a0f478fe67da744da3d8363f6febb9d
4
+ data.tar.gz: 3c08e461b2148fc055219e532ce266ac74838a122c54ff89a0227ae72d0f2a92
5
5
  SHA512:
6
- metadata.gz: 3d43b5cc99b4d23e6c170e477c17667ab1aa3da442991f919ad0e7218194a7bf504243fb508a28c30726596f76eae96ba1febe7b83f6fc192b8bccfe1fb5589d
7
- data.tar.gz: f52eacf8f1b0565bd732e712c594d56f944214f83845e2878b32d7729ea6b18a5890407f450c075eb8443a96a36df14b79e2098d2b948d3b9c2af8cdca44a917
6
+ metadata.gz: d3ed3a39edd89f0197740c2ad91dbe708e8626f168dfae9b358b7e6a8c2370ea9e977161cf897b8297912d9e041ab1ce2b44f5733322b5b46f8cda5302887464
7
+ data.tar.gz: 61d85c2bc49492948e16027059c2c51c38087ce232582c1ad89fbde121c60beec3ac5161471ff180def64666e1dfcfb4c21ae533249933ebfcaad70bcd79186d
data/README.md CHANGED
@@ -27,7 +27,7 @@ gem install rename_gem
27
27
  ## Usage
28
28
 
29
29
  ```Shell
30
- bundle exec rename_gem rename -f hello_world -t foo_bar -p spec/fixtures/
30
+ bundle exec rename_gem rename -f project_name -t foo_bar -p project
31
31
  ```
32
32
 
33
33
  ## Development
@@ -1,21 +1,19 @@
1
1
  # frozen_string_literal: true
2
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'
3
+ require 'pathname'
4
+ require 'rename_gem/renamer/context'
7
5
  require 'rename_gem/renamer/directory_handler'
8
- require 'rename_gem/renamer/modifier'
6
+ require 'rename_gem/renamer/file_handler'
7
+ require 'rename_gem/renamer/path'
9
8
  require 'rename_gem/renamer/possession'
9
+ require 'rename_gem/renamer/reporter'
10
+ require 'rename_gem/renamer/runner'
10
11
  require 'rename_gem/renamer/string_replacer'
11
12
 
12
13
  module RenameGem
13
14
  module Renamer
14
15
  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)
16
+ Runner.new(options).run
19
17
  end
20
18
  end
21
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
@@ -3,22 +3,48 @@
3
3
  module RenameGem
4
4
  module Renamer
5
5
  class DirectoryHandler
6
- attr_reader :name, :new_name
6
+ attr_reader :context, :path, :results
7
7
 
8
- def initialize(name, new_name)
9
- @name = name
10
- @new_name = new_name
8
+ def initialize(context)
9
+ @context = context
10
+ @path = Path.new(context.path, context.pwd)
11
+ @results = []
11
12
  end
12
13
 
13
- def recurse!(directory)
14
- directory.files.each { |file| file.change(name).to(new_name) }
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))
15
25
 
16
- directory.directories.each do |sub_directory|
17
- sub_directory.change(name).to(new_name)
26
+ FileUtils.mv(path.to_s, built_path.to_s)
18
27
 
19
- recurse!(sub_directory)
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))
20
39
  end
21
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
22
48
  end
23
49
  end
24
50
  end
@@ -13,22 +13,47 @@ module RenameGem
13
13
  @possession = Possession.new(file)
14
14
  end
15
15
 
16
- def change(modifier)
16
+ def edit(from, to)
17
17
  temp_file = Tempfile.new(path.filename)
18
+ changes = false
18
19
 
19
20
  file.each_line do |line|
20
- temp_file.puts modifier.replacement(line)
21
- rescue Modifier::ReplacementNotFound
21
+ temp_file.puts replacement(line, from, to)
22
+
23
+ changes = true
24
+ rescue StringReplacer::NoMatchError
22
25
  temp_file.puts line
23
26
  end
24
27
 
25
28
  temp_file.close
26
- FileUtils.mv(temp_file.path, path.to_s) if modifier.times_replaced.positive?
29
+
30
+ if changes
31
+ FileUtils.mv(temp_file.path, path.to_s)
32
+ possession.update(file)
33
+ end
34
+
27
35
  temp_file.unlink
28
36
 
29
- possession.update(file)
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
30
53
 
31
- puts "#{modifier.times_replaced} lines changed in #{path}"
54
+ def replacement(text, from, to)
55
+ replacer = StringReplacer.new(text)
56
+ replacer.replace(from).with(to)
32
57
  end
33
58
  end
34
59
  end
@@ -2,61 +2,57 @@
2
2
 
3
3
  module RenameGem
4
4
  module Renamer
5
+ require 'pathname'
6
+
5
7
  class Path
6
- RenameError = Class.new(StandardError)
8
+ attr_reader :path, :pwd
7
9
 
8
- def initialize(path)
9
- @pathname = Pathname.new(path)
10
+ def initialize(path, pwd)
11
+ @path = Pathname.new(path)
12
+ @pwd = Pathname.new(pwd)
10
13
  end
11
14
 
12
15
  def to_s
13
- @pathname.to_s
16
+ absolute_path.to_s
14
17
  end
15
18
 
16
19
  def filename
17
- @pathname.basename.to_s
20
+ absolute_path.basename.to_s
18
21
  end
19
22
 
20
23
  def build(new_path)
21
- self.class.new(dirname.join(new_path))
24
+ pathname = path.dirname.join(new_path)
25
+ self.class.new(pathname, pwd)
22
26
  end
23
27
 
24
28
  def directories
25
- @pathname.children.select(&:directory?).map do |pathname|
26
- self.class.new(pathname.to_s)
29
+ absolute_path.children.select(&:directory?).sort.map do |pathname|
30
+ self.class.new(relative_path(pathname), pwd)
27
31
  end
28
32
  end
29
33
 
30
34
  def files
31
- @pathname.children.select(&:file?).map do |pathname|
32
- self.class.new(pathname.to_s)
35
+ absolute_path.children.select(&:file?).sort.map do |pathname|
36
+ self.class.new(relative_path(pathname), pwd)
33
37
  end
34
38
  end
35
39
 
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
40
  def file?
49
- @pathname.file?
41
+ absolute_path.file?
50
42
  end
51
43
 
52
44
  def directory?
53
- @pathname.directory?
45
+ absolute_path.directory?
54
46
  end
55
47
 
56
48
  private
57
49
 
58
- def dirname
59
- @pathname.dirname
50
+ def absolute_path
51
+ pwd.join(path)
52
+ end
53
+
54
+ def relative_path(pathname)
55
+ pathname.relative_path_from(pwd)
60
56
  end
61
57
  end
62
58
  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
@@ -3,21 +3,39 @@
3
3
  module RenameGem
4
4
  module Renamer
5
5
  class Runner
6
- attr_reader :name, :new_name
6
+ EXCLUDED_DIRS = ['.git'].freeze
7
7
 
8
- def initialize(name, new_name)
9
- @name = name
10
- @new_name = new_name
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])
11
14
  end
12
15
 
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
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)
18
32
  end
19
33
 
20
- DirectoryHandler.new(name, new_name).recurse!(entity)
34
+ reporter << directory.results
35
+ end
36
+
37
+ def excluded_directory?(path)
38
+ EXCLUDED_DIRS.include? path.filename
21
39
  end
22
40
  end
23
41
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RenameGem
4
- VERSION = '0.0.5'
4
+ VERSION = '0.3.3'
5
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.5
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rónán Duddy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-11 00:00:00.000000000 Z
11
+ date: 2020-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -201,12 +201,12 @@ files:
201
201
  - lib/rename_gem.rb
202
202
  - lib/rename_gem/cli.rb
203
203
  - lib/rename_gem/renamer.rb
204
+ - lib/rename_gem/renamer/context.rb
204
205
  - lib/rename_gem/renamer/directory_handler.rb
205
- - lib/rename_gem/renamer/entity.rb
206
206
  - lib/rename_gem/renamer/file_handler.rb
207
- - lib/rename_gem/renamer/modifier.rb
208
207
  - lib/rename_gem/renamer/path.rb
209
208
  - lib/rename_gem/renamer/possession.rb
209
+ - lib/rename_gem/renamer/reporter.rb
210
210
  - lib/rename_gem/renamer/runner.rb
211
211
  - lib/rename_gem/renamer/string_replacer.rb
212
212
  - lib/rename_gem/version.rb
@@ -1,64 +0,0 @@
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
@@ -1,34 +0,0 @@
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