2016-02-22 19:59:49 -08:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
2017-11-14 07:41:58 +08:00
|
|
|
# Allow utf-8 charactor in config value
|
|
|
|
# For example, APP_NAME=中文字符
|
|
|
|
Encoding.default_external = Encoding::UTF_8
|
|
|
|
Encoding.default_internal = Encoding::UTF_8
|
|
|
|
|
2016-03-03 16:34:45 -08:00
|
|
|
# pick a custom env file if set
|
|
|
|
if File.exists?("/tmp/envfile")
|
|
|
|
custom_env = true
|
|
|
|
file = File.read("/tmp/envfile").strip
|
2016-09-13 19:09:41 -07:00
|
|
|
else
|
|
|
|
custom_env = false
|
2017-09-25 15:05:20 -04:00
|
|
|
file = ENV["ENVFILE"] || ".env"
|
2016-03-03 16:34:45 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
puts "Reading env from #{file}"
|
|
|
|
|
2016-09-13 18:49:57 -07:00
|
|
|
dotenv = begin
|
2017-09-25 15:04:39 -04:00
|
|
|
# https://regex101.com/r/cbm5Tp/1
|
2017-06-21 01:14:38 +01:00
|
|
|
dotenv_pattern = /^(?:export\s+|)(?<key>[[:alnum:]_]+)=((?<quote>["'])?(?<val>.*?[^\\])\k<quote>?|)$/
|
2017-07-05 20:06:28 +02:00
|
|
|
|
2016-09-13 18:49:57 -07:00
|
|
|
# find that above node_modules/react-native-config/ios/
|
2017-07-05 20:06:28 +02:00
|
|
|
path = File.join(Dir.pwd, "../../../#{file}")
|
|
|
|
if !File.exists?(path)
|
|
|
|
# try as absolute path
|
|
|
|
path = file
|
|
|
|
end
|
|
|
|
raw = File.read(path)
|
2016-09-13 18:49:57 -07:00
|
|
|
raw.split("\n").inject({}) do |h, line|
|
2017-04-26 17:58:25 -04:00
|
|
|
m = line.match(dotenv_pattern)
|
|
|
|
next h if m.nil?
|
|
|
|
key = m[:key]
|
|
|
|
# Ensure string (in case of empty value) and escape any quotes present in the value.
|
|
|
|
val = m[:val].to_s.gsub('"', '\"')
|
|
|
|
h.merge(key => val)
|
2016-09-13 18:49:57 -07:00
|
|
|
end
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
puts("**************************")
|
|
|
|
puts("*** Missing .env file ****")
|
|
|
|
puts("**************************")
|
|
|
|
{} # set dotenv as an empty hash
|
|
|
|
end
|
2016-02-22 19:59:49 -08:00
|
|
|
|
2016-02-23 15:50:03 -08:00
|
|
|
# create obj file that sets DOT_ENV as a NSDictionary
|
|
|
|
dotenv_objc = dotenv.map { |k, v| %Q(@"#{k}":@"#{v}") }.join(",")
|
|
|
|
template = <<EOF
|
|
|
|
#define DOT_ENV @{ #{dotenv_objc} };
|
|
|
|
EOF
|
2016-02-22 19:59:49 -08:00
|
|
|
|
2016-02-23 15:50:03 -08:00
|
|
|
# write it so that ReactNativeConfig.m can return it
|
|
|
|
path = File.join(ENV["SYMROOT"], "GeneratedDotEnv.m")
|
|
|
|
File.open(path, "w") { |f| f.puts template }
|
2016-02-22 19:59:49 -08:00
|
|
|
|
2016-12-06 17:44:24 -08:00
|
|
|
# create header file with defines for the Info.plist preprocessor
|
|
|
|
info_plist_defines_objc = dotenv.map { |k, v| %Q(#define __RN_CONFIG_#{k} #{v}) }.join("\n")
|
|
|
|
|
|
|
|
# write it so the Info.plist preprocessor can access it
|
2017-04-27 00:00:07 +02:00
|
|
|
path = File.join(ENV["BUILD_DIR"], "GeneratedInfoPlistDotEnv.h")
|
2016-12-06 17:44:24 -08:00
|
|
|
File.open(path, "w") { |f| f.puts info_plist_defines_objc }
|
|
|
|
|
2016-03-03 16:34:45 -08:00
|
|
|
if custom_env
|
|
|
|
File.delete("/tmp/envfile")
|
|
|
|
end
|
|
|
|
|
2016-08-28 21:03:52 -04:00
|
|
|
puts "Wrote to #{path}"
|