Created
November 24, 2023 05:47
-
-
Save wilsonsilva/e83095044b7d2be90da897f1bca8c6ea to your computer and use it in GitHub Desktop.
Rename a gem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# from https://bradgessler.com/articles/rename-a-rubygem/ | |
require 'pathname' | |
require 'fileutils' | |
class GemRename | |
def initialize(base_dir = ".") | |
@base_dir = Pathname.new(base_dir) | |
end | |
def constants(from:, to:) | |
update_file_contents(from, to) | |
end | |
def paths_and_contents(from:, to:) | |
rename_files_and_directories(from, to) | |
update_file_contents(from, to) | |
end | |
private | |
def rename_files_and_directories(old_name, new_name) | |
# Rename directories | |
@base_dir.glob("**/*#{old_name}*").select(&:directory?).each do |dir| | |
FileUtils.mv(dir, dir.to_s.gsub(old_name, new_name)) | |
end | |
# Rename files | |
@base_dir.glob("**/*#{old_name}*").each do |file| | |
FileUtils.mv(file, file.to_s.gsub(old_name, new_name)) if file.file? | |
end | |
end | |
def update_file_contents(old_text, new_text) | |
@base_dir.glob("**/*").select(&:file?).each do |file| | |
text = File.read(file) | |
new_contents = text.gsub(old_text, new_text) | |
File.open(file, "w") { |f| f.puts new_contents } | |
end | |
end | |
end | |
# Usage Example | |
# rename = GemRename.new(".") | |
# rename.constants from: "OldGem", to: "NewGem" | |
# rename.paths_and_contents from: "old_gem", to: "new_gem" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment