varslist 0.0.7 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d1d7241a9064fedd4345223ba66adc0ba6623ea8f576f2dae0e4c53700ef9013
4
- data.tar.gz: 6f404ccc9867a09a00015d51f6d61d76bb27456852084e68648c1f9586db881a
3
+ metadata.gz: abba4b6b17c6ee3060b2eaf7f8aba3db8904efdf095113a5db5ffd854287c8cc
4
+ data.tar.gz: bb731551f106ce196758676a81f256e3f2773090088d0fad08682fd53732840e
5
5
  SHA512:
6
- metadata.gz: dca53197bf5fca8e3b569b94736c252c21441780684e329adae94be13acda4d80f5370be02f56e18bab7a2c92432806a5f08c291d9cf6e8d75e5f90e4ef686df
7
- data.tar.gz: 368199a27f4ed8b1306a490cf978d591fb345db76dcdb9950dfe7cbaa778ef70684ea5050dcb1c4e7ab5b5e6668136b762d01f34a14e682d03bce7dfa6a10f95
6
+ metadata.gz: ffdcaa3e9ff3243c449bde4a880dbf59107f09124cdec1e45fa4a992c78b2f669f30599c80b3a2787c6519f917698253eefdeb8234097cec2558642f41141e8d
7
+ data.tar.gz: 4cefb456cfce202ba604b94126b0b9c266f8b36e53957e700ecb24fc06b611709b6b273aec3907e215095ef179d951bcdfb5947b0cfbfef5e5f810444f59077a
data/Gemfile CHANGED
@@ -3,3 +3,11 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  gem 'colorize'
6
+
7
+ gem 'dotenv'
8
+
9
+ group :development do
10
+ gem 'pry'
11
+ gem 'rubocop', '~> 1.60'
12
+ gem 'rubocop-performance'
13
+ end
data/bin/varslist CHANGED
@@ -1,10 +1,6 @@
1
1
  #!/usr/bin/env rails
2
2
 
3
- rails_root = Dir.pwd
4
-
5
3
  require 'rubygems'
6
- require_relative "#{rails_root}/config/environment"
7
- require_relative "#{rails_root}/config/application"
8
4
  require 'varslist'
9
5
  env_vars = Varslist.list_env_variables
10
6
 
@@ -19,7 +15,7 @@ if ARGV.include?('--file')
19
15
  end
20
16
 
21
17
  if ARGV.include?('--verify')
22
- Varslist.verify_var_list(env_vars)
18
+ Varslist.verify_var_list
23
19
  exit
24
20
  end
25
21
 
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ module Varslist
6
+ module Generators
7
+ class InstallGenerator < Rails::Generators::Base
8
+ source_root File.expand_path('../../../templates', __dir__)
9
+
10
+ def create_initializer
11
+ template 'varslist.rb', 'config/initializers/varslist.rb'
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ return unless defined?(Varslist)
4
+
5
+ Varslist.configure do |config|
6
+ # If true, enable varslist
7
+ # Default value is true
8
+ # config.enabled = true
9
+
10
+ # List of files to be skipped
11
+ # Default value is []
12
+ # config.skip_files = []
13
+
14
+ # If true, only show missing envs when the server starts
15
+ # Default value is true
16
+ # config.only_show_missing = true
17
+ end
data/lib/test.rb ADDED
@@ -0,0 +1,3 @@
1
+ content = File.read("./lib/varslist.rb")
2
+ FileUtils.mkdir_p("_plugin")
3
+ File.write("./_plugin/creator.rb", content)
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Varslist
4
+ # Default configuration
5
+ class Configuration
6
+ attr_accessor :skip_files, :enabled, :only_show_missing
7
+
8
+ def initialize
9
+ @enabled = true
10
+ @skip_files = []
11
+ @only_show_missing = true
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Varslist
2
+ class MissingEnvError < StandardError; end
3
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/railtie'
4
+
5
+ # Varslist is a Rails gem that provides environment variable management and validation.
6
+ # It ensures that all required environment variables are defined before the Rails server starts.
7
+ module Varslist
8
+ # Railtie hooks into Rails initialization process to verify environment variables.
9
+ # It runs during Rails startup and validates that all required variables are configured.
10
+ class Railtie < Rails::Railtie
11
+ initializer 'varslist.verify_env', after: :load_config_initializers do
12
+ next unless defined?(Rails::Server)
13
+
14
+ Varslist.config ||= Varslist::Configuration.new
15
+ next unless Varslist.config.enabled
16
+
17
+ Varslist.verify_var_list
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Varslist
4
+ VERSION = '0.3.0'
5
+ end
data/lib/varslist.rb CHANGED
@@ -1,90 +1,160 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'colorize'
4
- rails_root = Dir.pwd
5
- require_relative "#{rails_root}/config/environment"
4
+ require_relative 'varslist/errors'
5
+ require_relative 'varslist/configuration'
6
+ require_relative 'varslist/railtie' if defined?(Rails::Railtie)
6
7
 
7
8
  module Varslist
8
-
9
- def self.list_env_variables
10
- env_use_list = []
11
- watch_list = /\.rb$|\.html\.erb$/
12
- env_regex = /ENV\[['"]\w+['"]\]|ENV\.fetch/
13
- filelist = Dir['./**/**/*.*']
14
- filelist.each do |file|
15
- if File.basename(file) =~ watch_list
16
- File.foreach(file).with_index do |line, index|
17
- next if line.strip.empty? || line.strip.start_with?('#')
18
- if line =~ env_regex
19
- env_use_list << { "line" => line.strip, "file_name" => file, "line_number" => index + 1, "var_name" => check_var_name(line)}
9
+ class << self
10
+ attr_accessor :config
11
+
12
+ def env_vars
13
+ @env_vars ||= list_env_variables
14
+ end
15
+
16
+ def list_env_variables
17
+ # Ensure config is initialized
18
+ @config ||= Configuration.new
19
+
20
+ env_use_list = []
21
+ watch_list = /\.rb$|\.html\.erb$/
22
+ env_regex = /ENV\[['"]\w+['"]\]|ENV\.fetch/
23
+ filelist = Dir['./**/**/*.*']
24
+ filelist.each do |file|
25
+ next if @config.skip_files&.any? { |skip_item| matches_skip_item?(file, skip_item) }
26
+
27
+ if File.basename(file)&.match?(watch_list)
28
+ File.foreach(file).with_index do |line, index|
29
+ next if line.strip.empty? || line.strip.start_with?('#')
30
+ if line&.match?(env_regex)
31
+ check_var_name(line).each do |var_info|
32
+ env_use_list << {
33
+ "line" => line.strip,
34
+ "file_name" => file,
35
+ "line_number" => index + 1,
36
+ "var_name" => var_info[:name],
37
+ "has_default" => var_info[:has_default]
38
+ }
39
+ end
40
+ end
20
41
  end
21
42
  end
22
43
  end
44
+ env_use_list
23
45
  end
24
- env_use_list
25
- end
26
46
 
27
- def self.print_help
28
- puts "Help for Varslist".colorize(:magenta)
29
- puts "Use varslist to show all the ENV variables used in all .rb and .erb files"
30
- puts "--file = to show file list where the ENV variables are used"
31
- puts "--verify = to verify if all the ENV variable are valid"
32
- end
33
-
47
+ def print_help
48
+ puts "Help for Varslist".colorize(:magenta)
49
+ puts "Use varslist to show all the ENV variables used in all .rb and .erb files"
50
+ puts "--file = to show file list where the ENV variables are used"
51
+ puts "--verify = to verify if all the ENV variable are valid"
52
+ end
53
+
34
54
 
35
- def self.check_var_name(line)
36
- if match = line.scan(/ENV\[['"]([^'"]+)['"]\]|ENV\.fetch\(['"]([^'"]+)['"]/)
37
- vars=match.flatten.compact.each do |env_var|
38
- env_var
55
+ def check_var_name(line)
56
+ results = []
57
+
58
+ line.scan(/ENV\[['"]([^'"]+)['"]\]/) do |match|
59
+ results << { name: match[0], has_default: false }
60
+ end
61
+
62
+ line.scan(/ENV\.fetch\(\s*['"]([^'"]+)['"]\s*(?:,\s*([^)]*))?\s*\)\s*(\{)?/) do |name, default_arg, block_start|
63
+ has_default = !default_arg.nil? || !block_start.nil?
64
+ results << { name: name, has_default: has_default }
39
65
  end
40
- vars.join
41
- else
42
- nil
66
+
67
+ results
43
68
  end
44
- end
45
-
46
- def self.print_env(found_envs)
47
- found_envs.each do |env_line|
48
- puts "#{env_line["file_name"]}:#{env_line["line_number"]}\t#{env_line["var_name"]}".colorize(:magenta)
49
- puts "\n\t#{env_line["line"]}\n".colorize(:green)
69
+
70
+ def print_env(found_envs)
71
+ found_envs.each do |env_line|
72
+ puts "#{env_line["file_name"]}:#{env_line["line_number"]}\t#{env_line["var_name"]}".colorize(:magenta)
73
+ puts "\n\t#{env_line["line"]}\n".colorize(:green)
74
+ end
50
75
  end
51
- end
52
76
 
53
-
54
- def self.print_used_var_list(found_envs)
55
- puts "\n\nThe used ENV variables".colorize(:magenta)
56
- used_vars=[]
57
- found_envs.each do |found_env|
58
- used_vars << found_env["var_name"] unless used_vars.include?(found_env["var_name"])
77
+
78
+ def print_used_var_list(found_envs)
79
+ puts "\nThe used ENV variables".colorize(:magenta)
80
+ used_vars=[]
81
+ found_envs.each do |found_env|
82
+ used_vars << found_env["var_name"] unless used_vars.include?(found_env["var_name"])
83
+ end
84
+ used_vars.each do |var|
85
+ puts "\t#{var}".colorize(:green)
86
+ end
59
87
  end
60
- used_vars.each do |var|
61
- puts "\t#{var}".colorize(:green)
88
+
89
+ def verify_var_list
90
+ valid_env, invalid_env = fetch_used_and_unused_vars
91
+ if !valid_env.empty? && !config.only_show_missing
92
+ puts "\nThe valid envs are:".colorize(:magenta)
93
+ puts "#{valid_env.join(", ")}".colorize(:green)
94
+ end
95
+ if !invalid_env.empty?
96
+ puts "\nThe missing envs are:".colorize(:magenta)
97
+ puts "#{invalid_env.join(", ")}".colorize(:red)
98
+ end
62
99
  end
63
- end
64
100
 
65
- def self.verify_var_list(found_envs)
66
- valid_env = []
67
- invalid_env = []
68
- found_envs.each do |found_env|
69
- if ENV[found_env["var_name"]] != nil
70
- valid_env << found_env["var_name"] unless valid_env.include?(found_env["var_name"])
101
+ def verify!
102
+ invalid_env = fetch_used_and_unused_vars[1]
103
+ if invalid_env.empty?
104
+ puts "\n\nThe envs are valid".colorize(:green)
71
105
  else
72
- invalid_env << found_env["var_name"] unless invalid_env.include?(found_env["var_name"])
106
+ message = "The envs are invalid"
107
+ puts "\n\nThe envs are invalid".colorize(:red)
108
+ raise Varslist::MissingEnvError, message
73
109
  end
74
110
  end
75
- if !valid_env.empty?
76
- puts "\n\nThe valid envs are:".colorize(:magenta)
77
- valid_env.each do |valid|
78
- puts "\t#{valid}".colorize(:green)
111
+
112
+ def fetch_used_and_unused_vars
113
+ valid_env = []
114
+ invalid_env = []
115
+
116
+ # Track status for each variable
117
+ # A variable is invalid only if it is missing AND required at least once (no default)
118
+ vars_status = {}
119
+
120
+ env_vars.each do |found_env|
121
+ name = found_env["var_name"]
122
+ vars_status[name] ||= { required: false, present: !ENV[name].nil? }
123
+ vars_status[name][:required] = true unless found_env["has_default"]
79
124
  end
80
- end
81
- if !invalid_env.empty?
82
- puts "\n\nThe invalid envs are:".colorize(:magenta)
83
- invalid_env.each do |valid|
84
- puts "\t#{valid}".colorize(:red)
125
+
126
+ vars_status.each do |name, status|
127
+ if status[:present] || !status[:required]
128
+ valid_env << name
129
+ else
130
+ invalid_env << name
131
+ end
85
132
  end
133
+
134
+ [valid_env, invalid_env]
135
+ end
136
+ private
137
+
138
+
139
+ def matches_skip_item?(file, skip_item)
140
+ pattern = skip_item.to_s
141
+ # Check for glob matches
142
+ return true if File.fnmatch?(pattern, file, File::FNM_PATHNAME | File::FNM_DOTMATCH)
143
+ return true if File.fnmatch?(pattern, file.delete_prefix('./'), File::FNM_PATHNAME | File::FNM_DOTMATCH)
144
+
145
+ # Check for absolute path matches or directory prefix matches
146
+ abs_file = File.expand_path(file)
147
+ abs_pattern = File.expand_path(pattern)
148
+
149
+ return true if abs_file == abs_pattern
150
+ return true if abs_file.start_with?(File.join(abs_pattern, ''))
151
+
152
+ false
86
153
  end
87
154
  end
88
155
 
89
- # private_class_method :check_environment
156
+ def self.configure
157
+ self.config ||= Configuration.new
158
+ yield(config) if block_given?
159
+ end
90
160
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: varslist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sumit Pati
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-18 00:00:00.000000000 Z
11
+ date: 2026-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -30,42 +30,29 @@ dependencies:
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
32
  version: 1.0.4
33
- - !ruby/object:Gem::Dependency
34
- name: colorize
35
- requirement: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - "~>"
38
- - !ruby/object:Gem::Version
39
- version: '1.0'
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- version: 1.0.4
43
- type: :development
44
- prerelease: false
45
- version_requirements: !ruby/object:Gem::Requirement
46
- requirements:
47
- - - "~>"
48
- - !ruby/object:Gem::Version
49
- version: '1.0'
50
- - - ">="
51
- - !ruby/object:Gem::Version
52
- version: 1.0.4
53
- description:
54
- email:
33
+ description: Varslist is a ruby gem that helps to identify the env variables that
34
+ are being used in your project
35
+ email:
55
36
  executables:
56
37
  - varslist
57
- - vars
58
38
  extensions: []
59
39
  extra_rdoc_files: []
60
40
  files:
61
41
  - Gemfile
62
- - bin/vars
63
42
  - bin/varslist
43
+ - lib/generators/varslist/install/install_generator.rb
44
+ - lib/templates/varslist.rb
45
+ - lib/test.rb
64
46
  - lib/varslist.rb
65
- homepage: https://rubygems.org/gems/varslist
47
+ - lib/varslist/configuration.rb
48
+ - lib/varslist/errors.rb
49
+ - lib/varslist/railtie.rb
50
+ - lib/varslist/version.rb
51
+ homepage: https://github.com/sumitpati7/varslist
66
52
  licenses: []
67
- metadata: {}
68
- post_install_message:
53
+ metadata:
54
+ rubygems_mfa_required: 'true'
55
+ post_install_message:
69
56
  rdoc_options: []
70
57
  require_paths:
71
58
  - lib
@@ -80,8 +67,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
67
  - !ruby/object:Gem::Version
81
68
  version: '0'
82
69
  requirements: []
83
- rubygems_version: 3.3.5
84
- signing_key:
70
+ rubygems_version: 3.4.10
71
+ signing_key:
85
72
  specification_version: 4
86
73
  summary: Display the list of created Environment variables
87
74
  test_files: []
data/bin/vars DELETED
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env bash
2
- rails c
3
- Varslist.list_env_variables