Class: Rails::AppBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/generators/rails/app/app_generator.rb

Overview

The application builder allows you to override elements of the application generator without being forced to reverse the operations of the default generator.

This allows you to override entire operations, like the creation of the Gemfile, README, or JavaScript files, without needing to know exactly what those operations do so you can create another template action.

class CustomAppBuilder < Rails::AppBuilder
  def test
    @generator.gem "rspec-rails", group: [:development, :test]
    run "bundle install"
    generate "rspec:install"
  end
end

Instance Method Summary collapse

Instance Method Details

#appObject



82
83
84
85
86
87
88
89
# File 'lib/rails/generators/rails/app/app_generator.rb', line 82

def app
  directory "app"

  empty_directory_with_keep_file "app/assets/images"

  keep_file  "app/controllers/concerns"
  keep_file  "app/models/concerns"
end

#binObject



91
92
93
94
95
96
# File 'lib/rails/generators/rails/app/app_generator.rb', line 91

def bin
  directory "bin" do |content|
    "#{shebang}\n" + content
  end
  chmod "bin", 0755 & ~File.umask, verbose: false
end

#bin_when_updatingObject



98
99
100
# File 'lib/rails/generators/rails/app/app_generator.rb', line 98

def bin_when_updating
  bin
end

#configObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rails/generators/rails/app/app_generator.rb', line 102

def config
  empty_directory "config"

  inside "config" do
    template "routes.rb" unless options[:updating]
    template "application.rb"
    template "environment.rb"
    template "cable.yml" unless options[:updating] || options[:skip_action_cable]
    template "puma.rb"   unless options[:updating]
    template "storage.yml" unless options[:updating] || skip_active_storage?

    directory "environments"
    directory "initializers"
    directory "locales" unless options[:updating]
  end
end

#config_target_versionObject



252
253
254
# File 'lib/rails/generators/rails/app/app_generator.rb', line 252

def config_target_version
  defined?(@config_target_version) ? @config_target_version : Rails::VERSION::STRING.to_f
end

#config_when_updatingObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rails/generators/rails/app/app_generator.rb', line 119

def config_when_updating
  action_cable_config_exist       = File.exist?("config/cable.yml")
  active_storage_config_exist     = File.exist?("config/storage.yml")
  rack_cors_config_exist          = File.exist?("config/initializers/cors.rb")
  assets_config_exist             = File.exist?("config/initializers/assets.rb")
  asset_manifest_exist            = File.exist?("app/assets/config/manifest.js")
  asset_app_stylesheet_exist      = File.exist?("app/assets/stylesheets/application.css")
  csp_config_exist                = File.exist?("config/initializers/content_security_policy.rb")
  permissions_policy_config_exist = File.exist?("config/initializers/permissions_policy.rb")

  @config_target_version = Rails.application.config.loaded_config_version || "5.0"

  config

  if !options[:skip_action_cable] && !action_cable_config_exist
    template "config/cable.yml"
  end

  if !skip_active_storage? && !active_storage_config_exist
    template "config/storage.yml"
  end

  if skip_sprockets? && skip_propshaft? && !assets_config_exist
    remove_file "config/initializers/assets.rb"
  end

  if skip_sprockets? && !asset_manifest_exist
    remove_file "app/assets/config/manifest.js"
  end

  if skip_sprockets? && !asset_app_stylesheet_exist
    remove_file "app/assets/stylesheets/application.css"
  end

  unless rack_cors_config_exist
    remove_file "config/initializers/cors.rb"
  end

  if options[:api]
    unless csp_config_exist
      remove_file "config/initializers/content_security_policy.rb"
    end

    unless permissions_policy_config_exist
      remove_file "config/initializers/permissions_policy.rb"
    end
  end

  if !skip_sprockets?
    insert_into_file "config/application.rb", %(require "sprockets/railtie"), after: /require\(["']rails\/all["']\)\n/
  end
end

#configruObject



61
62
63
# File 'lib/rails/generators/rails/app/app_generator.rb', line 61

def configru
  template "config.ru"
end

#credentialsObject



181
182
183
184
185
186
# File 'lib/rails/generators/rails/app/app_generator.rb', line 181

def credentials
  return if options[:pretend] || options[:dummy_app]

  require "rails/generators/rails/credentials/credentials_generator"
  Rails::Generators::CredentialsGenerator.new([], quiet: options[:quiet]).add_credentials_file_silently
end

#database_ymlObject



188
189
190
# File 'lib/rails/generators/rails/app/app_generator.rb', line 188

def database_yml
  template "config/databases/#{options[:database]}.yml", "config/database.yml"
end

#dbObject



192
193
194
# File 'lib/rails/generators/rails/app/app_generator.rb', line 192

def db
  directory "db"
end

#db_when_updatingObject



196
197
198
199
200
201
202
# File 'lib/rails/generators/rails/app/app_generator.rb', line 196

def db_when_updating
  path = File.expand_path("db/schema.rb", destination_root)

  if File.exist?(path)
    gsub_file("db/schema.rb", /ActiveRecord::Schema\.define/, "ActiveRecord::Schema[6.1].define")
  end
end

#gemfileObject



57
58
59
# File 'lib/rails/generators/rails/app/app_generator.rb', line 57

def gemfile
  template "Gemfile"
end

#gitattributesObject



69
70
71
# File 'lib/rails/generators/rails/app/app_generator.rb', line 69

def gitattributes
  template "gitattributes", ".gitattributes"
end

#gitignoreObject



65
66
67
# File 'lib/rails/generators/rails/app/app_generator.rb', line 65

def gitignore
  template "gitignore", ".gitignore"
end

#libObject



204
205
206
207
208
# File 'lib/rails/generators/rails/app/app_generator.rb', line 204

def lib
  empty_directory "lib"
  empty_directory_with_keep_file "lib/tasks"
  empty_directory_with_keep_file "lib/assets"
end

#logObject



210
211
212
# File 'lib/rails/generators/rails/app/app_generator.rb', line 210

def log
  empty_directory_with_keep_file "log"
end

#master_keyObject



172
173
174
175
176
177
178
179
# File 'lib/rails/generators/rails/app/app_generator.rb', line 172

def master_key
  return if options[:pretend] || options[:dummy_app]

  require "rails/generators/rails/master_key/master_key_generator"
  master_key_generator = Rails::Generators::MasterKeyGenerator.new([], quiet: options[:quiet], force: options[:force])
  master_key_generator.add_master_key_file_silently
  master_key_generator.ignore_master_key_file_silently
end

#public_directoryObject



214
215
216
# File 'lib/rails/generators/rails/app/app_generator.rb', line 214

def public_directory
  directory "public", "public", recursive: false
end

#rakefileObject



45
46
47
# File 'lib/rails/generators/rails/app/app_generator.rb', line 45

def rakefile
  template "Rakefile"
end

#readmeObject



49
50
51
# File 'lib/rails/generators/rails/app/app_generator.rb', line 49

def readme
  copy_file "README.md", "README.md"
end

#ruby_versionObject



53
54
55
# File 'lib/rails/generators/rails/app/app_generator.rb', line 53

def ruby_version
  template "ruby-version", ".ruby-version"
end

#storageObject



218
219
220
221
# File 'lib/rails/generators/rails/app/app_generator.rb', line 218

def storage
  empty_directory_with_keep_file "storage"
  empty_directory_with_keep_file "tmp/storage"
end

#system_testObject



235
236
237
238
239
# File 'lib/rails/generators/rails/app/app_generator.rb', line 235

def system_test
  empty_directory_with_keep_file "test/system"

  template "test/application_system_test_case.rb"
end

#testObject



223
224
225
226
227
228
229
230
231
232
233
# File 'lib/rails/generators/rails/app/app_generator.rb', line 223

def test
  empty_directory_with_keep_file "test/fixtures/files"
  empty_directory_with_keep_file "test/controllers"
  empty_directory_with_keep_file "test/mailers"
  empty_directory_with_keep_file "test/models"
  empty_directory_with_keep_file "test/helpers"
  empty_directory_with_keep_file "test/integration"

  template "test/channels/application_cable/connection_test.rb"
  template "test/test_helper.rb"
end

#tmpObject



241
242
243
244
245
246
# File 'lib/rails/generators/rails/app/app_generator.rb', line 241

def tmp
  empty_directory_with_keep_file "tmp"
  empty_directory_with_keep_file "tmp/pids"
  empty_directory "tmp/cache"
  empty_directory "tmp/cache/assets"
end

#vendorObject



248
249
250
# File 'lib/rails/generators/rails/app/app_generator.rb', line 248

def vendor
  empty_directory_with_keep_file "vendor"
end

#version_controlObject



73
74
75
76
77
78
79
80
# File 'lib/rails/generators/rails/app/app_generator.rb', line 73

def version_control
  if !options[:skip_git] && !options[:pretend]
    run "git init", capture: options[:quiet], abort_on_failure: false
    if user_default_branch.strip.empty?
      `git symbolic-ref HEAD refs/heads/main`
    end
  end
end