rename_gem 0.2.0 → 0.3.0

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: d94c4504bc60d9df94db95f491c1c305504b62ccdbe81b855d7e5239423a41a8
4
- data.tar.gz: 2aaaab3e85bb4fd5be9a83d91dced99f44f2b2deb418ad4bb87944c3ca5f8efe
3
+ metadata.gz: 52e2fc8f66748e62b6e682fff38a23128b5f1f6c1dadd6362a86dd43919e6051
4
+ data.tar.gz: 5f25ef4d29d1a2e1e9b9fa48143305a19e6acb814495f105a78360d97d2708fa
5
5
  SHA512:
6
- metadata.gz: 31827b02f7039de9904e09b0414956184e5854a36e194354478fb14eba3701cc1da6a2efd812270b5533e1cc9c1f7171a22e84c435c7a396daf92db52dad26f2
7
- data.tar.gz: b3066e038a12fcf962418c29b43c4960aac1d37342bfdfd57433f7c2e78910788c625ec60d998eaf998fdc0fc78256cd017265636a9df317f29cca3be70f3419
6
+ metadata.gz: 6f781e51903be6a84a600d4c3bf375e3a2c6f2c06e4ddb1c150d5a979239fe4bfc88258dd99ec3a70fff835bdc7c6d5e2ab04d59b90fdc09bf23658e0b0ba150
7
+ data.tar.gz: e13ef6891d07982a026b2c6e6d179ef21d4b55ed7ae00ec7a9b93c048e5b0780481a92ea897057d333db280a09f6fa0e1fe48b31fea5d1b2caec44b831abb33d
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,22 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'pathname'
4
+ require 'rename_gem/renamer/context'
4
5
  require 'rename_gem/renamer/directory_handler'
5
- require 'rename_gem/renamer/entity'
6
6
  require 'rename_gem/renamer/file_handler'
7
- require 'rename_gem/renamer/modifier'
8
7
  require 'rename_gem/renamer/path'
9
8
  require 'rename_gem/renamer/possession'
9
+ require 'rename_gem/renamer/reporter'
10
10
  require 'rename_gem/renamer/runner'
11
11
  require 'rename_gem/renamer/string_replacer'
12
12
 
13
13
  module RenameGem
14
14
  module Renamer
15
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)
16
+ Runner.new(options).run
20
17
  end
21
18
  end
22
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,29 +3,47 @@
3
3
  module RenameGem
4
4
  module Renamer
5
5
  class DirectoryHandler
6
- EXCLUDED_DIRS = ['.git'].freeze
6
+ attr_reader :context, :path, :results
7
7
 
8
- attr_reader :name, :new_name
8
+ def initialize(context)
9
+ @context = context
10
+ @path = Path.new(context.path, context.pwd)
11
+ @results = []
12
+ end
9
13
 
10
- def initialize(name, new_name)
11
- @name = name
12
- @new_name = 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
13
21
  end
14
22
 
15
- def recurse!(directory)
16
- directory.files.each { |file| file.change(name).to(new_name) }
23
+ def rename
24
+ built_path = path.build(replacement(path.filename))
25
+
26
+ FileUtils.mv(path.to_s, built_path.to_s)
17
27
 
18
- directory.directories.each do |sub_directory|
19
- sub_directory.change(name).to(new_name)
28
+ results << "Rename #{path.path} -> #{built_path.filename}"
29
+ @path = built_path
30
+
31
+ true
32
+ rescue StringReplacer::NoMatchError
33
+ false
34
+ end
20
35
 
21
- recurse!(sub_directory) unless excluded_directory?(sub_directory)
36
+ def directories
37
+ path.directories.map do |directory_path|
38
+ self.class.new(context.using(directory_path.path))
22
39
  end
23
40
  end
24
41
 
25
42
  private
26
43
 
27
- def excluded_directory?(dir)
28
- EXCLUDED_DIRS.include? dir.path.filename
44
+ def replacement(text)
45
+ replacer = StringReplacer.new(text)
46
+ replacer.replace(context.from).with(context.to)
29
47
  end
30
48
  end
31
49
  end
@@ -5,34 +5,55 @@ module RenameGem
5
5
  require 'tempfile'
6
6
 
7
7
  class FileHandler
8
- attr_reader :path, :file, :possession, :changes
8
+ attr_reader :path, :file, :possession
9
9
 
10
10
  def initialize(path)
11
11
  @path = path
12
12
  @file = File.new(path.to_s)
13
13
  @possession = Possession.new(file)
14
- @changes = false
15
14
  end
16
15
 
17
- def change(modifier)
16
+ def edit(from, to)
18
17
  temp_file = Tempfile.new(path.filename)
18
+ changes = false
19
19
 
20
20
  file.each_line do |line|
21
- temp_file.puts modifier.replacement(line)
22
- @changes = true
23
- rescue Modifier::ReplacementNotFound
21
+ temp_file.puts replacement(line, from, to)
22
+
23
+ changes = true
24
+ rescue StringReplacer::NoMatchError
24
25
  temp_file.puts line
25
26
  end
26
27
 
27
28
  temp_file.close
28
29
 
29
- if @changes
30
+ if changes
30
31
  FileUtils.mv(temp_file.path, path.to_s)
31
32
  possession.update(file)
32
- puts "Edit #{path}"
33
33
  end
34
34
 
35
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)
36
57
  end
37
58
  end
38
59
  end
@@ -5,61 +5,54 @@ module RenameGem
5
5
  require 'pathname'
6
6
 
7
7
  class Path
8
- RenameError = Class.new(StandardError)
8
+ attr_reader :path, :pwd
9
9
 
10
- def initialize(path)
11
- @pathname = Pathname.new(path)
10
+ def initialize(path, pwd)
11
+ @path = Pathname.new(path)
12
+ @pwd = Pathname.new(pwd)
12
13
  end
13
14
 
14
15
  def to_s
15
- @pathname.to_s
16
+ absolute_path.to_s
16
17
  end
17
18
 
18
19
  def filename
19
- @pathname.basename.to_s
20
+ absolute_path.basename.to_s
20
21
  end
21
22
 
22
23
  def build(new_path)
23
- self.class.new(dirname.join(new_path))
24
+ pathname = path.dirname.join(new_path)
25
+ self.class.new(pathname, pwd)
24
26
  end
25
27
 
26
28
  def directories
27
- @pathname.children.select(&:directory?).map do |pathname|
28
- self.class.new(pathname.to_s)
29
+ absolute_path.children.select(&:directory?).sort.map do |pathname|
30
+ self.class.new(relative_path(pathname), pwd)
29
31
  end
30
32
  end
31
33
 
32
34
  def files
33
- @pathname.children.select(&:file?).map do |pathname|
34
- self.class.new(pathname.to_s)
35
+ absolute_path.children.select(&:file?).sort.map do |pathname|
36
+ self.class.new(relative_path(pathname), pwd)
35
37
  end
36
38
  end
37
39
 
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
40
  def file?
52
- @pathname.file?
41
+ absolute_path.file?
53
42
  end
54
43
 
55
44
  def directory?
56
- @pathname.directory?
45
+ absolute_path.directory?
57
46
  end
58
47
 
59
48
  private
60
49
 
61
- def dirname
62
- @pathname.dirname
50
+ def absolute_path
51
+ pwd.join(path)
52
+ end
53
+
54
+ def relative_path(pathname)
55
+ pathname.relative_path_from(pwd)
63
56
  end
64
57
  end
65
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.2.0'
4
+ VERSION = '0.3.0'
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.2.0
4
+ version: 0.3.0
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-15 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,60 +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
- 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
@@ -1,30 +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
-
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