Add rake dev:cache task to enable dev mode caching.
Taken from @Sonopa's commits on PR #19091. Add support for dev caching via "rails s" flags. Implement suggestions from @kaspth. Remove temporary cache file if server does not have flags. Break at 80 characters in railties/CHANGELOG.md Remove ability to disable cache based on server options. Add more comprehensive options: --dev-caching / --no-dev-caching
This commit is contained in:
parent
10e994cc07
commit
a01e58afd9
@ -1,3 +1,15 @@
|
|||||||
|
* Make enabling or disabling caching in development mode possible with
|
||||||
|
rake dev:cache.
|
||||||
|
|
||||||
|
Running rake dev:cache will create or remove tmp/caching-dev.txt. When this
|
||||||
|
file exists config.action_controller.perform_caching will be set to true in
|
||||||
|
config/environments/development.rb.
|
||||||
|
|
||||||
|
Additionally, a server can be started with either --dev-caching or
|
||||||
|
--no-dev-caching included to toggle caching on startup.
|
||||||
|
|
||||||
|
*Jussi Mertanen*, *Chuck Callebs*
|
||||||
|
|
||||||
* Add a `--api` option in order to generate plugins that can be added
|
* Add a `--api` option in order to generate plugins that can be added
|
||||||
inside an API application.
|
inside an API application.
|
||||||
|
|
||||||
|
@ -34,6 +34,9 @@ def option_parser(options)
|
|||||||
opts.on("-P", "--pid=pid", String,
|
opts.on("-P", "--pid=pid", String,
|
||||||
"Specifies the PID file.",
|
"Specifies the PID file.",
|
||||||
"Default: tmp/pids/server.pid") { |v| options[:pid] = v }
|
"Default: tmp/pids/server.pid") { |v| options[:pid] = v }
|
||||||
|
opts.on("-C", "--[no-]dev-caching",
|
||||||
|
"Specifies whether to perform caching in development.",
|
||||||
|
"true or false") { |v| options[:caching] = v }
|
||||||
|
|
||||||
opts.separator ""
|
opts.separator ""
|
||||||
|
|
||||||
@ -67,6 +70,7 @@ def start
|
|||||||
print_boot_information
|
print_boot_information
|
||||||
trap(:INT) { exit }
|
trap(:INT) { exit }
|
||||||
create_tmp_directories
|
create_tmp_directories
|
||||||
|
setup_dev_caching
|
||||||
log_to_stdout if options[:log_stdout]
|
log_to_stdout if options[:log_stdout]
|
||||||
|
|
||||||
super
|
super
|
||||||
@ -86,12 +90,23 @@ def default_options
|
|||||||
DoNotReverseLookup: true,
|
DoNotReverseLookup: true,
|
||||||
environment: (ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "development").dup,
|
environment: (ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "development").dup,
|
||||||
daemonize: false,
|
daemonize: false,
|
||||||
|
caching: false,
|
||||||
pid: File.expand_path("tmp/pids/server.pid")
|
pid: File.expand_path("tmp/pids/server.pid")
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def setup_dev_caching
|
||||||
|
return unless options[:environment] == "development"
|
||||||
|
|
||||||
|
if options[:caching] == false
|
||||||
|
delete_cache_file
|
||||||
|
elsif options[:caching]
|
||||||
|
create_cache_file
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def print_boot_information
|
def print_boot_information
|
||||||
url = "#{options[:SSLEnable] ? 'https' : 'http'}://#{options[:Host]}:#{options[:Port]}"
|
url = "#{options[:SSLEnable] ? 'https' : 'http'}://#{options[:Host]}:#{options[:Port]}"
|
||||||
puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}"
|
puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}"
|
||||||
@ -101,6 +116,14 @@ def print_boot_information
|
|||||||
puts "=> Ctrl-C to shutdown server" unless options[:daemonize]
|
puts "=> Ctrl-C to shutdown server" unless options[:daemonize]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create_cache_file
|
||||||
|
FileUtils.touch("tmp/caching-dev.txt")
|
||||||
|
end
|
||||||
|
|
||||||
|
def delete_cache_file
|
||||||
|
FileUtils.rm("tmp/caching-dev.txt") if File.exists?("tmp/caching-dev.txt")
|
||||||
|
end
|
||||||
|
|
||||||
def create_tmp_directories
|
def create_tmp_directories
|
||||||
%w(cache pids sockets).each do |dir_to_make|
|
%w(cache pids sockets).each do |dir_to_make|
|
||||||
FileUtils.mkdir_p(File.join(Rails.root, 'tmp', dir_to_make))
|
FileUtils.mkdir_p(File.join(Rails.root, 'tmp', dir_to_make))
|
||||||
|
@ -9,9 +9,19 @@ Rails.application.configure do
|
|||||||
# Do not eager load code on boot.
|
# Do not eager load code on boot.
|
||||||
config.eager_load = false
|
config.eager_load = false
|
||||||
|
|
||||||
# Show full error reports and disable caching.
|
# Show full error reports.
|
||||||
config.consider_all_requests_local = true
|
config.consider_all_requests_local = true
|
||||||
|
|
||||||
|
# Enable/disable caching. By default caching is disabled.
|
||||||
|
if Rails.root.join('tmp/caching-dev.txt').exist?
|
||||||
|
config.action_controller.perform_caching = true
|
||||||
|
config.static_cache_control = "public, max-age=172800"
|
||||||
|
config.cache_store = :memory_store
|
||||||
|
else
|
||||||
config.action_controller.perform_caching = false
|
config.action_controller.perform_caching = false
|
||||||
|
config.cache_store = :null_store
|
||||||
|
end
|
||||||
|
|
||||||
<%- unless options.skip_action_mailer? -%>
|
<%- unless options.skip_action_mailer? -%>
|
||||||
|
|
||||||
# Don't care if the mailer can't send.
|
# Don't care if the mailer can't send.
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
# Load Rails Rakefile extensions
|
# Load Rails Rakefile extensions
|
||||||
%w(
|
%w(
|
||||||
annotations
|
annotations
|
||||||
|
dev
|
||||||
framework
|
framework
|
||||||
initializers
|
initializers
|
||||||
log
|
log
|
||||||
|
15
railties/lib/rails/tasks/dev.rake
Normal file
15
railties/lib/rails/tasks/dev.rake
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
namespace :dev do
|
||||||
|
task :cache do
|
||||||
|
desc 'Toggle development mode caching on/off'
|
||||||
|
|
||||||
|
if File.exist? 'tmp/caching-dev.txt'
|
||||||
|
File.delete 'tmp/caching-dev.txt'
|
||||||
|
puts 'Development mode is no longer being cached.'
|
||||||
|
else
|
||||||
|
FileUtils.touch 'tmp/caching-dev.txt'
|
||||||
|
puts 'Development mode is now being cached.'
|
||||||
|
end
|
||||||
|
|
||||||
|
FileUtils.touch 'tmp/restart.txt'
|
||||||
|
end
|
||||||
|
end
|
35
railties/test/application/rake/dev_test.rb
Normal file
35
railties/test/application/rake/dev_test.rb
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
require 'isolation/abstract_unit'
|
||||||
|
|
||||||
|
module ApplicationTests
|
||||||
|
module RakeTests
|
||||||
|
class RakeDevTest < ActiveSupport::TestCase
|
||||||
|
include ActiveSupport::Testing::Isolation
|
||||||
|
|
||||||
|
def setup
|
||||||
|
build_app
|
||||||
|
boot_rails
|
||||||
|
end
|
||||||
|
|
||||||
|
def teardown
|
||||||
|
teardown_app
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'dev:cache creates file and outputs message' do
|
||||||
|
Dir.chdir(app_path) do
|
||||||
|
output = `rake dev:cache`
|
||||||
|
assert File.exist?('tmp/caching-dev.txt')
|
||||||
|
assert_match(/Development mode is now being cached/, output)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test 'dev:cache deletes file and outputs message' do
|
||||||
|
Dir.chdir(app_path) do
|
||||||
|
output = `rake dev:cache`
|
||||||
|
output = `rake dev:cache`
|
||||||
|
assert_not File.exist?('tmp/caching-dev.txt')
|
||||||
|
assert_match(/Development mode is no longer being cached/, output)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -44,6 +44,22 @@ def test_environment_with_rack_env
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_caching_without_option
|
||||||
|
args = []
|
||||||
|
options = Rails::Server::Options.new.parse!(args)
|
||||||
|
assert_equal nil, options[:caching]
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_caching_with_option
|
||||||
|
args = ["--dev-caching"]
|
||||||
|
options = Rails::Server::Options.new.parse!(args)
|
||||||
|
assert_equal true, options[:caching]
|
||||||
|
|
||||||
|
args = ["--no-dev-caching"]
|
||||||
|
options = Rails::Server::Options.new.parse!(args)
|
||||||
|
assert_equal false, options[:caching]
|
||||||
|
end
|
||||||
|
|
||||||
def test_log_stdout
|
def test_log_stdout
|
||||||
with_rack_env nil do
|
with_rack_env nil do
|
||||||
with_rails_env nil do
|
with_rails_env nil do
|
||||||
|
Loading…
Reference in New Issue
Block a user