class Tool
Public Class Methods
rename(old_prefix, new_prefix)
click to toggle source
# File lib/reprefix.rb, line 4 def self.rename(old_prefix, new_prefix) puts "⌛⌛⌛ 修改前缀中..." pattern = Regexp.new(old_prefix) Find.find(Dir.pwd).each do |file_path| if !File.directory?(file_path) && File.extname(file_path) != '.rb' ## 是目录并且不含有.rb text = File.read(file_path).encode!('UTF-8', 'UTF-8', :invalid => :replace) # 编码否则报错 replace = text.gsub(pattern, new_prefix) # 字符串替换 File.open(file_path, "w") do |file| # 重新写入文件 w : 用只写模式打开文件。文件不存在则创建新的文件;文件已存在则清空文件,即将文件大小设置为0。 file.puts replace end # 假如文件路径是src/ruby/file.c file_dir = File.dirname(file_path) # src/ruby file_name = File.basename(file_path) # fileName file.c new_file_name = file_name.gsub(pattern, new_prefix); # new_file_path = File.join(file_dir, new_file_name) File.rename(file_path, new_file_path) end end puts "✅✅✅ 修改前缀完成" end