Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unix-like commands to FileUtils #3420

Merged
merged 2 commits into from
Oct 15, 2016
Merged

Added unix-like commands to FileUtils #3420

merged 2 commits into from
Oct 15, 2016

Conversation

ghivert
Copy link
Contributor

@ghivert ghivert commented Oct 14, 2016

Hi.
I tried to add some unix-like commands in FileUtils, as asked in request #1058.
Hope it will be good.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@asterite
Copy link
Member

@ghivert Awesome!! ❤️

I'll review it soon.

# FileUtils.cd("to/directory") { puts "Do something useful here!" }
# ```
def cd(path : String, &block)
Dir.cd(path, &block)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please implement this as:

Dir.cd(path) { yield }

This avoids closures, as described here (might be improved in the future)

@RX14
Copy link
Member

RX14 commented Oct 14, 2016

@asterite Hopefully not too off-topic, but why exactly do we have FileUtils and methods on File:Class? What's the split?

@asterite
Copy link
Member

@ghivert Looks really good! :-)

The only thing I would change (in addition to the block comment above) is to mark methods that you have right now returning 0 as returning nil. You can do this by placing : Nil after the method signature, and removing the 0 at the end. There's no need to put a nil at the end. This will automatically make the method return nil, and it also shows on the docs so users know that the method doesn't return anything useful.

@asterite
Copy link
Member

@RX14 The idea is that FileUtils is a bit more convenient that both File and Dir, combines functions from both of them, accept enumerables in many places where in File and Dir just a single argument is accepted, and have unix-like names. You can do:

include FileUtils

# Now you can write things almost as in a unix shell
cd "foo"
mv "bar", "baz"
mkdir_p "qux"

In this particular case these aliases don't bother because it's not like there will be multiple possible implementations of this and these aliases will need to be defined over and over.

describe "pwd" do
it "returns the current working directory" do
cwd = Dir.current
(cwd == FileUtils.pwd).should be_true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FileUtils.pwd.should eq(Dir.current)?

@ghivert
Copy link
Contributor Author

ghivert commented Oct 15, 2016

@asterite Actually, functions returned 0 to mimic shell behavior. But it seems better for me now. :)
In FileUtils in Ruby, there are other functions (like touch). Should it be added in crystal too ?

@asterite
Copy link
Member

@ghivert Sure, that would be great!

I forgot about this PR, it looks good now. Thank you so much for this! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants