Skip to content

Commit ebf1e69

Browse files
committed
Allow Tapioca commands to accept environment variables
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.
1 parent b3cec5b commit ebf1e69

File tree

6 files changed

+48
-3
lines changed

6 files changed

+48
-3
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Commands:
8888
Options:
8989
-c, [--config=<config file path>] # Path to the Tapioca configuration file
9090
# Default: sorbet/tapioca/config.yml
91+
[--env=key:value] # Environment variables to set before running Tapioca
9192
-V, [--verbose], [--no-verbose], [--skip-verbose] # Verbose output for debugging purposes
9293
# Default: false
9394

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

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

@@ -376,6 +379,7 @@ Options:
376379
--typed, -t, [--typed-overrides=gem:level [gem:level ...]] # Override for typed sigils for pulled annotations
377380
-c, [--config=<config file path>] # Path to the Tapioca configuration file
378381
# Default: sorbet/tapioca/config.yml
382+
[--env=key:value] # Environment variables to set before running Tapioca
379383
-V, [--verbose], [--no-verbose], [--skip-verbose] # Verbose output for debugging purposes
380384
# Default: false
381385
@@ -505,6 +509,7 @@ Options:
505509
[--compiler-options=key:value] # Options to pass to the DSL compilers
506510
-c, [--config=<config file path>] # Path to the Tapioca configuration file
507511
# Default: sorbet/tapioca/config.yml
512+
[--env=key:value] # Environment variables to set before running Tapioca
508513
-V, [--verbose], [--no-verbose], [--skip-verbose] # Verbose output for debugging purposes
509514
# Default: false
510515
@@ -900,6 +905,7 @@ Options:
900905
-w, [--workers=N] # Number of parallel workers (default: auto)
901906
-c, [--config=<config file path>] # Path to the Tapioca configuration file
902907
# Default: sorbet/tapioca/config.yml
908+
[--env=key:value] # Environment variables to set before running Tapioca
903909
-V, [--verbose], [--no-verbose], [--skip-verbose] # Verbose output for debugging purposes
904910
# Default: false
905911
@@ -938,11 +944,17 @@ The full configuration file, with each option and its default value, would look
938944
```yaml
939945
---
940946
require:
947+
env: {}
948+
verbose: false
941949
postrequire: sorbet/tapioca/require.rb
942950
todo:
951+
env: {}
952+
verbose: false
943953
todo_file: sorbet/rbi/todo.rbi
944954
file_header: true
945955
dsl:
956+
env: {}
957+
verbose: false
946958
outdir: sorbet/rbi/dsl
947959
file_header: true
948960
only: []
@@ -958,6 +970,8 @@ dsl:
958970
skip_constant: []
959971
compiler_options: {}
960972
gem:
973+
env: {}
974+
verbose: false
961975
outdir: sorbet/rbi/gems
962976
file_header: true
963977
all: false
@@ -978,6 +992,8 @@ gem:
978992
environment: development
979993
halt_upon_load_error: true
980994
check_shims:
995+
env: {}
996+
verbose: false
981997
gem_rbi_dir: sorbet/rbi/gems
982998
dsl_rbi_dir: sorbet/rbi/dsl
983999
shim_rbi_dir: sorbet/rbi/shims
@@ -986,6 +1002,8 @@ check_shims:
9861002
payload: true
9871003
workers: 1
9881004
annotations:
1005+
env: {}
1006+
verbose: false
9891007
sources:
9901008
- https://raw.githubusercontent.com/Shopify/rbi-central/main
9911009
netrc: true

lib/tapioca/cli.rb

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class Cli < Thor
1717
type: :string,
1818
desc: "Path to the Tapioca configuration file",
1919
default: TAPIOCA_CONFIG_FILE
20+
class_option :env,
21+
type: :hash,
22+
desc: "Environment variables to set before running Tapioca",
23+
repeatable: true
2024
class_option :verbose,
2125
aliases: ["-V"],
2226
type: :boolean,

lib/tapioca/helpers/config_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def validate_config!(config_file, config)
100100
).returns(T::Array[ConfigError])
101101
end
102102
def validate_config_options(command_options, config_key, config_options)
103+
command_options = T.unsafe(self.class).class_options.merge(command_options)
103104
config_options.filter_map do |config_option_key, config_option_value|
104105
command_option = command_options[config_option_key.to_sym]
105106
error_msg = "unknown option `#{config_option_key}` for key `#{config_key}`"

lib/tapioca/helpers/env_helper.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,20 @@ module EnvHelper
88

99
requires_ancestor { Thor }
1010

11+
private
12+
1113
sig { params(options: T::Hash[Symbol, T.untyped]).void }
1214
def set_environment(options) # rubocop:disable Naming/AccessorMethodName
13-
ENV["RAILS_ENV"] = ENV["RACK_ENV"] = options[:environment]
15+
set_all_environment_variables(options[:env])
16+
ENV["RAILS_ENV"] = ENV["RACK_ENV"] = options[:environment] if options[:environment]
1417
ENV["RUBY_DEBUG_LAZY"] = "1"
1518
end
19+
20+
sig { params(options: T::Hash[String, String]).void }
21+
def set_all_environment_variables(options) # rubocop:disable Naming/AccessorMethodName
22+
options.each do |key, value|
23+
ENV[key] = value
24+
end
25+
end
1626
end
1727
end

spec/tapioca/cli/dsl_spec.rb

+11
Original file line numberDiff line numberDiff line change
@@ -2677,6 +2677,17 @@ def title=(title); end
26772677
RACK ENVIRONMENT: staging
26782678
OUT
26792679
end
2680+
2681+
it "must default to `development` as environment" do
2682+
@project.write!("lib/post.rb", <<~RB)
2683+
$stderr.puts "ENVIRONMENT: \#{ENV.to_h.inspect}"
2684+
RB
2685+
2686+
result = @project.tapioca("dsl --env Foo:Bar --env Baz:1")
2687+
2688+
assert_stderr_includes(result, '"Foo"=>"Bar"')
2689+
assert_stderr_includes(result, '"Baz"=>"1"')
2690+
end
26802691
end
26812692

26822693
describe "list compilers" do

tasks/readme.rake

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ task :readme do
2929
end
3030

3131
def skip_option?(option)
32-
option.name == "auth"
32+
ignored_options = ["auth", "config"]
33+
ignored_options.include?(option.name)
3334
end
3435

3536
def option_value(option)
@@ -54,7 +55,7 @@ task :readme do
5455
end
5556

5657
def command_options(command)
57-
command.options.filter_map do |name, opt|
58+
Tapioca::Cli.class_options.merge(command.options).filter_map do |name, opt|
5859
next if skip_option?(opt)
5960

6061
[name.to_s, option_value(opt)]

0 commit comments

Comments
 (0)