diff options
Diffstat (limited to 'railties/lib/rails/tasks')
-rw-r--r-- | railties/lib/rails/tasks/annotations.rake | 22 | ||||
-rw-r--r-- | railties/lib/rails/tasks/dev.rake | 11 | ||||
-rw-r--r-- | railties/lib/rails/tasks/engine.rake | 88 | ||||
-rw-r--r-- | railties/lib/rails/tasks/framework.rake | 58 | ||||
-rw-r--r-- | railties/lib/rails/tasks/initializers.rake | 9 | ||||
-rw-r--r-- | railties/lib/rails/tasks/log.rake | 41 | ||||
-rw-r--r-- | railties/lib/rails/tasks/middleware.rake | 9 | ||||
-rw-r--r-- | railties/lib/rails/tasks/misc.rake | 79 | ||||
-rw-r--r-- | railties/lib/rails/tasks/restart.rake | 9 | ||||
-rw-r--r-- | railties/lib/rails/tasks/routes.rake | 9 | ||||
-rw-r--r-- | railties/lib/rails/tasks/statistics.rake | 32 | ||||
-rw-r--r-- | railties/lib/rails/tasks/tmp.rake | 44 | ||||
-rw-r--r-- | railties/lib/rails/tasks/yarn.rake | 19 |
13 files changed, 430 insertions, 0 deletions
diff --git a/railties/lib/rails/tasks/annotations.rake b/railties/lib/rails/tasks/annotations.rake new file mode 100644 index 0000000000..3a78de418a --- /dev/null +++ b/railties/lib/rails/tasks/annotations.rake @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require "rails/source_annotation_extractor" + +task notes: :environment do + Rails::SourceAnnotationExtractor::Annotation.notes_task_deprecation_warning + Rails::Command.invoke :notes +end + +namespace :notes do + ["OPTIMIZE", "FIXME", "TODO"].each do |annotation| + task annotation.downcase.intern => :environment do + Rails::SourceAnnotationExtractor::Annotation.notes_task_deprecation_warning + Rails::Command.invoke :notes, ["--annotations", annotation] + end + end + + task custom: :environment do + Rails::SourceAnnotationExtractor::Annotation.notes_task_deprecation_warning + Rails::Command.invoke :notes, ["--annotations", ENV["ANNOTATION"]] + end +end diff --git a/railties/lib/rails/tasks/dev.rake b/railties/lib/rails/tasks/dev.rake new file mode 100644 index 0000000000..716fb6a331 --- /dev/null +++ b/railties/lib/rails/tasks/dev.rake @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +require "rails/command" +require "active_support/deprecation" + +namespace :dev do + task cache: :environment do + ActiveSupport::Deprecation.warn("Using `bin/rake dev:cache` is deprecated and will be removed in Rails 6.1. Use `bin/rails dev:cache` instead.\n") + Rails::Command.invoke "dev:cache" + end +end diff --git a/railties/lib/rails/tasks/engine.rake b/railties/lib/rails/tasks/engine.rake new file mode 100644 index 0000000000..8d77904210 --- /dev/null +++ b/railties/lib/rails/tasks/engine.rake @@ -0,0 +1,88 @@ +# frozen_string_literal: true + +task "load_app" do + namespace :app do + load APP_RAKEFILE + + desc "Update some initially generated files" + task update: [ "update:bin" ] + + namespace :update do + require "rails/engine/updater" + # desc "Adds new executables to the engine bin/ directory" + task :bin do + Rails::Engine::Updater.run(:create_bin_files) + end + end + end + task environment: "app:environment" + + if !defined?(ENGINE_ROOT) || !ENGINE_ROOT + ENGINE_ROOT = find_engine_path(APP_RAKEFILE) + end +end + +def app_task(name) + task name => [:load_app, "app:db:#{name}"] +end + +namespace :db do + app_task "reset" + + desc "Migrate the database (options: VERSION=x, VERBOSE=false)." + app_task "migrate" + app_task "migrate:up" + app_task "migrate:down" + app_task "migrate:redo" + app_task "migrate:reset" + + desc "Display status of migrations" + app_task "migrate:status" + + desc "Create the database from config/database.yml for the current Rails.env (use db:create:all to create all databases in the config)" + app_task "create" + app_task "create:all" + + desc "Drops the database for the current Rails.env (use db:drop:all to drop all databases)" + app_task "drop" + app_task "drop:all" + + desc "Load fixtures into the current environment's database." + app_task "fixtures:load" + + desc "Rolls the schema back to the previous version (specify steps w/ STEP=n)." + app_task "rollback" + + desc "Create a db/schema.rb file that can be portably used against any database supported by Active Record" + app_task "schema:dump" + + desc "Load a schema.rb file into the database" + app_task "schema:load" + + desc "Load the seed data from db/seeds.rb" + app_task "seed" + + desc "Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the database first)" + app_task "setup" + + desc "Dump the database structure to an SQL file" + app_task "structure:dump" + + desc "Retrieves the current schema version number" + app_task "version" + + # desc 'Load the test schema' + app_task "test:prepare" +end + +def find_engine_path(path) + return File.expand_path(Dir.pwd) if path == "/" + + if Rails::Engine.find(path) + path + else + find_engine_path(File.expand_path("..", path)) + end +end + +Rake.application.invoke_task(:load_app) diff --git a/railties/lib/rails/tasks/framework.rake b/railties/lib/rails/tasks/framework.rake new file mode 100644 index 0000000000..1a3711c446 --- /dev/null +++ b/railties/lib/rails/tasks/framework.rake @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +namespace :app do + desc "Update configs and some other initially generated files (or use just update:configs or update:bin)" + task update: [ "update:configs", "update:bin", "update:upgrade_guide_info" ] + + desc "Applies the template supplied by LOCATION=(/path/to/template) or URL" + task template: :environment do + template = ENV["LOCATION"] + raise "No LOCATION value given. Please set LOCATION either as path to a file or a URL" if template.blank? + template = File.expand_path(template) if template !~ %r{\A[A-Za-z][A-Za-z0-9+\-\.]*://} + require "rails/generators" + require "rails/generators/rails/app/app_generator" + generator = Rails::Generators::AppGenerator.new [Rails.root], {}, { destination_root: Rails.root } + generator.apply template, verbose: false + end + + namespace :templates do + # desc "Copy all the templates from rails to the application directory for customization. Already existing local copies will be overwritten" + task :copy do + generators_lib = File.expand_path("../generators", __dir__) + project_templates = "#{Rails.root}/lib/templates" + + default_templates = { "erb" => %w{controller mailer scaffold}, + "rails" => %w{controller helper scaffold_controller assets} } + + default_templates.each do |type, names| + local_template_type_dir = File.join(project_templates, type) + mkdir_p local_template_type_dir, verbose: false + + names.each do |name| + dst_name = File.join(local_template_type_dir, name) + src_name = File.join(generators_lib, type, name, "templates") + cp_r src_name, dst_name, verbose: false + end + end + end + end + + namespace :update do + require "rails/app_updater" + + # desc "Update config files from your current rails install" + task :configs do + Rails::AppUpdater.invoke_from_app_generator :create_boot_file + Rails::AppUpdater.invoke_from_app_generator :update_config_files + end + + # desc "Adds new executables to the application bin/ directory" + task :bin do + Rails::AppUpdater.invoke_from_app_generator :update_bin_files + end + + task :upgrade_guide_info do + Rails::AppUpdater.invoke_from_app_generator :display_upgrade_guide_info + end + end +end diff --git a/railties/lib/rails/tasks/initializers.rake b/railties/lib/rails/tasks/initializers.rake new file mode 100644 index 0000000000..f108517d1d --- /dev/null +++ b/railties/lib/rails/tasks/initializers.rake @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require "rails/command" +require "active_support/deprecation" + +task initializers: :environment do + ActiveSupport::Deprecation.warn("Using `bin/rake initializers` is deprecated and will be removed in Rails 6.1. Use `bin/rails initializers` instead.\n") + Rails::Command.invoke "initializers" +end diff --git a/railties/lib/rails/tasks/log.rake b/railties/lib/rails/tasks/log.rake new file mode 100644 index 0000000000..ec56957204 --- /dev/null +++ b/railties/lib/rails/tasks/log.rake @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +namespace :log do + ## + # Truncates all/specified log files + # ENV['LOGS'] + # - defaults to all environments log files i.e. 'development,test,production' + # - ENV['LOGS']=all truncates all files i.e. log/*.log + # - ENV['LOGS']='test,development' truncates only specified files + desc "Truncates all/specified *.log files in log/ to zero bytes (specify which logs with LOGS=test,development)" + task :clear do + log_files.each do |file| + clear_log_file(file) + end + end + + def log_files + if ENV["LOGS"] == "all" + FileList["log/*.log"] + elsif ENV["LOGS"] + log_files_to_truncate(ENV["LOGS"]) + else + log_files_to_truncate(all_environments.join(",")) + end + end + + def log_files_to_truncate(envs) + envs.split(",") + .map { |file| "log/#{file.strip}.log" } + .select { |file| File.exist?(file) } + end + + def clear_log_file(file) + f = File.open(file, "w") + f.close + end + + def all_environments + Dir["config/environments/*.rb"].map { |fname| File.basename(fname, ".*") } + end +end diff --git a/railties/lib/rails/tasks/middleware.rake b/railties/lib/rails/tasks/middleware.rake new file mode 100644 index 0000000000..3a7f86fdcb --- /dev/null +++ b/railties/lib/rails/tasks/middleware.rake @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +desc "Prints out your Rack middleware stack" +task middleware: :environment do + Rails.configuration.middleware.each do |middleware| + puts "use #{middleware.inspect}" + end + puts "run #{Rails.application.class.name}.routes" +end diff --git a/railties/lib/rails/tasks/misc.rake b/railties/lib/rails/tasks/misc.rake new file mode 100644 index 0000000000..e7786aa622 --- /dev/null +++ b/railties/lib/rails/tasks/misc.rake @@ -0,0 +1,79 @@ +# frozen_string_literal: true + +desc "Generate a cryptographically secure secret key (this is typically used to generate a secret for cookie sessions)." +task :secret do + require "securerandom" + puts SecureRandom.hex(64) +end + +desc "List versions of all Rails frameworks and the environment" +task about: :environment do + puts Rails::Info +end + +namespace :time do + desc "List all time zones, list by two-letter country code (`rails time:zones[US]`), or list by UTC offset (`rails time:zones[-8]`)" + task :zones, :country_or_offset do |t, args| + zones, offset = ActiveSupport::TimeZone.all, nil + + if country_or_offset = args[:country_or_offset] + begin + zones = ActiveSupport::TimeZone.country_zones(country_or_offset) + rescue TZInfo::InvalidCountryCode + offset = country_or_offset + end + end + + build_time_zone_list zones, offset + end + + namespace :zones do + # desc 'Displays all time zones, also available: time:zones:us, time:zones:local -- filter with OFFSET parameter, e.g., OFFSET=-6' + task :all do + build_time_zone_list ActiveSupport::TimeZone.all + end + + # desc 'Displays names of US time zones recognized by the Rails TimeZone class, grouped by offset. Results can be filtered with optional OFFSET parameter, e.g., OFFSET=-6' + task :us do + build_time_zone_list ActiveSupport::TimeZone.us_zones + end + + # desc 'Displays names of time zones recognized by the Rails TimeZone class with the same offset as the system local time' + task :local do + require "active_support" + require "active_support/time" + + jan_offset = Time.now.beginning_of_year.utc_offset + jul_offset = Time.now.beginning_of_year.change(month: 7).utc_offset + offset = jan_offset < jul_offset ? jan_offset : jul_offset + + build_time_zone_list(ActiveSupport::TimeZone.all, offset) + end + + # to find UTC -06:00 zones, OFFSET can be set to either -6, -6:00 or 21600 + def build_time_zone_list(zones, offset = ENV["OFFSET"]) + require "active_support" + require "active_support/time" + if offset + offset = if offset.to_s.match(/(\+|-)?(\d+):(\d+)/) + sign = $1 == "-" ? -1 : 1 + hours, minutes = $2.to_f, $3.to_f + ((hours * 3600) + (minutes.to_f * 60)) * sign + elsif offset.to_f.abs <= 13 + offset.to_f * 3600 + else + offset.to_f + end + end + previous_offset = nil + zones.each do |zone| + if offset.nil? || offset == zone.utc_offset + puts "\n* UTC #{zone.formatted_offset} *" unless zone.utc_offset == previous_offset + puts zone.name + previous_offset = zone.utc_offset + end + end + puts "\n" + end + end +end diff --git a/railties/lib/rails/tasks/restart.rake b/railties/lib/rails/tasks/restart.rake new file mode 100644 index 0000000000..074e3e89a1 --- /dev/null +++ b/railties/lib/rails/tasks/restart.rake @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +desc "Restart app by touching tmp/restart.txt" +task :restart do + verbose(false) do + mkdir_p "tmp" + touch "tmp/restart.txt" + end +end diff --git a/railties/lib/rails/tasks/routes.rake b/railties/lib/rails/tasks/routes.rake new file mode 100644 index 0000000000..21ce900a8c --- /dev/null +++ b/railties/lib/rails/tasks/routes.rake @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +require "rails/command" +require "active_support/deprecation" + +task routes: :environment do + ActiveSupport::Deprecation.warn("Using `bin/rake routes` is deprecated and will be removed in Rails 6.1. Use `bin/rails routes` instead.\n") + Rails::Command.invoke "routes" +end diff --git a/railties/lib/rails/tasks/statistics.rake b/railties/lib/rails/tasks/statistics.rake new file mode 100644 index 0000000000..8cacf4a49f --- /dev/null +++ b/railties/lib/rails/tasks/statistics.rake @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +# While global constants are bad, many 3rd party tools depend on this one (e.g +# rspec-rails & cucumber-rails). So a deprecation warning is needed if we want +# to remove it. +STATS_DIRECTORIES = [ + %w(Controllers app/controllers), + %w(Helpers app/helpers), + %w(Jobs app/jobs), + %w(Models app/models), + %w(Mailers app/mailers), + %w(Channels app/channels), + %w(JavaScripts app/assets/javascripts), + %w(JavaScript app/javascript), + %w(Libraries lib/), + %w(APIs app/apis), + %w(Controller\ tests test/controllers), + %w(Helper\ tests test/helpers), + %w(Model\ tests test/models), + %w(Mailer\ tests test/mailers), + %w(Job\ tests test/jobs), + %w(Integration\ tests test/integration), + %w(System\ tests test/system), +].collect do |name, dir| + [ name, "#{File.dirname(Rake.application.rakefile_location)}/#{dir}" ] +end.select { |name, dir| File.directory?(dir) } + +desc "Report code statistics (KLOCs, etc) from the application or engine" +task :stats do + require "rails/code_statistics" + CodeStatistics.new(*STATS_DIRECTORIES).to_s +end diff --git a/railties/lib/rails/tasks/tmp.rake b/railties/lib/rails/tasks/tmp.rake new file mode 100644 index 0000000000..7340b41ee4 --- /dev/null +++ b/railties/lib/rails/tasks/tmp.rake @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +namespace :tmp do + desc "Clear cache, socket and screenshot files from tmp/ (narrow w/ tmp:cache:clear, tmp:sockets:clear, tmp:screenshots:clear)" + task clear: ["tmp:cache:clear", "tmp:sockets:clear", "tmp:screenshots:clear"] + + tmp_dirs = [ "tmp/cache", + "tmp/sockets", + "tmp/pids", + "tmp/cache/assets" ] + + tmp_dirs.each { |d| directory d } + + desc "Creates tmp directories for cache, sockets, and pids" + task create: tmp_dirs + + namespace :cache do + # desc "Clears all files and directories in tmp/cache" + task :clear do + rm_rf Dir["tmp/cache/[^.]*"], verbose: false + end + end + + namespace :sockets do + # desc "Clears all files in tmp/sockets" + task :clear do + rm Dir["tmp/sockets/[^.]*"], verbose: false + end + end + + namespace :pids do + # desc "Clears all files in tmp/pids" + task :clear do + rm Dir["tmp/pids/[^.]*"], verbose: false + end + end + + namespace :screenshots do + # desc "Clears all files in tmp/screenshots" + task :clear do + rm Dir["tmp/screenshots/[^.]*"], verbose: false + end + end +end diff --git a/railties/lib/rails/tasks/yarn.rake b/railties/lib/rails/tasks/yarn.rake new file mode 100644 index 0000000000..4fb8586b69 --- /dev/null +++ b/railties/lib/rails/tasks/yarn.rake @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +namespace :yarn do + desc "Install all JavaScript dependencies as specified via Yarn" + task :install do + # Install only production deps when for not usual envs. + valid_node_envs = %w[test development production] + node_env = ENV.fetch("NODE_ENV") do + rails_env = ENV["RAILS_ENV"] + valid_node_envs.include?(rails_env) ? rails_env : "production" + end + system({ "NODE_ENV" => node_env }, "#{Rails.root}/bin/yarn install --no-progress --frozen-lockfile") + end +end + +# Run Yarn prior to Sprockets assets precompilation, so dependencies are available for use. +if Rake::Task.task_defined?("assets:precompile") + Rake::Task["assets:precompile"].enhance [ "yarn:install" ] +end |