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

Use Thor for built-in tmp task #47698

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

p8
Copy link
Member

@p8 p8 commented Mar 16, 2023

Currently we use both Thor and Rake for bin/rails commands. We eventually want to get all the built-ins task promoted to Thor Commands. This migrates the tmp task to Thor.

As tmp directories are sometimes hidden by editors, I've used temp instead.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@rails-bot rails-bot bot added the railties label Mar 16, 2023
@p8 p8 force-pushed the railties/thor-tmp-task branch 3 times, most recently from ff8d107 to 119c08c Compare March 16, 2023 21:15
@p8 p8 force-pushed the railties/thor-tmp-task branch 4 times, most recently from e4eae66 to 80612bd Compare March 16, 2023 21:44
Copy link
Member

@jonathanhefner jonathanhefner left a comment

Choose a reason for hiding this comment

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

As tmp directories are sometimes hidden by editors, I've used temp instead.

I don't think changing the code to suit the editor is the right approach.

Which editor are you seeing this problem in? Would it possible to add a dot file in the command's directory (like a .keep file) to force the editor to recognize the directory?

railties/lib/rails/commands/temp/helpers.rb Outdated Show resolved Hide resolved
railties/lib/rails/commands/temp/helpers.rb Outdated Show resolved Hide resolved
Rails::Command.invoke "tmp:storage:clear"
end

# desc :create, "Create tmp directories for cache, sockets, and pids"
Copy link
Member

Choose a reason for hiding this comment

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

I think we can uncomment this. It's not commented out for the current Rake task.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

Comment on lines 27 to 28
module Temp
class CacheCommand < Base # :nodoc:
Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately, putting this module in railties/lib/rails/commands/temp/temp_command.rb means lookup won't find these commands (because namespaces_to_paths won't return the right path):

$ bin/rails tmp:screenshots:clear
Unrecognized command "tmp:screenshots:clear" (Rails::Command::UnrecognizedCommandError)
Did you mean?  tmp:sockets:clear

$ bin/rails tmp:sockets:clear
Unrecognized command "tmp:sockets:clear" (Rails::Command::UnrecognizedCommandError)
Did you mean?  tmp:cache:clear

$ bin/rails tmp:cache:clear
Unrecognized command "tmp:cache:clear" (Rails::Command::UnrecognizedCommandError)
Did you mean?  tmp:clear

(But "Did you mean?" can subsequently suggest them because it uses printing_commands.)

So each command needs to go into a dedicated subdirectory.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've extracted these to separate files.

Comment on lines 12 to 22
FileUtils.mkdir_p("tmp/cache")
FileUtils.touch("tmp/cache/cache_file")

FileUtils.mkdir_p("tmp/sockets")
FileUtils.touch("tmp/sockets/socket_file")

FileUtils.mkdir_p("tmp/screenshots")
FileUtils.touch("tmp/screenshots/fail.png")

FileUtils.mkdir_p("tmp/storage/6h/np")
FileUtils.touch("tmp/storage/6h/np/6hnp81jvgt42pcfqtlpoy8qshfb0")
Copy link
Member

Choose a reason for hiding this comment

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

We can use the app_file helper for each of these.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice. I've made the change.

Comment on lines 26 to 29
assert_not File.exist?("tmp/cache/cache_file")
assert_not File.exist?("tmp/sockets/socket_file")
assert_not File.exist?("tmp/screenshots/fail.png")
assert_not File.exist?("tmp/storage/6h/np/6hnp81jvgt42pcfqtlpoy8qshfb0")
Copy link
Member

Choose a reason for hiding this comment

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

It might be worth adding a private assert_file helper for these that also incorporates app_path calls. Then we can drop the Dir.chdir block.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added an assert_dir_cleared that checks the content of the directory.
To make sure we don't delete the directory, this assertion also fails if the directory no longer exists.

Comment on lines 35 to 39
rails "tmp:create"
assert Dir.exist?("tmp/cache")
assert Dir.exist?("tmp/sockets")
assert Dir.exist?("tmp/pids")
assert Dir.exist?("tmp/cache/assets")
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be good to call FileUtils.rm_rf(tmp_dirs) before rails "tmp:create". That way we can be sure we are actually testing creation.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@p8
Copy link
Member Author

p8 commented Mar 18, 2023

Which editor are you seeing this problem in? Would it possible to add a dot file in the command's directory (like a .keep file) to force the editor to recognize the directory?

I'm using Neovim with NerdTree.
I thought this was a more common problem, but I'm probably mistaken.
I've updated my vim config and renamed the files 😄

@p8 p8 force-pushed the railties/thor-tmp-task branch 4 times, most recently from 714140b to 800f8da Compare March 18, 2023 12:16
Currently we use both Thor and Rake for `bin/rails` commands.
We eventually want to get all the built-ins task promoted to Thor Commands.
This migrates the `tmp` task to Thor.

Co-authored-by: Juan Vásquez <javasgon@gmail.com>
Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants