bulldoze_renamer 0.0.1
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 +7 -0
- data/.github/FUNDING.yml +4 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +12 -0
- data/Guardfile +6 -0
- data/README.md +85 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/bulldoze_renamer.gemspec +22 -0
- data/exe/bulldoze_rename +5 -0
- data/exe/bulldoze_rename_rb +6 -0
- data/lib/bulldoze_renamer.rb +19 -0
- data/lib/bulldoze_renamer/file_content_substitutor.rb +46 -0
- data/lib/bulldoze_renamer/file_finder.rb +60 -0
- data/lib/bulldoze_renamer/renaming_orchestrator.rb +190 -0
- data/lib/bulldoze_renamer/shell.rb +50 -0
- data/lib/bulldoze_renamer/string_inflector.rb +81 -0
- data/lib/bulldoze_renamer/version.rb +3 -0
- metadata +92 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 810a46d0c33d974031e0ec0aad18faa747f24bbc1b64d759103ac2c46987d7d1
|
4
|
+
data.tar.gz: f97652392e76b61b2f4b2ef228ec141cddc12c36209b120c5c3a31baf82d0916
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b0d7863a26ffeb3293c4612dee705fedaef80cd65a89c5a69be0d71600fff259545e5d2906ae4c8c35695165b4a38b22b01ccaf0dc0342750a6dbc9e22167a78
|
7
|
+
data.tar.gz: b062a5af199faae684d5908a4d2313b096b74f03a1e8752b3342996e596994db4f6ec00c972f4146d312120986429a7e5b08ef98d394b464b866416be17215a9
|
data/.github/FUNDING.yml
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
# Bulldoze Renamer
|
2
|
+
|
3
|
+
Bulldoze Renamer is a tool to rename things in a code project.
|
4
|
+
|
5
|
+
Suppose you have an object that you called PoohBear but you want to now rename it to HoneyBear.
|
6
|
+
|
7
|
+
That means that everywhere in your project you want to replace `PoohBear -> HoneyBear`.
|
8
|
+
But also `pooh_bear -> honey_bear`. Any there may be some constant `POOH_BEAR_ACTIONS ->
|
9
|
+
HONEY_BEAR_ACTIONS` somewhere. And maybe the class is defined in `lib/bears/pooh_bear/pooh_bear.rb`
|
10
|
+
and so that file should be renamed `lib/bears/pooh_bear -> lib/bears/honey_bear` and
|
11
|
+
`lib/bears/pooh_bear/pooh_bear.rb -> lib/bears/honey_bear/honey_bear.rb`.
|
12
|
+
|
13
|
+
This tool aims to do all those replacements for you in the entire project in one go.
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Install by installing the gem:
|
18
|
+
|
19
|
+
$ gem install bulldoze_renamer
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
It runs like so: `bulldoze_rename target_directory PoohBear HoneyBear`
|
24
|
+
|
25
|
+
It will only check files that are known to the git repo of the target_directory.
|
26
|
+
|
27
|
+
Invoked like this it will only show an overview of the changes that will take place.
|
28
|
+
The output looks like this:
|
29
|
+
|
30
|
+
underscore : pooh_bear -> honey_bear
|
31
|
+
camelize : PoohBear -> HoneyBear
|
32
|
+
dasherize : pooh-bear -> honey-bear
|
33
|
+
upcase : POOH_BEAR -> HONEY_BEAR
|
34
|
+
js_camelize: poohBear -> honeyBear
|
35
|
+
|
36
|
+
camelize
|
37
|
+
| upcase
|
38
|
+
| | filename
|
39
|
+
| | |
|
40
|
+
_ _ 1 lib/bears/pooh_bear
|
41
|
+
1 1 1 lib/bears/pooh_bear/pooh_bear.rb
|
42
|
+
|
43
|
+
The first section shows the possible mappings it looks for in the target_directory.
|
44
|
+
The section below that shows which of those actually occur in which files. Files
|
45
|
+
that will need to be renamed also have a value for `filename`.
|
46
|
+
|
47
|
+
To actually perform the substitutions, add the `-p` option to the earlier command.
|
48
|
+
Then it will print out the filenames as they are being updated. That looks like
|
49
|
+
this:
|
50
|
+
|
51
|
+
Performing:
|
52
|
+
R lib/bears/pooh_bear/pooh_bear.rb -> lib/bears/pooh_bear/honey_bear.rb
|
53
|
+
d lib/bears/pooh_bear -> lib/bears/honey_bear
|
54
|
+
|
55
|
+
Where the first character denotes:
|
56
|
+
- f: substituted value in file
|
57
|
+
- r: only renamed the file
|
58
|
+
- R: renamed file and substituted values in it
|
59
|
+
- d: directory which was moved
|
60
|
+
|
61
|
+
### Warning
|
62
|
+
|
63
|
+
As suggested by the name `bulldozer`, this tool is rather crude. Undesirable things
|
64
|
+
can happen, especially if it is not clear from the original value or target value
|
65
|
+
what type of format it should be.
|
66
|
+
|
67
|
+
For example for `pooh_bear` it is clear that it is in underscore format, but if it
|
68
|
+
were just `pooh` it could also be in js_camelize format.
|
69
|
+
|
70
|
+
Therefore there should be no issue if both original and new values have multiple words.
|
71
|
+
When replacing a single word with another single word, there should also be no issue.
|
72
|
+
|
73
|
+
But when you replace single word with multiple words it will be ambiguous which format to
|
74
|
+
use.
|
75
|
+
|
76
|
+
When you replace multiple words with a single word, that will work fine, but afterwards
|
77
|
+
renaming it again may be problematic.
|
78
|
+
|
79
|
+
Always ensure by checking changes in git that they are all correct.
|
80
|
+
|
81
|
+
## Development
|
82
|
+
|
83
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
84
|
+
|
85
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "bulldoze_renamer"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative 'lib/bulldoze_renamer/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "bulldoze_renamer"
|
5
|
+
spec.version = BulldozeRenamer::VERSION
|
6
|
+
spec.authors = ["Tom ten Thij"]
|
7
|
+
spec.email = ["code@tomtenthij.nl"]
|
8
|
+
|
9
|
+
spec.summary = %q{A gem environment to learn code in}
|
10
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
11
|
+
|
12
|
+
# Specify which files should be added to the gem when it is released.
|
13
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
14
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
15
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
end
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
spec.add_dependency('activesupport', '>= 6')
|
21
|
+
spec.add_dependency('ruby-filemagic', '~> 0.7.2')
|
22
|
+
end
|
data/exe/bulldoze_rename
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
Encoding.default_external = 'UTF-8'
|
4
|
+
|
5
|
+
require "bulldoze_renamer/version"
|
6
|
+
|
7
|
+
module BulldozeRenamer
|
8
|
+
class LinesNotFoundError < IOError; end
|
9
|
+
|
10
|
+
def self.root
|
11
|
+
Pathname.new(File.absolute_path(File.join(File.dirname(__FILE__), '..')))
|
12
|
+
end
|
13
|
+
|
14
|
+
autoload :FileContentSubstitutor , 'bulldoze_renamer/file_content_substitutor'
|
15
|
+
autoload :FileFinder , 'bulldoze_renamer/file_finder'
|
16
|
+
autoload :RenamingOrchestrator , 'bulldoze_renamer/renaming_orchestrator'
|
17
|
+
autoload :Shell , 'bulldoze_renamer/shell'
|
18
|
+
autoload :StringInflector , 'bulldoze_renamer/string_inflector'
|
19
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'tmpdir'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
module BulldozeRenamer
|
5
|
+
class FileContentSubstitutor
|
6
|
+
def initialize(dir)
|
7
|
+
@dir = dir
|
8
|
+
end
|
9
|
+
|
10
|
+
def full_path(path)
|
11
|
+
File.join(@dir, path)
|
12
|
+
end
|
13
|
+
|
14
|
+
def with_content_written_to_temporary_file(original_path, content)
|
15
|
+
Dir.mktmpdir do |dir|
|
16
|
+
filename = File.join(dir, 'replaced_content')
|
17
|
+
|
18
|
+
# First copy original file to preserve permissions, even though we will overwrite
|
19
|
+
FileUtils.cp_r(File.join(@dir, original_path), filename, preserve: true)
|
20
|
+
|
21
|
+
File.open(filename, 'w') do |f|
|
22
|
+
f.print content
|
23
|
+
end
|
24
|
+
yield(filename)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def move_directory(original_path, new_path)
|
29
|
+
FileUtils.mv(full_path(original_path), full_path(new_path))
|
30
|
+
end
|
31
|
+
|
32
|
+
def substitute_in(original_path, new_path, mappings)
|
33
|
+
content = File.read(full_path(original_path))
|
34
|
+
mappings.each do |m|
|
35
|
+
content.gsub!(m[0], m[1])
|
36
|
+
end
|
37
|
+
|
38
|
+
with_content_written_to_temporary_file(original_path, content) do |f|
|
39
|
+
FileUtils.cp_r(f, full_path(new_path), preserve: true)
|
40
|
+
if new_path != original_path
|
41
|
+
FileUtils.rm(full_path(original_path))
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'find'
|
2
|
+
require 'filemagic'
|
3
|
+
|
4
|
+
module BulldozeRenamer
|
5
|
+
class FileFinder
|
6
|
+
def self.file_magic
|
7
|
+
@@file_magic ||= FileMagic.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.file_excluded?(file)
|
11
|
+
file.include?(".git/") ||
|
12
|
+
file.include?("node_modules") ||
|
13
|
+
File.basename(file) == '.git' ||
|
14
|
+
file == '.'
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.file_considered?(file)
|
18
|
+
return true if FileTest.directory?(file)
|
19
|
+
|
20
|
+
fm = file_magic.file(file)
|
21
|
+
fm.include?('text') ||
|
22
|
+
fm.include?('JSON')
|
23
|
+
end
|
24
|
+
|
25
|
+
# Using git ls-files has many advantages but it does not
|
26
|
+
# return directories. This method extracts all directories
|
27
|
+
# as present for the files found.
|
28
|
+
def self.extract_directories(files)
|
29
|
+
result = Set.new
|
30
|
+
files.each do |f|
|
31
|
+
dir = File.dirname(f)
|
32
|
+
|
33
|
+
# File.dirname('foo') => "."
|
34
|
+
# File.dirname('/foo') => "/"
|
35
|
+
while(dir != '.' && dir != '/' && !result.include?(dir))
|
36
|
+
result << dir
|
37
|
+
dir = File.dirname(dir)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
result.to_a.sort
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.find(path)
|
44
|
+
files = []
|
45
|
+
Dir.chdir(path) do
|
46
|
+
files = `git ls-files`.split
|
47
|
+
files = (files + extract_directories(files)).sort
|
48
|
+
|
49
|
+
if block_given?
|
50
|
+
files.select { |f| yield(f) }
|
51
|
+
else
|
52
|
+
files.select do |p|
|
53
|
+
!file_excluded?(p) &&
|
54
|
+
file_considered?(p)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,190 @@
|
|
1
|
+
require 'pp'
|
2
|
+
|
3
|
+
module BulldozeRenamer
|
4
|
+
class RenamingOrchestrator
|
5
|
+
def initialize(path:, current_name:, target_name:, perform: nil)
|
6
|
+
@path = path
|
7
|
+
@current_name = current_name
|
8
|
+
@target_name = target_name
|
9
|
+
@perform = perform
|
10
|
+
end
|
11
|
+
|
12
|
+
def count_in_content(target, content)
|
13
|
+
('~' + content + '~').split(target).size - 1
|
14
|
+
end
|
15
|
+
|
16
|
+
def find_occurences(file, mappings)
|
17
|
+
result = {}
|
18
|
+
Dir.chdir @path do
|
19
|
+
occurs_in_filename =
|
20
|
+
mappings[:inflections][:underscore] &&
|
21
|
+
File.basename(file).include?(mappings[:inflections][:underscore][:current])
|
22
|
+
occurs_in_filename ||=
|
23
|
+
mappings[:inflections][:dasherize] &&
|
24
|
+
File.basename(file).include?(mappings[:inflections][:dasherize][:current])
|
25
|
+
result[:filename] = occurs_in_filename ? 1 : 0
|
26
|
+
|
27
|
+
unless FileTest.directory?(file)
|
28
|
+
mappings[:inflections].each do |inflection, values|
|
29
|
+
result[inflection] = count_in_content(values[:current], File.read(file))
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
result
|
34
|
+
end
|
35
|
+
|
36
|
+
def was_found(occ)
|
37
|
+
occ.values.sum > 0
|
38
|
+
end
|
39
|
+
|
40
|
+
def format_number(number)
|
41
|
+
number && number > 0 ? "%4d" % number : ' _'
|
42
|
+
end
|
43
|
+
|
44
|
+
def inflections_mapping
|
45
|
+
@inflections_mapping ||= StringInflector.new(@current_name, @target_name).mappings
|
46
|
+
end
|
47
|
+
|
48
|
+
def files
|
49
|
+
@files ||= FileFinder.find(@path)
|
50
|
+
end
|
51
|
+
|
52
|
+
def file_occurences
|
53
|
+
return @file_occurences if @file_occurences
|
54
|
+
|
55
|
+
result = {}
|
56
|
+
|
57
|
+
files.each do |file|
|
58
|
+
result[file] = find_occurences(file, inflections_mapping)
|
59
|
+
end
|
60
|
+
|
61
|
+
@file_occurences = result
|
62
|
+
end
|
63
|
+
|
64
|
+
def inflections_that_are_present
|
65
|
+
@inflections_that_are_present ||=
|
66
|
+
StringInflector.inflections + [:filename] &
|
67
|
+
file_occurences.values.inject({}) { |a,h| h.each { |k,v| v > 0 && (a[k] ||= 0 ; a[k] += v) };a }.keys
|
68
|
+
end
|
69
|
+
|
70
|
+
def report_header_file_occurences
|
71
|
+
result = ""
|
72
|
+
inflections_that_are_present.each_with_index do |inflection, index|
|
73
|
+
result += (" |" * index + ' ' + inflection.to_s + "\n")[1..-1]
|
74
|
+
end
|
75
|
+
result += (" |" * inflections_that_are_present.size + "\n")[1..-1]
|
76
|
+
result
|
77
|
+
end
|
78
|
+
|
79
|
+
def report_file_occurences_line(file)
|
80
|
+
result = ""
|
81
|
+
inflections_that_are_present.each_with_index do |inflection, index|
|
82
|
+
result += format_number(file_occurences[file][inflection])
|
83
|
+
end
|
84
|
+
result + ' ' + file + "\n"
|
85
|
+
end
|
86
|
+
|
87
|
+
def files_that_have_occurences
|
88
|
+
files.select { |f| was_found(file_occurences[f]) }
|
89
|
+
end
|
90
|
+
|
91
|
+
def report_file_occurences
|
92
|
+
result = ""
|
93
|
+
files_that_have_occurences.each do |f|
|
94
|
+
result += report_file_occurences_line(f)[1..-1]
|
95
|
+
end
|
96
|
+
result
|
97
|
+
end
|
98
|
+
|
99
|
+
def report_inflections_mapping
|
100
|
+
longest_current = inflections_mapping[:inflections].values.map { |v| v[:current].size }.max
|
101
|
+
|
102
|
+
result = ""
|
103
|
+
inflections_mapping[:inflections].each do |i, v|
|
104
|
+
result += "%-11s: %-#{longest_current}s -> %s\n" % [i, v[:current], v[:target]]
|
105
|
+
end
|
106
|
+
result + "\n"
|
107
|
+
end
|
108
|
+
|
109
|
+
def reports_for_files
|
110
|
+
report_header_file_occurences +
|
111
|
+
report_file_occurences
|
112
|
+
end
|
113
|
+
|
114
|
+
def new_filename_for(filename)
|
115
|
+
dirname = File.dirname(filename)
|
116
|
+
basename = File.basename(filename)
|
117
|
+
inflections_mapping[:inflections].each do |m, v|
|
118
|
+
if basename.include?(v[:current])
|
119
|
+
return File.join(dirname, basename.gsub(v[:current], v[:target]))
|
120
|
+
end
|
121
|
+
end
|
122
|
+
filename
|
123
|
+
end
|
124
|
+
|
125
|
+
def perform_directory!(dirname, out)
|
126
|
+
new_dirname = new_filename_for(dirname)
|
127
|
+
out.puts "d #{dirname} -> #{new_dirname}"
|
128
|
+
FileContentSubstitutor.new(@path).move_directory(dirname, new_dirname)
|
129
|
+
end
|
130
|
+
|
131
|
+
def perform_file!(filename, occurences, out)
|
132
|
+
new_filename = if occurences[:filename] > 0
|
133
|
+
x = new_filename_for(filename)
|
134
|
+
mode = 'r'
|
135
|
+
unless occurences.select { |k,v| k != :filename && v > 0 }.empty?
|
136
|
+
mode = 'R'
|
137
|
+
end
|
138
|
+
|
139
|
+
out.puts "#{mode} #{filename} -> #{x}"
|
140
|
+
x
|
141
|
+
else
|
142
|
+
out.puts "f #{filename}"
|
143
|
+
filename
|
144
|
+
end
|
145
|
+
|
146
|
+
present_mappings = occurences.select { |k,v| k != :filename && v > 0 }
|
147
|
+
mappings = present_mappings.map do |m,c|
|
148
|
+
inflections_mapping[:inflections][m].values
|
149
|
+
end
|
150
|
+
|
151
|
+
FileContentSubstitutor.new(@path).
|
152
|
+
substitute_in(filename, new_filename, mappings)
|
153
|
+
end
|
154
|
+
|
155
|
+
def perform!(out:)
|
156
|
+
out.puts "Performing:"
|
157
|
+
directories = []
|
158
|
+
files_that_have_occurences.each do |filename|
|
159
|
+
occurences = file_occurences[filename]
|
160
|
+
if FileTest.directory?(File.join(@path, filename))
|
161
|
+
directories << filename
|
162
|
+
else
|
163
|
+
perform_file!(filename, occurences, out)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
directories.reverse.each do |dir|
|
168
|
+
perform_directory!(dir, out)
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
def self.rename_with_options(options, out:, err:)
|
174
|
+
orch = RenamingOrchestrator.new(options)
|
175
|
+
out.puts orch.report_inflections_mapping
|
176
|
+
|
177
|
+
if orch.files_that_have_occurences.empty?
|
178
|
+
out.puts "'#{options[:current_name]}' can not be found in any of the files in '#{options[:path]}'"
|
179
|
+
else
|
180
|
+
out.puts orch.reports_for_files
|
181
|
+
if options[:perform]
|
182
|
+
puts
|
183
|
+
orch.perform!(out: out)
|
184
|
+
else
|
185
|
+
out.puts "\nThis is an overview of changes that would be made\nRun same command with -p option to perform"
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
|
3
|
+
module BulldozeRenamer
|
4
|
+
class Shell
|
5
|
+
BANNER = <<"EOT"
|
6
|
+
usage: #{$0} path CurrentNameInCamelCase TargetName
|
7
|
+
|
8
|
+
Will do a crude renaming of everything in the current directory. It
|
9
|
+
uses `git ls-files` to find the files so only things checked into
|
10
|
+
git are subject to change.
|
11
|
+
|
12
|
+
This script is still potentially dangerous, use with caution.
|
13
|
+
|
14
|
+
By default only an overview is shown of what changes will be made
|
15
|
+
in what files. To actually perform those changes, pass the '-p'
|
16
|
+
option.
|
17
|
+
EOT
|
18
|
+
|
19
|
+
def self.usage(err: STDERR)
|
20
|
+
err.puts BANNER
|
21
|
+
exit 1
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.start(argv, out: STDOUT, err: STDERR)
|
25
|
+
options = {
|
26
|
+
current_name: nil,
|
27
|
+
target_name: nil,
|
28
|
+
perform: false
|
29
|
+
}
|
30
|
+
|
31
|
+
OptionParser.new do |parser|
|
32
|
+
parser.banner = BANNER
|
33
|
+
|
34
|
+
parser.on("-p", "--perform", "Perform the substitutions on the files") do |perform|
|
35
|
+
options[:perform] = perform
|
36
|
+
end
|
37
|
+
end.parse! argv
|
38
|
+
|
39
|
+
unless argv.size == 3
|
40
|
+
self.usage(err: err)
|
41
|
+
end
|
42
|
+
|
43
|
+
options[:path] = argv[0]
|
44
|
+
options[:current_name] = argv[1]
|
45
|
+
options[:target_name] = argv[2]
|
46
|
+
|
47
|
+
RenamingOrchestrator.rename_with_options(options, out: out, err: err)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require "active_support"
|
2
|
+
require "forwardable"
|
3
|
+
|
4
|
+
module BulldozeRenamer
|
5
|
+
class StringInflector
|
6
|
+
class StringDoesNotInflectToItselfError < ArgumentError; end
|
7
|
+
|
8
|
+
extend Forwardable
|
9
|
+
|
10
|
+
def self.active_support_inflections
|
11
|
+
[:underscore, :camelize]
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.inflections
|
15
|
+
active_support_inflections + [:dasherize, :upcase, :js_camelize]
|
16
|
+
end
|
17
|
+
|
18
|
+
def_delegators ActiveSupport::Inflector, *StringInflector.active_support_inflections
|
19
|
+
|
20
|
+
def initialize(current, target)
|
21
|
+
@current = current
|
22
|
+
@target = target
|
23
|
+
end
|
24
|
+
|
25
|
+
def upcase(w)
|
26
|
+
underscore(w).upcase
|
27
|
+
end
|
28
|
+
|
29
|
+
def dasherize(w)
|
30
|
+
ActiveSupport::Inflector.dasherize(underscore(w))
|
31
|
+
end
|
32
|
+
|
33
|
+
def js_camelize(w)
|
34
|
+
c = camelize(w).dup
|
35
|
+
c[0] = c[0].downcase
|
36
|
+
c
|
37
|
+
end
|
38
|
+
|
39
|
+
def mappings
|
40
|
+
result = {
|
41
|
+
current_inflection: nil,
|
42
|
+
target_inflection: nil,
|
43
|
+
inflections: {}
|
44
|
+
}
|
45
|
+
StringInflector.inflections.each do |i|
|
46
|
+
unless result[:current_inflection]
|
47
|
+
if @current == send(i, @current)
|
48
|
+
result[:current_inflection] = i
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
unless result[:target_inflection]
|
53
|
+
if @target == send(i, @target)
|
54
|
+
result[:target_inflection] = i
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
mapping = {
|
59
|
+
current: send(i, @current),
|
60
|
+
target: send(i, @target)
|
61
|
+
}
|
62
|
+
|
63
|
+
unless result[:inflections].
|
64
|
+
values.
|
65
|
+
map { |i| i[:current] }.
|
66
|
+
include?(mapping[:current])
|
67
|
+
result[:inflections][i] = mapping
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
unless result[:current_inflection]
|
72
|
+
raise StringDoesNotInflectToItselfError.new(@current)
|
73
|
+
end
|
74
|
+
|
75
|
+
unless result[:target_inflection]
|
76
|
+
raise StringDoesNotInflectToItselfError.new(@target)
|
77
|
+
end
|
78
|
+
result
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bulldoze_renamer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tom ten Thij
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-01-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: ruby-filemagic
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.7.2
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.7.2
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- code@tomtenthij.nl
|
44
|
+
executables:
|
45
|
+
- bulldoze_rename
|
46
|
+
- bulldoze_rename_rb
|
47
|
+
extensions: []
|
48
|
+
extra_rdoc_files: []
|
49
|
+
files:
|
50
|
+
- ".github/FUNDING.yml"
|
51
|
+
- ".gitignore"
|
52
|
+
- ".rspec"
|
53
|
+
- ".travis.yml"
|
54
|
+
- Gemfile
|
55
|
+
- Guardfile
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- bin/console
|
59
|
+
- bin/setup
|
60
|
+
- bulldoze_renamer.gemspec
|
61
|
+
- exe/bulldoze_rename
|
62
|
+
- exe/bulldoze_rename_rb
|
63
|
+
- lib/bulldoze_renamer.rb
|
64
|
+
- lib/bulldoze_renamer/file_content_substitutor.rb
|
65
|
+
- lib/bulldoze_renamer/file_finder.rb
|
66
|
+
- lib/bulldoze_renamer/renaming_orchestrator.rb
|
67
|
+
- lib/bulldoze_renamer/shell.rb
|
68
|
+
- lib/bulldoze_renamer/string_inflector.rb
|
69
|
+
- lib/bulldoze_renamer/version.rb
|
70
|
+
homepage:
|
71
|
+
licenses: []
|
72
|
+
metadata: {}
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: 2.3.0
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubygems_version: 3.0.3
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: A gem environment to learn code in
|
92
|
+
test_files: []
|