Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --environment flag to dsl/gem commands #979

Merged
merged 2 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ Options:
# Default: sorbet/rbi/dsl
[--rbi-max-line-length=N] # Set the max line length of generated RBIs. Signatures longer than the max line length will be wrapped
# Default: 120
-e, [--environment=ENVIRONMENT] # The Rack/Rails environment to use when generating RBIs
# Default: development
-c, [--config=<config file path>] # Path to the Tapioca configuration file
# Default: sorbet/tapioca/config.yml
-V, [--verbose], [--no-verbose] # Verbose output for debugging purposes
Expand Down Expand Up @@ -357,6 +359,8 @@ Options:
# Default: 1
[--rbi-max-line-length=N] # Set the max line length of generated RBIs. Signatures longer than the max line length will be wrapped
# Default: 120
-e, [--environment=ENVIRONMENT] # The Rack/Rails environment to use when generating RBIs
# Default: development
-c, [--config=<config file path>] # Path to the Tapioca configuration file
# Default: sorbet/tapioca/config.yml
-V, [--verbose], [--no-verbose] # Verbose output for debugging purposes
Expand Down Expand Up @@ -696,6 +700,7 @@ dsl:
quiet: false
workers: 1
rbi_max_line_length: 120
environment: development
gem:
outdir: sorbet/rbi/gems
file_header: true
Expand All @@ -712,6 +717,7 @@ gem:
auto_strictness: true
dsl_dir: sorbet/rbi/dsl
rbi_max_line_length: 120
environment: development
check_shims:
gem_rbi_dir: sorbet/rbi/gems
dsl_rbi_dir: sorbet/rbi/dsl
Expand Down
15 changes: 15 additions & 0 deletions lib/tapioca/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Cli < Thor
include ConfigHelper
include SorbetHelper
include ShimsHelper
include EnvHelper

FILE_HEADER_OPTION_DESC = "Add a \"This file is generated\" header on top of each generated RBI file"

Expand Down Expand Up @@ -102,7 +103,14 @@ def todo
type: :numeric,
desc: "Set the max line length of generated RBIs. Signatures longer than the max line length will be wrapped",
default: 120
option :environment,
aliases: ["-e"],
type: :string,
desc: "The Rack/Rails environment to use when generating RBIs",
default: "development"
def dsl(*constants)
set_environment

command = Commands::Dsl.new(
requested_constants: constants,
outpath: Pathname.new(options[:outdir]),
Expand Down Expand Up @@ -196,8 +204,15 @@ def dsl(*constants)
type: :numeric,
desc: "Set the max line length of generated RBIs. Signatures longer than the max line length will be wrapped",
default: 120
option :environment,
aliases: ["-e"],
type: :string,
desc: "The Rack/Rails environment to use when generating RBIs",
default: "development"
def gem(*gems)
Tapioca.silence_warnings do
set_environment

all = options[:all]
verify = options[:verify]

Expand Down
16 changes: 16 additions & 0 deletions lib/tapioca/helpers/env_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# typed: true
# frozen_string_literal: true

module Tapioca
module EnvHelper
extend T::Sig
extend T::Helpers

requires_ancestor { Thor }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't it be under cli_helper if we depend on Thor to get the options?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because CliHelper is messed up. It requires Thor::Shell as an ancestor, which doesn't expose options, and we can't change that to Thor since it is included by the base command as well 😱 .

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Urgh alright, let's keep a note that we need to fix this as well 😱


sig { void }
def set_environment
ENV["RAILS_ENV"] = ENV["RACK_ENV"] = options[:environment]
end
end
end
1 change: 1 addition & 0 deletions lib/tapioca/internal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
require "tapioca/helpers/signatures_helper"
require "tapioca/helpers/rbi_helper"
require "tapioca/helpers/shims_helper"
require "tapioca/helpers/env_helper"

require "tapioca/repo_index"
require "tapioca/gemfile"
Expand Down
96 changes: 96 additions & 0 deletions spec/tapioca/cli/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,102 @@ def bar(&block); end
refute_success_status(result)
end
end

describe "environment" do
before(:all) do
@project.tapioca("init")

@project.write("lib/post.rb", <<~RB)
require "smart_properties"
paracycle marked this conversation as resolved.
Show resolved Hide resolved

$stderr.puts "RAILS ENVIRONMENT: \#{ENV["RAILS_ENV"]}"
$stderr.puts "RACK ENVIRONMENT: \#{ENV["RACK_ENV"]}"

class Post
include SmartProperties
property :title, accepts: String
end

if ENV["RAILS_ENV"] == "development"
class Post::Rails < Post
end
end

if ENV["RACK_ENV"] == "development"
class Post::Rack < Post
end
end
RB

@project.require_real_gem("smart_properties", "1.15.0")
@project.bundle_install
end

it "must default to `development` as environment" do
result = @project.tapioca("dsl")

assert_equal(<<~OUT, result.err)
RAILS ENVIRONMENT: development
RACK ENVIRONMENT: development
OUT

assert_project_file_equal("sorbet/rbi/dsl/post/rack.rbi", <<~RBI)
# typed: true

# DO NOT EDIT MANUALLY
# This is an autogenerated file for dynamic methods in `Post::Rack`.
# Please instead update this file by running `bin/tapioca dsl Post::Rack`.

class Post::Rack
include SmartPropertiesGeneratedMethods

module SmartPropertiesGeneratedMethods
sig { returns(T.nilable(::String)) }
def title; end

sig { params(title: T.nilable(::String)).returns(T.nilable(::String)) }
def title=(title); end
end
end
RBI

assert_project_file_equal("sorbet/rbi/dsl/post/rails.rbi", <<~RBI)
# typed: true

# DO NOT EDIT MANUALLY
# This is an autogenerated file for dynamic methods in `Post::Rails`.
# Please instead update this file by running `bin/tapioca dsl Post::Rails`.

class Post::Rails
include SmartPropertiesGeneratedMethods

module SmartPropertiesGeneratedMethods
sig { returns(T.nilable(::String)) }
def title; end

sig { params(title: T.nilable(::String)).returns(T.nilable(::String)) }
def title=(title); end
end
end
RBI

assert_success_status(result)
end

it "must accept another value for environment" do
result = @project.tapioca("dsl --environment staging")

assert_success_status(result)

refute_project_file_exist("sorbet/rbi/dsl/post/rack.rbi")
refute_project_file_exist("sorbet/rbi/dsl/post/rails.rbi")

assert_equal(<<~OUT, result.err)
RAILS ENVIRONMENT: staging
RACK ENVIRONMENT: staging
OUT
end
end
end
end
end
38 changes: 38 additions & 0 deletions spec/tapioca/cli/gem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1717,6 +1717,44 @@ class << self
assert_success_status(result)
end
end

describe "environment" do
before(:all) do
@project.tapioca("init")

foo = mock_gem("foo", "0.0.1") do
write("lib/foo.rb", <<~RB)
$stderr.puts "RAILS ENVIRONMENT: \#{ENV["RAILS_ENV"]}"
$stderr.puts "RACK ENVIRONMENT: \#{ENV["RACK_ENV"]}"
RB
end

@project.require_mock_gem(foo, require: false)
@project.bundle_install
end

it "must default to `development` as environment" do
result = @project.tapioca("gem foo")

assert_success_status(result)

assert_equal(<<~OUT, result.err)
RAILS ENVIRONMENT: development
RACK ENVIRONMENT: development
OUT
end

it "must accept another value for environment" do
result = @project.tapioca("gem foo --environment staging")

assert_success_status(result)

assert_equal(<<~OUT, result.err)
RAILS ENVIRONMENT: staging
RACK ENVIRONMENT: staging
OUT
end
end
end
end
end