rails_app_version 1.0.0 → 1.2.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: 521458301f505b418494a8ff6cc39fe5f40c4b192f2cfb5f8183186c3161101d
4
- data.tar.gz: c5ac04bdaaff5a942e831511df3a8eeed9b564cfbcd213e05cf79a0140a1325a
3
+ metadata.gz: 6ceff4b502902ed60d708fd295202078d48fc55a56abbd427b92daed2c5da7ad
4
+ data.tar.gz: f517710e94b7d97c4e90593f3ca6f9ab9b2351848d3fdb1e2ead54e91aee59a7
5
5
  SHA512:
6
- metadata.gz: 68e00da37bc81058bd32825d1d02a4a96bff4383a5d457bae668a01fa4f5b1c929dc8cbb9d3b6b874688802691b2a7591185305a0125ce6c1526d9c31e80d066
7
- data.tar.gz: f9e53048da586ed9c9d8c7927bc840144dbb7e3f13655b09da398368b88c3cb401ca8d7a3b926696d58b3439de7c2941d5fb0d7157616d45a348bb86f6d732ff
6
+ metadata.gz: 656786a178a739b22a45d8e1ec71e013c4dfd121e979726dd9536ff6c90630cfb0152b6585f0b081acdd1eab584a760db21388ff278c490d4c86620ebecd1b95
7
+ data.tar.gz: e124ff8fb6fdc72478947a215e41865cb93bbb1c08607f74a5d7fffd9f4776964e9566433ac812eecb8a6a187f5288114ff67263dc4f144b5d1284a4c3db2e8a
data/README.md CHANGED
@@ -1,45 +1,263 @@
1
1
  # Rails AppVersion
2
2
 
3
- Rails AppVersion is a Ruby on Rails engine designed to easily manage and access your application's version and environment information.
3
+ Rails AppVersion provides an opinionated version and environment management for your Rails applications. By exposing
4
+ version and environment information throughout your application, it enables better error tracking, debugging, and
5
+ deployment management.
4
6
 
5
- It allows for a seamless integration of versioning within your Rails application, providing a straightforward way to access the current application version and environment without hardcoding these values.
7
+ ## Why Use Rails AppVersion?
6
8
 
7
- This gem is exclusively build for Rails applications and is compatible with Rails 7.0 and above.
9
+ Version and environment tracking are important for modern web applications, particularly when debugging issues in
10
+ production. Rails AppVersion helps you:
8
11
 
9
- ## Usage
10
- How to use my plugin.
12
+ - Track errors with version context in error reporting services
13
+ - Identify which version of your application is running in each environment
14
+ - Cache bust assets between versions
15
+ - Verify deployment success across environments
16
+ - Avoid homegrown version management solutions
17
+
18
+ ### Error Reporting Integration Example
19
+
20
+ ```ruby
21
+ Sentry.init do |config|
22
+ config.release = Rails.application.version.to_s
23
+ config.environment = Rails.application.env
24
+ end
25
+ ```
26
+
27
+ ### Cache Management Example
28
+
29
+ ```ruby
30
+
31
+ class AssetManifest
32
+ def asset_path(path)
33
+ "/assets/#{path}?v=#{Rails.application.version.to_cache_key}"
34
+ end
35
+ end
36
+ ```
11
37
 
12
38
  ## Installation
39
+
13
40
  Add this line to your application's Gemfile:
14
41
 
15
42
  ```ruby
16
43
  gem "rails_app_version"
17
44
  ```
18
45
 
19
- And then execute:
46
+ Then execute:
47
+
20
48
  ```bash
21
- $ bundle
49
+ $ bundle install
50
+ $ rails app:version:config # Copies the default configuration file
51
+ $ echo "1.0.0" > VERSION # Create initial version file
52
+ ```
53
+
54
+ ## Version Management
55
+
56
+ Rails AppVersion supports two methods for managing your application's version:
57
+
58
+ ### Recommended: Using a VERSION File
59
+
60
+ The recommended approach is to maintain a `VERSION` file in your application's root directory. This file should contain
61
+ only the version number:
62
+
63
+ ```plaintext
64
+ # VERSION
65
+ 1.2.3
66
+ ```
67
+
68
+ This approach offers several advantages:
69
+
70
+ - Clear version history in source control
71
+ - Easy automated updates during deployment
72
+ - Separation of version from configuration
73
+ - Compatibility with CI/CD pipelines
74
+
75
+ ### Alternative: Configuration in YAML
76
+
77
+ While not recommended for production applications, you can also specify the version directly in the configuration file.
78
+ The default configuration file is located at `config/app_version.yml`:
79
+
80
+ ```yaml
81
+ shared:
82
+ # Attempts to read from VERSION file, falls back to '0.0.0'
83
+ version: <%= Rails.root.join('VERSION').read.strip rescue '0.0.0' %>
84
+ # Attempts to read from REVISION file, then tries git commit hash, finally falls back to '0'
85
+ revision: <%= Rails.root.join('REVISION').read.strip rescue (`git rev-parse HEAD`.strip rescue '0') %>
86
+ show_revision: <%= Rails.env.local? %>
87
+ environment: <%= ENV.fetch('RAILS_APP_ENV', Rails.env) %>
88
+ ```
89
+
90
+ You can customize this configuration for different environments, though we recommend maintaining version information in
91
+ the VERSION file:
92
+
93
+ ```yaml
94
+ shared:
95
+ # Not recommended: hardcoding version in YAML
96
+ version: '1.2.3'
97
+ environment: production
98
+
99
+ development:
100
+ environment: local
101
+ show_revision: true
102
+
103
+ staging:
104
+ environment: staging
22
105
  ```
23
106
 
24
- ## Usage:
25
- After adding the gem, you can access the application version and environment information anywhere in your Rails application.
107
+ ## Usage
108
+
109
+ ### Accessing Version Information
110
+
26
111
  ```ruby
27
- Rails.application.version # => "1.0.0"
28
- Rails.application.environment # => "staging"
112
+ # Get the current version
113
+ Rails.application.version.to_s # => "1.2.3"
114
+
115
+ # Check version components
116
+ Rails.application.version.major # => 1
117
+ Rails.application.version.minor # => 2
118
+ Rails.application.version.patch # => 3
119
+
120
+ # Check version status
121
+ Rails.application.version.production_ready? # => true
122
+ Rails.application.version.prerelease? # => false
123
+
124
+ # Get a cache-friendly version string
125
+ Rails.application.version.to_cache_key # => "1-2-3"
29
126
  ```
30
127
 
31
- ## Features
32
- - Easy Configuration: Set up your application version and environment in a simple YAML file.
33
- - Automatic Integration: The engine automatically integrates with your Rails application, making the version and environment information accessible.
34
- - Flexible Usage: Access the application version and environment information anywhere in your Rails application.
128
+ ## Version Headers Middleware
129
+
130
+ Rails AppVersion includes an optional middleware that adds version and environment information to HTTP response headers.
131
+ This is particularly useful in staging and development environments to verify deployment success and track which version
132
+ of the application is serving requests.
133
+
134
+ ### Configuring the Middleware
135
+
136
+ Enable and configure the middleware in your `config/app_version.yml`:
137
+
138
+ ```yaml
139
+ development:
140
+ middleware:
141
+ enabled: true
142
+ options:
143
+ include_revision: true # Include git revision in headers
144
+ version_header: X-App-Version
145
+ environment_header: X-App-Environment
146
+ revision_header: X-App-Revision
147
+
148
+ staging:
149
+ middleware:
150
+ enabled: true
151
+ options:
152
+ version_header: X-Staging-Version
153
+ environment_header: X-Staging-Environment
154
+ ```
155
+
156
+ ### Manual Middleware Configuration
157
+
158
+ You can also add the middleware manually in your application configuration:
159
+
160
+ ```ruby
161
+ # config/application.rb or config/environments/staging.rb
162
+ config.middleware.use RailsAppVersion::AppInfoMiddleware, {
163
+ version_header: 'X-Custom-Version',
164
+ environment_header: 'X-Custom-Environment',
165
+ include_revision: true
166
+ }
167
+ ```
168
+
169
+ The middleware will add the following headers to each response:
170
+
171
+ - X-App-Version: Current application version, optionally including revision (e.g., "1.2.3" or "1.2.3 (abc123de)")
172
+ - X-App-Environment: Current environment (e.g., "staging")
173
+
174
+ When `include_revision` is enabled, the version header will include the first 8 characters of the git revision in
175
+ parentheses. This provides a quick way to verify both the version and the specific deployment in a single header.
176
+
177
+ This makes it easy for developers to verify which version is deployed and running in each environment, particularly
178
+ useful during deployments and debugging.
179
+
180
+ ### Environment Management
181
+
182
+ ```ruby
183
+ # Get the current environment
184
+ Rails.application.env # => "staging"
185
+
186
+ # Environment checks
187
+ Rails.application.env.production? # => false
188
+ Rails.application.env.staging? # => true
189
+ ```
190
+
191
+ ### Console Integration
192
+
193
+ The gem automatically displays version and environment information when you start a Rails console:
194
+
195
+ ```
196
+ Welcome to the Rails console!
197
+ Ruby version: 3.2.0
198
+ Application environment: staging
199
+ Application version: 1.2.3
200
+ To exit, press `Ctrl + D`.
201
+ ```
202
+
203
+ ## Version Format
204
+
205
+ Rails AppVersion supports several version formats:
206
+
207
+ - Standard versions: "1.2.3" (major.minor.patch)
208
+ - Short versions: "1.2" (major.minor)
209
+ - Pre-release versions: "2.0.0-alpha" (with pre-release identifier)
210
+
211
+ Version strings are parsed according to Semantic Versioning principles and maintain compatibility with `Gem::Version`
212
+ for comparison operations.
213
+
214
+ ## Version Headers
215
+
216
+ Enable version headers in HTTP responses to verify deployments and track running versions:
217
+
218
+ ```yaml
219
+ # config/app_version.yml
220
+ development:
221
+ middleware:
222
+ enabled: true
223
+ options:
224
+ include_revision: true
225
+
226
+ staging:
227
+ middleware:
228
+ enabled: true
229
+ ```
230
+
231
+ Or add the middleware manually:
232
+
233
+ ```ruby
234
+ # config/application.rb
235
+ config.middleware.use RailsAppVersion::AppInfoMiddleware, {
236
+ version_header: 'X-App-Version',
237
+ environment_header: 'X-App-Environment',
238
+ include_revision: true
239
+ }
240
+ ```
241
+
242
+ Headers added:
243
+
244
+ - X-App-Version: "1.2.3" (or "1.2.3 (abc123de)" with revision)
245
+ - X-App-Environment: "production"
35
246
 
36
247
  ## Contributing
37
- Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.
38
- 1. Fork the repo
39
- 2. Create your feature branch (git checkout -b my-new-feature)
40
- 3. Commit your changes (git commit -am 'Add some feature')
41
- 4. Push to the branch (git push origin my-new-feature)
42
- 5. Create a new Pull Request
248
+
249
+ We welcome contributions! Here's how you can help:
250
+
251
+ 1. Fork the repository
252
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
253
+ 3. Add tests for your changes
254
+ 4. Make your changes and ensure tests pass
255
+ 5. Commit your changes (`git commit -am 'Add some feature'`)
256
+ 6. Push to the branch (`git push origin my-new-feature`)
257
+ 7. Create a new Pull Request
258
+
259
+ Please ensure your changes include appropriate tests and documentation.
43
260
 
44
261
  ## License
262
+
45
263
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsAppVersion
4
+ module AppEnvironment
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ def env
9
+ @env ||= railties.find do |railtie|
10
+ railtie.is_a?(RailsAppVersion::Railtie)
11
+ end.env
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsAppVersion
4
+ class AppInfoMiddleware
5
+ DEFAULT_OPTIONS = {
6
+ version_header: "X-App-Version",
7
+ environment_header: "X-App-Environment",
8
+ revision_header: "X-App-Revision",
9
+ include_revision: false
10
+ }.freeze
11
+
12
+ def initialize(app, options = {})
13
+ @app = app
14
+ @options = DEFAULT_OPTIONS.merge(options)
15
+ end
16
+
17
+ def call(env)
18
+ status, headers, response = @app.call(env)
19
+
20
+ headers[@options[:version_header]] = Rails.application.version.full
21
+ headers[@options[:environment_header]] = Rails.application.env
22
+
23
+ [ status, headers, response ]
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsAppVersion
4
+ module AppVersion
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ def version
9
+ @version ||= railties.find do |railtie|
10
+ railtie.is_a?(RailsAppVersion::Railtie)
11
+ end.version
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,53 @@
1
+ module RailsAppVersion
2
+ class Railtie < ::Rails::Railtie
3
+ class << self
4
+ def root
5
+ @root ||= Pathname.new(File.expand_path(File.expand_path("../../", __dir__)))
6
+ end
7
+ end
8
+
9
+ attr_reader :app_config, :version, :env
10
+
11
+ rake_tasks do
12
+ namespace :app do
13
+ namespace :version do
14
+ desc "Copy config/app_version.yml to the main app config directory"
15
+ task :config do
16
+ source = Railtie.root.join("config", "app_version.yml")
17
+ destination = Rails.root.join("config", "app_version.yml")
18
+
19
+ FileUtils.cp(source, destination)
20
+
21
+ puts "Installed app_version.yml to #{destination}"
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ # Console
28
+ console do
29
+ # rubocop:disable Rails/Output
30
+ puts "Welcome to the Rails console!"
31
+ puts "Ruby version: #{RUBY_VERSION}"
32
+ puts "Application environment: #{Rails.application.env}"
33
+ puts "Application version: #{Rails.application.version}"
34
+ puts "To exit, press `Ctrl + D`."
35
+ # rubocop:enable Rails/Output
36
+ end
37
+
38
+ initializer "fetch_config" do |app|
39
+ @app_config = begin
40
+ app.config_for(:app_version, env: Rails.env)
41
+ rescue RuntimeError => e # file is not found
42
+ # Load the default configuration from the gem
43
+ require "erb"
44
+ yaml = Railtie.root.join("config", "app_version.yml")
45
+ all_configs = ActiveSupport::ConfigurationFile.parse(yaml).deep_symbolize_keys
46
+ all_configs[:shared]
47
+ end
48
+
49
+ @version = RailsAppVersion::Version.create(@app_config[:version], @app_config[:revision])
50
+ @env = ActiveSupport::StringInquirer.new(@app_config.fetch(:environment, Rails.env))
51
+ end
52
+ end
53
+ end
@@ -1,5 +1,72 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsAppVersion
4
- VERSION = "1.0.0"
4
+ VERSION = "1.2.0"
5
+
6
+ class Version < Gem::Version
7
+ attr_reader :major, :minor, :patch, :pre, :revision
8
+
9
+ def self.create(version_string, revision = nil)
10
+ new(version_string).tap { |v| v.set_revision(revision) }
11
+ end
12
+
13
+ def set_revision(revision)
14
+ @revision = revision
15
+ end
16
+
17
+ def full
18
+ return to_s unless revision
19
+ "#{self} (#{short_revision})"
20
+ end
21
+
22
+ def to_cache_key
23
+ parts = [ major, minor ]
24
+ parts << patch if has_patch?
25
+ parts << pre if prerelease?
26
+ parts.join("-")
27
+ end
28
+
29
+ def short_revision
30
+ revision&.slice(0, 8)
31
+ end
32
+
33
+ def prerelease?
34
+ !@pre.nil?
35
+ end
36
+
37
+ def production_ready?
38
+ !prerelease? && major.positive?
39
+ end
40
+
41
+ def has_patch?
42
+ !@patch.nil?
43
+ end
44
+
45
+ protected
46
+
47
+ def initialize(version)
48
+ super
49
+ parse_version(version)
50
+ end
51
+
52
+ private
53
+
54
+ def parse_version(version_string)
55
+ if version_string.nil? || version_string.empty?
56
+ raise ArgumentError, "Version string cannot be nil or empty"
57
+ end
58
+
59
+ parts = version_string.split(".")
60
+ pre_parts = parts.last.split("-")
61
+
62
+ if pre_parts.length > 1
63
+ parts[-1] = pre_parts[0]
64
+ @pre = pre_parts[1]
65
+ end
66
+
67
+ @major = parts[0].to_i
68
+ @minor = parts[1]&.to_i || 0
69
+ @patch = parts[2]&.to_i
70
+ end
71
+ end
5
72
  end
@@ -3,88 +3,16 @@
3
3
  require "rails"
4
4
  require "rails/application"
5
5
  require "rails_app_version/version"
6
+ require "rails_app_version/railtie"
7
+ require "rails_app_version/app_version"
8
+ require "rails_app_version/app_environment"
9
+ require "rails_app_version/version"
6
10
  require "action_controller/railtie"
7
11
 
8
12
  module RailsAppVersion
9
- class Version < Gem::Version
10
- # This method cache used by Rails.cache.fetch to generate a cache key
11
- def to_cache_key
12
- (to_s).parameterize
13
- end
14
- end
15
- class Railtie < ::Rails::Railtie
16
- attr_reader :app_config, :version, :env
17
-
18
- def root
19
- @root ||= Pathname.new(File.expand_path("..", __dir__))
20
- end
21
-
22
- rake_tasks do
23
- namespace :app do
24
- namespace :version do
25
- desc "Copy config/app_version.yml to the main app config directory"
26
- task :config do
27
- source = RailsAppVersion::Railtie.root.join("config", "app_version.yml")
28
- destination = Rails.root.join("config", "app_version.yml")
29
-
30
- FileUtils.cp(source, destination)
31
-
32
- puts "Installed app_version.yml to #{destination}"
33
- end
34
- end
35
- end
36
- end
37
-
38
- # Console
39
- console do
40
- # rubocop:disable Rails/Output
41
- puts "Welcome to the Rails console!"
42
- puts "Ruby version: #{RUBY_VERSION}"
43
- puts "Application environment: #{Rails.application.env}"
44
- puts "Application version: #{Rails.application.version}"
45
- puts "To exit, press `Ctrl + D`."
46
- # rubocop:enable Rails/Output
47
- end
48
-
49
- initializer "fetch_config" do |app|
50
- @app_config = begin
51
- app.config_for(:app_version, env: Rails.env)
52
- rescue RuntimeError
53
- # Load the default configuration from the gem, if the app does not have one
54
- require "erb"
55
- yaml = Railtie.root.join("config", "app_version.yml")
56
- all_configs = ActiveSupport::ConfigurationFile.parse(yaml).deep_symbolize_keys
57
- all_configs[:shared]
58
- end
59
-
60
- @version = Version.new(@app_config[:version])
61
- @env = ActiveSupport::StringInquirer.new(@app_config[:environment] || Rails.env)
62
- end
63
- end
64
-
65
- module AppVersion
66
- extend ActiveSupport::Concern
67
-
68
- included do
69
- def version
70
- @version ||= railties.find do |railtie|
71
- railtie.is_a?(RailsAppVersion::Railtie)
72
- end.version
73
- end
74
- end
75
- end
76
-
77
- module AppEnvironment
78
- extend ActiveSupport::Concern
13
+ extend ActiveSupport::Autoload
79
14
 
80
- included do
81
- def env
82
- @env ||= railties.find do |railtie|
83
- railtie.is_a?(RailsAppVersion::Railtie)
84
- end.env
85
- end
86
- end
87
- end
15
+ autoload :AppInfoMiddleware
88
16
  end
89
17
 
90
18
  Rails::Application.include RailsAppVersion::AppVersion
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_app_version
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-21 00:00:00.000000000 Z
11
+ date: 2024-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '7.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '8.1'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
25
28
  - !ruby/object:Gem::Version
26
29
  version: '7.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '8.1'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: dotenv-rails
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -63,6 +69,10 @@ files:
63
69
  - Rakefile
64
70
  - config/app_version.yml
65
71
  - lib/rails_app_version.rb
72
+ - lib/rails_app_version/app_environment.rb
73
+ - lib/rails_app_version/app_info_middleware.rb
74
+ - lib/rails_app_version/app_version.rb
75
+ - lib/rails_app_version/railtie.rb
66
76
  - lib/rails_app_version/version.rb
67
77
  homepage: https://github.com/seuros/rails_app_version
68
78
  licenses: []
@@ -71,7 +81,7 @@ metadata:
71
81
  source_code_uri: https://github.com/seuros/rails_app_version
72
82
  changelog_uri: https://github.com/seuros/rails_app_version/blob/master/CHANGELOG.md
73
83
  rubygems_mfa_required: 'true'
74
- post_install_message:
84
+ post_install_message:
75
85
  rdoc_options: []
76
86
  require_paths:
77
87
  - lib
@@ -79,15 +89,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
79
89
  requirements:
80
90
  - - ">="
81
91
  - !ruby/object:Gem::Version
82
- version: 3.1.0
92
+ version: 3.2.0
83
93
  required_rubygems_version: !ruby/object:Gem::Requirement
84
94
  requirements:
85
95
  - - ">="
86
96
  - !ruby/object:Gem::Version
87
97
  version: '0'
88
98
  requirements: []
89
- rubygems_version: 3.5.11
90
- signing_key:
99
+ rubygems_version: 3.5.22
100
+ signing_key:
91
101
  specification_version: 4
92
102
  summary: Get the version of your Rails app
93
103
  test_files: []