Skip to content

Commit a51364d

Browse files
Merge pull request rails#47619 from p8/railties/thor-restart-task
Use Thor for built-in restart task
2 parents 0dc919b + 51c965c commit a51364d

File tree

7 files changed

+61
-52
lines changed

7 files changed

+61
-52
lines changed

railties/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
* Allow calling `bin/rails restart` outside of app directory.
2+
3+
The following would previously fail with a "No Rakefile found" error.
4+
5+
```bash
6+
blog/bin/rails restart
7+
```
8+
9+
*Petrik de Heus*
10+
111
* Support prerelease rubies in Gemfile template if RubyGems version is 3.3.13 or higher.
212

313
*Yasuo Honda*, *David Rodríguez*

railties/lib/rails/command.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,15 @@ def find_by_namespace(namespace, command_name = nil) # :nodoc:
108108
def root
109109
if defined?(ENGINE_ROOT)
110110
Pathname.new(ENGINE_ROOT)
111-
elsif defined?(APP_PATH)
112-
Pathname.new(File.expand_path("../..", APP_PATH))
111+
else
112+
application_root
113113
end
114114
end
115115

116+
def application_root # :nodoc:
117+
Pathname.new(File.expand_path("../..", APP_PATH)) if defined?(APP_PATH)
118+
end
119+
116120
def printing_commands # :nodoc:
117121
lookup!
118122

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
module Rails
4+
module Command
5+
class RestartCommand < Base # :nodoc:
6+
desc "restart", "Restart app by touching tmp/restart.txt"
7+
def perform
8+
require "fileutils"
9+
FileUtils.mkdir_p Rails::Command.application_root.join("tmp")
10+
FileUtils.touch Rails::Command.application_root.join("tmp/restart.txt")
11+
end
12+
end
13+
end
14+
end

railties/lib/rails/tasks.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
framework
88
log
99
misc
10-
restart
1110
tmp
1211
yarn
1312
zeitwerk

railties/lib/rails/tasks/restart.rake

Lines changed: 0 additions & 9 deletions
This file was deleted.

railties/test/application/rake/restart_test.rb

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
require "isolation/abstract_unit"
4+
require "rails/command"
5+
6+
class Rails::Command::RestartTest < ActiveSupport::TestCase
7+
include ActiveSupport::Testing::Isolation
8+
setup :build_app
9+
teardown :teardown_app
10+
11+
test "rails restart touches tmp/restart.txt" do
12+
Dir.chdir(app_path) do
13+
rails "restart"
14+
assert File.exist?("tmp/restart.txt")
15+
16+
prev_mtime = File.mtime("tmp/restart.txt")
17+
sleep(1)
18+
rails "restart"
19+
curr_mtime = File.mtime("tmp/restart.txt")
20+
assert_not_equal prev_mtime, curr_mtime
21+
end
22+
end
23+
24+
test "rails restart should work even if tmp folder does not exist" do
25+
Dir.chdir(app_path) do
26+
FileUtils.remove_dir("tmp")
27+
rails "restart"
28+
assert File.exist?("tmp/restart.txt")
29+
end
30+
end
31+
end

0 commit comments

Comments
 (0)