pathname
pathname copied to clipboard
Pathname + FileUtils making sweet music together
I love Pathname. I love FileUtils. Let's bring these two classes EVEN CLOSER TOGETHER by adding some tragically missing helpers. I would be happy to create a PR for this amazing functionality. Something like this, perhaps?
class Pathname
def mkdir_p(...) = FileUtils.mkdir_p(@path, ...)
def ln(...) = FileUtils.ln(@path, ...)
def ln_s(...) = FileUtils.ln_s(@path, ...)
def ln_sf(...) = FileUtils.ln_sf(@path, ...)
def cp(...) = FileUtils.cp(@path, ...)
def cp_r(...) = FileUtils.cp_r(@path, ...)
def mv(...) = FileUtils.mv(@path, ...)
def rm(...) = FileUtils.rm(@path, ...)
def rm_r(...) = FileUtils.rm_r(@path, ...)
def rm_rf(...) = FileUtils.rm_rf(@path, ...)
end
Don't get too excited, but I have a branch. It looks like this, which matches the style of the Dir facade methods. One oddity - the existing rmtree method returnsself for some reason. I have no idea why and I didn't do that here.
class Pathname # * FileUtils *
# See <tt>FileUtils.mkdir_p</tt>.
def mkdir_p(...) require 'fileutils' ; FileUtils.mkdir_p(self, ...) end
# See <tt>FileUtils.ln</tt>.
def ln(...) require 'fileutils' ; FileUtils.ln(self, ...) end
# See <tt>FileUtils.ln_s</tt>.
def ln_s(...) require 'fileutils' ; FileUtils.ln_s(self, ...) end
# See <tt>FileUtils.ln_sf</tt>.
def ln_sf(...) require 'fileutils' ; FileUtils.ln_sf(self, ...) end
# See <tt>FileUtils.cp</tt>.
def cp(...) require 'fileutils' ; FileUtils.cp(self, ...) end
# See <tt>FileUtils.cp_r</tt>.
def cp_r(...) require 'fileutils' ; FileUtils.cp_r(self, ...) end
# See <tt>FileUtils.mv</tt>.
def mv(...) require 'fileutils' ; FileUtils.mv(self, ...) end
# See <tt>FileUtils.rm</tt>.
def rm(...) require 'fileutils' ; FileUtils.rm(self, ...) end
# See <tt>FileUtils.rm_r</tt>.
def rm_r(...) require 'fileutils' ; FileUtils.rm_r(self, ...) end
# See <tt>FileUtils.rm_rf</tt>.
def rm_rf(...) require 'fileutils' ; FileUtils.rm_rf(self, ...) end
alias rmtree rm_rf
end
I even have tests, which look like this:
def test_mv
with_tmpchdir('rubytest-pathname') {|dir|
Pathname("a").write("abc")
Pathname("a").mv("b")
assert_file.not_exist?("a")
assert_equal "abc", Pathname("b").read
}
end
The style seem a bit dated. I wanted to use do and tap and add a helper to DRY it up, but I showed restraint and matched the existing stuff.
I think this would make sense (though adding this would need others to approve it besides me). Could you make a PR?
Great, thanks. See #65. Happy to do more work here, whatever is required.
I have no strong opinion for oppose or agree.
I have a concern this proposal make harder to Pathname into the core classes might become more difficult as a result of this proposal, since pathname.rb would become more dependent on FileUtils functionality.
Hi @hsbt. I read through some of the earlier discussion and I understand what you are saying. Your concern is valid. I think we should still be able to add features like this to Pathname, though, and the community would benefit.
What's the best way to achieve that?
@eregon maybe you can help me to understand the options, I would am happy to do whatever work is required to make this happen.
I have a concern this proposal make harder to
Pathnameinto the core classes might become more difficult as a result of this proposal, sincepathname.rbwould become more dependent onFileUtilsfunctionality.
Yeah I have related concerns as well, and I'm thinking to file a https://bugs.ruby-lang.org/ ticket to discuss it.
The fundamental issue seems that core Pathname doesn't have all methods that the Pathname gem has (specially it doesn't have any of the methods that uses require), that seems a big source of confusion, and I'm not sure why it's done that way.
For instance why is it a concern that core Pathname require 'fileutils' in some methods if it just works anyway?
For instance why is it a concern that core Pathname require 'fileutils' in some methods if it just works anyway?
That's consensus with akr and me. We should avoid loading other libraries by simply calling methods from the embedded core classes.
What's the way forward? Here are some ideas:
- Rewrite these methods from scratch to avoid using FileUtils
- Only define these methods if FileUtils is available
- Just use
require, life goes on - Something else?
I'm happy to do whatever, I just want the functionality.
I could put together a PR for any of these... Just bringing this up again since I'm still excited to do it :)
Ah I forgot to backlink here but I filed https://bugs.ruby-lang.org/issues/21640
Thanks, I will chime in there