GemSorter
GemSorter is a simple gem to sort the contents of your Gemfile alphabetically while preserving comments and group structure. It helps maintain a clean and organized Gemfile.
Features
- Sorts gems alphabetically.
- Preserves comments and their association with gems.
- Maintains group structure in the Gemfile.
- Optionally creates a backup of the original Gemfile.
- Update the comments of the gems based on their descriptions.
- Optionally converts single quotes to double quotes in gem declarations.
- Optionally removes version constraints from gems while preserving other parameters.
Installation
Add the gem to your project's Gemfile:
gem "gem_sorter"
or install it globally:
gem install gem_sorter
Usage
Once installed, you can use the provided Rake task to sort your Gemfile:
rake gemfile:sort
Inside a Rails application
When you add gem_sorter to a Rails app's Gemfile, the gemfile:sort task is
registered automatically through a Railtie, using Rails' own task-loading
pipeline. This means the task is reliably available under both bin/rails and
bin/rake, and the gem no longer defines tasks during application boot:
bin/rails gemfile:sort
# or
bin/rake gemfile:sort
Standalone (global)
You can also run gem_sorter globally, without adding it to your Gemfile:
rake -r gem_sorter gemfile:sort
Options
backup: Passtrueto create a backup of your Gemfile asGemfile.oldbefore sorting.update_comments: Passtrueto update the comments of the gems based on their descriptions.update_versions: Passtrueto update the versions of the gems based on the lockfile.force_update: Passtrueto update gems to their latest available versions from RubyGems, ignoring the lockfile. This will print a summary of updated gems and remind you to runbundle install.use_double_quotes: Passtrueto convert single quotes to double quotes in gem declarations.remove_versions: Passtrueto remove version constraints from gems while preserving other parameters likerequireorplatforms. This option takes precedence overupdate_versionsif both are enabled.
Example:
rake gemfile:sort[true,true,true,true,true]
This will sort your Gemfile, create a backup, update comments and versions, convert single quotes to double quotes, and remove version constraints.
Options File
Create a file in the root of your project called gem_sorter.yml
backup: Passtrueto create a backup of your Gemfile asGemfile.oldbefore sorting.update_comments: Passtrueto update the comments of the gems based on their descriptions.update_versions: Passtrueto update the versions of the gems based on the lockfile.force_update: Passtrueto update gems to their latest available versions from RubyGems, ignoring the lockfile. This will print a summary of updated gems and remind you to runbundle install.use_double_quotes: Passtrueto convert single quotes to double quotes in gem declarations.remove_versions: Passtrueto remove version constraints from gems while preserving other parameters likerequireorplatforms. This option takes precedence overupdate_versionsif both are enabled.ignore_gems: Pass an array of GEMs you want to ignore versions and commentsignore_gem_versions: Pass an array of GEMs you want to ignore versionsignore_gem_comments: Pass an array of GEMs you want to ignore comments
Example:
backup: true
update_comments: true
update_versions: false
force_update: false
use_double_quotes: true
remove_versions: true
ignore_gems:
- byebug
- discard
ignore_gem_versions:
- rspec
ignore_gem_comments:
- otpor
This will sort your Gemfile, create a backup, update comments, remove version constraints, and ignore specific gems for different operations.
Example
Input Gemfile
source "https://rubygems.org"
# Framework
gem "rails"
gem "puma", "~> 5.3"
gem "thruster", "~> 0.1.13", require: false
gem "countries", "~> 7.1", ">= 7.1.1"
gem "tzinfo-data", platforms: %i[ windows jruby ]
group :development do
gem "dotenv-rails"
gem "pry"
end
Output Gemfile
source "https://rubygems.org"
# A Ruby/Rack web server built for parallelism.
gem "puma", "~> 5.3"
# Full-stack web application framework.
gem "rails", "~> 8.0", ">= 8.0.1"
gem "thruster", "~> 0.1.13", require: false
gem "countries", "~> 7.1", ">= 7.1.1"
gem "tzinfo-data", platforms: %i[ windows jruby ]
group :development do
# Autoload dotenv in Rails.
gem "dotenv-rails", "~> 3.1", ">= 3.1.7"
# A runtime developer console and IRB alternative with powerful introspection capabilities.
gem "pry"
end
Output Gemfile (with remove_versions: true)
source "https://rubygems.org"
# A Ruby/Rack web server built for parallelism.
gem "puma"
# Full-stack web application framework.
gem "rails"
gem "thruster", require: false
gem "countries"
gem "tzinfo-data", platforms: %i[ windows jruby ]
group :development do
# Autoload dotenv in Rails.
gem "dotenv-rails"
# A runtime developer console and IRB alternative with powerful introspection capabilities.
gem "pry"
end
Development
To contribute to this project:
- Clone the repository:
bash git clone https://github.com/renan-garcia/gem_sorter.git - Navigate to the project directory:
bash cd gem_sorter - Install dependencies:
bash bundle install - Run the tests:
bash rspec
Contributing
We welcome contributions! Here's how you can help:
- Fork the repository.
- Create a feature branch:
bash git checkout -b feature/my-new-feature - Commit your changes:
bash git commit -m "Add a new feature" - Push to the branch:
bash git push origin feature/my-new-feature - Open a pull request.
Acknowledgments
Special thanks to the Ruby community for their guidance and support!