rails_app_version 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: de1ff73be83ef61042e2e2baddd1b94805f4f41a8ce2f1a021802ccad42c2323
4
- data.tar.gz: a410eac1d9ac536a352f8d3158bf1e5bf6b872aa1fced4c2c152db4adc71d840
3
+ metadata.gz: 6ceff4b502902ed60d708fd295202078d48fc55a56abbd427b92daed2c5da7ad
4
+ data.tar.gz: f517710e94b7d97c4e90593f3ca6f9ab9b2351848d3fdb1e2ead54e91aee59a7
5
5
  SHA512:
6
- metadata.gz: f1cfbe373f72deb414e9bbe6900e690552da4f8f5a68ca7d1df286bb553556a1e16c28f162b6098a7027cdcf7ac1a6d3126bfe986449834049d7a64716b29eb4
7
- data.tar.gz: c09bae51fe6e6ee8e714cc04ef3b4f5558f2618ece745423bab6248372ab10de8e45d5d92b8aaac2dee94c75727b9b667e7bf0f13b7e00672a25655ecdfe73c8
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"
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"
126
+ ```
127
+
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
28
184
  Rails.application.env # => "staging"
185
+
186
+ # Environment checks
187
+ Rails.application.env.production? # => false
188
+ Rails.application.env.staging? # => true
29
189
  ```
30
190
 
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.
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.1.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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_app_version
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-03 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
@@ -69,6 +69,10 @@ files:
69
69
  - Rakefile
70
70
  - config/app_version.yml
71
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
72
76
  - lib/rails_app_version/version.rb
73
77
  homepage: https://github.com/seuros/rails_app_version
74
78
  licenses: []
@@ -85,14 +89,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
85
89
  requirements:
86
90
  - - ">="
87
91
  - !ruby/object:Gem::Version
88
- version: 3.1.0
92
+ version: 3.2.0
89
93
  required_rubygems_version: !ruby/object:Gem::Requirement
90
94
  requirements:
91
95
  - - ">="
92
96
  - !ruby/object:Gem::Version
93
97
  version: '0'
94
98
  requirements: []
95
- rubygems_version: 3.5.16
99
+ rubygems_version: 3.5.22
96
100
  signing_key:
97
101
  specification_version: 4
98
102
  summary: Get the version of your Rails app