dotenv-ios 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +4 -0
- data/LICENSE +19 -0
- data/README.md +65 -0
- data/bin/dotenv-ios +7 -0
- data/lib/dotenv-ios/cli.rb +72 -0
- data/lib/dotenv-ios/generator.rb +93 -0
- data/lib/dotenv-ios/ui.rb +33 -0
- data/lib/dotenv-ios/util.rb +17 -0
- data/lib/dotenv-ios/version.rb +9 -0
- data/lib/dotenv_ios.rb +3 -0
- metadata +177 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: '028bc8ba2b914550dfda93c441f49551ccc780da06adebbf7537c637dc3f1826'
|
4
|
+
data.tar.gz: 565963f35047f3b03f70d8564db9a1bdee3661e462022e7d4d41215922266ff1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ddfccdf062a1cf7c50e15c9134bfc19f02ba64a5312f6ae492f0e61016939e488af36c64e6998ac8a5102e8619cf08c883dd1c0d1c6aa0dba629f5cb854ed848
|
7
|
+
data.tar.gz: 744cfe0508b946dab9688e252f1c7659912139af0864cab79da50669c9db6af33f4194affb9f41c87a58f5f954921e3fdbad1200aa7a5e1a9e68a531b46619b8
|
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2019 Levi Bostian <levi.bostian@gmail.com>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# dotenv-ios
|
2
|
+
|
3
|
+
Give access to `.env` environment variables file within your iOS projects.
|
4
|
+
|
5
|
+
`dotenv-ios` is a simple CLI tool you can run on each XCode build to inject environment variables into your iOS app. This tool was inspired by [the twelve-factor app](https://12factor.net/config) to make environmental changes in your app simple.
|
6
|
+
|
7
|
+
*Note: At this time, only Swift is supported.*
|
8
|
+
|
9
|
+
# Getting started
|
10
|
+
|
11
|
+
* Install this tool:
|
12
|
+
|
13
|
+
```
|
14
|
+
gem install dotenv-ios
|
15
|
+
```
|
16
|
+
|
17
|
+
* In the root of your iOS project, create a `.env` file and store all of the environment variables you wish inside. (Make sure to add this file to your `.gitignore` to avoid checking it into source control!)
|
18
|
+
|
19
|
+
* In your iOS app's source code, reference environment variables that you want to use:
|
20
|
+
|
21
|
+
```swift
|
22
|
+
let apiHost: String = Env.apiHost
|
23
|
+
```
|
24
|
+
|
25
|
+
At first, XCode will complain that `Env.apiHost` cannot be found. Don't worry. We will be fixing that. `dotenv-ios` CLI crawls your source code looking for `Env.X` requests and generating a `Env.swift` file for you! Anytime you want to use environmental variables, you just need to add it to your source. Super easy.
|
26
|
+
|
27
|
+
* Create a new Build Phase in XCode to run this command.
|
28
|
+
|
29
|
+
The shell command for this build phase is quite simple: `dotenv-ios --source PathToYourSourceCode/`
|
30
|
+
|
31
|
+
Reorder the new Build Phase to be first to run. That way this tool can generate the environment variables before XCode tries to compile your app's source code.
|
32
|
+
|
33
|
+
* Run a build in XCode (Cmd + B) to run the `dotenv-ios` CLI tool.
|
34
|
+
|
35
|
+
* Add the newly generated `PathToYourSourceCode/Env.swift` file to your XCode project.
|
36
|
+
|
37
|
+
* Done!
|
38
|
+
|
39
|
+
## Development
|
40
|
+
|
41
|
+
```bash
|
42
|
+
$> bundle install
|
43
|
+
```
|
44
|
+
|
45
|
+
You're ready to start developing!
|
46
|
+
|
47
|
+
## Deployment
|
48
|
+
|
49
|
+
This gem is setup automatically to deploy to RubyGems on a git tag deployment.
|
50
|
+
|
51
|
+
* Add `RUBYGEMS_KEY` secret to Travis-CI's settings.
|
52
|
+
* Make a new git tag, push it up to GitHub. Travis will deploy for you.
|
53
|
+
|
54
|
+
## Author
|
55
|
+
|
56
|
+
* Levi Bostian - [GitHub](https://github.com/levibostian), [Twitter](https://twitter.com/levibostian), [Website/blog](http://levibostian.com)
|
57
|
+
|
58
|
+

|
59
|
+
|
60
|
+
## Contribute
|
61
|
+
|
62
|
+
dotenv-ios is open for pull requests. Check out the [list of issues](https://github.com/levibostian/dotenv-ios/issues) for tasks I am planning on working on. Check them out if you wish to contribute in that way.
|
63
|
+
|
64
|
+
**Want to add features?** Before you decide to take a bunch of time and add functionality to the library, please, [create an issue]
|
65
|
+
(https://github.com/levibostian/dotenv-ios/issues/new) stating what you wish to add. This might save you some time in case your purpose does not fit well in the use cases of this project.
|
data/bin/dotenv-ios
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'colorize'
|
4
|
+
require 'optparse'
|
5
|
+
require 'set'
|
6
|
+
require 'pathname'
|
7
|
+
require_relative './ui'
|
8
|
+
require_relative './util'
|
9
|
+
require_relative './generator'
|
10
|
+
require_relative './version'
|
11
|
+
require 'dotenv'
|
12
|
+
|
13
|
+
module DotEnviOS
|
14
|
+
Options = Struct.new(:source, :out, :verbose, :debug)
|
15
|
+
|
16
|
+
class CLI
|
17
|
+
def initialize
|
18
|
+
@options = parse_options
|
19
|
+
|
20
|
+
@ui = DotEnviOS::UI.new(@options.verbose, @options.debug)
|
21
|
+
|
22
|
+
assert_options
|
23
|
+
|
24
|
+
DotEnviOS::Generator.new(@options).start
|
25
|
+
end
|
26
|
+
|
27
|
+
def parse_options
|
28
|
+
options = Options.new
|
29
|
+
options.verbose = false
|
30
|
+
options.debug = false
|
31
|
+
|
32
|
+
opt_parser = OptionParser.new do |opts|
|
33
|
+
opts.banner = 'Usage: dotenv-ios [options]'
|
34
|
+
|
35
|
+
opts.on('-v', '--version', 'Print version') do
|
36
|
+
puts DotEnviOS::Version.get
|
37
|
+
exit
|
38
|
+
end
|
39
|
+
opts.on('-s', '--source DIR', 'Source code directory to check for requested environment variables') do |source|
|
40
|
+
options.source = source
|
41
|
+
options.out = Pathname.new(source).join('Env.swift') # set default
|
42
|
+
end
|
43
|
+
opts.on('--verbose', 'Verbose output') do
|
44
|
+
options.verbose = true
|
45
|
+
end
|
46
|
+
opts.on('--debug', 'Debug output (also turns on verbose)') do
|
47
|
+
options.verbose = true
|
48
|
+
options.debug = true
|
49
|
+
end
|
50
|
+
opts.on('-o', '--out FILE', 'Output file') do |out|
|
51
|
+
options.out = out
|
52
|
+
end
|
53
|
+
opts.on('-h', '--help', 'Prints this help') do
|
54
|
+
puts opts
|
55
|
+
exit
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
help = opt_parser.help
|
60
|
+
abort(help) if ARGV.empty?
|
61
|
+
|
62
|
+
opt_parser.parse!(ARGV)
|
63
|
+
|
64
|
+
options
|
65
|
+
end
|
66
|
+
|
67
|
+
def assert_options
|
68
|
+
prefix = '[ERROR]'
|
69
|
+
@ui.fail("#{prefix} --source required") unless @options.source
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'colorize'
|
4
|
+
require 'optparse'
|
5
|
+
require 'set'
|
6
|
+
require 'pathname'
|
7
|
+
require_relative './ui'
|
8
|
+
require_relative './util'
|
9
|
+
require 'dotenv'
|
10
|
+
|
11
|
+
module DotEnviOS
|
12
|
+
class Generator
|
13
|
+
def initialize(options)
|
14
|
+
@options = options
|
15
|
+
|
16
|
+
@ui = DotEnviOS::UI.new(@options.verbose, @options.debug)
|
17
|
+
end
|
18
|
+
|
19
|
+
def start
|
20
|
+
requests = iterate_source
|
21
|
+
env_variables = get_values(requests)
|
22
|
+
generate_output(env_variables)
|
23
|
+
end
|
24
|
+
|
25
|
+
def iterate_source
|
26
|
+
source_pattern = File.expand_path("#{@options.source}/**/*.swift")
|
27
|
+
@ui.verbose("Searching for environment vars in source: #{source_pattern}")
|
28
|
+
|
29
|
+
requests = Set[]
|
30
|
+
|
31
|
+
Dir.glob(source_pattern) do |swift_file|
|
32
|
+
next if File.directory? swift_file
|
33
|
+
|
34
|
+
@ui.verbose("Looking for Env usage in: #{swift_file}")
|
35
|
+
requests.merge(get_env_requests(swift_file))
|
36
|
+
@ui.verbose("Found #{requests.count} requests")
|
37
|
+
@ui.debug("Requests found for file: #{requests.to_a}")
|
38
|
+
end
|
39
|
+
|
40
|
+
requests.to_a
|
41
|
+
end
|
42
|
+
|
43
|
+
def get_env_requests(file)
|
44
|
+
requests = []
|
45
|
+
|
46
|
+
File.readlines(file).each do |line|
|
47
|
+
line.split(' ').each do |word|
|
48
|
+
# https://regexr.com/4pmp0
|
49
|
+
next unless /Env\.[a-z]\w*/.match? word
|
50
|
+
|
51
|
+
requested_variable = word.split('.')[1]
|
52
|
+
requested_variable = DotEnviOS::Util.to_snakecase(requested_variable).upcase
|
53
|
+
|
54
|
+
requests.push(requested_variable)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
requests
|
59
|
+
end
|
60
|
+
|
61
|
+
def get_values(requests)
|
62
|
+
variables = Dotenv.parse('.env')
|
63
|
+
values = {}
|
64
|
+
|
65
|
+
requests.each do |request|
|
66
|
+
@ui.fail("Environment variable #{request} not found in .env") unless variables[request]
|
67
|
+
|
68
|
+
values[request] = variables[request]
|
69
|
+
end
|
70
|
+
|
71
|
+
@ui.debug("Values: #{values}")
|
72
|
+
values
|
73
|
+
end
|
74
|
+
|
75
|
+
def generate_output(env_variables)
|
76
|
+
@ui.verbose("Outputting environment variables to #{@options.out}")
|
77
|
+
|
78
|
+
file_contents = "class Env {\n\n"
|
79
|
+
env_variables.each do |key, value|
|
80
|
+
file_contents += " static var #{DotEnviOS::Util.snake_to_camel(key)}: String = \"#{value}\"\n"
|
81
|
+
end
|
82
|
+
|
83
|
+
file_contents += "\n}"
|
84
|
+
|
85
|
+
@ui.debug("Output file: #{file_contents}")
|
86
|
+
|
87
|
+
File.open(@options.out, 'w') { |file| file.write(file_contents) }
|
88
|
+
|
89
|
+
@ui.success('Environment variables file generated!')
|
90
|
+
@ui.success("Add file, #{@options.out}, to your XCode project")
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'colorize'
|
4
|
+
require 'optparse'
|
5
|
+
|
6
|
+
module DotEnviOS
|
7
|
+
class UI
|
8
|
+
def initialize(verbose, debug)
|
9
|
+
@verbose = verbose
|
10
|
+
@debug = debug
|
11
|
+
end
|
12
|
+
|
13
|
+
def success(message)
|
14
|
+
puts message.colorize(:green)
|
15
|
+
end
|
16
|
+
|
17
|
+
def fail(message)
|
18
|
+
abort(message.colorize(:red))
|
19
|
+
end
|
20
|
+
|
21
|
+
def warning(message)
|
22
|
+
puts message.to_s.colorize(:yellow)
|
23
|
+
end
|
24
|
+
|
25
|
+
def verbose(message)
|
26
|
+
puts message.to_s if @verbose
|
27
|
+
end
|
28
|
+
|
29
|
+
def debug(message)
|
30
|
+
puts message.to_s.colorize(:light_blue) if @debug
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DotEnviOS
|
4
|
+
class Util
|
5
|
+
def self.to_snakecase(string)
|
6
|
+
string.gsub(/(.)([A-Z])/, '\1_\2').downcase
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.snake_to_camel(string)
|
10
|
+
# takes API_HOST => ApiHost
|
11
|
+
s = string.split('_').collect(&:capitalize).join
|
12
|
+
# Takes ApiHost => apiHost
|
13
|
+
s[0] = s[0].downcase
|
14
|
+
s
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/dotenv_ios.rb
ADDED
metadata
ADDED
@@ -0,0 +1,177 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dotenv-ios
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Levi Bostian
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-11-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: colorize
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.8'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.8.1
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0.8'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.8.1
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: dotenv
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '2.7'
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 2.7.5
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '2.7'
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 2.7.5
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: rubocop
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0.58'
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 0.58.2
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0.58'
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 0.58.2
|
73
|
+
- !ruby/object:Gem::Dependency
|
74
|
+
name: rake
|
75
|
+
requirement: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - "~>"
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '12.3'
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 12.3.1
|
83
|
+
type: :development
|
84
|
+
prerelease: false
|
85
|
+
version_requirements: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '12.3'
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: 12.3.1
|
93
|
+
- !ruby/object:Gem::Dependency
|
94
|
+
name: rspec
|
95
|
+
requirement: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - "~>"
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '3.8'
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: 3.8.0
|
103
|
+
type: :development
|
104
|
+
prerelease: false
|
105
|
+
version_requirements: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - "~>"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '3.8'
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: 3.8.0
|
113
|
+
- !ruby/object:Gem::Dependency
|
114
|
+
name: rspec_junit_formatter
|
115
|
+
requirement: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - "~>"
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0.4'
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: 0.4.1
|
123
|
+
type: :development
|
124
|
+
prerelease: false
|
125
|
+
version_requirements: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - "~>"
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0.4'
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: 0.4.1
|
133
|
+
description: It is said to be good practice when you configure your app with a .env
|
134
|
+
file. This library will (1) scan your source code looking for requests for environment
|
135
|
+
variables, (2) parse a .env file and the variables defined on the machine, and (3)
|
136
|
+
generate source code file to compile into app.
|
137
|
+
email: levi.bostian@gmail.com
|
138
|
+
executables:
|
139
|
+
- dotenv-ios
|
140
|
+
extensions: []
|
141
|
+
extra_rdoc_files: []
|
142
|
+
files:
|
143
|
+
- CHANGELOG.md
|
144
|
+
- LICENSE
|
145
|
+
- README.md
|
146
|
+
- bin/dotenv-ios
|
147
|
+
- lib/dotenv-ios/cli.rb
|
148
|
+
- lib/dotenv-ios/generator.rb
|
149
|
+
- lib/dotenv-ios/ui.rb
|
150
|
+
- lib/dotenv-ios/util.rb
|
151
|
+
- lib/dotenv-ios/version.rb
|
152
|
+
- lib/dotenv_ios.rb
|
153
|
+
homepage: http://github.com/levibostian/dotenv-ios
|
154
|
+
licenses:
|
155
|
+
- MIT
|
156
|
+
metadata: {}
|
157
|
+
post_install_message:
|
158
|
+
rdoc_options: []
|
159
|
+
require_paths:
|
160
|
+
- lib
|
161
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
162
|
+
requirements:
|
163
|
+
- - ">="
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '0'
|
166
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
167
|
+
requirements:
|
168
|
+
- - ">="
|
169
|
+
- !ruby/object:Gem::Version
|
170
|
+
version: '0'
|
171
|
+
requirements: []
|
172
|
+
rubyforge_project:
|
173
|
+
rubygems_version: 2.7.7
|
174
|
+
signing_key:
|
175
|
+
specification_version: 4
|
176
|
+
summary: Access environment variables at runtime of iOS app
|
177
|
+
test_files: []
|