class Rails::Generators::CredentialsGenerator

:nodoc:

def add_credentials_file

def add_credentials_file
  in_root do
    return if File.exist?(content_path)
    say "Adding #{content_path} to store encrypted credentials."
    say ""
    content = render_template_to_encrypted_file
    say "The following content has been encrypted with the Rails master key:"
    say ""
    say content, :on_green
    say ""
    say "You can edit encrypted credentials with `bin/rails credentials:edit`."
    say ""
  end
end

def encrypted_file

def encrypted_file
  ActiveSupport::EncryptedConfiguration.new(
    config_path: content_path,
    key_path: key_path,
    env_key: "RAILS_MASTER_KEY",
    raise_if_missing_key: true
  )
end

def render_template_to_encrypted_file

def render_template_to_encrypted_file
  empty_directory File.dirname(content_path)
  content = nil
  encrypted_file.change do |tmp_path|
    template("credentials.yml", tmp_path, force: true, verbose: false) do |rendered|
      content = rendered
    end
  end
  content
end

def secret_key_base

def secret_key_base
  @secret_key_base ||= SecureRandom.hex(64)
end