-
Notifications
You must be signed in to change notification settings - Fork 125
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
Allow Tapioca commands to accept environment variables #1957
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the If so, I think we should document this in the option description or the README. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, if this is indeed what we want, is there a reason not to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I didn't think of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
What else could they be doing? Tapioca sets those env variables as the user has passed them and we don't care if they override some values or not. |
||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a test showing the behaviour of overriding system env? |
||
|
||
assert_stderr_includes(result, '"Foo"=>"Bar"') | ||
assert_stderr_includes(result, '"Baz"=>"1"') | ||
end | ||
end | ||
|
||
describe "list compilers" do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: it might be my lack of familiarity with Thor, but it wasn't immediately obvious to me how we'd be passing multiple environment variables to the same command (I understood after reading the tests).
Could we add an example somewhere in the README of how to pass these?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, we already have a few
hash
type options and how they work is documented here: https://github.com/rails/thor/wiki/Method-Options#types-for-method_optionsThe main way is to use the
--opt=key:value key2:value2
way, but if you mark the option asrepeatable: true
, then you can also do--opt=key:value --opt=key2:value2
too.I guess I can add a section for how to pass env variables via command line or config file to make that more clear and link to the Thor docs for details.