Add script folder and generator (#52335)

Add a new script default folder to hold one-off or general purpose
scripts, such as data migration scripts, cleanup scripts, etc. Also add
a script generator to create such scripts.

Co-authored-by: Haroon Ahmed <haroon.ahmed25@gmail.com>
This commit is contained in:
Jerome Dalbert 2024-07-16 03:28:20 -07:00 committed by GitHub
parent 44548856c4
commit c01a846658
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 94 additions and 0 deletions

@ -187,6 +187,7 @@ of the files and folders that Rails creates by default:
|public/|Contains static files and compiled assets. When your app is running, this directory will be exposed as-is.|
|Rakefile|This file locates and loads tasks that can be run from the command line. The task definitions are defined throughout the components of Rails. Rather than changing `Rakefile`, you should add your own tasks by adding files to the `lib/tasks` directory of your application.|
|README.md|This is a brief instruction manual for your application. You should edit this file to tell others what your application does, how to set it up, and so on.|
|script/|Contains one-off or general purpose [scripts](https://github.com/rails/rails/blob/main/railties/lib/rails/generators/rails/script/USAGE) and [benchmarks](https://github.com/rails/rails/blob/main/railties/lib/rails/generators/rails/benchmark/USAGE).|
|storage/|Active Storage files for Disk Service. This is covered in [Active Storage Overview](active_storage_overview.html).|
|test/|Unit tests, fixtures, and other test apparatus. These are covered in [Testing Rails Applications](testing.html).|
|tmp/|Temporary files (like cache and pid files).|

@ -1,3 +1,18 @@
* Add script folder and generator
Add a new script default folder to hold your one-off or general purpose
scripts, such as data migration scripts, cleanup scripts, etc.
A new script generator allows you to create such scripts:
`rails generate script my_script`
You can run the generated script using:
`ruby script/my_script.rb`
*Jerome Dalbert*, *Haroon Ahmed*
* Enable tracking route source locations only when using routes command. Previously,
it was enabled in development mode, but is only needed for `bin/rails routes -E`.

@ -232,6 +232,10 @@ def public_directory
directory "public", "public", recursive: false
end
def script
empty_directory_with_keep_file "script"
end
def storage
empty_directory_with_keep_file "storage"
empty_directory_with_keep_file "tmp/storage"
@ -445,6 +449,11 @@ def create_public_files
build(:public_directory)
end
def create_script_folder
return if options[:dummy_app]
build(:script)
end
def create_tmp_files
build(:tmp)
end

@ -0,0 +1,18 @@
Description:
Generate a one-off or general purpose script, such as a data migration
script, cleanup script, etc.
Example:
`bin/rails generate script my_script`
This will create:
script/my_script.rb
You can run the script using:
`ruby script/my_script.rb`
You can specify a folder:
`bin/rails generate script cleanup/my_script`
This will create:
script/cleanup/my_script.rb

@ -0,0 +1,18 @@
# frozen_string_literal: true
require "rails/generators/named_base"
module Rails
module Generators
class ScriptGenerator < NamedBase
def generate_script
template("script.rb.tt", "script/#{file_path}.rb")
end
private
def depth
class_path.size + 1
end
end
end
end

@ -0,0 +1,3 @@
require_relative "<%= '../' * depth %>config/environment"
# Your code goes here

@ -70,6 +70,7 @@
public/icon.png
public/icon.svg
public/robots.txt
script/.keep
storage/.keep
test/application_system_test_case.rb
test/channels/application_cable/connection_test.rb
@ -1048,6 +1049,7 @@ def test_create_keeps
lib/tasks
lib/assets
log
script
test/fixtures/files
test/controllers
test/mailers

@ -0,0 +1,28 @@
# frozen_string_literal: true
require "generators/generators_test_helper"
require "rails/generators/rails/script/script_generator"
module Rails
module Generators
class ScriptGeneratorTest < Rails::Generators::TestCase
include GeneratorsTestHelper
def test_generate_script
run_generator ["my_script"]
assert_file "script/my_script.rb" do |script|
assert_match('require_relative "../config/environment"', script)
end
end
def test_generate_script_with_folder
run_generator ["my_folder/my_script"]
assert_file "script/my_folder/my_script.rb" do |script|
assert_match('require_relative "../../config/environment"', script)
end
end
end
end
end