Created
October 6, 2018 03:19
-
-
Save marcandre/c7992f788e561623fc755fbf05c922fa to your computer and use it in GitHub Desktop.
require -> require_relative
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
require 'pathname' | |
Pathname('./lib').children.select(&:directory?).map(&:basename).each do |name| | |
Dir["./lib/#{name}/**/*.rb"].each do |path| | |
begin | |
code = File.read(path) | |
code.gsub!(%r{^(\s*)require (['"])#{name}/([\w/]*)['"]}) do | |
relative_path = Pathname("./lib/#{name}/#{$3}.rb").relative_path_from(Pathname(path).dirname) | |
%Q{#{$1}require_relative #{$2}#{relative_path.to_s[0..-4]}#{$2}} | |
end | |
File.write(path, code) | |
rescue Exception => e | |
puts "Error while processing #{path}:", e | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment