class ForestLiana::InstallGenerator

Public Instance Methods

install() click to toggle source
# File lib/generators/forest_liana/install_generator.rb, line 10
def install
  if ForestLiana.env_secret.present?
    puts "\nForest liana already installed on this app.\nHere is your current environment " +
      "secret: #{ForestLiana.env_secret}\nYou can update the config/secrets.yml file with the " +
      "new environment secret: #{env_secret}"
    return
  end

  route "mount ForestLiana::Engine => '/forest'"

  auth_secret = SecureRandom.urlsafe_base64

  puts "\nForest generated a random authentication secret to secure the " +
    "data access of your local project.\nYou can change it at any time in " +
    "your config/secrets.yml file.\n\n"

  # NOTICE: If it is a Rails 5.2+ apps, the secrets.yml file might not exist
  #         (replaced by credentials.yml.enc but still supported).
  if File.exist? 'config/secrets.yml'
    inject_into_file 'config/secrets.yml', after: "development:\n" do
      "  forest_env_secret: #{env_secret}\n" +
      "  forest_auth_secret: #{auth_secret}\n" +
      "  forest_application_url: #{application_url}\n"
    end

    inject_into_file 'config/secrets.yml', after: "staging:\n", force: true do
      "  forest_env_secret: <%= ENV[\"FOREST_ENV_SECRET\"] %>\n" +
      "  forest_auth_secret: <%= ENV[\"FOREST_AUTH_SECRET\"] %>\n" +
      "  forest_application_url: <%= ENV[\"FOREST_APPLICATION_URL\"] %>\n"
    end

    inject_into_file 'config/secrets.yml', after: "production:\n", force: true do
      "  forest_env_secret: <%= ENV[\"FOREST_ENV_SECRET\"] %>\n" +
      "  forest_auth_secret: <%= ENV[\"FOREST_AUTH_SECRET\"] %>\n" +
      "  forest_application_url: <%= ENV[\"FOREST_APPLICATION_URL\"] %>\n"
    end
  else
    create_file 'config/secrets.yml' do
      "development:\n" +
      "  forest_env_secret: #{env_secret}\n" +
      "  forest_auth_secret: #{auth_secret}\n" +
      "  forest_application_url: #{application_url}\n" +
      "staging:\n" +
      "  forest_env_secret: <%= ENV[\"FOREST_ENV_SECRET\"] %>\n" +
      "  forest_auth_secret: <%= ENV[\"FOREST_AUTH_SECRET\"] %>\n" +
      "  forest_application_url: <%= ENV[\"FOREST_APPLICATION_URL\"] %>\n" +
      "production:\n" +
      "  forest_env_secret: <%= ENV[\"FOREST_ENV_SECRET\"] %>\n" +
      "  forest_auth_secret: <%= ENV[\"FOREST_AUTH_SECRET\"] %>\n" +
      "  forest_application_url: <%= ENV[\"FOREST_APPLICATION_URL\"] %>\n"
    end
  end

  initializer 'forest_liana.rb' do
    "ForestLiana.env_secret = Rails.application.secrets.forest_env_secret" +
    "\nForestLiana.auth_secret = Rails.application.secrets.forest_auth_secret" +
    "\nForestLiana.application_url = Rails.application.secrets.forest_application_url"
  end
end