dotenv-android 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0f6a53019c82367cac781d5b598a0c03bb46e1712d28317d236fdb26b630a868
4
+ data.tar.gz: 25a44ce79404add065ff8e23a46405c67e1141c8b82c68a6fe42086f474ed920
5
+ SHA512:
6
+ metadata.gz: de0253f90662bd6a45c4afecbd8bd6184290867a9597ae448fb21770213e06afd1d32010e32c424980f67d4d256dc24174f1d6771951954324bb6169b3a8fc3f
7
+ data.tar.gz: 771fe82ea8241df5466da3f1e0a7a4267a20f9cfcfe6b72c15c3f278d4329f694639cecda3f939a47ab13c4e53eacf550c70be8dcc4609bd6cd204c1e8384104
@@ -0,0 +1,4 @@
1
+ # [0.1.0] - 2019-11-30
2
+
3
+ ## Added
4
+ - Create CLI that reads `.env` files and generates `Env.kt` file with values requested in source code.
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.
@@ -0,0 +1,67 @@
1
+ # dotenv-android
2
+
3
+ Give access to `.env` environment variables file within your Android projects.
4
+
5
+ `dotenv-android` is a simple CLI tool you can run on each Gradle build to inject environment variables into your Android 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 Kotlin is supported.*
8
+
9
+ # Getting started
10
+
11
+ * Install this tool:
12
+
13
+ ```
14
+ gem install dotenv-android
15
+ ```
16
+
17
+ * In the root of your Android 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 Android app's source code, reference environment variables that you want to use:
20
+
21
+ ```kotlin
22
+ val apiHost: String = Env.apiHost
23
+ ```
24
+
25
+ At first, Android Studio will complain that `Env.apiHost` cannot be found. Don't worry. We will be fixing that. `dotenv-android` CLI crawls your source code looking for `Env.X` requests and generating a `Env.kt` file for you! Anytime you want to use environmental variables, you just need to add it to your source. Super easy.
26
+
27
+ * In your `app/build.gradle` file, add the following to the bottom of the file:
28
+
29
+ ```
30
+ task buildEnv << {
31
+ dotenv-android --source app/src/main/java/com/example/
32
+ }
33
+
34
+ build.dependsOn buildEnv
35
+ ```
36
+
37
+ * Run a build in Android Studio to run the `dotenv-android` CLI tool.
38
+
39
+ * Done!
40
+
41
+ ## Development
42
+
43
+ ```bash
44
+ $> bundle install
45
+ ```
46
+
47
+ You're ready to start developing!
48
+
49
+ ## Deployment
50
+
51
+ This gem is setup automatically to deploy to RubyGems on a git tag deployment.
52
+
53
+ * Add `RUBYGEMS_KEY` secret to Travis-CI's settings.
54
+ * Make a new git tag, push it up to GitHub. Travis will deploy for you.
55
+
56
+ ## Author
57
+
58
+ * Levi Bostian - [GitHub](https://github.com/levibostian), [Twitter](https://twitter.com/levibostian), [Website/blog](http://levibostian.com)
59
+
60
+ ![Levi Bostian image](https://gravatar.com/avatar/22355580305146b21508c74ff6b44bc5?s=250)
61
+
62
+ ## Contribute
63
+
64
+ dotenv-android is open for pull requests. Check out the [list of issues](https://github.com/levibostian/dotenv-android/issues) for tasks I am planning on working on. Check them out if you wish to contribute in that way.
65
+
66
+ **Want to add features?** Before you decide to take a bunch of time and add functionality to the library, please, [create an issue]
67
+ (https://github.com/levibostian/dotenv-android/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.
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'dotenv_android'
5
+
6
+ DotEnvAndroid::CLI.new
@@ -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 DotEnvAndroid
14
+ Options = Struct.new(:source, :out, :verbose, :debug)
15
+
16
+ class CLI
17
+ def initialize
18
+ @options = parse_options
19
+
20
+ @ui = DotEnvAndroid::UI.new(@options.verbose, @options.debug)
21
+
22
+ assert_options
23
+
24
+ DotEnvAndroid::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-android [options]'
34
+
35
+ opts.on('-v', '--version', 'Print version') do
36
+ puts DotEnvAndroid::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.kt') # 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 (example: Path/Env.kt)') 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 DotEnvAndroid
12
+ class Generator
13
+ def initialize(options)
14
+ @options = options
15
+
16
+ @ui = DotEnvAndroid::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}/**/*.kotlin")
27
+ @ui.verbose("Searching for environment vars in source: #{source_pattern}")
28
+
29
+ requests = Set[]
30
+
31
+ Dir.glob(source_pattern) do |kotlin_file|
32
+ next if File.directory? kotlin_file
33
+
34
+ @ui.verbose("Looking for Env usage in: #{kotlin_file}")
35
+ requests.merge(get_env_requests(kotlin_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 = DotEnvAndroid::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 = "object Env {\n\n"
79
+ env_variables.each do |key, value|
80
+ file_contents += " val #{DotEnvAndroid::Util.snake_to_camel(key)} = \"#{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("File generated, #{@options.out}. If you don't like this output path, run command again with `-o` CLI option")
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 DotEnvAndroid
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 DotEnvAndroid
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
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DotEnvAndroid
4
+ class Version
5
+ def self.get
6
+ '0.1.0'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dotenv-android/cli'
metadata ADDED
@@ -0,0 +1,177 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dotenv-android
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-android
140
+ extensions: []
141
+ extra_rdoc_files: []
142
+ files:
143
+ - CHANGELOG.md
144
+ - LICENSE
145
+ - README.md
146
+ - bin/dotenv-android
147
+ - lib/dotenv-android/cli.rb
148
+ - lib/dotenv-android/generator.rb
149
+ - lib/dotenv-android/ui.rb
150
+ - lib/dotenv-android/util.rb
151
+ - lib/dotenv-android/version.rb
152
+ - lib/dotenv_android.rb
153
+ homepage: http://github.com/levibostian/dotenv-android
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 Android app
177
+ test_files: []