2020-11-20 14:20:38 +01:00
|
|
|
# typed: strict
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
require "hardware"
|
2020-09-11 12:05:22 +01:00
|
|
|
require "diagnostic"
|
2015-08-03 13:09:07 +01:00
|
|
|
require "extend/ENV/shared"
|
|
|
|
require "extend/ENV/std"
|
|
|
|
require "extend/ENV/super"
|
2013-08-19 12:32:57 -05:00
|
|
|
|
2020-11-20 14:20:38 +01:00
|
|
|
module Kernel
|
|
|
|
sig { params(env: T.nilable(String)).returns(T::Boolean) }
|
|
|
|
def superenv?(env)
|
|
|
|
return false if env == "std"
|
|
|
|
|
|
|
|
!Superenv.bin.nil?
|
|
|
|
end
|
|
|
|
private :superenv?
|
2013-08-19 12:32:57 -05:00
|
|
|
end
|
|
|
|
|
2023-01-11 18:16:43 -08:00
|
|
|
# @!parse
|
|
|
|
# # ENV is not actually a class, but this makes `YARD` happy
|
|
|
|
# # @see https://rubydoc.info/stdlib/core/ENV ENV core documentation
|
|
|
|
# class ENV; end
|
2013-08-19 12:32:57 -05:00
|
|
|
module EnvActivation
|
2020-11-20 14:20:38 +01:00
|
|
|
sig { params(env: T.nilable(String)).void }
|
2020-07-28 02:04:50 +02:00
|
|
|
def activate_extensions!(env: nil)
|
|
|
|
if superenv?(env)
|
2013-08-19 12:32:57 -05:00
|
|
|
extend(Superenv)
|
|
|
|
else
|
2013-08-19 12:32:59 -05:00
|
|
|
extend(Stdenv)
|
2013-08-19 12:32:57 -05:00
|
|
|
end
|
|
|
|
end
|
2013-08-19 12:32:59 -05:00
|
|
|
|
2021-01-17 22:45:55 -08:00
|
|
|
sig {
|
2020-11-20 14:20:38 +01:00
|
|
|
params(
|
2022-07-26 19:36:43 +01:00
|
|
|
env: T.nilable(String),
|
|
|
|
cc: T.nilable(String),
|
|
|
|
build_bottle: T::Boolean,
|
|
|
|
bottle_arch: T.nilable(String),
|
|
|
|
debug_symbols: T.nilable(T::Boolean),
|
|
|
|
_block: T.proc.returns(T.untyped),
|
2020-11-20 14:20:38 +01:00
|
|
|
).returns(T.untyped)
|
2021-01-17 22:45:55 -08:00
|
|
|
}
|
2022-07-30 11:10:26 +01:00
|
|
|
def with_build_environment(env: nil, cc: nil, build_bottle: false, bottle_arch: nil, debug_symbols: false, &_block)
|
2013-08-19 12:32:59 -05:00
|
|
|
old_env = to_hash.dup
|
|
|
|
tmp_env = to_hash.dup.extend(EnvActivation)
|
2020-11-20 14:20:38 +01:00
|
|
|
T.cast(tmp_env, EnvActivation).activate_extensions!(env: env)
|
|
|
|
T.cast(tmp_env, T.any(Superenv, Stdenv))
|
2022-08-05 16:06:22 -07:00
|
|
|
.setup_build_environment(cc: cc, build_bottle: build_bottle, bottle_arch: bottle_arch,
|
2022-07-30 11:10:26 +01:00
|
|
|
debug_symbols: debug_symbols)
|
2013-08-19 12:32:59 -05:00
|
|
|
replace(tmp_env)
|
2020-11-20 14:20:38 +01:00
|
|
|
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
replace(old_env)
|
|
|
|
end
|
2013-08-19 12:32:59 -05:00
|
|
|
end
|
2017-04-22 16:31:19 +01:00
|
|
|
|
2020-11-20 14:20:38 +01:00
|
|
|
sig { params(key: T.any(String, Symbol)).returns(T::Boolean) }
|
2019-07-13 22:48:22 +08:00
|
|
|
def sensitive?(key)
|
2021-03-29 16:58:07 +05:30
|
|
|
key.match?(/(cookie|key|token|password|passphrase)/i)
|
2019-07-13 22:48:22 +08:00
|
|
|
end
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2020-11-20 14:20:38 +01:00
|
|
|
sig { returns(T::Hash[String, String]) }
|
2019-07-13 22:48:22 +08:00
|
|
|
def sensitive_environment
|
|
|
|
select { |key, _| sensitive?(key) }
|
|
|
|
end
|
|
|
|
|
2020-11-20 14:20:38 +01:00
|
|
|
sig { void }
|
2019-07-13 22:48:22 +08:00
|
|
|
def clear_sensitive_environment!
|
|
|
|
each_key { |key| delete key if sensitive?(key) }
|
2017-04-22 16:31:19 +01:00
|
|
|
end
|
2013-08-19 12:32:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
ENV.extend(EnvActivation)
|