Skip to content

Commit

Permalink
Allow Tapioca commands to accept environment variables
Browse files Browse the repository at this point in the history
This change allows Tapioca commands to accept environment variables via the command line or the configuration file. This is useful for setting up the environment for the Tapioca commands, such as setting the custom environment variables that affect how an application is loaded.
  • Loading branch information
paracycle committed Jul 12, 2024
1 parent b3cec5b commit cc5011f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 3 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Commands:
Options:
-c, [--config=<config file path>] # Path to the Tapioca configuration file
# Default: sorbet/tapioca/config.yml
[--env=key:value] # Environment variables to set before running Tapioca
-V, [--verbose], [--no-verbose], [--skip-verbose] # Verbose output for debugging purposes
# Default: false

Expand Down Expand Up @@ -122,6 +123,7 @@ Usage:
Options:
-c, [--config=<config file path>] # Path to the Tapioca configuration file
# Default: sorbet/tapioca/config.yml
[--env=key:value] # Environment variables to set before running Tapioca
-V, [--verbose], [--no-verbose], [--skip-verbose] # Verbose output for debugging purposes
# Default: false

Expand Down Expand Up @@ -202,6 +204,7 @@ Options:
# Default: true
-c, [--config=<config file path>] # Path to the Tapioca configuration file
# Default: sorbet/tapioca/config.yml
[--env=key:value] # Environment variables to set before running Tapioca
-V, [--verbose], [--no-verbose], [--skip-verbose] # Verbose output for debugging purposes
# Default: false

Expand Down Expand Up @@ -376,6 +379,7 @@ Options:
--typed, -t, [--typed-overrides=gem:level [gem:level ...]] # Override for typed sigils for pulled annotations
-c, [--config=<config file path>] # Path to the Tapioca configuration file
# Default: sorbet/tapioca/config.yml
[--env=key:value] # Environment variables to set before running Tapioca
-V, [--verbose], [--no-verbose], [--skip-verbose] # Verbose output for debugging purposes
# Default: false
Expand Down Expand Up @@ -505,6 +509,7 @@ Options:
[--compiler-options=key:value] # Options to pass to the DSL compilers
-c, [--config=<config file path>] # Path to the Tapioca configuration file
# Default: sorbet/tapioca/config.yml
[--env=key:value] # Environment variables to set before running Tapioca
-V, [--verbose], [--no-verbose], [--skip-verbose] # Verbose output for debugging purposes
# Default: false
Expand Down Expand Up @@ -900,6 +905,7 @@ Options:
-w, [--workers=N] # Number of parallel workers (default: auto)
-c, [--config=<config file path>] # Path to the Tapioca configuration file
# Default: sorbet/tapioca/config.yml
[--env=key:value] # Environment variables to set before running Tapioca
-V, [--verbose], [--no-verbose], [--skip-verbose] # Verbose output for debugging purposes
# Default: false
Expand Down Expand Up @@ -938,11 +944,17 @@ The full configuration file, with each option and its default value, would look
```yaml
---
require:
env: {}
verbose: false
postrequire: sorbet/tapioca/require.rb
todo:
env: {}
verbose: false
todo_file: sorbet/rbi/todo.rbi
file_header: true
dsl:
env: {}
verbose: false
outdir: sorbet/rbi/dsl
file_header: true
only: []
Expand All @@ -958,6 +970,8 @@ dsl:
skip_constant: []
compiler_options: {}
gem:
env: {}
verbose: false
outdir: sorbet/rbi/gems
file_header: true
all: false
Expand All @@ -978,6 +992,8 @@ gem:
environment: development
halt_upon_load_error: true
check_shims:
env: {}
verbose: false
gem_rbi_dir: sorbet/rbi/gems
dsl_rbi_dir: sorbet/rbi/dsl
shim_rbi_dir: sorbet/rbi/shims
Expand All @@ -986,6 +1002,8 @@ check_shims:
payload: true
workers: 1
annotations:
env: {}
verbose: false
sources:
- https://raw.githubusercontent.com/Shopify/rbi-central/main
netrc: true
Expand Down
4 changes: 4 additions & 0 deletions lib/tapioca/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class Cli < Thor
type: :string,
desc: "Path to the Tapioca configuration file",
default: TAPIOCA_CONFIG_FILE
class_option :env,
type: :hash,
desc: "Environment variables to set before running Tapioca",
repeatable: true
class_option :verbose,
aliases: ["-V"],
type: :boolean,
Expand Down
1 change: 1 addition & 0 deletions lib/tapioca/helpers/config_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def validate_config!(config_file, config)
).returns(T::Array[ConfigError])
end
def validate_config_options(command_options, config_key, config_options)
command_options = T.unsafe(self.class).class_options.merge(command_options)
config_options.filter_map do |config_option_key, config_option_value|
command_option = command_options[config_option_key.to_sym]
error_msg = "unknown option `#{config_option_key}` for key `#{config_key}`"
Expand Down
12 changes: 11 additions & 1 deletion lib/tapioca/helpers/env_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@ module EnvHelper

requires_ancestor { Thor }

private

sig { params(options: T::Hash[Symbol, T.untyped]).void }
def set_environment(options) # rubocop:disable Naming/AccessorMethodName
ENV["RAILS_ENV"] = ENV["RACK_ENV"] = options[:environment]
set_all_environment_variables(options[:env])
ENV["RAILS_ENV"] = ENV["RACK_ENV"] = options[:environment] if options[:environment]
ENV["RUBY_DEBUG_LAZY"] = "1"
end

sig { params(options: T.nilable(T::Hash[String, String])).void }
def set_all_environment_variables(options) # rubocop:disable Naming/AccessorMethodName
(options || {}).each do |key, value|
ENV[key] = value
end
end
end
end
11 changes: 11 additions & 0 deletions spec/tapioca/cli/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2677,6 +2677,17 @@ def title=(title); end
RACK ENVIRONMENT: staging
OUT
end

it "must accept and set custom environment variables" do
@project.write!("lib/post.rb", <<~RB)
$stderr.puts "ENVIRONMENT: \#{ENV.to_h.inspect}"
RB

result = @project.tapioca("dsl --env Foo:Bar --env Baz:1")

assert_stderr_includes(result, '"Foo"=>"Bar"')
assert_stderr_includes(result, '"Baz"=>"1"')
end
end

describe "list compilers" do
Expand Down
5 changes: 3 additions & 2 deletions tasks/readme.rake
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ task :readme do
end

def skip_option?(option)
option.name == "auth"
ignored_options = ["auth", "config"]
ignored_options.include?(option.name)
end

def option_value(option)
Expand All @@ -54,7 +55,7 @@ task :readme do
end

def command_options(command)
command.options.filter_map do |name, opt|
Tapioca::Cli.class_options.merge(command.options).filter_map do |name, opt|
next if skip_option?(opt)

[name.to_s, option_value(opt)]
Expand Down

0 comments on commit cc5011f

Please sign in to comment.