Skip to content

Commit c01a846

Browse files
Add script folder and generator (rails#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 <[email protected]>
1 parent 4454885 commit c01a846

File tree

8 files changed

+94
-0
lines changed

8 files changed

+94
-0
lines changed

guides/source/getting_started.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ of the files and folders that Rails creates by default:
187187
|public/|Contains static files and compiled assets. When your app is running, this directory will be exposed as-is.|
188188
|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.|
189189
|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.|
190+
|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).|
190191
|storage/|Active Storage files for Disk Service. This is covered in [Active Storage Overview](active_storage_overview.html).|
191192
|test/|Unit tests, fixtures, and other test apparatus. These are covered in [Testing Rails Applications](testing.html).|
192193
|tmp/|Temporary files (like cache and pid files).|

railties/CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
* Add script folder and generator
2+
3+
Add a new script default folder to hold your one-off or general purpose
4+
scripts, such as data migration scripts, cleanup scripts, etc.
5+
6+
A new script generator allows you to create such scripts:
7+
8+
`rails generate script my_script`
9+
10+
You can run the generated script using:
11+
12+
`ruby script/my_script.rb`
13+
14+
*Jerome Dalbert*, *Haroon Ahmed*
15+
116
* Enable tracking route source locations only when using routes command. Previously,
217
it was enabled in development mode, but is only needed for `bin/rails routes -E`.
318

railties/lib/rails/generators/rails/app/app_generator.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ def public_directory
232232
directory "public", "public", recursive: false
233233
end
234234

235+
def script
236+
empty_directory_with_keep_file "script"
237+
end
238+
235239
def storage
236240
empty_directory_with_keep_file "storage"
237241
empty_directory_with_keep_file "tmp/storage"
@@ -445,6 +449,11 @@ def create_public_files
445449
build(:public_directory)
446450
end
447451

452+
def create_script_folder
453+
return if options[:dummy_app]
454+
build(:script)
455+
end
456+
448457
def create_tmp_files
449458
build(:tmp)
450459
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Description:
2+
Generate a one-off or general purpose script, such as a data migration
3+
script, cleanup script, etc.
4+
5+
Example:
6+
`bin/rails generate script my_script`
7+
8+
This will create:
9+
script/my_script.rb
10+
11+
You can run the script using:
12+
`ruby script/my_script.rb`
13+
14+
You can specify a folder:
15+
`bin/rails generate script cleanup/my_script`
16+
17+
This will create:
18+
script/cleanup/my_script.rb
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
require "rails/generators/named_base"
4+
5+
module Rails
6+
module Generators
7+
class ScriptGenerator < NamedBase
8+
def generate_script
9+
template("script.rb.tt", "script/#{file_path}.rb")
10+
end
11+
12+
private
13+
def depth
14+
class_path.size + 1
15+
end
16+
end
17+
end
18+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require_relative "<%= '../' * depth %>config/environment"
2+
3+
# Your code goes here

railties/test/generators/app_generator_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
public/icon.png
7171
public/icon.svg
7272
public/robots.txt
73+
script/.keep
7374
storage/.keep
7475
test/application_system_test_case.rb
7576
test/channels/application_cable/connection_test.rb
@@ -1048,6 +1049,7 @@ def test_create_keeps
10481049
lib/tasks
10491050
lib/assets
10501051
log
1052+
script
10511053
test/fixtures/files
10521054
test/controllers
10531055
test/mailers
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
require "generators/generators_test_helper"
4+
require "rails/generators/rails/script/script_generator"
5+
6+
module Rails
7+
module Generators
8+
class ScriptGeneratorTest < Rails::Generators::TestCase
9+
include GeneratorsTestHelper
10+
11+
def test_generate_script
12+
run_generator ["my_script"]
13+
14+
assert_file "script/my_script.rb" do |script|
15+
assert_match('require_relative "../config/environment"', script)
16+
end
17+
end
18+
19+
def test_generate_script_with_folder
20+
run_generator ["my_folder/my_script"]
21+
22+
assert_file "script/my_folder/my_script.rb" do |script|
23+
assert_match('require_relative "../../config/environment"', script)
24+
end
25+
end
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)